Get the list of Feature layer which contains a specific layer

3056
8
Jump to solution
01-24-2022 11:58 AM
Aravinthkumar
New Contributor III

Hello All, 

I have a layer (0) with its name, for example River , I wanted to list all the Feature layers which contain the specific layer (0) River based on the name and JSON file using the API. How can I get the list of feature layers with its Id, owner name using ArcGIS Python API? I can't get the proper idea of it. Kindly guide me. 

Thanks for any insights. 

0 Kudos
2 Solutions

Accepted Solutions
MehdiPira1
Esri Contributor

@Aravinthkumar ,

If you mean the layers of a feature layer collection, then the following script should get you the layer name and layer id. Note that the first line is set to all layers but I have put a limit to go through 10 items. However you can change filter and/or increase the max_items value:

 

layer_items = gis.content.search("*", item_type='Feature Layer', max_items=10)

for item in layer_items:
    for layer in item.layers:
        for lyr in layer.properties:
            if "river" in layer.properties.name.lower():
                print(f"Layer name: {layer.properties.name}, \tLayer ID: {layer.properties.id}")
                break

 

Cheers

Mehdi

View solution in original post

MehdiPira1
Esri Contributor

@Aravinthkumar ,

No, it returns all types of geometry as long as they are Feature Layers specified in the item_type and you have the right access to them.

layer.properties.name.upper() will make all the layer name an upper case, so there is no need to worry about that.

There should be something wrong either with your feature layers (e.g., types or names, ...) or your level of access (role) if you are searching other members' items.

View solution in original post

0 Kudos
8 Replies
MehdiPira1
Esri Contributor

@Aravinthkumar ,

The following code snippet lists up to 100 feature layers which contain "River" in their title along with feature layer owner, and their IDs:

layer_items = gis.content.search("river", item_type='Feature Service', max_items=100)
for layer in layer_items:
    if "river" in layer.title.lower():
        print(f"Layer name: {layer.title}, \tOwner: {layer.owner}, \tLayer ID: {layer.id}")

I hope that's helpful.

Mehdi

0 Kudos
Aravinthkumar
New Contributor III

@MehdiPira1 , Thank you for the input, the code runs without any errors but no layer is printed out. I know I have those layers in my portal. Kindly let me know why it is like this ??

0 Kudos
MehdiPira1
Esri Contributor

@Aravinthkumar ,

What layer items do you get when you run the first line followed by layer_items?

layer_items = gis.content.search("river", item_type='Feature Service', max_items=100)
layer_items

 

Aravinthkumar
New Contributor III

I did not get any layers if I print the "layer_items", I have got a suggestion that layer doesn't have a title property. The parent item has a title, but the layers have properties, in which I can find a name. 

0 Kudos
MehdiPira1
Esri Contributor

@Aravinthkumar ,

If you mean the layers of a feature layer collection, then the following script should get you the layer name and layer id. Note that the first line is set to all layers but I have put a limit to go through 10 items. However you can change filter and/or increase the max_items value:

 

layer_items = gis.content.search("*", item_type='Feature Layer', max_items=10)

for item in layer_items:
    for layer in item.layers:
        for lyr in layer.properties:
            if "river" in layer.properties.name.lower():
                print(f"Layer name: {layer.properties.name}, \tLayer ID: {layer.properties.id}")
                break

 

Cheers

Mehdi

Aravinthkumar
New Contributor III

@MehdiPira1 ,

Thank you, but this returns results only if the Geometry type of the layer as: esriGeometryPolygon, but my query has the layer Geometrytype: esriGeometryPoint, so it doesn't print any layers. What can be done to get the point layers too?

To addition: if the layer name contains capital letters I have changed properties.layer.name.upper() but what can be done if the layer name has both lower and upper case with numbers. 

Kindly help me to understand this. 

Thank you. 

0 Kudos
MehdiPira1
Esri Contributor

@Aravinthkumar ,

No, it returns all types of geometry as long as they are Feature Layers specified in the item_type and you have the right access to them.

layer.properties.name.upper() will make all the layer name an upper case, so there is no need to worry about that.

There should be something wrong either with your feature layers (e.g., types or names, ...) or your level of access (role) if you are searching other members' items.

0 Kudos
Aravinthkumar
New Contributor III

@MehdiPira1 , 

I am one of the admin in the portal so I have access to all the features and layers, I have used these exact lines of code, 


layer_items = gis.content.search("*", item_type="Feature Layer", max_items=100)

for item in layer_items:

      print(item.layers)

      for layer in item.layers or []:

            for lyr in layer.properties:

if "WETTBEWERBER" in layer.properties.name.upper():

                      print(f"Layer name: {layer.properties.name}, \tLayer ID: {layer.properties.id}, \tFeature layer: {item.layers}")

                break

 

The part of the printed results are shown below, the printed resutls are for the print(item.layers) in the third line of the code, even though some have [] empty lists and None, I am not sure, why its like this, so the name condition don't satisfy here. To check the name condition I took of the printed URL and took the name of the layer from the URL that has given the result but only for the esrigeometryPolygon not if I take the layer name of the esrigeometryPoint, Also If I can mention the ID of the feature layer I can get the layer in the result, but I don't know the ID for all the feature layer which has the exact layer, so this is the problem now. 

 

[<FeatureLayer url:"https://maps.com/server/rest/services/MDB2B/FeatureServer/0">]

[<FeatureLayer url:"https://maps.com/server/rest/services/Hosted/Shape_STRAB/FeatureServer/0">]

[<FeatureLayer url:"https://maps.com/server/rest/services/Hosted/MARKTSTAMM_MARKTSTAMM_PUNKT/FeatureServer/0">]

[]

[<FeatureLayer url:"https://maps.com/server/rest/services/Hosted/ShapesOrte2_verbinden/FeatureServer/0">]

[<FeatureLayer url:"https://maps.com/server/rest/services/Hosted/SONDERIMMOBILIEN_INT/FeatureServer/0">]

None

[<FeatureLayer url:"https://maps.com/server/rest/services/Hosted/Marktstamm/FeatureServer/0">]

[<FeatureLayer url:"https://maps.com/server/rest/services/BEFRAGUNGSC/FeatureServer/0">, <FeatureLayer url:"https://maps.com/server/rest/services/BEFUSTER_INT/FeatureServer/1">, <FeatureLayer url:"https://maps.com/server/rest/services/BEFRAGUN/FeatureServer/2">]

[<FeatureLayer url:"https://maps.com/server/rest/services/Hosted/Ortsliste/FeatureServer/0">]

Any other insights will be very helpfull, Thank you so much.. 

0 Kudos