Select to view content in your preferred language

What is the best way to update layer symbology using ArcGIS API for Python?

3475
8
Jump to solution
11-23-2017 12:00 PM
deleted-user-qPVBo_SO6NvL
Deactivated User

#symbology #api for python‌ 

What is the best way to update the symbology of a feature layer hosted on ArcGIS Online using the ArcGIS API for python?

1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Does the method demonstrated here work for you?: Service definitions | ArcGIS for Developers 

For example:

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

gis = GIS('https://www.arcgis.com', 'username', 'password')

search_result = gis.content.search(query='title:test', item_type='Feature Layer Collection')
flc=FeatureLayerCollection.fromitem(search_result[0])

flc.manager.update_definition({"maxRecordCount":500})

layer = flc.layers[0]
layer.manager.update_definition({"maxRecordCount":500})

# Check the results:
search_result = gis.content.search(query='title:test', item_type='Feature Layer Collection')
flc=FeatureLayerCollection.fromitem(search_result[0])

print("Service max record count: {}".format(flc.properties.maxRecordCount))
print("Layer max record count: {}".format(flc.layers[0].properties.maxRecordCount))

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

You indicate the 'best', .... does that mean you are using a method documented here.... that you want to compare to? or are you looking for something specific?

deleted-user-qPVBo_SO6NvL
Deactivated User

I'm following this tutorial but it is not working for me. This is what I'm doing

from arcgis import GIS
gis = GIS('https://www.arcgis.com', 'username', 'password')

search_result = gis.content.search('test', item_type='Feature Layer Collection')
layer_collection=search_result[0]
layer = layer_collection[0]
layer.properties.maxRecordCount = 500

layer_collection.update()

The update returns True, however the maxRecordCount value is not updated in ArcGIS Online.  After supposedly updating the value printed is the layer's original maxRecordCount and not 500.

search_result = gis.content.search('test', item_type='Feature Layer Collection')
layer_collection=search_result[0]
layer = layer_collection[0]
print(layer.properties.maxRecordCount)

The same thing happens when I try to update the drawing_info. I used maxRecordCount as an example for simplicity.

0 Kudos
by Anonymous User
Not applicable

Does the method demonstrated here work for you?: Service definitions | ArcGIS for Developers 

For example:

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

gis = GIS('https://www.arcgis.com', 'username', 'password')

search_result = gis.content.search(query='title:test', item_type='Feature Layer Collection')
flc=FeatureLayerCollection.fromitem(search_result[0])

flc.manager.update_definition({"maxRecordCount":500})

layer = flc.layers[0]
layer.manager.update_definition({"maxRecordCount":500})

# Check the results:
search_result = gis.content.search(query='title:test', item_type='Feature Layer Collection')
flc=FeatureLayerCollection.fromitem(search_result[0])

print("Service max record count: {}".format(flc.properties.maxRecordCount))
print("Layer max record count: {}".format(flc.layers[0].properties.maxRecordCount))
MatthewWalker
Occasional Contributor

I am trying to update the feature layer collection (change syncEnabled to True, add or remove editing capabilities) and have the same code as above, but get a runtime error: "Error performing update definition operation (error code: 500)" and cannot change the properties.  I've also attempted to send a post request to change the Feature Service properties, but that does not change the properties either (no error is thrown, and printing out the post response does not seem to be an error either).  My code, after publishing the Feature Service is:

#configure Feature Layer properties
gis = GIS(PORTAL_URL, USERNAME, PASSWORD)
search_result = gis.content.search(service_name, 'Feature Layer')
fl_item = search_result[0]
fl_flc = FeatureLayerCollection.fromitem(fl_item)
update_dict = {"capabilities": "Query", "syncEnabled": True}
print(fl_flc.properties)  # you can see the properties targeted to be changed listed, they just do not get changed
fl_flc.manager.update_definition({"syncEndabled": True})#update_dict)  # this throws the 500 error
"""
# trying this a different way--post changes directly
import requests
URL = PORTAL_URL
r = requests.post(URL, update_dict)
print(r.text)  # this prints the return html listed below
"""

commented out code's print statement displays:

HTML Request:
<!DOCTYPE html>
<html>
<head>
<script src="login/config/dojo.js"></script>
<script src="jsapi/dojo/dojo.js"></script>
<script>
require(["login", "dojo/domReady!"], function(login) {
login.run();
});
</script>
</head>
<body>
</body>
</html>

I am ultimately trying to automate the publishing of feature classes to Feature Services then bundle several Feature Services into a shared web map (must be automated as the underlying feature classes can vary from map to map, and there are hundreds of map needed to be shared).

0 Kudos
deleted-user-qPVBo_SO6NvL
Deactivated User

That did the trick. Thanks a lot. I hadn't found the update_definition method inside the manager classes.

0 Kudos
StephenRoss
Emerging Contributor

Could you also use this method to update the drawing info, such as the transparency of the layer?

0 Kudos
by Anonymous User
Not applicable

It's been a while since this was asked, but here's what I've been doing in a sample of my own and it seems to work well:

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

gis = GIS(profile="arcgis_online")

# Upload shapefile as zip archive, and publish new layer:
shp = arcgis_enterprise.content.add({type: "Shapefile"}, r'D:\Path\To\Shapefile.zip')
new_layer = shp.publish()

# Update symbology of layer from an existing item used as a template:
template_layer = gis.content.get("a86e128a1a5b472ca926be0ead7e0b10").layers[0]
flc = FeatureLayerCollection.fromitem(new_layer)
flc.layers[0].manager.update_definition({"drawingInfo": template_layer.properties['drawingInfo']})‍‍‍‍‍‍‍‍‍‍‍‍‍

The drawingInfo property is a dictionary object that you can update as you see fit...though if you already have a layer with a matching schema that is published with a style you want to re-use, you can get it's drawingInfo property, and apply it to another layer.

0 Kudos
MaximeCampeau
Emerging Contributor

The transparency of the layer ('opacity') can be edited upon creation of the feature layer via the add_layer function called over a WebMap just like so :

item_to_search = 'City_limits_' + str(municipality)
owner = 'XXXXXXXX'
item = gis.content.search(query="title:" + item_to_search + " AND owner: " + owner, item_type="Feature Layer", sort_field='title', sort_order='asc')
print(item)
item_selected = item[0]
empty_webmap.add_layer(item_selected, options={'title':'City limits','opacity':'0.4'})

Hope this helps,

M.C.

0 Kudos