Select to view content in your preferred language

Configure Layer definition using the Python API?

2156
5
Jump to solution
09-14-2021 09:51 AM
ngrid_ShelbyZ
Occasional Contributor

Hello API Community, 

I'm working to automatically copy maps using the Clone Content function. Part of this process is to alter layer definitions for each cloned map. I am looking to create a map, and query a layer in said map where job = A, then clone another map, and query layers where job = B. I don't see anywhere in the API where layers can  be configured to allow queries.

I see in the Service Definition portion of the documentation, a layer can have many parameters set, but I don't see a layer definition/query is one of them.

I am wondering if anyone knows how a layer's query/definition can be changed programmatically?

0 Kudos
1 Solution

Accepted Solutions
MehdiPira1
Esri Contributor

@ngrid_ShelbyZ ,

Yes, that's possible. See the code snippet below:

# get the layer
buffer_item = gis.content.get('item_id')
# add the layer with query to the web map with a new title/name
web_map.add_layer(buffer_item.layers[0].query("Location_Status = 'Active'"),options={'title':'Active Buffers'})
# save as a new web map
webmap_item_properties = {'title':'Some Active Areas',
                         'snippet':'Map created using Python API showing locations',
                         'tags':['automation', 'webmap', 'features', 'python']}

web_map.save(webmap_item_properties)

Cheers

Mehdi

View solution in original post

5 Replies
MehdiPira1
Esri Contributor

@ngrid_ShelbyZ ,

If you want to query feature layers, the following link provides some good examples of querying feature layers:

https://developers.arcgis.com/python/guide/working-with-feature-layers-and-features/

ngrid_ShelbyZ
Occasional Contributor

Maybe Im not being clear. I'm trying to add layer to a map while specifying a query on layers in that map. I dont see a corresponding parameter that can be set on a layer. Can you confirm if this is not possible via the Python API Add_Layer Function?

Create and Add Layer to Web Map

Set Layer to show Where type= 'Apples'

Save Layer and Map

 

0 Kudos
MehdiPira1
Esri Contributor

@ngrid_ShelbyZ ,

Yes, that's possible. See the code snippet below:

# get the layer
buffer_item = gis.content.get('item_id')
# add the layer with query to the web map with a new title/name
web_map.add_layer(buffer_item.layers[0].query("Location_Status = 'Active'"),options={'title':'Active Buffers'})
# save as a new web map
webmap_item_properties = {'title':'Some Active Areas',
                         'snippet':'Map created using Python API showing locations',
                         'tags':['automation', 'webmap', 'features', 'python']}

web_map.save(webmap_item_properties)

Cheers

Mehdi

ngrid_ShelbyZ
Occasional Contributor

Mehdi,

 

Thank you, this is the solution Im looking for. I'm wondering what format it creates? It seems to be a different format than a normal feature service. Notice how you cant navigate to a description of the service, and the dropdown list of the layer is different. Do you know if this is different that a hosted feature service?

 

44.png45.png

0 Kudos
MehdiPira1
Esri Contributor

@ngrid_ShelbyZ ,

Glad that was helpful.

This is a temporary feature layer which still does not exist in the Content.

To make it permanent, you need to Save it as layer and create a new item.

MehdiPira1_1-1632115880536.png