[nycphp-talk] Geocoder and PHP
Ken Robinson
kenrbnsn at rbnsn.com
Fri Jun 10 19:18:27 EDT 2005
At 02:59 PM 6/10/2005, John Corry wrote:
>I am working on a project that will require running about 3500 street
>addresses through a geocoder application to return lat/long pairs.
>
>My first idea was to use cURL to post the address to the http://geocoder.us/
>site...once for each address, parsing the returned result to get lat/long
>and store those with the address record in my db.
This request intrigued me, since I've been looking at creating an
interface to Google maps via PHP and I needed to get lat/long points
for the addresses currently in my database. My first attempt was to
use a query to Google Maps, capture the output and use preg_replace
on the returned XML string to extract the lat/long pair. Looking at
http://geocoder.us/ I saw that one use SOAP, XML-RPC, and REST-ful
RDF clients to get the information. I chose to use the RDF method,
since it returns the shortest string. I then used the miniXML
package <http://minixml.psychogenic.com/index.html> to turn the
returned XML string into an associative array.
Here's the code I used:
<?
include('./minixml.inc.php');
set_time_limit(0); // the code took a long time to go out and back
function get_lat_long($q) { // $q is the address string with ZIP code
$xmlDoc = new MiniXMLDoc();
$gm = fopen('http://rpc.geocoder.us/service/rest?address=' .
str_replace(' ','%20',$q),'r');
$tmp = @fread($gm,30000);
fclose($gm);
//
// I probably should do some error checking here to make sure I got
an XML string
//
$xmlDoc->fromString($tmp);
$tmpa = $xmlDoc->toArray();
return(array($tmpa['rdf:RDF']['geo:Point']['geo:lat'],$tmpa['rdf:RDF']['geo:Point']['geo:long']));
}
?>
I wrote and debugged this in less than an hour.
Ken Robinson
kenrbnsn at rbnsn.com
PS ... I'm looking for freelance/contract/permanent PHP work
More information about the talk
mailing list