-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcuspszip5_needs work.py
185 lines (119 loc) · 5.32 KB
/
gcuspszip5_needs work.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""This script was a start point for specs"""
# Utah Street Address Locator (Geocoding) Service
# AGRC, 20130329
# 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
import string
from time import strftime
def gcHouseNumsAndAddress(inList):
for hn in inList[1]:
houseNumStreetAddress = hn + " " + inList[2]
gcAddressZoneByCenterline([inList[0], houseNumStreetAddress, inList[3]])
# evaluate results
# return GCresults array
def gcAddressZoneByCenterline(inParams):
apiKey= "AGRC-A6B6D4B4761878" #obtain a mapserv UserName by registering at http://mapserv.utah.gov/registration/Register
streetAddressForURL = urllib2.quote(inParams[1])
zoneForURL = urllib2.quote(inParams[2])
gcServiceURL = r'http://api.mapserv.utah.gov/api/v1/Geocode/{0}/{1}?locators=roadCenterlines&apiKey={2}'.format(streetAddressForURL,zoneForURL, apiKey)
try:
response = urllib2.urlopen(gcServiceURL)
except urllib2.HTTPError:
# No record will be written for this record of inAddressTable
print "No address found"
emptyStr =""
return [objID,streetAddress,zone,emptyStr,emptyStr,emptyStr,emptyStr,emptyStr,emptyStr]
jsonGCResults = json.load(response)["result"]
splitMatchAddress = jsonGCResults["matchAddress"].split(",")
matchAddressStreet = ""
matchAddressZone = ""
if len(splitMatchAddress) == 2:
matchAddressStreet = splitMatchAddress[0]
matchAddressZone = splitMatchAddress[1]
return[inParams[0],inParams[1],inParams[2],matchAddressStreet,matchAddressZone,jsonGCResults["locator"],jsonGCResults["score"],jsonGCResults["location"]["x"],jsonGCResults["location"]["y"]]
#### SET THESE 6 PARAMETERS
#1
#set api key in
#2
inAddressTable= r"C:\KW_Working\Geocoder_Tools\Zip_plus4\2012_11.mdb\ZIP4_845_Table"
#3
inAddressFieldName = "AddressFieldName"
#field containing basic street address exs. 120 N 200 W, 99 S Main St
#4
inZoneFieldName = "ZipCode" #field containing zipcode, standardized city name, or udot route number exs. 84105, Heber City
#5
inUniqueIdentifierFieldName = "UpdateKeyNo" #this is default mode, will auto-number results, otherwise specify the name of objid fieldname
#6
outFileFolder = r"C:\KW_Working\Geocoder_Tools\Zip_plus4\TestOutputs"
#### END SET PARAMETERS
sqlString = "RecordTypeCode = 'S' or RecordTypeCode = 'H' and not(AddrPrimaryHighNo is null or StreetName is null)"
rows = arcpy.SearchCursor(inAddressTable, sqlString)
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()
#remove all non digit chars and strip leading zeros
highnum = ''.join(i for i in row.getValue("AddrPrimaryHighNo") if i.isdigit())
#print highnum
highnum = str(int(highnum)).strip()
lownum = ''.join(i for i in row.getValue("Addr PrimaryLowNo") if i.isdigit())
houseNumList = [lownum]
numdiff = int(highnum) - int(lownum)
if numdiff > 0:
houseNumList.append(highnum)
if numdiff > 40:
if numdiff % 4 == 0:
midnum = int(lownum) + (numdiff/2)
elif numdiff % 2 == 0:
midnum = int(lownum) + (numdiff/2) + 1
houseNumList.insert(1,midnum)
streetAddress = ""
preDir = row.getValue("StreetPreDrctnAbbrev")
if not preDir == None:
if len(preDir) > 0:
streetAddress = preDir
streetAddress = streetAddress + " " + row.getValue("StreetName").strip()
stType = row.getValue("StreetSuffixAbbrev")
if not stType == None:
if len(stType) > 0:
streetAddress = streetAddress + " " + stType
sufDir = row.getValue("StreetPostDrctnAbbrev")
if not sufDir == None:
if len(sufDir) > 0:
streetAddress = streetAddress + " " + sufDir
#remove unnecessary character
for c in range(34,48):
streetAddress = streetAddress.replace(chr(c)," ")
streetAddress = streetAddress.replace("_"," ")
zone = str(row.getValue(inZoneFieldName))
if zone[:1] == "8":
zone = zone.strip()[:5]
if len(objID) == 0 and len(streetAddress) == 0 and len(zone) == 0:
print "incomplete record detected"
continue
#print objID
print streetAddress #+ " " + zone
#print houseNumList
#gcResults = gcHouseNumsAndAddress([objID, houseNumList, streetAddress,zone])
#csvWriter.writerow(gcResults)
del csvWriter