Skip to content

Instantly share code, notes, and snippets.

@maptastik
Last active November 26, 2018 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maptastik/06d27d3d5b5315b4ea4c4f1c63d7ce29 to your computer and use it in GitHub Desktop.
Save maptastik/06d27d3d5b5315b4ea4c4f1c63d7ce29 to your computer and use it in GitHub Desktop.
Modified from https://socalgis.org/2018/03/28/extracting-more-features-from-map-services/ to work with ArcGIS Pro's Python 3 installation
import arcpy
import json
import urllib
arcpy.env.overwriteOutput = True
baseURL = "http://services.gis.ca.gov/arcgis/rest/services/Environment/Wildfires/MapServer/0"
fields = "*"
out_data = "H:/cal_data/data.gdb/testdata"
# 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: %s" % 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: %s" % 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 fs.items():
fslist.append(value)
arcpy.Merge_management(fslist, out_data)
print("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment