In [2]:
#https://github.com/goodalljl/hydroinformatics_class/blob/master/Class21_NHDGeocoder.py
from urllib.request import urlopen
import json
from pprint import pprint
#input by user
Name = "Ivy Creek"
State = "VA"
#build URL
url = "http://ofmpub.epa.gov/waters10/Name.Service?" \
+ "pFullText=" + Name.replace(' ', '+') \
+ "&pFullTextRegex=" \
+ "&pBasename=" \
+ "&pBasenameRegex=" \
+ "&pHydrography=" \
+ "&pHydrographyRegex=" \
+ "&pDirectional=" \
+ "&pDirectionalRegex=" \
+ "&pOperator=EQ" \
+ "&pQueryLimit=" \
+ "&pJWThreshold=90" \
+ "&pResolution=3" \
+ "&pSourceTable=" \
+ "&pState=" + State \
+ "&pStateMod=%2C" \
+ "&pCountyFips5=" \
+ "&pCountyFips5Mod=%2C" \
+ "&pSubbasin=" \
+ "&pSubbasinMod=%2C" \
+ "&pGnisClass=" \
+ "&pGnisClassMod=%2C" \
+ "&pFtype=" \
+ "&pBreakBySubbasin=false" \
+ "&pBreakByFcode=false" \
+ "&optNHDPlusDataset=2.1" \
+ "&optCache=1415283785917" \
+ "&optJSONPCallback=" \
#load response into JSON object
f = urlopen(url)
response = json.loads(f.read().decode())
#uncomment to see structure of response
#pprint(response)
#get lat and lon coordinates of centroid
print("{} result(s) found".format(len(response['output']['results'])))
#there may be multiple responses if it is a common stream/river/waterbody name
lats = []
lons = []
for i in range(0, len(response['output']['results'])):
coords = response['output']['results'][i]['gnis_centroid_geom']['coordinates']
lon = coords[0]
lat = coords[1]
lats.append(lat)
lons.append(lon)
print("Pt {}: The centroid point is {}, {}".format(i, lat, lon))