Select to view content in your preferred language

How to access layer using feature service URL/path

1353
4
09-27-2023 04:50 PM
Labels (2)
ZacharyKasson
New Contributor III

Hey there,

I am trying to access a feature layer within my portal using the layers path. I have been accessing the data within visual studio code using the feature layers ID like this: 

 

feature_service_layer = gis.content.get('964b4b7b112347e9ff76609bfc75432').layers[0]

 

I'd like to make a Geo Processing tool out of the code. The tool would accept the parameter under the format of  the URL which would be something along the lines of:  'https://maps.portaladdress.com/arcgis/rest/services/Hosted/Feature_layer/FeatureServer/0'

 

I have tried to add the url to the code but it is unsuccessful. Here is what it looked like (I also tried the .Search method):

 

feature_service_layer = gis.content.get(https://maps.portaladdress.com/arcgis/rest/services/Hosted/Feature_layer/FeatureServer/0)

feature_service_layer = gis.content.search(https://maps.portaladdress.com/arcgis/rest/services/Hosted/Feature_layer/FeatureServer/0)

 

If I can't use these methods how am I supposed to take a feature layer in as a parameter and access its data?

 

Any help is appreciated. 

Thanks! 

0 Kudos
4 Replies
ZacharyKasson
New Contributor III

Update,

I have found that this actually does work : 

feature_service_layer = gis.content.search('https://maps.portal.com/arcgis/rest/services/Hosted/feature_layer/FeatureServer')

But it gives me the content as a a list which has proven hard to get to a sdf.

I tried :

sdf = pd.DataFrame.spatial.from_layer(feature_service_layer[0])

 but received this error:

Traceback (most recent call last):
  File "y:\Zack\Other_Projects\Python_projects\Duplicate Stand ID Tool\id_duplicate_tool_featureservice.py", line 33, in <module>
    sdf = pd.DataFrame.spatial.from_layer(feature_service_layer[0])
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py", line 3077, in from_layer
    raise Exception("Could not load the dataset: %s" % str(e))
Exception: Could not load the dataset: 'Item' object has no attribute 'filter'
PS Y:\Zack\Other_Projects\Python_projects\Duplicate Stand ID Tool>
0 Kudos
EarlMedina
Esri Regular Contributor

Have you considered just using FeatureLayer? If you know ahead of time what the URL is, you don't have to search for it.

from arcgis import GIS
from arcgis.features import FeatureLayer

gis = GIS()
url = "https://urlforlayer/0"
fl = FeatureLayer(url, gis)
0 Kudos
ZacharyKasson
New Contributor III

I tried that. Similar issue. I can access the layer but not the table.

feature_service_item = arcgis.features.FeatureLayer('https://maps.url.com/arcgis/rest/services/Hosted/FL/FeatureServer')
print(feature_service_item)

sdf = pd.DataFrame.spatial.from_layer(feature_service_item)

Returns 

Exception: Could not load the dataset: Cannot perform query. Invalid query parameters.

 

0 Kudos
maile
by
New Contributor

Hi, there is a section on the site featurelayercollection , that will solve your problem.

Example:

You already know the url of the feature service and login credentials

url = 'https://maps.portaladdress.com/arcgis/rest/services/Hosted/Feature_layer/FeatureServer'
portalUrl = 'https://maps.portaladdress.com/portal/'
portalUser = 'portaladmin'
portalPassw = 'xxxxxxxxxx'

GIS login

from arcgis.gis import GIS
gisP = GIS(portalUrl, portalUser, portalPassw)

and you have access to layers and tables in the feature service

from arcgis.features import FeatureLayerCollection
flc=FeatureLayerCollection(url, gisP)
print(flc.layers) # or flc.tables

 and you can continue processing

for item in flc:
   print(item.url)
   ...
0 Kudos