Query on MapServer layer creates an empty set of layers - even though same code works with a feature service layer...

534
0
07-15-2020 07:25 AM
HannahDean
New Contributor II
#DC Road Data

##"https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Transportation_WebMercator/MapServer/154/query?outFields=*&where=1%3D1"
#gathering a list of field names into a comma separated list is key to copying over the feature service layer into a local geodatabase so that it can be utilized in the Clip analysis
from arcgis.features import FeatureLayer

#identify the boundary layer you will use to query the data
arcpy.env.workspace = r"O:\Professional Services\GIS Information\GIS\Boundary\NAMA_Boundary.gdb" #enter gdb where your park boundary is located
boundary = "NAMA_Boundary_ply"

# Describe the projection of the feature service so that your extents of the boundary layer will be in the appropriate spatial reference
desc = arcpy.Describe("https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Transportation_WebMercator/MapServer/154")
boundaryproj1 = r"O:\Professional Services\GIS Information\GIS\Boundary\NAMA_Boundary.gdb\boundary_proj_1"
arcpy.Project_management(boundary,boundaryproj1, desc.spatialReference)


#query parameters for all rows
base= "https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Transportation_WebMercator/MapServer/154"
where = '0=0'
token = ''
outPath = r"O:\Professional Services\GIS Information\GIS\Flora\2020_Python_TreeRisk_Assessment\2020_Python_TreeRisk_Assessment.gdb"
fields = "*"

##find extent of your parks boundaries and assign it to variable "boundaryext"
descenv = arcpy.Describe(boundaryproj1)
descenvext = descenv.extent
boundaryext = ('{},{},{},{}'.format(descenvext.XMin, descenvext.YMin, descenvext.XMax, descenvext.YMax))

#################################start of code to try to get >2000 records
# Get record extract limit
from urllib.request import urlopen
base= "https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Transportation_WebMercator/MapServer/154"
urlstring = base + "?f=json"
j = urllib.request.urlopen(urlstring)
js = json.load(j)
maxrc = int(js["maxRecordCount"])
print ("Record extract limit: %s" % maxrc)

outdata = r"O:\Professional Services\GIS Information\GIS\Flora\2020_Python_TreeRisk_Assessment\2020_Python_TreeRisk_Assessment.gdb\DC_Roads_all"

# Get object ids of features
where = "1=1"
urlstring = base + "/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))
  query = "/query?where={}&outFields={}&returnGeometry=true&f=json&token={}&geometry={}&geometryType=esriGeometryEnvelope&returnExceededLimitFeatures=false".format(where, fields, token, boundaryext)
  urlstring = base + query
  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, outdata)
print ("Done!")
#################################end

#create local copy of traffic layer and clip by the boundary layer
arcpy.env.workspace =r"O:\Professional Services\GIS Information\GIS\Flora\2020_Python_TreeRisk_Assessment\2020_Python_TreeRisk_Assessment.gdb"
where = '1=1'
boundaryext = "393932.62135367095,131367.2651843205,400957.7405239567,139578.9519640468"
query = "/query?where={}&outFields={}&returnGeometry=true&f=json&token={}&geometry={}&geometryType=esriGeometryEnvelope&returnExceededLimitFeatures=false".format(where, fields, token, boundaryext)
fsURL = base + query
fs = arcpy.FeatureSet()
fs.load(fsURL)
outputName = 'DCRoads_extent'
outputPath = os.path.join(outPath, outputName)
arcpy.CopyFeatures_management(fs, outputPath)
0 Kudos
0 Replies