Accessing Feature Layer Collection Properties

1053
8
02-22-2023 11:19 AM
by Anonymous User
Not applicable

Hi, I'm trying to query and edit my feature layer collection's alternative titles and layer names but can't access either property. This is the code I'm testing with:

groups = gis.groups.search()
for g in groups: # looping through groups
    content = g.content()
    print(content) # printing list of each group's content
    for c in content:
        print(c) # printing each group's content separately as an item, has type Feature Layer Collection
        print(c.properties) # trying to print content's properties for viewing

From the API's documentation, I wanted to use the ``.properties`` command  to access the feature layer collection's properties but this line in my code outputted None.

Is ``.properties`` the correct command to use and if not, could someone point me in the right direction? For each of the Feature Layer Collections that I'm looping through in the second loop, I'm trying to access and edit the alternative title and layer name.

Thanks!

0 Kudos
8 Replies
MobiusSnake
MVP

The search() method gives you back Item objects, not just feature layers.  You could get S123 forms, web maps, all kinds of other stuff.  You need to filter the results, either pre-query using the weird query language that the content API uses, or checking each returned item.

0 Kudos
by Anonymous User
Not applicable

Hi MobiusSnake, thanks for your response! I was planning on checking each returned item and if their alternative title is empty or what I'm searching for, I'd set it to be the same as the layer name. However, right now I'm unable to access these values to check or pre-query. Any ideas on the syntax to access the properties for pre-querying or checking returns?

0 Kudos
MobiusSnake
MVP

Bunch of ways to do this, but if you want to do it quick and greasy, use the FeatureLayerCollection::fromitem class method.  Wrap some exception handling around that, bam!

by Anonymous User
Not applicable

Thanks for the suggestion! I'm trying it now and tried it twice so far: once with my content and once following the documentation's example: https://developers.arcgis.com/python/guide/updating-feature-layer-properties/ but both times, I'm getting this same error:

AttributeError: 'PropertyMap' instance has no attribute 'layers'

 at the ports_flc = FeatureLayerCollection.fromitem(ports_item) line. Have you gotten this error before?

0 Kudos
MobiusSnake
MVP

Is it possible you've got some other items with similar names that you're getting instead of your feature service?  That looks to me like an error you'd see if you were getting something like a File GDB item or a Survey123 form item instead of your feature layer item.

Try inserting this line before you call fromitem, it should say "Feature Service", if it says something else then you're grabbing the wrong item:

# Assuming your item is referenced by "ports_item" like the sample
print(f"Item Type: {ports_item.type}")
by Anonymous User
Not applicable

Hi MobiusSnake, I can access the properties now. For the alternative title, I realized it was something my organization formatted into each item's description, I'm sorry for any confusion I caused. Thank you so much for all your help!

0 Kudos
Clubdebambos
Occasional Contributor III

Hi @Anonymous User 

Im not sure what you mean by alternative titles. Perhaps this is the 'name' property as shown below? If the item is a feature service, it will print the layers and tables within the service.

groups = gis.groups.search()
for g in groups: # looping through groups
    content = g.content()
    print(content) # printing list of each group's content
    for c in content:
        print(c) # printing each group's content separately as an item, has type Feature Layer Collection
        print(c.title)
        print(c.name)
        print(c.type)
        ## if it is a feature service, print the names of layers and tables
        if c.type == "Feature Service":
            for lyr in c.layers:
                print(lyr.properties.name)
            for tbl in c.tables:
                print(tbl.properties.name)

 

You can access any item properties using the below. Just remember that these are the ITEM properties only, if you wanted the properties of a Feature Layer Collection, Feature Layer, Table, or WebMap for examples, you need to access the properties of these as their own objects.

from arcgis.gis import GIS

agol = GIS("home")
item = agol.content.get("AGOL_ITEM_ID")

for prpty in item:
    print(prpty, item[prpty])

  

~ learn.finaldraftmapping.com
by Anonymous User
Not applicable

Hi Clubdebambos, thanks for your reply! I was able to use the lyr.properties.name syntax to access my layer name. As for alternative title, I realized it was something my organization formatted in the description, I'm sorry for any confusion this caused. I can access both elements now and am using the .update() command to edit my description's alternative title. Thank you again for the help!