Hello, I am looking to query a dataset of US roads to just New York State. Here is the dataset in question: Transportation - Overview (arcgis.com). The "Local Roads" layer does not contain a state identifier. Rather than downloading all 17.6 million records, I'd like to only return and download those for New York State. Below is a sample code I have used in the past to extract features from ArcGIS Online. Any suggestions on how to adapt it to query based on geography (envelope or shapefile) would be greatly appreciated.
Thank you,
Adam
arcpy.env.overwriteOutput = True
baseURL = "https://services2.arcgis.com/FiaPA4ga0iQKduv3/arcgis/rest/services/Transportation_v1/FeatureServer/8"
fields = "*"
outdata = "/NYS_Roads.gdb/Local_roads"
# Get record extract limit
urlstring = baseURL + "?f=json"
j = urllib.request.urlopen(urlstring)
js = json.load(j)
maxrc = int(js["maxRecordCount"])
print("Record extract limit: {}".format(maxrc))
# Get object ids of features
where = "1=1"
urlstring = baseURL + "/query?where={}&returnIdsOnly=true&f=json".format(where)
j = urllib.request.urlopen(urlstring)
js = json.load(j)
idfield = js["objectIdFieldName"]
idlist = js["objectIds"]
idlist.sort()
numrec = len(idlist)
print("Number of target records: {}".format(numrec))
# Gather features
print ("Gathering records...")
fs = dict()
for i in range(0, numrec, maxrc):
torec = i + (maxrc - 1)
if torec > numrec:
torec = numrec - 1
fromid = idlist[i]
toid = idlist[torec]
where = "{} >= {} and {} <= {}".format(idfield, fromid, idfield, toid)
print(" {}".format(where))
urlstring = baseURL + "/query?where={}&returnGeometry=true&outFields={}&f=json".format(where,fields)
fs[i] = arcpy.FeatureSet()
fs[i].load(urlstring)
# Save features
print ("Saving features...")
fslist = []
for key,value in list(fs.items()):
fslist.append(value)
arcpy.Merge_management(fslist, outdata)