I'm trying to match latitude DECIMAL (10,8) and longitude DECIMAL (11,8) in my database by latitude and longitude that only have 4 decimal places. I'm trying to compare addresses but since addresses are all in different formats, I figured latitude and longitude is the next best thing.
So right now I have to do this:
select *
from locations
where (latitude BETWEEN 33.29940 AND 33.29949) AND
(longitude BETWEEN -111.89889 AND -111.89880)
Where I search between 0 and 9 of each number. But it's pretty clunky. I'd rather be able to do something like LIKE %33.2994% instead.
So what's the best way (performance-wise and easy to read) to search these latitude and longitude fields?
Related
I stored the latitude and longitude from a lot of gasstations in a database. I also made a Google Maps. Now, I want to select the gasstations from my database where latitude and longitude are between the North East Latitude / Longitude and South West Latitude / Longitude from my Google Maps.
How can i calculate which gasstations I have to select?
Simply modify your database query in the backend to only return locations between your points. Something like the first answer here.
Obviously the syntax will depend on the database that you are using.
Actually I would suggest using:
SELECT * FROM tilistings WHERE lat BETWEEN a AND c AND lng between b AND d
Where a and b are you top left coordinates and c and d are your bottom right. I think BETWEEN is more performant.
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.
I'm working on a simulator that plots the flight path of an aircraft on Google Maps.
The simulator is not aware that the latitude is only defined between -90 and +90 degrees and the longitude between -180 and +180 deg. As a result of this, the flight path may include points beyond the map boundaries. Exceeding in longitude is not an issue as it still plots correctly (a point at longitude x and x+360 is the same), but the latitude is a problem.
Is there any way of telling Google Maps to keep the points between the correct boundaries and plot them correctly?
Otherwise, do you have any ideas of where to find functions that do so?
Longitude, latitude and elevation are a bad coordinate system for a flight simulator, because the mapping presents singularities i.e. there are points infinitely close on the earth that have very different coordinates. For example where you're close to one of the poles longitude variation speed can become arbitrarily big compared to airplane speed. When standing exactly on the pole the longitude doesn't even make sense.
A better solution is to use an XYZ coordinate system for the simulator and only convert to longitude/latitude and elevation for plotting. If you can approximate the earth to a sphere for your use case the computation of this transformation is trivial... otherwise things can get much more complex depending on how accurate you want it to be.
That said it's still possible to give "a" meaning to a point with latitude slightly outside the range -90...90 by extending it over the pole...
if latitude < -90:
latitude = -180 - latitude
longitude = longitude + 180
if latitude > 90:
latitude = 180 - latitude
longitude = longitude + 180
but using this coordinate system for navigating is a very bad idea (the same point in space can have multiple triplets of coordinates).
If your simulator doesn't know that the maximum value for latitude is 90 degrees it is broken and needs to be fixed. Google Maps works correctly for valid/possible values of latitude and longitude.
I have latitude and longitude columns in my table.
I'm currently storing latitude with float(16,14) and longitude with float(17,14). Is that the best way to store them? The values I'm inserting are from JS navigator.geolocation, and they don't tend to have more than 14 digits after the decimal place.
Should I be using decimal instead?
You should use decimal! Read on this: Problems with Floating-Point Values
The DECIMAL type stores exact numeric data values (if you use MySQL5.0.3 or above), the FLOAT type represents approximate numeric data values.
Since as far as I know latitude and longitude values are usually exact numeric data values for most use cases, I'd go for a DECIMAL.
I have a MySQL database that looks as such:
Postcode int(4),
City varchar,
State varchar,
Latitude decimal(7,4),
Longitude decimal(7,4)
I want the user to enter their post code (1660 for example) and a radius of x (lets say 10) miles or kilometers. When they hit search, I want to return a list of all the cities within that radius. I have a database that contains all of the post codes, cities, latitudes, longitudes etc. of all areas within Australia.
If you could sacrifice some precisions (by selecting cities within a rectangle area 10 miles from post code 1660), then the solution can be as simple as:
Find out the latitude and longitude of post code 1660
Calculate the top left and bottom right of the rectangle area (10 miles to the left and top, 10 miles to the bottom and right)
Use the query like: select city from table where latitude between bottom_right_latitude and top_left_latitude and longitude between top_left_longitude and bottom_right_longitude
If it has to be precisely radius 10 miles (which means the area is a circle), then the solution will be very complicated, couldn't do it in a single query I afraid. You need to think about some helper columns and use the distance and bearing calculations formulas to do it.
Reference: Calculate distance, bearing and more between Latitude/Longitude points