How to query a specific layer from a Feature Service

793
4
01-26-2022 08:07 AM
Aravinthkumar
New Contributor III

Hello everyone,
I am trying to get the list of feature service which has a specific layer (for example WETTBEWERBER (the layer id is 0)) from a feature service,

 

from arcgis.gis import GIS
gis = GIS(url='https://url/portal', username='arcgis_python', password='amazing_arcgis_123')

layer_items = gis.content.search(query="WETTBEWERBER", item_type="Feature Layer")
for layer in layer_items:
    if "WETTBEWERBER" in layer.title.upper():
        print(f"Layer name: {layer.title}, \tOwner: {layer.owner}, \tLayer ID: {layer.id}")

 But no results are printed out, but the layer WETTBEWERBER is present in most of my feature services. What can be done in this case ??

I have tried to get the layers first using the following code to check whether I can get the layers using the API and I can get the layers. 

 

layer_items = gis.content.search(query="5fg239sdsfs56sdfcs466s4f", item_type="Feature Service")

for item in self.api_query_result:
    layers = item.layers
    
for lyr in layers:
        
print(item)
        
try:
            
print(item.iditem.urllyr.properties["id"]lyr.properties["name"])
        
except Exception as e:
            
print(e)

I can get all the layers including the layer WETTBEWERBER (0) in it. 

But I cannot list all the feature service which contains this particular layer, What am I doing wrong ?? 

Thank you for the insights. 

0 Kudos
4 Replies
jcarlson
MVP Esteemed Contributor

layer.title.lower() would not match an all-caps string like "WETTBEWERBER". Did you mean "layer.title.upper()"?

- Josh Carlson
Kendall County GIS
Aravinthkumar
New Contributor III

Yes, sorry for the typo here it is "layer.title.upper()" I have updated the post. 

0 Kudos
jcarlson
MVP Esteemed Contributor

Looking at it again, a layer doesn't have a title property. The parent item has a title, but the layers have properties, in which you can find a name, as shown in the other code block you quoted.

To get the feature service itself, you have to reference the parent item. This snippet would return a list of all Feature Layers in your search:

 

items = gis.content.search('some string', item_type='Feature Service')

[a.url for b in items for a in b.layers]

 

 To filter this list, we can include a simple ternary operator in the same line.

 

[a for b in items for a in b.layers if a.properties['name'] == 'WETTBEWERBER']

 

I tested this on my own layers, and searching for the layer name "Records" across a bunch of layers yielded the following:

 
['https://maps.co.kendall.il.us/server/rest/services/Cadastral/Ordinance_Fabric/FeatureServer/1',
 'https://maps.co.kendall.il.us/server/rest/services/Hosted/ord_fabric_hosted/FeatureServer/0']
- Josh Carlson
Kendall County GIS
Aravinthkumar
New Contributor III

Thank you for the detailed explanation, whether I should leave the query empty in the place of "some string" and kindly can you edit it with the exact syntax I cannot figure out from this, that will be really helpful. 

0 Kudos