A Bash Script for Geocoding Addresses in New York State

If you are on your typical Linux or Unix server you probably have everything you need to run this script such as cURL and sed, except for possibly for jq which was on my home computer but has no dependencies and can be easily installed on most Unix servers by just compiling the source code.

#!/bin/bash
url="https://gisservices.its.ny.gov/arcgis/rest/services/Locators/Street_and_Address_Composite/GeocodeServer/geocodeAddresses"

query=''
i=1
while read -r LINE; do
	LINE=$(echo $LINE | sed -e 's/"//g')

	if [ $i -ne 1 ]; then query+=","; fi

	query+=$(printf "{ \"attributes\": { \"OBJECTID\": %s, " $i)
	query+=$(printf "\"SINGLELINE\": \"%s\" } }\n" "$LINE")

	if [ $i -eq 1000 ]; then 
		query="{\"records\": [ ${query} ] }"

		curl -s -F f=pjson -F outSR=32768 -F addresses="$query" $url |
			jq --raw-output '.locations |= sort_by(.attributes.ResultID) | .locations[] | [.address, .score, .attributes.Match_addr, .attributes.ResultID, .location.x, .location.y ] | @csv'
		i=1
		query=''
	fi 
	
	((i++))
done < /dev/stdin

# handle remaining records
query="{\"records\": [ ${query} ] }"
curl -s -F f=pjson -F outSR=32768 -F addresses="$query" $url |
	jq --raw-output '.locations |= sort_by(.attributes.ResultID) | .locations[] | [.address, .score, .attributes.Match_addr, .attributes.ResultID, .location.x, .location.y ] | @csv'

Leave a Reply

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