Simple as Shit Geocoding with SAM

I like this PHP geocoding script I wrote to use with SAM, as it’s so simple, just six lines. Should I have used Python or Perl? Probably and it would have probably been 3 lines in Python or in case of Pathologically Eclectic Rubbish Lister probably one line. Basically you call this on command line with a text file list of addresses, and spits out a CSV file with the addresses followed by the coordinates from the NY State Address Management system. Most other states have something similar, as it’s kind of important that the fire truck and your Uber show up at the right house.

$output = "";		
foreach (file($argv[1]) as $address) {
	$json = file_get_contents('https://gisservices.its.ny.gov/arcgis/rest/services/Locators/Street_and_Address_Composite/GeocodeServer/findAddressCandidates?Street=&City=&State=&ZIP=&SingleLine='.urlencode($address).'&outFields=&maxLocations=1&matchOutOfRange=true&langCode=&locationType=&sourceCountry=&category=&location=&distance=&searchExtent=&outSR=4326&magicKey=&f=pjson');
	$coord = json_decode($json); 
	$output .= '"'.str_replace('"','\"', chop($address)).'",'.$coord->candidates[0]->location->y.','.$coord->candidates[0]->location->x."\n";
}
file_put_contents(pathinfo($argv[1], PATHINFO_FILENAME).'-geocoded.csv', $output);

Leave a Reply

Your email address will not be published. Required fields are marked *