Error: Error in setting scale value; Using ConvertWebMaptoMapDocument/arcpy.mapping

825
1
Jump to solution
12-04-2020 05:55 AM
by Anonymous User
Not applicable

Hi! I have created a widget that creates a PDF from a web map. It's working beautifully, and now I need to set the scale of the map on the printed PDF to a specific scale. It should be pretty straightforward (df.scale = 6000), but no matter what I do, I get the following error: RuntimeError: DataFrameObject: Error in setting scale value

I've done a lot of searching and tried many different things, but I either get the same error, or it runs but doesn't set the scale.

Here's what I've done (or at least what I remember):

  • Run the script with the data frame scale set in the .mxd as automatic and fixed scale (same error)
  • Added arcpy.RefreshActiveView() 

Below is a piece of my script; the selected point gets centered on the map, and I want it to zoom to a scale of 1:6,000, rather than stay at the scale of the web map. Can anyone help identify what I'm doing wrong? In case you need to know, the map document is in 10.5.1 and Python is v2.

Your assistance is much appreciated! Thanks!

Annina

# analysis
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "Points")[0]
# Selects the user-created point, zooms the data frame to the selected point, and then unselects the point
# so it doesn't show as teal in the map
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION")
df.zoomToSelectedFeatures()
df.scale = 6000
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
# Removes the user-created point layer from the legend but keeps it in the map
drawn = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]
drawn.removeItem(lyr)
## arcpy.AddMessage("Layer Name: " + lyr.name)
dictResult = PerformAnalysis(lyr, ExcelFileName)

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I did more searching and tried to understand some of the posts better. I used this post and Dev Tools. I was having trouble with reading it because of the one-line python script, but I finally got through it. 

I added my piece before the

result = arcpy.mapping.ConvertWebMapToMapDocument(Web_Map_as_JSON, templateMxd,                     r'C:\temp\notes.gdb')

section, which is where the web map is converted to the document.

Here's my addition:

#Extra parameter - Scale
Scale = float(6000)
# modify Web_Map_as_JSON so that the web map scale matches the scale provided.
Web_Map_Dict = json.loads(Web_Map_as_JSON)
Web_Map_Dict["mapOptions"]["scale"] = Scale
Web_Map_as_JSON = json.dumps(Web_Map_Dict)

For those of you that are newbies to Python, like I am, this part was a little difficult because I wasn't understanding where some of the variables were coming from, such as "mapOptions" and "scale". Turns out, these are in the json, and this section is looking for the "scale" section in the "mapOptions" section of the json that's being taken from the web map and applying the new scale value from the Python script.

View solution in original post

0 Kudos
1 Reply
by Anonymous User
Not applicable

I did more searching and tried to understand some of the posts better. I used this post and Dev Tools. I was having trouble with reading it because of the one-line python script, but I finally got through it. 

I added my piece before the

result = arcpy.mapping.ConvertWebMapToMapDocument(Web_Map_as_JSON, templateMxd,                     r'C:\temp\notes.gdb')

section, which is where the web map is converted to the document.

Here's my addition:

#Extra parameter - Scale
Scale = float(6000)
# modify Web_Map_as_JSON so that the web map scale matches the scale provided.
Web_Map_Dict = json.loads(Web_Map_as_JSON)
Web_Map_Dict["mapOptions"]["scale"] = Scale
Web_Map_as_JSON = json.dumps(Web_Map_Dict)

For those of you that are newbies to Python, like I am, this part was a little difficult because I wasn't understanding where some of the variables were coming from, such as "mapOptions" and "scale". Turns out, these are in the json, and this section is looking for the "scale" section in the "mapOptions" section of the json that's being taken from the web map and applying the new scale value from the Python script.

0 Kudos