Last active
November 6, 2016 21:32
-
-
Save dbarlett/89dae3923bb2dd103cda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import requests | |
URL_BASE = "https://gis.arlingtonva.us:6443/arcgis/rest/services/" | |
TIMEOUT = 5 | |
def find_address_candidate(single_line): | |
path = "geoproc/Composite_Add_loc/GeocodeServer/findAddressCandidates" | |
params = { | |
"SingleLine": single_line, | |
"maxLocations": "1", | |
"outSR": "4326", | |
"f": "pjson", | |
} | |
try: | |
r = requests.get(URL_BASE + path, params=params, timeout=TIMEOUT) | |
candidate = r.json()["candidates"][0] | |
return ( | |
candidate["address"], | |
candidate["location"]["x"], | |
candidate["location"]["y"], | |
) | |
except (requests.exceptions.RequestException, IndexError, TypeError): | |
return None | |
def identify(service, x, y, layers=None): | |
path = "public/{}/MapServer/identify".format(service) | |
params = { | |
"geometry": "{},{}".format(x, y), | |
"geometryType": "esriGeometryPoint", | |
"sr": "4326", | |
"tolerance": "10", | |
"mapExtent": "10", | |
"imageDisplay": "600,550,96", | |
"returnGeometry": "false", | |
"returnZ": "false", | |
"returnM": "false", | |
"f": "pjson", | |
} | |
if layers: | |
params["layers"] = layers | |
r = requests.get(URL_BASE + path, params=params, timeout=TIMEOUT) | |
return r.json() | |
def civic_association(x, y): | |
try: | |
return identify("Civic_Associations", x, y)["results"][0]["value"] | |
except (IndexError, TypeError): | |
return "Unknown" | |
def police_district(x, y): | |
try: | |
return identify("Police_Districts", x, y)["results"][0]["value"] | |
except (IndexError, TypeError): | |
return "Unknown" | |
def polling_place(x, y): | |
try: | |
precinct = identify("Voter_Locations", x, y, layers="4")["results"][0]["attributes"] | |
return "{}, {}".format(precinct["POLLING_PLACE"], precinct["ADDRESS"]) | |
except (IndexError, TypeError): | |
return "Unknown" | |
def high_school(x, y): | |
try: | |
school = identify("High_school_boundary", x, y)["results"][0]["attributes"] | |
return "{}, {}".format(school["SCHOOL_NAM"], school["ADDRESS"]) | |
except (IndexError, TypeError): | |
return "Unknown" | |
def middle_school(x, y): | |
try: | |
school = identify("Middle_school_boundary", x, y)["results"][0]["attributes"] | |
return "{}, {}".format(school["SCHOOL_NAM"], school["ADDRESS"]) | |
except (IndexError, TypeError): | |
return "Unknown" | |
def elementary_school(x, y): | |
try: | |
school = identify("Elementary_school_boundary", x, y, layers="3")["results"][0]["attributes"] | |
return "{}, {}".format(school["SCHOOL_NAM"], school["ADDRESS"]) | |
except (IndexError, TypeError): | |
return "Unknown" | |
if __name__ == "__main__": | |
candidate = find_address_candidate(sys.argv[1]) | |
if candidate: | |
lon = candidate[1] | |
lat = candidate[2] | |
print "Address standardized as:", candidate[0] | |
print "Latitude:", lat | |
print "Longitude:", lon | |
print "Civic Association:", civic_association(lon, lat) | |
print "Polling Place:", polling_place(lon, lat) | |
print "Police District:", police_district(lon, lat) | |
print "Elementary School:", elementary_school(lon, lat) | |
print "Middle School:", middle_school(lon, lat) | |
print "High School:", high_school(lon, lat) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import geojson | |
from collections import defaultdict | |
TRAILS_URL = "http://gisdata.arlgis.opendata.arcgis.com/datasets/add6458ac5b241368d459fce8e53cfe9_1.geojson" | |
r = requests.get(TRAILS_URL) | |
trails = geojson.loads(r.text) | |
length_ft = sum([i["properties"]["SHAPElen"] for i in trails["features"]]) | |
print "Total miles:", length_ft / 5280 | |
lengths = defaultdict(int) | |
for i in trails["features"]: | |
lengths[i["properties"]["TRAIL_NAME"]] += i["properties"]["SHAPElen"] | |
for i in sorted(lengths.keys()): | |
print "{0},{1:04.2f}".format(i, lengths[i]/5280) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2016-07-29 | |
https://gis.arlingtonva.us:6443/arcgis/rest/services/geoproc/Composite_Add_loc/GeocodeServer/findAddressCandidates?SingleLine=2100+clarendon+blvd&maxLocations=1&f=pjson&callback=foo | |
foo({ | |
"spatialReference": { | |
"wkid": 102746, | |
"latestWkid": 2283 | |
}, | |
"candidates": [ | |
{ | |
"address": "2100 CLARENDON BLVD", | |
"location": { | |
"x": 11885559.359552318, | |
"y": 7010448.2899357406 | |
}, | |
"score": 100, | |
"attributes": { | |
} | |
} | |
] | |
}); | |
http://gis.arlingtonva.us/arlgis/rest/services/public/Fireboxes/MapServer/identify?geometry=-77.085343040321547%2C38.890487388176247&geometryType=esriGeometryPoint&sr=4326&layers=&layerDefs=&time=&layerTimeOptions=&tolerance=10&mapExtent=10&imageDisplay=600%2C550%2C96&returnGeometry=false&maxAllowableOffset=&geometryPrecision=&dynamicLayers=&returnZ=false&returnM=false&gdbVersion=&f=pjson | |
{ | |
"results": [ | |
{ | |
"layerId": 1, | |
"layerName": "Fireboxes", | |
"displayFieldName": "FIRSTDUE", | |
"value": "Station 10", | |
"attributes": { | |
"OBJECTID": "118", | |
"BOX_NUM": "11003", | |
"FIRSTDUE": "Station 10", | |
"HWY": "0", | |
"FIRE_STATION": "0", | |
"BOX_TEXT": "110-03", | |
"COLOR": "1", | |
"Shape": "Polygon", | |
"SHAPE.STArea()": "7364570.060817", | |
"SHAPE.STLength()": "11661.22042" | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment