UK postcodes -> co-ordinates : possible purely in php? - php

Say have a mysql database with these cols:
| house | postcode | lat | lon |
The postcode's are complete but lat and lon are empty.
I want to write/find a script that i can run once to complete the table.
Is this possible purely in php (any tips?) or do i need to use a javascript api and $_POST to a php file?

Without needing any external API
UK Postcode data is now freely available from the Ordnance Survey CodePoint as a downloadable series of CSV files (Feb 2009); but unfortunately the location point is stored using Northing/Eastings rather than Latitude and Longitude.
You'd need to convert the Northings/Eastings to Latitude/Longitude. There's a number of articles about this on the web, e.g.
http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/
http://mediakey.dk/~cc/convert-northing-and-easting-utm-to-longitude-and-latitude/
but you need to be aware that OS Northings/Eastings are based on the Airy 1830 ellipsoid rather than the WGS84 model used by Google maps (and most GSM systems). Failing to allow for this difference can put you out by anything between 70-120m between Cornwall and East Anglia.
Using PHP, the best solution for this conversion is PHPCoord by Jonathan Stott

There's an XML/JSON API PHP can access via HTTP requests like file_get_contents or cURL.
http://code.google.com/apis/maps/documentation/geocoding/

This can be done purely in PHP, relying on some external API that provides you with the lat/lon data.
Generally, web APIs do not care much about the language that's running on their clients. With "JavaScript API" you probably mean a service that returns data in some JavaScript-friendly format, like JSON. However, JSON does not have to be processed by a JavaScript program; PHP programs can parse it just fine.

Related

Finding geology of a GPS location like in water or on a road

I am working on a project that I have array of GPS locations.
I want to know for each of those points, where are they?
I have a specific list that I am trying to match their location to one of those such as: Water, Highway, railway, ...
I tried this API (http://developers.cloudmade.com/wiki/geocoding-http-api/Documentation) but it doesn't give me any accurate data! even less than 50% correct!
Do you know any better solution?
I use php for server side that is responsible to gathering data from different webservices.
Our client sides are Android, Java
I expect system search and find the type of location automaticall
You don't give any hint of your platform (pc? smartphone? by hand? In automated program?)
But as a great starting point working with a few points by hand, in maps.google.com if you put the latitude and longitude (may be longitude and latitude try both) it will show them to you on the map. Google maps even has simple APIs so you can use this functionality reasonably simply (for example from a matlab program).

Generate Lat / Lon from addresses in mySQL table

I currently have a mySQL table of addresses of certain locations that I need to obtain Latitude and Longitude for. This is a table being used in WordPress to map store locations to google maps. I am looking for the best way to obtain the lat/lon for all of the records in this database.
Does anyone know a simple strategy I could use to run through all of the records and obtain the lat/lon of each address?
Google have the Geocoding API - http://code.google.com/apis/maps/documentation/geocoding/
Not sure what kind of addresses you mean. If IP then there are databases that will give you the approx lat and long for most of your visitor's IPs. You will have to capture it then.
If you mean postal addresses then, I know of nothing to help world wide. In the UK, the Royal Mail's PAF used to contain lat and long, but you must have the post code. I suspect there might be similar data available for ZIP codes in the US and Cedex codes in France.
Other than that, I expect you will be out of luck.
Are you asking for code? I use a bash script to solve it. It's not a copy actually it is quite simple with Linux bash commandl-ine. Most likely you want to dump your database into a text file and then read and query it from them shell. It took me about 4-5 hours for a country like Germany with postcode from 0-99999.

Post Code Lookup

A client wants a page on their website where the user can search for a stockist of their product range. What they want is the ability to enter a UK PostCode and for a list of their stockists to appear in closest order.
Is there a way to utilise Google Maps to determine the closest supplier?
I have a PHP MySQL database with ALL the suppliers Postcodes and details and I'll have the postcode of the user to use as well.
I think you'll need the Ordnance Survey CodePoint data to map your postcodes to a longitude and latitude. Codepoint is available as a commercial product, or there's also Codepoint Open
Once you have the latitude and longitude, then you can use standard algorithms such as Haversine or Vincenty to calculate distances between the points
EDIT
Structure of the CodePoint Open CSV files can be found here. Note that location is held as Northings and Esatings rather than Longitude and Latitude, so it will need converting. There's a number of articles about this on the web, e.g.
http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/
http://mediakey.dk/~cc/convert-northing-and-easting-utm-to-longitude-and-latitude/
but you need to be aware that OS Northings/Eastings are based on the Airy 1830 ellipsoid rather than the WGS84 model used by Google maps (and most GSM systems). Failing to allow for this difference can put you out by anything between 70-120m between Cornwall and East Anglia.
You can also find PHP functions to do this conversion at the Movable Type site (essential reading for any PHP developer, working with GeoData. I'd recommend adding a couple off columns to your stockist database for longitude and latitude, and a one-off script to update all existing data using the Codepoint data, then modify your insert/update routines for stockists to keep this information up-to-date.
Using PHP, another solution for this conversion is PHPCoord by Jonathan Stott
For higher performance in your database query, do a lookup of lat/long against a "bounding box" before calculating distances. I've explained how to do this in response to a similar query.
I think you'll need a little more data than that. I'd suggest that you need to store the latitude and longitude of every UK postcode alongside the postcode itself. I think this data is available from the Royal Mail for a cost and I remember reading somewhere that it was going to be available in the public domain too. Have a user enter their postcode, look up their latitude and longitude in the db and then use that to perform another query that calculates the closest supplier to them, perhaps within a certain number of miles. You could perhaps create a stores procedure to do all of this on the db. This post seems to have some details on how to do this.
If you want it to be fast then pre-compute the distances between postcodes within a certain radius of each other - see the following:
Calculate distance between zip codes and users
This should work fine in your case unless you think a user would be willing to travel more than 100 miles to purchase a product ??
Instead of Google Maps, this open postcode geocoding API may be of use to you. As others have mentioned, once you have lat long for both points you can use standard algos to find distances. A previous question contains some info on how to do this directly in SQL.
You may find this website helpful: SQL/CSV Postcode Database
I've previously used this to lookup long/lat from a postcode "outcode".
There's also a php script (which I've not tested) which calculates distance between two postcodes.

How to get local time based on GPS coordinates?

My script has as input the latitude and longitude values as GPS coordinates.
How can I get the current local time?
Are out there solutions that do not use databases or web services?
PHP has a method to retrieve location based on a timezone, I am wondering if the reverse exists or not
geonames.org provide a range of webservices that can be used to get details from latitude and longitude. For example http://ws.geonames.org/timezone?lat=47.01&lng=10.2&style=full
You can just fetch that with your PHP code and parse the result.
See http://www.geonames.org/export/ws-overview.html for details of the available webservices.
Use this datafile. The data you are looking for is in zone.tab. wikipedia also has an overview of it - the table is generated from the datafiles.

google Maps getDistance without loading the page

Google maps has a function that lets you retreive the distance between two points: getDistance(), of the GDirections class.
Now, this is Javascript, so it'll only be able to calculate once the page has loaded.
My search form however, has the ability to indicate what's the maximum distance you want between yourself and another person and based on that, a list of search results has to be provided.
Since I can't use this function in PHP, is there another way for me to calculate the distance between 2 points on the earth? By giving up the street, postal code and city name, just like what Google maps needs.
This page has a list of google maps parameters for use in http connection. By specifying the output paramter you can choose to give back kml files or similar and can be used in any lanuage that can make http connections.
Looks like you want to calculate a Great Circle Distance
Formulas have been discussed here on stackoverflow before.
A point on earth is defined by it's latitude and longitude. If you want to calculate the distance between 2 points on earth by giving up the street, postal code and city name, you will need geo-referene data.
This data is available for free on the internet, but the accuracy and availability differ greatly from region to region. (USA data is of good quality, data for Kenia for example will be harder to come by)
So to answer your question:
Yes, there are other ways to calculate what you want. But they require more work/are more complex than just querying the google API.
You might want tot read: Creating a Store Locator with PHP, MySQL & Google Maps
Hope this points you into the right direction.
You can use kml file. It's xml-formatted file that you can recieve by link like http://maps.google.com/maps?f=d&hl=en&saddr=<src latitude>,<src longitude>&daddr=<dist latitude>,<dist longitude>&ie=UTF8&0&om=0&output=kml
in recieved file you can parse and summate distances from

Categories