1

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 "<string>", line 42, in <module>
  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?

2

I recently needed to do convert a REST service to a Feature Class as well. Here is a MWE of my approach (using requests rather than urllib).

import json

import arcpy
import requests

# arcpy env settings
arcpy.env.workspace = 'path_to_your_gdb'

# get data from rest service
params = {'where': '1=1', 'outFields': '*', 'f': 'pjson', 'returnGeometry': True}
r = requests.get('service_url/query', params)
data = r.json()

# save JSON as a Feature class
json_path = 'path_to_rest_service.json'
with open(json_path, 'w') as f:
    json.dump(data, f)
arcpy.JSONToFeatures_conversion(json_path, 'output_fc')

It retrieves all the data from the rest service (if you want to specify other parameters check this), creates a intermediary JSON file in your computer and the uses that JSON file to call the JSON To Features tool.

Bear in mind that queries to REST services have transaction limits so this might not be the best approach for services with a large number of features. In your example is clear that you have a relative high number of features so you could do each query and append the information to the intermediary JSON file before calling the tool.

1
  • I just used this to download a json from a map service! This helped a lot. Thanks. – jtgis Mar 20 at 2:29
0

I use the same code, with one exception. I use urllib2

Not sure if that is the cause of your error -- also I run this in ArcMap , does not work in ArcGIS Pro.

Also make sure you do not have the database open in another program - as in - open in both ArcMap and Pro.

import arcpy
import urllib2
import json
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.