I'm creating a webapp where I want to do a search by radius based on latitude and longitude. However, if a user enters just they're city and state, a lot of cities will match multiple zipcodes, which in turn will match multiple latitude and longitude points. My question is, what is the recommended way to deal with multiple latitudes? Is taking the average an option? I assumed that would violate principles of distance calculation..
Typically a radius based search is done when you only have one latitude /longitude point. Otherwise, if you have multiple points then they are bound to form a polygon. You can then do bounds calculations to determine if a given point is within the boundaries of the polygon. You could also set a threshold distance outside of the polygon.
Related
I want to calculate distances between two coordinates but sometimes the origin coordinate is over / beyond the destination coordinate. And it is really problematic at one-way roads, because in this case the distance can be eg. 1 km to get back to the destination. In real life it is OK, but actually the real distance is only eg. 10 meters (if I could turn back on one-way roads). So only the direction is wrong.
I can resolve this problem if I call the API twice (origins=my_origin_coordinate&destinations=my_destination_coordinate and origins=my_destination_coordinate&destinations=my_origin_coordinate).
But is there a easier way to get both distance in one query? (to save my query limit...)
The distance matrix allows multiple results in a single query. You can do origins=my_origin_coordinate|my_destination_coordinate&destinations=my_origin_coordinate|my_destination_coordinate in a single request, which will give you both results. That won't really help with your quota though as the quota is based on "elements", and whether you do it in one query or two, it will be the same number of "elements".
In a MySQL database, how do I find circular areas that fall entirely or partially within a certain distance from another point? There are plenty of examples to find points within a certain radius, but not circular areas that intersect that certain radius.
I have a list of contractors that service certain areas (point and radius). Customers need to be able to find these contractors based on a distance from them.
I think you are looking for ST_Buffer, which will buffer a geometry by a certain distance. In your case this will turn your point into a circle, and you can then use ST_Intersects to find intersecting circles representing contractor areas.
Something like:
Select id from contractor c where intersects(c.geom, st_buffer(point, radius));
where obviously you need to provide values for point and a radius.
I need some help, I've never done my own SQL search before and I'm trying to do this:
I have a database of names and locations (the locations are listed with a Latitude record and a Longitude record). Then, a user can search by entering their zip code (which is converted to longitude and latitude) and a distance they're willing to travel (in miles, which I can convert to lon/lat distance).
How can I return the results ordered by the distance away from their ZipCode?
Please keep in mind, I haven't ever done anything like this before.
There's a mathematical formula for figuring the shortest distance between two points on a sphere. The formula and a JS implementation of it are here:
http://www.movable-type.co.uk/scripts/latlong.html
A T-SQL implementation is here:
http://weblogs.asp.net/jimjackson/archive/2009/02/13/calculating-distances-between-latitude-and-longitude-t-sql-haversine.aspx
I would like to store thousands of latitude/longitude points in a MySQL db. I was successful at setting up the tables and adding the data using the geospatial extensions where the column 'coord' is a Point(lat, lng).
Problem:
I want to quickly find the 'N' closest entries to latitude 'X' degrees and longitude 'Y' degrees. Since the Distance() function has not yet been implemented, I used GLength() function to calculate the distance between (X,Y) and each of the entries, sorting by ascending distance, and limiting to 'N' results. The problem is that this is not calculating shortest distance with spherical geometry. Which means if Y = 179.9 degrees, the list of closest entries will only include longitudes of starting at 179.9 and decreasing even though closer entries exist with longitudes increasing from -179.9.
How does one typically handle the discontinuity in longitude when working with spherical geometries in databases? There has to be an easy solution to this, but I must just be searching for the wrong thing because I have not found anything helpful.
Should I just forget the GLength() function and create my own function for calculating angular separation? If I do this, will it still be fast and take advantage of the geospatial extensions?
Thanks!
josh
UPDATE:
This is exactly what I am describing above. However, it is only for SQL Server. Apparently SQL Server has a Geometry and Geography datatypes. The geography does exactly what I need. Is there something similar in MySQL?
How does one typically handle the discontinuity in longitude when working with spherical geometries in databases?
Not many people use MySQL for this, because it's geospatial extensions aren't really up to snuff.
From the docs:
"All calculations are done assuming Euclidean (planar) geometry."
The solution is usually to roll your own.
Alternatively, you can fake it -- if your distances are less than a 500 miles or so, then you can treat your latitude and longitude as rectangular coordinates and just use the euclidean distance formula (sqrt(a^2 + b^2)).
Given the following input:
known longitudes/latitudes of 1..n locations
known distance between locations 1..n and another location "m"
How can I calculate the longitude/latitude of the location "m"?
This sounds like a basic latitude-longitude triangulation question. The common approaches are outlined in a Yahoo! Answers topic here. There are likely libraries to do this in many languages. A google search for "latitude longitude triangulation" plus your language of choice will likely reveal some existing code to use. "Geocoding" is another common task rolled into similar libraries, so that may be another useful keyword.
Edit: As others have mentioned, "trilateration" seems to be the best term. However, depending on your data and requirements, there are simpler approximation solutions that may satisfy your requirements.
The Yahoo! Answers post is quoted below for convenience:
"For larger distances, spherical
geometry. For relatively small ones,
treat the earth as flat, and the
coordinates as xy coordinates. For the
distances to work with the degrees of
the coordinates, you will have to use
the cosine function to convert from
one to the other. (While degrees of
latitude are about 69 miles all over
the earth, degrees of longitude vary
from the same at the equator to 0 at
the poles.)
You have the center points of three
circles and the radius of those
circles. They are supposed to
intersect at one point, so you can
treat them in pairs to find the
intersection points of each and throw
out the ones that don't match
http://mathworld.wolfram.com/Circle-CircleIntersection.html."
(mike1942f)
Trilateration is what you want. This only requires 3 of your reference points, however the rest can be used to increase accuracy if you want to get really clever.
The trickiest part is working with long/lat as opposed to Cartesian coordinates, especially as the earth is not a perfect sphere.
This is a trilateration problem. In your case, you have multiple points of reference, so you can minimize the sum of squared-errors between the given distances and those corresponding to the optimal position of m.