Skip to content

Instantly share code, notes, and snippets.

@BGranberg
Last active August 7, 2017 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save BGranberg/5272025 to your computer and use it in GitHub Desktop.
Save BGranberg/5272025 to your computer and use it in GitHub Desktop.
For Utah Addresses: This code will geocodes an input table in one of the arcgis-compatible file formats (.csv, .dbf, .xls. .gdb, etc) and produces # an new output .csv table with the geocoded results. Uses AGRC statewide geocoding web service
# Utah Street Address Locator (Geocoding) Service
# AGRC, 20131220
# WHAT?
# Geocodes an input table in one of the arcgis-compatible file formats and produces
# an new output .csv table with the geocoded results
# IMPORTANT NOTES:
#
# individualized api key will be required in near future, here's how to get one:
# 1) register at http://developer.mapserv.utah.gov/AccountAccess
# 2) confirm the email address that you register with
# 3) generate an api key for your web application or desktop machine
# NOTE: arcpy is used so that the inAddressTable argument can be used with any ArcMap readable table.
# While this requires an ArcGIS Desktop license, the code could easily be written to remove the
# arcpy dependency.
import json
import arcpy
import urllib2
import os
import csv
from time import strftime
#### SET THESE 6 PARAMETERS
#1
apiKey= "" #obtain a mapserv UserName by registering at http://mapserv.utah.gov/registration/Register
#2
inAddressTable= r"c:/temp/myAddressTableToGeocode.csv"
#3
inAddressFieldName = "AddressFieldName"
#field containing basic street address exs. 120 N 200 W, 99 S Main St
#4
inZoneFieldName = "ZipFieldName" #field containing zipcode, standardized city name, or udot route number exs. 84105, Heber City
#5
inUniqueIdentifierFieldName = "" #this is default mode, will auto-number results, otherwise specify the name of objid fieldname
#6
outFileFolder = r"c:/temp/"
#### END SET PARAMETERS
rows = arcpy.SearchCursor(inAddressTable)
csvWriter = csv.writer(open(os.path.join(outFileFolder, "mapservGeocodeResults_" + strftime("%Y%m%d%H%M%S") + ".csv"), 'wb'), delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
csvWriter.writerow(['OBJID','INADDR','INZONE','MatchAddr','MatchZone','Geocoder','Score','X','Y'])
x = 0
for row in rows:
x += 1
if inUniqueIdentifierFieldName == "":
objID = str(x)
else:
objID = str(row.getValue(inUniqueIdentifierFieldName)).strip()
streetAddress = str(row.getValue(inAddressFieldName)).strip().replace("#","")
#remove unnecessary character
for c in range(0,31):
streetAddress = streetAddress.replace(chr(c)," ")
for c in range(33,37):
streetAddress = streetAddress.replace(chr(c)," ")
for c in range(39-47):
streetAddress = streetAddress.replace(chr(c)," ")
for c in range(58-64):
streetAddress = streetAddress.replace(chr(c)," ")
for c in range(91-96):
streetAddress = streetAddress.replace(chr(c)," ")
for c in range(123-255):
streetAddress = streetAddress.replace(chr(c)," ")
zone = str(row.getValue(inZoneFieldName))
if zone[:1] == "8":
zone = zone.strip()[:5]
if len(objID.replace(" ","")) == 0 or len(streetAddress.replace(" ","")) == 0 or len(zone.replace(" ","")) == 0:
print "incomplete record detected"
continue
print objID + " " + streetAddress + " " + zone
streetAddressForURL = urllib2.quote(streetAddress)
zoneForURL = urllib2.quote(zone)
gcServiceURL = r'http://api.mapserv.utah.gov/api/v1/Geocode/{0}/{1}?apiKey={2}'.format(streetAddressForURL,zoneForURL, apiKey)
try:
response = urllib2.urlopen(gcServiceURL)
except urllib2.HTTPError, e:
# No record will be written for this record of inAddressTable
if "Invalid API key." in e.read():
print e.read()
print "API keys are specific to your IP connection, you may have to generate a new key if you"
print "are on a different computer or your network manager has assigned you a new IP address."
break
print "No address found"
emptyStr =""
csvWriter.writerow([objID,streetAddress,zone,emptyStr,emptyStr,emptyStr,emptyStr,emptyStr,emptyStr])
continue
jsonGCResults = json.load(response)["result"]
splitMatchAddress = jsonGCResults["matchAddress"].split(",")
matchAddressStreet = ""
matchAddressZone = ""
if len(splitMatchAddress) == 2:
matchAddressStreet = splitMatchAddress[0]
matchAddressZone = splitMatchAddress[1]
csvWriter.writerow([objID,streetAddress,zone,matchAddressStreet,matchAddressZone,jsonGCResults["locator"],jsonGCResults["score"],jsonGCResults["location"]["x"],jsonGCResults["location"]["y"]])
del csvWriter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment