I have recently upgraded ArcGIS API for python from 2.3.0 to 2.4.0 and found out that Map.content.update_layer() doesn't work with the old labeling_info json. There's no error while running the code, but it just doesn't do anything at all to web map. Here is the code for 2.3.0:
target_map_item = gis.content.get(target_map_id)
webmap = WebMap(target_map_item)
labeling_info = [
{
"labelExpression": "[owner]",
"labelExpressionInfo": {
"expression": "$feature[\"owner\"]"
},
"labelPlacement": "esriServerPolygonPlacementAlwaysHorizontal",
"maxScale": 0,
"minScale": 10000,
"repeatLabel": true,
"symbol": {
"type": "esriTS",
"color": [
255,
255,
255,
255
],
"font": {
"family": "Arial",
"size": 11
},
"horizontalAlignment": "center",
"kerning": true,
"haloColor": [
0,
0,
0,
255
],
"haloSize": 1,
"rotated": false,
"text": "",
"verticalAlignment": "baseline",
"xoffset": 0,
"yoffset": 0,
"angle": 0
},
"where": "area>= 10"
}
]
lyr = webmap.layers[0]
lyr['layerDefinition']['drawingInfo']['labelingInfo'] = labeling_info
webmap.update()
And here is the new code for 2.4.0:
wmap_item = gis.content.get(wmap_item_id)
wmap = Map(wmap_item)
wmap.content.update_layer(index = 0,labeling_info=labeling_info)
wmap.update()
Does anyone have the same problem? Can anyone help me with this?
Very appreciate!
Solved! Go to Solution.
Hi @ShiyuDing,
Try this...
labeling_info = [
{
"labelExpression": "[owner]",
"labelExpressionInfo": {
"expression": "$feature[\"owner\"]"
},
"labelPlacement": "esriServerPolygonPlacementAlwaysHorizontal",
"maxScale": 0,
"minScale": 10000,
"repeatLabel": True,
"symbol": {
"type": "esriTS",
"color": [
255,
255,
255,
255
],
"font": {
"family": "Arial",
"size": 11
},
"horizontalAlignment": "center",
"kerning": True,
"haloColor": [
0,
0,
0,
255
],
"haloSize": 1,
"rotated": False,
"text": "",
"verticalAlignment": "baseline",
"xoffset": 0,
"yoffset": 0,
"angle": 0
},
"where": "area>= 10"
}
]
options_dict = {
"showLabels" : True,
"layerDefinition" : {
"drawingInfo" : {
"labelingInfo" : labeling_info
}
}
}
wmap.content.update_layer(
index=lyr_index,
options=options_dict
)
Just a heads up, using the Map update() method is prone to cause issues in version 2.4.0 (Esri have stated this is already fixed in the next version). You will get weird things happen, contained to the WebMap, like Table corruption or WebMap corruption.
An option to overcome this is to access and update the JSON definition directly via the WebMap Item object item.get_data().
I hope that helps. Let us know if this worked with your labels.
All the best,
Glen
Hi @ShiyuDing,
Try this...
labeling_info = [
{
"labelExpression": "[owner]",
"labelExpressionInfo": {
"expression": "$feature[\"owner\"]"
},
"labelPlacement": "esriServerPolygonPlacementAlwaysHorizontal",
"maxScale": 0,
"minScale": 10000,
"repeatLabel": True,
"symbol": {
"type": "esriTS",
"color": [
255,
255,
255,
255
],
"font": {
"family": "Arial",
"size": 11
},
"horizontalAlignment": "center",
"kerning": True,
"haloColor": [
0,
0,
0,
255
],
"haloSize": 1,
"rotated": False,
"text": "",
"verticalAlignment": "baseline",
"xoffset": 0,
"yoffset": 0,
"angle": 0
},
"where": "area>= 10"
}
]
options_dict = {
"showLabels" : True,
"layerDefinition" : {
"drawingInfo" : {
"labelingInfo" : labeling_info
}
}
}
wmap.content.update_layer(
index=lyr_index,
options=options_dict
)
Just a heads up, using the Map update() method is prone to cause issues in version 2.4.0 (Esri have stated this is already fixed in the next version). You will get weird things happen, contained to the WebMap, like Table corruption or WebMap corruption.
An option to overcome this is to access and update the JSON definition directly via the WebMap Item object item.get_data().
I hope that helps. Let us know if this worked with your labels.
All the best,
Glen
Hi @Clubdebambos , thank you so much for the solution and that works! It also gave me a lot of insight about migrating the renderer from 2.3.0 to 2.4.0. About updating with JSON definition, do you mean using item.update() by specifying data parameters? Thanks a lot!
Hi @ShiyuDing,
Yes, I haven't got a direct example for labels but it would be similar to updating the popup via the item object as per this example, except you would target the specific layers "labelingInfo" instead.