I have a php script and a database of geolocations.
At the moment my users can enter their postcode, using Yahaa I get their geo location.
Now that I have their geolocation I want to write a script that loads their local businesses in order of distance. I'm guessing to do this I need to load records from the database that have the most similar coordinates to the users current geolocation. I tried this code:
$sql = "SELECT * FROM dirlistings WHERE ABS(latitude - $lat) AND ABS(longitude - $long)";
However it just displays the results in normal order.
Have I missed anything?
I think you want an ORDER clause in there somewhere.
But what you really want is the Haversine formula: MySQL Great Circle Distance (Haversine formula)
It's more complicated than you think.
This is a great article that helped me a lot. Although its written in javascript, you easily change it to php.
http://www.movable-type.co.uk/scripts/latlong.html
Note that circle distance isn't going to be precise enough if you're talking about large distances (thousands of miles, for example), as the earth's surface isn't flat. If you need a better formula for geo-distance calculation, you can use something like this:
$dlat = ((double)$lat) / 57.29577951;
$dlon = ((double)$lon) / 57.29577951;
$sql = "SELECT *
FROM dirlistings
WHERE 3963.0 * acos(sin(latitude / 57.29577951) * sin($dlat) + cos(latitude / 57.29577951) * cos($dlat) * cos($dlon - longitude / 57.29577951)) < MAX_DIST
ORDER BY acos(sin(latitude / 57.29577951) * sin($dlat) + cos(latitude / 57.29577951) * cos($dlat) * cos($dlon - longitude / 57.29577951))
";
The distances here are in miles - make sure to specify correct max distance - and this formula will give very close results for distances of even ten thousand miles. Note though that such computation is quite time- and power-intensive and if you are not dealing with large distances (i.e. nothing more than a couple hundred miles), then you should use a quicker approximation.
You've missed quite a few things. In order to do what you're trying to do, you need to compute (using the Pythagorean theorem) the distance between two points, and then order by that distance.
You can calculate distance ( (lat - $lat)^2 + (lon - $lon)^2 )^0.5 via:
SQRT(
POW(latitude - ' . $lat . ',2)
+
POW(longitude - ' . $lon . ',2)
) AS distance
Then it's as simple as:
ORDER BY distance ASC
Try ORDER BY latitude, longitude at the end. That should do it or approximately do it.
Related
Evening,
I have a PHP script that is selecting results and attempting to filter those results based on a certain distance in miles. I have a database of results, which includes longitude and latitude data collected from Google Maps.
My query currently looks like this...
$stmt = $pdo->prepare("SELECT *,
(((acos(sin(('$latitude'*pi()/180))
* sin((latitude*pi()/180))
+ cos(('$latitude'*pi()/180))
* cos((latitude*pi()/180))
* cos((('$longitude' - longitude)*pi()/180))))
* 180/pi()) * 60 * 1.1515) AS distance
FROM directory
HAVING distance <= '$distance'
ORDER by distance ASC");
The query is returning data. However, it does not seem to filter accurately. My $distance variable is currently set to 10, so I am hoping only results within 10 miles will be returned.
Does anyone know what is going wrong here?
Thanks,
Luke
MySQL supports spatial functions since version 5.7, which relief you from the burden of typing such complicated formula. You can, for example, use st_distance_sphere() like so:
select d.*,
st_distance_sphere(point(latitude, longitude), point(:latitude, :longitude)) as distance
from directory d
having distance <= :distance * 1609.344
order by distance
st_distance_sphere() returns a value in meters, so this uses an approximation to convert the input miles value to meters.
Note that this uses named query parameters (denoted by the starting :) rather than concatenating variables in the query string: this is both safer and more efficient.
I want to calculate distance between two latitude and longitude sets.
My problem
I have 2500+ zip codes in a table and I have 10000+ shops, Every shops and zip codes have their lat, lng.
Every shop has their delivery distance in KMS.
I want to save all zip codes of a shop which falls within their delivery distance in a table named shop_delivery_zips. I can't use maps at the time on placing order, because shop might not want to deliver at every zips which falls in their delivery distance range.
So, first time i want to insert the data in shop_delivery_zips table by checking their delivery distance.
My solution
First, I thought that i will write a code which will go to every zips codes and calculate distance between shop lat, lng and zip lat,lng and will save data in shop_delivery_zips table, but if i do that, then i have to call the google apis for every time and if i do for all our data, then i have to make 2500*10000 google api calls, which google don't allow in one day, so I can't go with those option.
So, i want to know that is there any way by which i calculate the distance between two lat,lng set. May be by the calculation google use to calculate the same. Please suggest me that solution or any other solution if you know.
Please let me know, if anything is not clear.
I know of this solution to calculate as needed (taken from https://github.com/shafiqpab/distance-between-two-addresses-google-maps-api-php/blob/master/index.php):
//Calculate distance from latitude and longitude
$theta = $longitudeFrom - $longitudeTo;
$dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +
cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) *
cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.8515;
$unit = strtoupper($unit);
// for units in KM
$kilometers = round($miles * 1.609344, PHP_ROUND_HALF_UP).' km';
Is that what you are looking for?
While #tbedner has the correct solution mathematically, you do not want to perform this calculation against every location in the list, as the math is very non-trivial.
First you define a simple bounding box, eg:
lat BETWEEN $min_lat AND $max_lat
AND
lng BETWEEN $min_lng AND $max_lng
Which is very simple and can easily leverage indexes, and then you do the complicated math on the much smaller subset of locations that fall inside the box to find the ones that are within the radius you want.
I'm interested in making a site which will store several locations and needs to be able to find them based on a user's query. Similar to how sites like Yelp, Zomato, Zillow, etc. function. My preferred tech stack is PHP/MySQL.
That said, I have a few related questions. Given a database of locations with their associate latitude/longitude coordinates...
How are they able to query their database of locations and return the ones within X distance of a specific city or ZIP code?
Furthermore, how can they query their database for locations that, instead of being within proximity, are within very specific city or state limits? Where do they get their boundary data from and how do you determine if a coordinate falls within that boundary in an efficient manner?
Lastly, I notice that some of the sites have city-specific links on their site. I would have expected city data to be pulled from remote mapping APIs, but they often associate images and other content with those cities. Do these sites store the names of all cities in their own database? Where do they get their list?
This will help with your first point.
I use this query to search properties in my database by distance from a starting point. The starting point is the longitude and latitude, $coordX and $coordY. I get these from doing a geocode lookup on the Google Maps API:
SELECT properties.*,
(
( ACOS(SIN(".$coordX." * PI() / 180) *
SIN(coordX * PI() / 180) + COS(".$coordX." * PI() / 180) *
COS(coordX * PI() / 180) * COS((".$coordY." - coordY) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515
)
AS distance
FROM properties
order by distance
In my table I store the coordinates of the coordinates of individual properties in the coordX and coordY columns.
Here's MySQL function that will take two latitude/longitude pairs, and give you the distance in degrees between the two points.
It uses the Haversine formula to calculate the distance. Since the Earth is not a perfect sphere, there is some error near the
poles and the equator.
To convert to miles, multiply by 3961.
To convert to kilometers, multiply by 6373.
To convert to meters, multiply by 6373000.
To convert to feet, multiply by (3961 * 5280) 20914080.
DELIMITER $$
CREATE FUNCTION haversine(
lat1 FLOAT, lon1 FLOAT,
lat2 FLOAT, lon2 FLOAT
) RETURNS float
NO SQL
DETERMINISTIC
COMMENT 'Returns the distance in degrees on the Earth between two known points of latitude and longitude. To get miles, multiply by 3961, and km by 6373'
BEGIN
RETURN DEGREES(ACOS(
COS(RADIANS(lat1)) *
COS(RADIANS(lat2)) *
COS(RADIANS(lon2) - RADIANS(lon1)) +
SIN(RADIANS(lat1)) * SIN(RADIANS(lat2))
));
END;
DELIMITER ;
Add columns to your address table for latitude and longitude with a type of FLOAT(10,6).
Write a php script to do the lookup of the lat/long when the record is saved.
Then you can do a select against the table to get a list of addresses with distances. You can even sort
the results by distance, or limit the result to a certain radius from the reference location.
SELECT
`street`,
`city`,
`state`,
`zip`,
(haversine($ref_location_lat,$ref_location_long,`lat`,`long) * 3961) as `distance`
FROM `address_table`
WHERE (haversine($ref_location_lat,$ref_location_long,`lat`,`long) * 3961) < 300 // Example for limiting returned records to a raduis of 300 miles.
ORDER BY haversine($ref_location_lat,$ref_location_long,`lat`,`long) DESC; // Don't need actual distance for sorting, just relative distance.
I am writing a tool for a game that involves calculating the distance between two coordinates on a toroidal plane 500 units across. That is, [0,0] through [499,499] are valid coordinates, and [0,0] and [499,499] are also right next to each other.
Currently, in my application, I am comparing the distance between a city with an [X,Y] location respective to the user's own [X,Y] location, which they have configured in advance.
To do this, I found this algorithm, which kind of works:
Math.sqrt ( dx * dx + dy * dy );
Because sorting a paged list by distance is a useful thing to be able to do, I implemented this algorithm in a MySQL query and have made it available to my application using the following part of my SELECT statement:
SQRT( POW( ( ".strval($sourceX)." - cityX ) , 2 ) + POW( ( ".strval($sourceY)." - cityY ) , 2 ) ) AS distance
This works fine for many calculations, but does not take into account the fact that [0,0] and [499,499] are kitty-corner to one another.
Is there any way I can tweak this algorithm to generate an accurate distance, given that 0 and 499 are adjacent?
I assume you mean wrapping coordinates and nothing spherical shaped. Like a flat piece of paper where the ends are magically connected to each other.
That means that for a map sized 500x500, the distance in the x (or y) direction is at most 250. (If it would be more than 250 steps, we could better walk 500-x steps backward.)
A simple way to fix this, would be
dx = Math.abs(dx);
dy = Math.abs(dy);
if (dx > 250)
dx = 500 - dx;
if (dy > 250)
dy = 500 - dy;
distance = Math.sqrt ( dx * dx + dy * dy );
Update (torus):
OK, from your own comments, it seems that you do mean the torus -- the surface of a donut -- and not the sphere. (This is a big difference, and you should edit your question: calling it a sphere is wrong.)
For this, the answer is fairly simple -- the cartesian formula you give is more or less correct. However, you need to wrap distances around so that anything greater than or equal to 250=500/2 gets translated down to between 0 and 250.
So the answer is something like (I don't know PHP at all, so this may need to be modified for syntax)
dx1 = min(dx, 500-dx)
dy1 = min(dy, 500-dy);
distance = Math.sqrt(dx1*dx1 + dy1*dy1);
(This assumes that you have defined dx and dy to be the absolute value of the differences.)
Just found this routine which implements the same calculation in a nicely-packaged function.
Original Answer (sphere):
You haven't explained how your (x,y) coordinates map to points on the sphere!
There are (literally) an infinite number of choices, each corresponding to a different map projection, and the formula is different for each of them. Note that no matter what choice you make, the meaning of the two coordinates is very different.
If your (x,y) are really longitude and latitude, for example, there are plenty of canned formulae (i.e., haversine) but you'll have to first translate 0->499 to 0->360 degrees for longitude and -90->90 degrees for latitude. (Note that lon and lat behave very differently on the sphere!)
But I emphasize that any choice you make will distort from the flat geometry that you get if you plot in (x,y) versus the way it really looks on the sphere.
(Finally, if you really mean that the top edge is the same as the bottom and the right the same as the left, then you probably have a torus and not a sphere!)
If you know latitude and longitude of two points - you could use haversine formula to compute distance between two points on sphere.
But as I understood you want formula which is accurate for nearly antipodal points. Haversine formula fails here. In such case you need Vincenty's formula which is accurate even in antipodal cases.
http://en.wikipedia.org/wiki/Great-circle_distance#Formulae
It sounds like you are simply using a special finite Cartesian space that is "tiled". In this case each object does not have a unique position. Instead of (x, y) it is (x + i*w, y + j*h) for all possible integer values i and j and where w and h are the widths and heights of your "window" respectively.
Obviously then the distance is not unique but the minimum distance is which is just min(d(p1,p2))) over all i, j.
If your coordinates are wrapped then you just need to compute it for i=-1,0,1 and j=-1,0,1 then take the smallest one.
That general algorithm is fine for rectangular coordinate systems or very sort distances in spherical coordinates, but it's not appropriate for a spherical coordinate system.
I think a better approach would be based on latitude and longitude, like this:
http://jan.ucc.nau.edu/~cvm/latlongdist.html
MySQL has geo-coding built into it. Why not use that?
http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
Although some of the answers here were very close, the problem was finally solved with this SELECT segment:
SQRT( POW( LEAST( ABS($sourceXstr-cityX), ( 500 +LEAST($sourceXstr,cityX)-GREATEST($sourceXstr,cityX))) , 2 ) + POW( LEAST( ABS($sourceYstr-cityY), ( 500 +LEAST($sourceYstr,cityY)-GREATEST($sourceYstr,cityY))) , 2 ) ) AS distance
I am writing answer if two coordinates are in 2 dimensional plane where ends are not meeting each other which OP has not asked. But it might help someone in future.
If your points are in a 2 dimensional plane, the distance between points (x1, y1) and (x2, y2) is given by the Pythagorean theorem:
d = squareroot( square(x2 - x1) + square(y2 - y1) )
In PHP,
$x1 = $y1 = 2;
$x2 = $y2 = 5;
$distance = sqrt( pow(($x2-$x1),2) + pow(($y2-$y1),2) );
echo $distance; // 4.2426406871193
I have just been developing a postcode distance calculator for my Dads company, where all our customers are kept on file and every time a new potential customer makes an enquiry the system will check against all other customers' postcodes. The problem is that it doesn't yet do the distance calculation. Typing in a DT1 postcode with the distance set to 5 miles says that DT1, 2 and 3 are close by. DT2 says DT1 and 2, DT3 says DT1, 3 and 4. This doesn't make any sense. Yet if I put in BH2 with a distance of 50 miles it will bring back Bournemouth, Dorchester, Portsmouth, Bath, Southampton and a couple of other towns. These are all correct - however I do not know what is going on with the DT postcodes. DT11 doesn't bring back any (under 5 miles), nor does DT10 (except for themselves).
I first of all used the root of (x * x + y * y) - I believe this is called Pythagoras' theorem.. not sure since it have been years since I used that term!!
I have also tried the following:
$sinstuff = sin(deg2rad($latitude))*sin(deg2rad($latitude2));
$cosstuff = cos(deg2rad($latitude))*cos(deg2rad($latitude2))*cos(deg2rad($longitude-$longitude2));
$sumstuff = $sinstuff + $cosstuff;
$acosstuff = rad2deg(acos($sumstuff));
$distance = $acosstuff*60*1.1515; //dunno what the 60 and 1.1515 is all about - I got this formula off a website.
This is the formula which seems to be bringing up the weird results. I also notice that the exact distance between DT11 and DT11 is something like +/- 0.00000989{and lots of other numbers}. This also seems a bit odd since, surely, the distance between DT11 and itself is 0...
Has anyone ever done anything like this successfully before?
I do have a massive batch of files - about 158MB in total - containing every UK postcode in full, with its corresponding latitude and longitude. My aim is, rather than comparing the specified full postcode to every other full postcode, to work out which area codes are less than a certain distance away then compare the specified postcode to all the full postcodes which are within these areas. Is this efficient? Is there a more efficient way?
Any help with this would be greatly appreciated.
Regards,
Richard
PS I know there are some sites out there which tell you how to work out distances but I dont seem to be able to get any of them working correctly (as you can tell from above).
PPS I know I can use the Google API for this - but this is not an option since this is going to be an offline application running on a local installation of wamp server. It is for use at trade shows where we cannot guarantee an internet connection.
You're use the haversine formula:
$l1 ==> latitude1
$o1 ==> longitude1
$l2 ==> latitude2
$o2 ==> longitude2
(taken from http://www.go4expert.com/forums/showthread.php?t=339)
function haversine ($l1, $o1, $l2, $o2)
{
$l1 = deg2rad ($l1);
$sinl1 = sin ($l1);
$l2 = deg2rad ($l2);
$o1 = deg2rad ($o1);
$o2 = deg2rad ($o2);
return (7926 - 26 * $sinl1) * asin (min (1, 0.707106781186548 * sqrt ((1 - (sin ($l2) * $sinl1) - cos ($l1) * cos ($l2) * cos ($o2 - $o1)))));
}
That should give you the distance between both points.
I think the Post Office maintains a Postcode-Address file which lists
every UK postcode and its associated address (not sure if it has
lattitude and longitude information). I've seen copies of this (maybe
not totally up to date) on cover discs on various PC magazines in the
past. I'm sure you have to pay for the latest version, but you might be
able to get a starting point by checking out the cover discs from back
issues.