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'

Map: Alma Pond
Map: Dobbins Memorial State Forest
Map: Donahue Woods State Forest
Map: Little John Wildlife Management Area
Map: Otter Lake
Map: South Hill State Forest (Oneida 23)
Map: Summer Hill State Forest
Map: West Parishville State Forest
SVGZ Graphic: albany-snow-depth
SVGZ Graphic: college-rate
SVGZ Graphic: december-holidays
SVGZ Graphic: ht2025
SVGZ Graphic: lt2025
SVGZ Graphic: May-sunset [Expires June 10 2026]
SVGZ Graphic: PCEPI - May 2026 [Expires June 11 2026]
SVGZ Graphic: Places Named Bethlehem
SVGZ Graphic: Towns with Most Similiar Land Cover to the Town of Bethlehem
Terrain Map: Happy World Milk Day!
Photo: Unburned Sections Look Fairly Typical
Photo: Fall at Dolly Sods
Photo: Hilldabrandt Vly Campsite
Photo: Out On Moss Lake
Photo: Guilderland Town Planning Board Protest
Photo: Bear Rocks View
Photo: Mountain Laurel
Photo: Newcomb Lake Road Trail
Photo: No, it doesn't work
Photo: Harris Lake

Leave a Reply

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