Make Arcpy use Esri Feature Service

3593
5
Jump to solution
05-31-2019 02:41 AM
HermanGroeneveld
New Contributor III

I'm a ArcGIS for Desktop user who is a noob to Python and would like to be pointed in the right direction. How can I use/acces feature services like the one below (directly) through arcpy?

What I'm trying to do looks very simple:

1. I have a ZIP-code as input, say '2623HB'

2. Using this Zip-code Web Feature Layer ("Postcodevlakkekken PC 6", provided by Esri Nederland), I want to select the correct polygon by using the SelectLayerByAttributes-function.

3. Export this polygon to a shapefile or maybe even use it as intermediate data.

Step 2 gives me problems. In the Modelbuilder I made a very simple scheme, using SelectlayerByAttributes and it works (attached picture). But when I export the model to python , the very same script raises an error, saying:

"ERROR 000732: Layer Name or Table View: Dataset Postcodevlakken PC 6\PC6 does not exist or is not supported"

how can i acces a feature service with an url like:

https://services.arcgis.com/nSZVuSZjHpEZZbRo/arcgis/rest/services/Postcodevlakken_PC6/FeatureServer 

here is the python code:

# ---------------------------------------------------------------------------
# selbyattr2.py
# Created on: 2019-05-31 09:24:45.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy


# Local variables:
PC6 = "Postcodevlakken PC 6\\PC6"
PC6__4_ = PC6

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(PC6, "NEW_SELECTION", "\"PC6\" = '2623HB'")
0 Kudos
1 Solution

Accepted Solutions
HermanGroeneveld
New Contributor III

I've got the answer. Esri Nederland helped me out! they provided me this code and advised to read about the Rest API. 

import arcpy

# Query URL opbouwen op basis van bepaalde postcode
ppc = '2623HB'
url = 'https://services.arcgis.com/nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Postcodevlakken_PC6/FeatureServer/0/query?where=PC6+%3D+%27{}%27&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false&outFields=*&returnGeometry=true&returnCentroid=false&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pjson&token='.format(ppc)

# Omzetten van query naar feature class
fs = arcpy.FeatureSet()
fs.load(url)
fs.save(r'<Pad naar Geodatabase>\PPC6_{}'.format(ppc))
print ("Klaar")

View solution in original post

5 Replies
NeilAyres
MVP Alum

Did you build and run the model from within ArcMap?

ie by adding the required layer from the TOC pulldown?

Once the code is in python there is no way that the code knows where

PC6 = "Postcodevlakken PC 6\\PC6"

is actually on your local storage.

HermanGroeneveld
New Contributor III

Thanks for your answer, Neil! Indeed, I added the required layer from the TOC and I understand that python can't find the layer among my local storage, thanks for pointing out.

But in what way (without adding from the TOC or storing Web content locally) can I point to the PC6-feature service? 

0 Kudos
NeilAyres
MVP Alum

By using the little envelope thingy at the side to directly navigate to the actual feature in its fgdb or where ever it is. Not just picking it from the layer list.

0 Kudos
HermanGroeneveld
New Contributor III

Uhmmm, maybe a bit silly, but I'm not quiet sure where to look for a little envelope thingy. I'm working in Pycharm/IDLE and ArcGIS for Desktop. Can you help me with a screenshot?

0 Kudos
HermanGroeneveld
New Contributor III

I've got the answer. Esri Nederland helped me out! they provided me this code and advised to read about the Rest API. 

import arcpy

# Query URL opbouwen op basis van bepaalde postcode
ppc = '2623HB'
url = 'https://services.arcgis.com/nSZVuSZjHpEZZbRo/ArcGIS/rest/services/Postcodevlakken_PC6/FeatureServer/0/query?where=PC6+%3D+%27{}%27&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false&outFields=*&returnGeometry=true&returnCentroid=false&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pjson&token='.format(ppc)

# Omzetten van query naar feature class
fs = arcpy.FeatureSet()
fs.load(url)
fs.save(r'<Pad naar Geodatabase>\PPC6_{}'.format(ppc))
print ("Klaar")