How to geocode a single address (not table of addresses) in ModelBuilder and/or python Script? I want to enter the address and then geocode it. I am using arcgis 10.2.2
-
You need a geocoder, or a geocoder service, and you would use the 'Geocode an Address' tool in the model, where the input is a text variable and the locator is the geocoder mentioned above. – DPSSpatial Dec 1 '14 at 20:41
-
1I think this may be a duplicate of gis.stackexchange.com/questions/44732/… and/or gis.stackexchange.com/questions/16938/… – PolyGeo♦ Dec 1 '14 at 21:58
The geopy
module is quick and easy for tasks such as this. Straight from the docs:
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("175 5th Avenue NYC")
>>> print(location.address)
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, ...
>>> print(location.latitude, location.longitude)
(40.7410861, -73.9896297241625)
>>> print(location.raw)
{u'place_id': u'9167009604', u'type': u'attraction', ...}
Note that it includes classes to use just about any geocoding service that is out there.
I wrote a quick little function to do this with the ESRI python libraries You can grab it at: https://community.esri.com/groups/arcgis-python-api/blog/2018/12/28/single-address-geocode-with-python or just check out the code below
import requests
geoCodeUrl = "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates"
def singleAdressGeocode(address, geoCodeUrl, outSR = "4326"):
#clean up the address for url encoding
address = address.replace(" ", "+")
address = address.replace(",", "%3B")
#send address to geocode service
lookup = requests.get(geoCodeUrl + "?SingleLine=" + address + "&outSR=" + outSR + "&maxLocations=1&f=pjson")
data = lookup.json()
if data["candidates"]:
#woo hoo results
coords = data["candidates"][0]["location"]
return coords
else:
#no results
return "Address not geocoded: " + address
print singleAdressGeocode("1600 Pennsylvania Ave NW, Washington, DC 20500", geoCodeUrl)
-
1