GPS Latitude and longitude from hexadecimal value - binary

I'm getting the following replies from a GPS to a microcontroller, rather than try parse the string and convert that to hex to send, I'd rather just use the hex value supplied to then send to my server but I'm having difficulty working out what format it's stored in.
Lat: 37 Deg 39 Min 48.84 Sec S (0xFF94DE3A)
Lon: 144 Deg 58 Min 10.30 Sec E (0x019C5B97)
(another example:)
Lat: 49 Deg 10 Min 21.49 Sec N (0x008BDE6C)
Lon: 123 Deg 4 Min 14.76 Sec W (0xFEA1EE9A)
I've tried answers to other questions with similar titles to no avail..
Lat in the first example looks like -7020998 dec since it's south, similar to Lon in the second example (-22942054) is also negative in the west.
I've tried dividing the numbers by the converted expected result (using degrees + minutes/60 + seconds/3600) I come up with a rough number,
ie:
0xFF94DE3A = -7020998 / -37.66357 = ~186413.502
0x019C5B97 = 27024279 / 144.96953 = ~186413.510
0x008BDE6C = 9166444 / 49.17264 = ~186413.501
0xFEA1EE9A = -22942054 / -123.07077 = ~186413.508
This looks close to 180000, but if I divide by that it doesn't look right (-7020998 / 180000 = -39.00554). What does 186413 relate to though? I feel like I'm missing something completely obvious..
Edit: I'm able to get it working using the below quick example (I know it's not pretty):
if ($GPSLatitude > 0x7FFFFFFF) // ensure correct signedness
$GPSLatitude-=0x100000000;
$GPSf = floatval($GPSLatitude) / 186413.51334561207757602506827277;
$GPSD = floor($GPSf);
$GPSt = abs($GPSf - $GPSD) * 60;
$GPSM = floor($GPSt);
$GPSS = floor(($GPSt - $GPSM) * 6000) / 100;
echo 'GPS Latitude: ' . $GPSD . ' Deg ' . $GPSM . ' Min ' . number_format($GPSS,2,'.','') . ' Sec';
The results match perfectly, but I know there's got to be a better way than dividing by that horrible number?

According to http://en.wikipedia.org/wiki/Geotagging, the GPS coordinates could be set even in rationals:
When stored in EXIF, the coordinates are represented as a series of
rational numbers in the GPS sub-IFD. Here is a hexadecimal dump of the
relevant section of the EXIF metadata (with big-endian byte order):
I am afraid, you have not all hex digits and you see only the upper part of the rational number. And the invisible divisor could be that magic number.
I am not insisting it is so, I only demonstrate there could be ANYTHING hidden in the format.
And you are counting in degrees, and the coord could simply be gaussian X,Y.
Anyway, what is the name of your GPS format?

Related

Retrieving JSON data from the osmbuildings.org data site

I am trying to retrieve building data from osmbuildings.org
The osmbuildings documentation (https://osmbuildings.org/documentation/data/) features an example URL (https://data.osmbuildings.org/0.2/anonymous/tile/15/17605/10743.json) that returns the JSON file for buildings that are located in Berlin at roughly 52°32'30.6"N 13°25'23.2"E coordinates.
Can I use this same site to get a JSON file for buildings in a different location? For example, I believe the X,Y coordinates using XYZ tile coordinates would be 25835 and 5221 for a location in Singapore for a zoom of 15 and the corresponding url would be https://data.osmbuildings.org/0.2/anonymous/tile/15/25835/5221.json.
However, when I put this into the web browser, I don't get the json file like in the Berlin case. Can someone please explain?
I also tried different zoom values at the same location:
https://data.osmbuildings.org/0.2/anonymous/tile/16/51672/8779.json
And at a slightly different location with fewer buildings:
https://data.osmbuildings.org/0.2/anonymous/tile/16/51666/9459.json
If you do not get the JSON file, you are probably getting an empty reply. This is likely because your specified tile does not contain any building data.
This can have various reasons, e.g. there are regions where no building data is available. In your case however, it seems to me that your conversion into X,Y coordinates is not correct for Singapore.
In my following example, I use the coordinates for the Singapore Marina Bay (https://www.openstreetmap.org/#map=15/1.2742/103.8617).
I converted the lon/lat to tile numbers using the formula from the OSM wiki (https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames). In pseudo code:
n = 2 ^ zoom
xtile = n * ((lon_deg + 180) / 360)
ytile = n * (1 - (ln(tan(lat_rad) + sec(lat_rad)) / π)) / 2
= n * (1 - (ln(tan(lat_rad) + (1 / cos(lat_rad))) / π)) / 2
Filling in the according lon = 103.8617 and lat = 1.2742 and zoom = 15, you get:
n = 2^15
= 32768
xtile = 32768 * ((103.8617 + 180) / 360)
= 25837.722737778
ytile = 32768 * (1 - (ln(tan(1.2742) + (1 / cos(1.2742))) / π)) / 2
= 16268.009923134
Disregarding the decimals, we get X = 25837 and Y = 16268. Making this into a link (according to https://osmbuildings.org/documentation/data/), we get
https://data.osmbuildings.org/0.2/anonymous/tile/15/25837/16268.json
which does return JSON data for some 40 buildings.
This also worked fine for me on other zoom levels. E.g. zooming in on the famous "Marina Bay Sands" (https://www.openstreetmap.org/#map=17/1.28338/103.86148) and calculating the link according to the pseudo-code above, I get:
https://data.osmbuildings.org/0.2/anonymous/tile/17/103350.810851556/65068.69652388.json
which returns a JSON that contains named buildings such as "Marina Bay Sands Tower 1", "Marina Bay Sands Tower 2", etc., which shows it worked as intended.

Convert from one range to another

I have two sets of ranges that I need to be translated from one to the other.
The first range is -100 ↔ 100 with 0 being default.
The second range is 0.0 ↔ 10.0 with 1 being default.
I am working in AS3 with the first range and the second range is a python class and I need these numbers to line up.
I am adjusting brightness of a video in realtime with a slider. The video filter accepts values between -100 ↔ 100. I need to then take that value and pass it to a python script but it only accepts values from 0.0 ↔ 10.0
I tried this function I found on the net, but it doesn't translate the values correctly in this particular case.
private function convertRange(originalStart:Number,originalEnd:Number,newStart:Number,newEnd:Number,value:Number):Number
{
var originalRange:Number = originalEnd - originalStart;
var newRange:Number = newEnd - newStart;
var ratio:Number = newRange / originalRange;
var newValue:Number = value * ratio;
var finalValue:Number = newValue + newStart;
return finalValue;
}
Is this even possible? Hopefully my question is clear, please let me know if it needs clarification.
This is the python class I am referring to: https://github.com/dubhater/vapoursynth-adjust It uses the second range whereas AS3 uses the first range.
Why not trying something like this :
function from_AS_to_PY(as_value:Number): Number // as_value : -100 ----- 0 ----- 100
{
var py_value:Number = (as_value / 100);
py_value = (py_value <= 0 ? py_value : py_value * 9) + 1;
return py_value;
}
function from_PY_to_AS(py_value:Number): Number // py_value : 0 - 1 --------- 10
{
var as_value:Number = (py_value <= 1 ? py_value - 1 : ((py_value - 1) / 9)) * 100;
return as_value;
}
trace(from_AS_to_PY(-100)); // gives : 0
trace(from_AS_to_PY(-99)); // gives : 0.01
trace(from_AS_to_PY(-1)); // gives : 0.99
trace(from_AS_to_PY(0)); // gives : 1
trace(from_AS_to_PY(1)); // gives : 1.09
trace(from_AS_to_PY(99)); // gives : 9.91
trace(from_AS_to_PY(100)); // gives : 10
//---------------------------------------------------
trace(from_PY_to_AS(0)); // gives : -100
trace(from_PY_to_AS(0.01)); // gives : -99
trace(from_PY_to_AS(0.99)); // gives : -1
trace(from_PY_to_AS(1)); // gives : 0
trace(from_PY_to_AS(1.09)); // gives : 1
trace(from_PY_to_AS(9.91)); // gives : 99
trace(from_PY_to_AS(10)); // gives : 100
Hope that can help.
There is a fundamental difficulty with the problem. You are trying to fit three point of one range onto another range. If you were just interested in matching the two end points that would be easy you can use a linear interpolation y = 10 * (x+100) /200 or simply y = (x+100)/20 or equivalently y=x/20+5. The problem is it this does not match the default value and x=0 -> 5.
This might be the solution you want. However if it is important that he default values match you need to use a non-linear solution. There are many possible solutions. You can use a piecewise-linear solution like akmozo solution, which needs an if statement. if x<0 then y = x/100+1 else y = 1 + 9 x /100. The problem with this one is that you do not get a smooth response. Consider adjusting the slider from min to max, you see a very slow increase in brightness to start with and then it starts to increase much faster once you pass zero.
The big difference between the first half of the range and the second half suggests an exponential type solution. y = A exp(b x). Taking y = exp(x * ln(10)/100) matches the center point and the top end, the bottom end is just a little bit high at 0.1, rather than zero. This might be fine, if not you could find an exponential solution y = A exp(b x)-c which matches all three points.
Another possibility is using a power. An equation like y = A pow(x,n). A bit of calculation shows y=10 pow(x/200+0.5),3.321928095) matches all three points. The constant 3.321928095 = ln(0.1)/ln(0.5).
In the diagram the black curve is a simple linear solution. The red curve is the piecewise linear one, the green is the exponential one then the blue is the power.

Getting latitude and longitude of the Sun on a world map with PyEphem

I'm trying to determine the latitude and longitude of say the Sun, the Moon and Mars. I need the result relative to the Earth's equator and the Prime Meridian in order to produce a result similar to this map.
I believe that's also what the author of this question wanted, however the answer there doesn't add up for me (comparing with values from the first link).
Expected result, obtained from the page linked to earlier:
On Thursday, 1 January 2015, 00:00:00 UTC the Sun is at its zenith at Latitude: 23° 02' South, Longitude: 179° 29' West
>>> import ephem; from math import degrees
>>> b = ephem.Sun(epoch='date'); b.compute('2015/1/1 00:00:00')
>>> print("{},{}".format(degrees(b.dec), degrees(b.ra)))
-23.040580418272267,281.12827017399906
So the latitude/declination seems about right, but no 180° wraparound will fix that right ascension, probably because it starts at the Vernal Equinox.
I have also unsuccessfully tried to use an observer at 0,0.
Can this be done using PyEphem, Skyfield or astropy? It seems odd that artificial satellites in PyEphem have the convenient sublat and sublong attributes, but it's so hard for celestial bodies.
I finally figured it out. Sort of. Actually I just ported the relevant bits of libastro to Python. Note that this code runs against the current git version of Skyfield (be6c7296).
Here goes (gist version):
#!/usr/bin/env python3
from datetime import datetime, timezone
from math import atan, atan2, degrees, floor, pi, radians, sin, sqrt
from skyfield.api import earth, JulianDate, now, sun
def earth_latlon(x, y, z, time):
"""
For an object at the given XYZ coordinates relative to the center of
the Earth at the given datetime, returns the latitude and longitude
as it would appear on a world map.
Units for XYZ don't matter.
"""
julian_date = JulianDate(utc=time).tt
# see https://en.wikipedia.org/wiki/Julian_date#Variants
# libastro calls this "mjd", but the "Modified Julian Date" is
# something entirely different
dublin_julian_date = julian_date - 2415020
# the following block closely mirrors libastro, so don't blame me
# if you have no clue what the variables mean or what the magic
# numbers are because I don't either
sidereal_solar = 1.0027379093
sid_day = floor(dublin_julian_date)
t = (sid_day - 0.5) / 36525
sid_reference = (6.6460656 + (2400.051262 * t) + (0.00002581 * (t**2))) / 24
sid_reference -= floor(sid_reference)
lon = 2 * pi * ((dublin_julian_date - sid_day) *
sidereal_solar + sid_reference) - atan2(y, x)
lon = lon % (2 * pi)
lon -= pi
lat = atan(z / sqrt(x**2 + y**2))
return degrees(lat), degrees(-lon)
if __name__ == '__main__':
print("2015-01-01 00:00:00:")
time = datetime(2015, 1, 1, tzinfo=timezone.utc)
x, y, z = earth(JulianDate(utc=time)).observe(sun).apparent().position.au
print(earth_latlon(x, y, z, time))
print("now:")
time = datetime.now(timezone.utc)
x, y, z = earth(JulianDate(utc=time)).observe(sun).apparent().position.au
print(earth_latlon(x, y, z, time))
Output:
2015-01-01 00:00:00:
(-23.05923949080624, -179.2173856294249)
now:
(-8.384551051991025, -47.12917634395421)
As you can see, the values for 2015-01-01 00:00:00 match the reference values from the question. Not precisely, but it's good enough for me. For all I know, my values might be better.
Due to my ignorance about the undocumented magic numbers used in the libastro code, I cannot make this work for bodies other than Earth.
#BrandonRhodes: Let me know if you're interested in having this functionality in Skyfield, then I'll try to throw together a pull request.

How can I round an integer to the nearest 1000 in Pascal?

I've got a Integer variable in Pascal. Is there any possible function I can use that can round that value to the nearest 1000, for example:
RoundTo(variable, 1000);
Does anything of the sort exist? Or is there another method I should try using?
Thanks!
The general solution for this kind of problem is to scale before and after rounding, e.g.
y = 1000 * ROUND(x / 1000);
Use RoundTo(variable, 3).
The second parameter specifies the digits you want to round to. Since you want to round to 1000 = 103 you need to specifiy 3, not 1000.
The documentation for RoundTo says:
function RoundTo(const AValue: Extended; const ADigit: TRoundToEXRangeExtended): Extended;
Rounds a floating-point value to a specified digit or power of ten using "Banker's rounding".
ADigit indicates the power of ten to which you want AValue rounded. It can be any value from –37 to 37 (inclusive).
The following examples illustrate the use of RoundTo:
RoundTo(1234567, 3) = 1235000
(I left out parts not relevant to your question)
Side-note: RoundTo uses Banker's round, so RoundTo(500, 3) = 0 and RoundTo(1500, 3) = 2000.
x = 1000*(x/1000), or x = x - (x mod 1000)

Howto convert decimal (xx.xx) to binary

This isn't necessarily a programming question but i'm sure you folks know how to do it. How would i convert floating point numbers into binary.
The number i am looking at is 27.625.
27 would be 11011, but what do i do with the .625?
On paper, a good algorithm to convert the fractional part of a decimal number is the "repeated multiplication by 2" algorithm (see details at http://www.exploringbinary.com/base-conversion-in-php-using-bcmath/, under the heading "dec2bin_f()"). For example, 0.8125 converts to binary as follows:
1. 0.8125 * 2 = 1.625
2. 0.625 * 2 = 1.25
3. 0.25 * 2 = 0.5
4. 0.5 * 2 = 1.0
The integer parts are stripped off and saved at each step, forming the binary result: 0.1101.
If you want a tool to do these kinds of conversions automatically, see my decimal/binary converter.
Assuming you are not thinking about inside a PC, just thinking about binary vs decimal as physically represented on a piece of paper:
You know .1 in binary should be .5 in decimal, so the .1's place is worth .5 (1/2)
the .01 is worth .25 (1/4) (half of the previous one)
the .001 is worth (1/8) (Half of 1/4)
Notice how the denominator is progressing just like the whole numbers to the left of the decimal do--standard ^2 pattern? The next should be 1/16...
So you start with your .625, is it higher than .5? Yes, so set the first bit and subtract the .5
.1 binary with a decimal remainder of .125
Now you have the next spot, it's worth .25dec, is that less than your current remainder of .125? No, so you don't have enough decimal "Money" to buy that second spot, it has to be a 0
.10 binary, still .125 remainder.
Now go to the third position, etc. (Hint: I don't think there will be too much etc.)
There are several different ways to encode a non-integral number in binary. By far the most common type are floating point representations, especially the one codified in IEEE 754.
the code works for me is as below , you can use this code to convert any type of dobule values:
private static String doubleToBinaryString( double n ) {
String val = Integer.toBinaryString((int)n)+"."; // Setting up string for result
String newN ="0" + (""+n).substring((""+n).indexOf("."));
n = Double.parseDouble(newN);
while ( n > 0 ) { // While the fraction is greater than zero (not equal or less than zero)
double r = n * 2; // Multiply current fraction (n) by 2
if( r >= 1 ) { // If the ones-place digit >= 1
val += "1"; // Concat a "1" to the end of the result string (val)
n = r - 1; // Remove the 1 from the current fraction (n)
}else{ // If the ones-place digit == 0
val += "0"; // Concat a "0" to the end of the result string (val)
n = r; // Set the current fraction (n) to the new fraction
}
}
return val; // return the string result with all appended binary values
}