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

自古美人都是妖i 提交于 2019-12-06 06:05:58

问题


Can you tell me if there is a formula to change a latitude and longitude into a single number?

I plan to use this for a database table in software that provides routing for deliveries. The table row would have that number as well as the postal address. The database table would be sorted in ascending numeric order so the software can figure out which address the truck would need to go to first, second etc.

Please can you respond showing VB or VB.Net syntax so I can understand how it works?

For example I would use the following numbers for the latitude and longitude: Lat = 40.71412890 Long = -73.96140740

Additional Information:

I'm developing an Android app using Basic4Android. Basic4Android uses a VB or VB.Net syntax with SQLite as the database.

Part of this app will have route planning. I want to use this number as the first column in an SQLite table and the other columns will be for the address. If I do a query within the app that sorts the rows in numerical ascending order, I will be able to figure out which postal address are closest to each other so it will take less time for me to go from house to house.

For example, if the numbers were: 194580, 199300, 178221

I can go to postal address 178221 then to 194580 and finally to 199300 and I won't need to take the long way around town to do my deliveries after they were sorted.

As an alternative, I would be happy if there was an easy way to call a web service that returns maybe a json response that has the single number if I send a postal address to the web site. Basic4Android does have http services that can send requests to a web site.


回答1:


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.




回答2:


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.




回答3:


How about this:

(lat+90)*180+lng

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




回答4:


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.




回答5:


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

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



回答6:


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




回答7:


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.




回答8:


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




回答9:


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


来源:https://stackoverflow.com/questions/8285599/is-there-a-formula-to-change-a-latitude-and-longitude-into-a-single-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!