views:

66

answers:

3

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?

A: 

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/

ceejayoz
interesting, so it kind of scrapes for json?
Haroldo
Just be careful with the size of your database. If it's over a few hundred rows, you may want to take it easy so you don't trigger Google's fair use policy (2500/day, but much lower per minute). For practical numbers, I started triggering it after about 400 rows in a minute or two.
Matthew Scharley
"the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited."
symcbean
@symcbean Yes, you need to be doing this with the intent to display them on a Google map.
ceejayoz
A: 

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.

Thomas
+2  A: 

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

Mark Baker