Suppose a service like Foursquare, I want to save the location of a check-in. Should I save this as latitude-longitude or the address/area name such as 123 Portmill St, NY 12345 or SoHo, NY.
In the first case, I can have users type an address and my service looks up and stores lat-long information. By doing this my service can search check-ins within a polygon-boundary.
While on the second case, I can store location as a bucket and avoid redundant information such as (lat,long) = (100000.1,100000.1), (100000.2,100000.2) which are very close together and can even be considered the same location.
I don't think I've completely understood the nuances of what you're trying to do, but computers usually work better with numbers like latitude and longitude rather than human-readable text information. For example, with the text address, how would you ensure consistency, e.g. dealing with extra spaces, ZIP+4 codes rather than just shorter zip codes etc etc.
I guess it's just my instinct that latitude and longitude might be better than text. Where I live in the UK, there are a lot of examples where towns and cities have two roads of the same name, so I do think that there are likely to be more pitfalls with storing text instead of latitude/longitude.
How about this. I'd store 3 columns in the database, namely (Latitude, Longitude, TextLocation), but it's the pair (Latitude, Longitude) that's regarded as the key of the table. The TextLocation is just the last known result from the reverse geocoder when the geocoder was asked for the text corresponding to the given (Latitude, Longitude).
When a new position arrives, (New_Latitude, New_Longitude), I'd search the database to find all closest rows in the database. To calculate distance of (New_Latitude, New_Longitude) to (Latitude, Longitude), I'd use the following code
float LatDiff = New_Latitude - Latitude;
float LongDiff = New_Longitude - Longitude;
float CosNewLat = Math.cos(New_Latitude);
float ConversionFac = 6371000 * Math.PI / 180; // 6371000 is earth radius in metres
float Dist_metres = ConversionFac * Math.sqrt(LatDiff*LatDiff + LongDiff*LongDiff*CosNewLat*CosNewLat);
Then for each of the closest points to (New_Latitude, New_Longitude), I'd update the TextLocation in the database using reverse geocoding. If the new position doesn't match the current reverse geocoding for any of the existing locations, I'd add it into the database.
Part of my thinking here is that even storing 3 columns which includes a column of text, the database is still going to be tiny compared to modern storage capacities.
Related
All of my lat/lng locations have been taken off of Google Earth using the mouse.
When I enter the data into Google Maps I have multiple instances where the marker does not show the proper location. For example: a marker for lat 38.015986 lng -84.355413 (coordinates for The Aviation Museum of Kentucky) shows up at 38.005754, -84.211968 (again using my mouse pointer to provide lat/lng of marker.)
I can't use geocode because many of my locations do not have addresses - in road medians, internal to 430 acre park, etc.
How do I correct for (or get) the proper lat/lng?
Possibly you have the units of your Lat Long incorrectly entered.
Lat and long can be entered as
hdd.ddddd (decimal degrees)
hddd'mm.mmm' (degrees with decimal minutes)
hdd'mm'ss.ss'' (degrees, minutes seconds)
This seems likely, since you get close to the correct answer.
If you need to convert between these or any positional grid system and datum I recommend using Garmin Basecamp, it is free.
I'm trying to generate some annotations on a map. Currently, I'm using openstreetmap but the query is general. I have two end points on a map and a corpus of few selected points I would like to highlight.
The two endpoints are given in the form lat, long
<walkSteps>
<distance>9.742464221826811</distance>
<streetName>5th St NW</streetName>
<absoluteDirection>EAST</absoluteDirection>
<stayOn>false</stayOn>
<bogusName>false</bogusName>
<lon>-84.3937361115149</lon>
<lat>33.77692965678444</lat>
<elevation/>
</walkSteps>
<walkSteps>
<distance>508.2608917548245</distance>
<relativeDirection>LEFT</relativeDirection>
<streetName>Fowler St NW</streetName>
<absoluteDirection>NORTH</absoluteDirection>
<stayOn>false</stayOn>
<bogusName>false</bogusName>
<lon>-84.39363494600667</lon>
<lat>33.77692904176358</lat>
<elevation/>
</walkSteps>
My aim is to highlight those points on the map, which are present in the corpus and lie in the line connecting these two points.
How can I go about querying the corpus for the same? Annotating on map given lat, lng is not an issue
Rounding errors will prevent you from directly doing as you want. What you should be doing instead is determining the great-circle path between the two end points and highlighting those members of the corpus which are within a certain distance of the great circle route. This is known as the cross-track distance or cross-track error. Formulas for computing the cross-track distance can be found at one of the standard reference sites for geospatial equations but there are others as well.. The problem then becomes one of searching for points in the corpus which are close enough to the great circle path between the two end points.
I have this file from tsp.gatech but the lat lng coordinate is divide into two half. Why is this?
COMMENT: Created July 7, 2012, www.tsp.gatech.edu/data/usa/
1 33613.158800 86118.306100
2 33100.954000 85529.675300
3 31571.835200 85250.489300
For example the first coordinate should be 33.613158800 86.118306100.
Update: I searched for New York City and I found it lat lng coordinate to be similar.
Update 2: I think it's incorrect formated see this image of points: http://www.tsp.gatech.edu/data/usa/img/usa115475_large.jpg. I get the points from a file from this website: http://www.tsp.gatech.edu/data/usa/index.html. The site is about a challenge and the file I downloaded is usa115475.tsp.
Euclidean Distance would tend to suggest the values are X,Y distances from a reference point (in feet, meters, kilometers, miles, ...). But this is normally reserved for small scale mapping where the effects of the curvature of the earth can be considered minor.
If the data seems to correspond to decimal degrees that are incorrectly formatted, there could be an error in whatever system is returning the data. But its better to review your own processes before pointing the finger. What query/process/code are you doing to obtain this data?
In my project I have to find [latitude, longitude] coordinate(s) from one point in distance of 500 meters (this could be any random coordinate or an array of coordinates around my point). How can I do this?
Note: I need this in order to find multiple paths between points different from shortest one which is returned us via Google Maps Directions Api..So using my method I will define the center of the road from A to B and then find some coordinates below and above that center position and use this as another waypoint to go from A to B - I guess this might help me to find multiple paths...
Any suggestions from GIS professionals?
EDIT: UTM conversion is the most preferable one for such calculations, and I've created UTM Java class if anyone needs..
If I understand your question right you have a known point in Lat/Long and you need calculate the Lat/Long of another point or points 500m away from your starting point.
If this is what you are doing, you have several options most of which involve specialist GIS APIs. However, I'm guesing you're a programmer/mathematician rather than a Geographer so, you may prefer to opt for using the Haversine formula. You can find a discussion on this topic here plus the formula.
One caveat is that the distamce you are working with (500m is quite small) and the Earth is far from being a perfect sphere or even a slightly flattened spheroid. It is locally "lumpy" and that can put your calculation out. If you need more accuracy you will have to account for these imperfections by using an appropriate local Datum (model of the Earth - there are many e.g. see EPSG list) and to do that you will probably need to start using the GIS libraries as the maths gets very detailed otherwise.
This is the code used by google map (SphericalUtil.java)
// from SphericalUtil.java
// compile 'com.google.maps.android:android-maps-utils:0.4.4'
public static LatLng computeOffset(LatLng from, double distance, double heading) {
distance /= 6371009.0D; //earth_radius = 6371009 # in meters
heading = Math.toRadians(heading);
double fromLat = Math.toRadians(from.latitude);
double fromLng = Math.toRadians(from.longitude);
double cosDistance = Math.cos(distance);
double sinDistance = Math.sin(distance);
double sinFromLat = Math.sin(fromLat);
double cosFromLat = Math.cos(fromLat);
double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * Math.cos(heading);
double dLng = Math.atan2(sinDistance * cosFromLat * Math.sin(heading), cosDistance - sinFromLat * sinLat);
return new LatLng(Math.toDegrees(Math.asin(sinLat)), Math.toDegrees(fromLng + dLng));
}
to use it, you just have to enter the centerLatLng, the distance in meters, and the heading in degrees from centerLatLng.
you can change the formula to the language of your preference.
I have a list of zipcoded in a MySQL Database together with their Latitude & Longitude data (Column names: ZipCode, Lat, Lon).
Now I have to make a search requests (search for the zipcode) to extract information from a website. When I make this search requests the results include all information within a radius of 50km of the zipcode.
Now, I don't want to make an unnessary high amount of search requests, so I would like to minimize the amount of zipcodes. So I'm looking for a way to filter all zipcodes, so that I have only the zipcodes where the distance between them is >50km.
Unfortunately I have no idea how to to it.
Can someone help me to solve this?
You may be interested in checking out the following presentation:
Geo/Spatial Search with MySQL by Alexander Rubin
The author describes how you can use the Haversine Formula in MySQL to limit your searches to a defined range. He also describes how to avoid a full table scan for such queries, using traditional indexes on the latitude and longitude columns.
You can use the google geocoding api , it allows you to get distances between 2 locations (lat/long, it also allows you to get zip from lat/long). From this you should be able to get the distance between each of your zipcodes and put them into a table, then you can do searches on just these.
Well, I see no other way then to iterate all rows on each request and filter them by calculating distance between selected zipcode and others (all of them), based on Lat & Lon.
I am using something similiar...
http://webarto.com/googlemaps
http://webarto.com/izrada-web-stranica/belgrade
PHP function for distance between two LL...
function distance($lat1, $lon1, $lat2, $lon2){
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return round($miles * 1.609344,3);
}
I calculate it this way...
$sql = mysql_query("SELECT * FROM geoip WHERE city = '$city'");
while($row = mysql_fetch_array($sql)){
$ll = explode(",",$row["ll"]);
$x = distance(44.5428009033,18.6693992615,$ll[0],$ll[1]);
$road = intval($x+($x/3));
echo "Distance between ".$row["city"]." and Tuzla is ".$x." kilometers of airline, that's about ".$road." kilometers of road way.";
}
Daniel's link deals with selecting all the zip codes within 50km of a given latitude/longitude. Once you can do that, you can build a filtered list of zipcodes like this...
Select a zip code at random and add it to the filtered list
Delete all zip codes which lie within 50km of the selected zip code
Select a new zip code at random from the remaining zip codes, repeat until no more are left.
You know that you're only picking zip codes that are >50km from the ones already picked, and you know that once the original table is empty it must be because all zip codes lie within 50km of at least one of your selected zip codes.
That doesn't guarantee the smallest possible list of zip codes, and the size of the result will depend on the random choices. However, I think that this simple algorithm is likely to be "good enough", and that saving a few searches wouldn't justify the extra effort involved in finding a truly optimal solution.
The problem has been discussed previously here on SO with various solutions
I had a similar problem and I used this solution to find the answer. Not sure if you are using java or some other language but the logic can be used in any programming language
Geo Location API and finding user within a radius