Using longitude/latitude to calculate OSGR - php

Our database currently stores 2 values - the longitude and the latitude. E.g.:
Longitude: -0.310150 N
Latitude: 52.688930 W
What we would like to do now is convert these values into the Ordanance Survey Grid Reference (OSGR) for British locations. Is there an easy way to do this in PHP?

The formula for this conversion is pretty complex because osgr and gps use different projections
This page provides an example in C that should be easy enough to re write in PHP.

Related

UK Postcode - Sort by Distance [duplicate]

I am looking for a way to calculate approximate distance between two UK postcodes (distance in the straight line is good enough) for analysing data. Preferably easily accessible from java, but C#, native C++ etc. are fine as well.
First, you need to translate the postcode into useful coordinates. For example, the Easting and Northing values from a Postcode lookup table, like the one from here: http://www.doogal.co.uk/UKPostcodes.php
These Easting and Northing are UK Ordnance Survey grid coordinates in metres from the OS map origin.
Convert them into Kilometres by dividing by 1000
Then use a simple Pythagoros triangle formula. Say the two points have Easting and Northing values (in kilometres) of E1, N1 and E2, N2
Distance between them in Kilometres = Square root of ( abs(E1-E2)^2 + abs(N1-M2)^2 )
The following question answers the exact same question only in PHP specifically:
Using PHP and google Maps Api to work out distance between 2 post codes (UK)
It leverages a web service API though, so you should be able to use any basic rest api to leverage it (I'm sure there are well documented options in Java, C# and C++).

Search query based on distance

how do I write a query based on distance, its a bit hard to explain but heres what I am looking for
On websites like gum tree, there is an option which says "WITH IN MILES" +10 miles, +5 miles e.t.c.
How do I calculate the area based on the city or postcode provided by the user.
How will the query execute and return results based on this search. thank you.
Im using PHP and MYSQL for this project.
Distances like that are calculated using the haversine formula using the latitude and longitude of two points. The lat and long data is widely available through simple Google searches.

Compare GPS co-ordinates from two seperate arrays

I am new to PHP, and am attempting to build a basic php website for a university project. The aim of the site is to compare GPS co-ordinates (using the abs() function) in order to find the closest parking space to a given landmark.
I have all of the GPS Co-ordinate data stored in my database, and can so far manage to retrieve it using mysql_fetch_array functions and echo it, but I am unsure of how to isolate a given landmark or parking from an array, and then compare the two.
Any suggestions would be greatly appreciated.
you could use the distance formula as a measure of closeness: d = sqrt((ax-bx)^2 + (ay-by)^2)
where (ax, ay) and (bx, by) are the coordinates of landmark and parking. you can also remove the sqrt() function altogether since you will just be comparing the distances for other coordinate pairs.

Structuring and searching SQL database/table for GPS coordinates

I created a database in SQL locally on my phone for an app and then information is uploaded to a database on a server in the same format. In the tables I have GPS coordinates structured as a string with a tab between latitude and longitude. Looking back it makes more sense to have the database structured so that latitude and longitude are in two separate columns as type doubles (seems like it would perform better for searching and sorting).
Would it be much faster for sorting if I turned the string into two Doubles latitude and longitude columns? Is it even possible to search GPS coordinates in this format: "x.xxxx y.yyyy"? Is there a better way to sort/store GPS coordinates in SQL tables for searching?
Looking back, I wish I had planned this out more carefully. I know planning saves you lots of time down the road. Well, I suppose that applies here.
Yes, you should use separate columns for lat/lon.
Floating point representation may or may not be a good idea; when searching for exact coordinates this would not work too well. When looking for a range of values float will be just fine.
Depending on the size of your table and query patterns you may consider creating indexes for performance.
You can use microdegrees to store the values efficiently as integers.
int lat = (int)(location.getLatitude() * 1000000);
int lon = (int)(location.getLongitude() * 1000000);
There is a bonus if you are going to add a MapView later on, because Android Maps API is using the same number format in GeoPoint
Yes if you need to use the two numbers separately in a calculation they should be kept separately.
If you don't care about the accuracy of your numbers then use type Double but be warned numbers are stored as an approximation and not as they appear.
If you require exact output as input then try using Decimal or Numeric as types.
More information here: MYSQL precision math

Finding cities close to one another using longitude and latitude [duplicate]

This question already has answers here:
MySQL Great Circle Distance (Haversine formula)
(9 answers)
Closed 2 years ago.
Each user in my db is associated to a city (with it's longitude and latitude)
How would I go about finding out which cities are close to one another?
i.e. in England, Cambridge is fairly close to London.
So If I have a user who lives in Cambridge. Users close to them would be users living in close surrounding cities, such as London, Hertford etc.
Any ideas how I could go about this? And also, how would I define what is close? i.e. in the UK close would be much closer than if it were in the US as the US is far more spread out.
Ideas and suggestions. Also, do you know any services that provide this sort of functionality?
Thanks
If you can call an external web service, you can use the GeoNames API for locating nearby cities within some radius that you define:
http://www.geonames.org/export/web-services.html
Getting coordinates from City names is called reverse geo coding. Google maps has a nice Api fot that.
There is also the Geonames project where you get huge databases of cities, zip codes etc and their cooridnates
However if you already have the coordinates, its a simple calculation to get the distance.
The tricky thing is to get a nice performant version of it. You probably have it stored in a mysql database, so you need to do it there and fast.
It is absolutely possible. I once did a project including that code, I will fetch it and post it here.
However to speed things up I would recommend first doing a rectangular selection around the center coordinates. This is very, very fast using bee tree indexes or even better stuff like multidimensional range search. Then inside that you can then calculate the exact distances on a limited set of data.
Outside that recangular selection the directions are so vast that it does not need to be displayed or calculated so accurately. Or just display the country, continent or something like that.
I am still at the office but when i get home i can fetch the codes for you. Int he meantime it would be good if you could inform me how you store your data.
Edit: in the mean time here you have a function which looks right to me (i did it without a function in one query...)
CREATE FUNCTION `get_distance_between_geo_locations`(`lat1` FLOAT, `long1` FLOAT, `lat2` FLOAT, `long2` FLOAT)
RETURNS FLOAT
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE distance FLOAT DEFAULT -1;
DECLARE earthRadius FLOAT DEFAULT 6371.009;
-- 3958.761 --miles
-- 6371.009 --km
DECLARE axis FLOAT;
IF ((lat1 IS NOT NULL) AND (long1 IS NOT NULL) AND (lat2 IS NOT NULL) AND (long2 IS NOT NULL)) THEN -- bit of protection against bad data
SET axis = (SIN(RADIANS(lat2-lat1)/2) * SIN(RADIANS(lat2-lat1)/2) + COS(RADIANS(lat1)) * COS(RADIANS(lat2)) * SIN(RADIANS(long2-long1)/2) * SIN(RADIANS(long2-long1)/2));
SET distance = earthRadius * (2 * ATAN2(SQRT(axis), SQRT(1-axis)));
END IF;
RETURN distance;
END;
i quoted this from here: http://sebastian-bauer.ws/en/2010/12/12/geo-koordinaten-mysql-funktion-zur-berechnung-des-abstands.html
and here is another link: http://www.andrewseward.co.uk/2010/04/sql-function-to-calculate-distance.html
The simplest way to do this would be to calculate a bounding box from the latitude and longitude of the city and a distance (by converting the distance to degrees of longitude).
Once you have that box (min latitude, max latitude, min longitude, max longitude), query for other cities whose latitude and longitude are inside the bounding box. This will get you an approximate list, and should be quite fast as it will be able to use any indexes you might have on the latitude and longitude columns.
From there you can narrow the list down if desired using a real "distance between points on a sphere" function.
You need a spatial index or GIS functionality. What database are you using? MySQL and PostgreSQL both have GIS support which would allow you to find the N nearest cities using an SQL query.
Another option you might want to consider would be to put all of the cities into a spatial search tree like a kd-tree. Kd-trees efficiently support nearest-neighbor searches, as well as fast searches for all points in a given bounding box. You could then find nearby cities by searching for a few of the city's nearest neighbors, then using the distance to those neighbors to get an estimate size for a bounding box to search in.

Categories