#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?
Does the method demonstrated here work for you?: Service definitions | ArcGIS for Developers
For example:
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features <SPAN class="keyword token">import</SPAN> FeatureLayerCollection gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token"><SPAN>'</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fwww.arcgis.com" target="_blank">https://www.arcgis.com</A><SPAN>'</SPAN></SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'username'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN><SPAN class="punctuation token">)</SPAN> search_result <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN>query<SPAN class="operator token">=</SPAN><SPAN class="string token">'title:test'</SPAN><SPAN class="punctuation token">,</SPAN> item_type<SPAN class="operator token">=</SPAN><SPAN class="string token">'Feature Layer Collection'</SPAN><SPAN class="punctuation token">)</SPAN> flc<SPAN class="operator token">=</SPAN>FeatureLayerCollection<SPAN class="punctuation token">.</SPAN>fromitem<SPAN class="punctuation token">(</SPAN>search_result<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> flc<SPAN class="punctuation token">.</SPAN>manager<SPAN class="punctuation token">.</SPAN>update_definition<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"maxRecordCount"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">500</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN> layer <SPAN class="operator token">=</SPAN> flc<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> layer<SPAN class="punctuation token">.</SPAN>manager<SPAN class="punctuation token">.</SPAN>update_definition<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"maxRecordCount"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">500</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Check the results:</SPAN> search_result <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN>query<SPAN class="operator token">=</SPAN><SPAN class="string token">'title:test'</SPAN><SPAN class="punctuation token">,</SPAN> item_type<SPAN class="operator token">=</SPAN><SPAN class="string token">'Feature Layer Collection'</SPAN><SPAN class="punctuation token">)</SPAN> flc<SPAN class="operator token">=</SPAN>FeatureLayerCollection<SPAN class="punctuation token">.</SPAN>fromitem<SPAN class="punctuation token">(</SPAN>search_result<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Service max record count: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>flc<SPAN class="punctuation token">.</SPAN>properties<SPAN class="punctuation token">.</SPAN>maxRecordCount<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Layer max record count: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>flc<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>properties<SPAN class="punctuation token">.</SPAN>maxRecordCount<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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 propertiesgis = 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 changedfl_flc.manager.update_definition({"syncEndabled": True})#update_dict) # this throws the 500 error"""# trying this a different way--post changes directlyimport requestsURL = PORTAL_URLr = 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).
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:
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features <SPAN class="keyword token">import</SPAN> FeatureLayerCollection gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN>profile<SPAN class="operator token">=</SPAN><SPAN class="string token">"arcgis_online"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Upload shapefile as zip archive, and publish new layer:</SPAN> shp <SPAN class="operator token">=</SPAN> arcgis_enterprise<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN>type<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Shapefile"</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> r<SPAN class="string token">'D:\Path\To\Shapefile.zip'</SPAN><SPAN class="punctuation token">)</SPAN> new_layer <SPAN class="operator token">=</SPAN> shp<SPAN class="punctuation token">.</SPAN>publish<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Update symbology of layer from an existing item used as a template:</SPAN> template_layer <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"a86e128a1a5b472ca926be0ead7e0b10"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> flc <SPAN class="operator token">=</SPAN> FeatureLayerCollection<SPAN class="punctuation token">.</SPAN>fromitem<SPAN class="punctuation token">(</SPAN>new_layer<SPAN class="punctuation token">)</SPAN> flc<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>manager<SPAN class="punctuation token">.</SPAN>update_definition<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"drawingInfo"</SPAN><SPAN class="punctuation token">:</SPAN> template_layer<SPAN class="punctuation token">.</SPAN>properties<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'drawingInfo'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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.
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 = 'xyz' 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.
Could you also use this method to update the drawing info, such as the transparency of the layer?
That did the trick. Thanks a lot. I hadn't found the update_definition method inside the manager classes.
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.
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?
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.