Monday, 11 December 2017

arcpy - Converting REST Service to file geodatabase feature class?


I'm trying to convert a rest service to a feature class but Python is getting hung up in the middle of the process.


Input:


import arcpy
import urllib
import json

# Setup
arcpy.env.overwriteOutput = True
baseURL = "https://maps.somewhere.com/arcgis/rest/services/Bathymetry/MapServer/0"

fields = "*"
outdata = "E:/Tools/Rest/rest.gdb/bathymetry"

# 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 list(fs.items()):

fslist.append(value)
arcpy.Merge_management(fslist, outdata)
print("Done!")

Output


Record extract limit: 1000
Number of target records: 43001
Gathering records...
OBJECTID >= 1 and OBJECTID <= 1000
OBJECTID >= 1001 and OBJECTID <= 2000

OBJECTID >= 2001 and OBJECTID <= 3000
OBJECTID >= 3001 and OBJECTID <= 4000
OBJECTID >= 4001 and OBJECTID <= 5000
OBJECTID >= 5001 and OBJECTID <= 6000
OBJECTID >= 6001 and OBJECTID <= 7000
OBJECTID >= 7001 and OBJECTID <= 8000
OBJECTID >= 8001 and OBJECTID <= 9000
OBJECTID >= 9001 and OBJECTID <= 10000
OBJECTID >= 10001 and OBJECTID <= 11000
OBJECTID >= 11001 and OBJECTID <= 12000

OBJECTID >= 12001 and OBJECTID <= 13000
OBJECTID >= 13001 and OBJECTID <= 14000
OBJECTID >= 14001 and OBJECTID <= 15000
OBJECTID >= 15001 and OBJECTID <= 16000
OBJECTID >= 16001 and OBJECTID <= 17000
OBJECTID >= 17001 and OBJECTID <= 18000
OBJECTID >= 18001 and OBJECTID <= 19000
OBJECTID >= 19001 and OBJECTID <= 20000
OBJECTID >= 20001 and OBJECTID <= 21000
OBJECTID >= 21001 and OBJECTID <= 22000

Traceback (most recent call last):
File "", line 42, in
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\arcobjects\arcobjects.py", line 421, in load
return convertArcObjectToPythonObject(self._arc_object.Load(*gp_fixargs(args)))
RuntimeError: RecordSetObject: Cannot open table for Load

Can anyone explain to me what might be going on here?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...