Is there a formula to change a latitude and longitude into a single number?

筅森魡賤 提交于 2019-12-04 10:52:27
AlexWien

A latitude an longitude, can both be represented as 4 byte integer, such that the coordinates has an accuracy of 3cm which is sufficent for most applications.

Steps to create one 8 byte value of type long from latitude and longitude:

1) convert lat and lon to int by: int iLat = lat * 1E7;
2) Use a 8 byte long value to store both 4 byte int.
set upper 4byte to latitude, and lower 4 to longitude.

Now you have a 8 byte long representing a point on world up to 3cm accuracy.

There are other, better solutions, such ones that maintain similar numbers for near locations, but these are more complex.

You can add them up, but it makes little sense.

For instance a total of "10" - 8 lat and 2 long would then be the same as "10" - 3 lat and 7 long.

You can concatenate them, maybe with a dash.

But why do either? They are both really bad choices. A delivery system would want real x-y co-ordinates and if planning a route would want them seperate in order to calculate things like Euclidean distances.

Is this a homework question? I doubt a delivery service is designing their service structure on SO. Least hope not.

Steve Spencer

How about this:

(lat+90)*180+lng

From Tom Clarkson's comment in Geospatial Indexing with Redis & Sinatra for a Facebook App

If you want to treat location as "one thing", the best way to handle this is to create a data structure that contains both values. A Class for OO languages, or a struct otherwise. Combining them into a single scalar value has little value, even for display.

Location is a really rich problem space, and there are dozens of ways to represent it. Lat/Lon is the tip of the iceberg.

As always, the right answer depends on what you're using it for, which you haven't mentioned.

sirlunchalot

Based on AlexWien's anwser this is a solution in JavaScript:

pairCoordinates = function(lat, lng) {
  return lat * 1e7 << 16 & 0xffff0000 | lng * 1e7 & 0x0000ffff;
}

I have created a method of putting the latitude and longitude into one base-36 number which for now I'm calling a geohexa.

The method works by dividing the world into a 36 x 36 grid. The first character is a longitude and the second character is a latitude. The latitude and longitude those two characters represent is the midpoint of that 'rectangle'. You just keep adding characters, alternating between longitude and latitude. Eventually the geohexa, when converted back to a lat and lon will be close enough to your original lat and lon.

Nine characters will typically get you within 5 meters of a randomly generated lat and lon.

The geohexa for London Bridge is hszaounu and for Tower Bridge is hszaqu88.

It is possible to sort the geohexa, and locations that are near each other will tend to be next to each other in a sorted list to some extent. However it by no means solves the travelling salesman problem!

The project, including a full explanation, implementations in Python, Java and JavaScript can be found here: https://github.com/Qarj/geohexa

In a nutshell:

Let X,Y be latitude,longitude

Truncate both to the 5th decimal point and convert to integers multiplying by 100000

Let XY = X+Y and YX = X-Y

Convert XY,YX to binary, and merge them into XYX by alternating the bits

Convert XYX to decimal

Add an extra number (1,2,3,4) to indicate when one or both XY,YX are negative numbers.

Now you have a single number that can be converted back to latitude,longitude and which preserves all their positional properties.

You can use the Hilbert space filling curve to convert latitude,longitude into a single number: e.g., https://geocode.xyz/40.71413,-73.96141?geoit=xml 2222211311031 and https://geocode.xyz/40.71413,-73.96151?geoit=xml 2222211311026

The source code is here: https://github.com/eruci/geocode

I found I can get good results by adding the latitude and longitude of a particular address by not including the house number and sorting the results in the database table by the added number following by a 2nd sort on the house number in ascending order.

I used this url to get the numbers I needed to add together:

http://where.yahooapis.com/geocode?q=stedman+st,+lowell,+ma
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!