Day: December 11, 2024

Show Only ...
Maps - Photos - Videos

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.

01#!/bin/bash
03 
04query=''
05i=1
06while read -r LINE; do
07    LINE=$(echo $LINE | sed -e 's/"//g')
08 
09    if [ $i -ne 1 ]; then query+=","; fi
10 
11    query+=$(printf "{ \"attributes\": { \"OBJECTID\": %s, " $i)
12    query+=$(printf "\"SINGLELINE\": \"%s\" } }\n" "$LINE")
13 
14    if [ $i -eq 1000 ]; then
15        query="{\"records\": [ ${query} ] }"
16 
17        curl -s -F f=pjson -F outSR=32768 -F addresses="$query" $url |
18            jq --raw-output '.locations |= sort_by(.attributes.ResultID) | .locations[] | [.address, .score, .attributes.Match_addr, .attributes.ResultID, .location.x, .location.y ] | @csv'
19        i=1
20        query=''
21    fi
22     
23    ((i++))
24done < /dev/stdin
25 
26# handle remaining records
27query="{\"records\": [ ${query} ] }"
28curl -s -F f=pjson -F outSR=32768 -F addresses="$query" $url |
29    jq --raw-output '.locations |= sort_by(.attributes.ResultID) | .locations[] | [.address, .score, .attributes.Match_addr, .attributes.ResultID, .location.x, .location.y ] | @csv'