usage of ConvertWebMapToMapDocument

1494
2
03-14-2017 05:10 AM
Nicole_Ueberschär
Esri Regular Contributor

I am trying to somehow backup WebMaps from AGOL and am thinking the ConvertWebMapToMapDocument function could be helpful with this. I thought of saving (1) the json file itself and (2) save it as mxd. While I think saving the json file itself shouldn't be that dificult, I'm struggeling finding a way to make ConvertWebMapToMapDocument work.

Unfortunately, when looking through different examples in the documentation and through the web, I always find a WebMap_as_JSON variable (or similar named) that gets its value from a tool as it seems (getAttributeAsText(0)). I assume that it expects the json code of the WebMap which could be this one http://www.arcgis.com/sharing/rest/content/items/95562b6a24994641bbbaa3d1f2b7cf4f/data?f=pjson

I managed to read the content by adding

jsonResponse = urllib.urlopen(webmap)
WebMap_as_JSON = json.loads(jsonResponse.read())

When I print now WebMap_as_JSON I get the same content as at the URL above though in a different order. 

But when I try to convert now my WebMap_as_JSON to a mapdocument with 

result = arcpy.mapping.ConvertWebMapToMapDocument(WebMap_as_JSON)

I receive a Runtime Error:

RuntimeError: Expected to find comma, colon or start of array; state : startOfObject; buffer : {u'authoringAppVersion': u'5.1', u'authoringApp':

So I assume it's still not the right content as the input. 

I would highly appreciate if someone could give me a hint how to get the right input for ConvertWebMapToMapDocument. Thanks a lot!

For you to try, this is what I have so far: 

import arcrest
import arcpy, urllib, urllib2, json

webmap="http://www.arcgis.com/sharing/rest/content/items/95562b6a24994641bbbaa3d1f2b7cf4f/data?f=pjson"
jsonResponse = urllib.urlopen(webmap)
WebMap_as_JSON = json.loads(jsonResponse.read())
print(WebMap_as_JSON)
result = arcpy.mapping.ConvertWebMapToMapDocument(WebMap_as_JSON)

mxd = result.mapDocument
mxd.saveACopy(r"<here is my path to the output mxd file>")

print("Map document saved")‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
2 Replies
JonathanQuinn
Esri Frequent Contributor

ConvertWebMapToMapDocument takes a string as the WebMap_As_JSON parameter, which is essentially what you have when you do 

WebMap_as_JSON = jsonResponse.read()

Note I got rid of the json.loads as that converts the contents to JSON, (which is why you see the u character, indicating it's unicode).

What ends up working is something like this:

import arcpy, urllib, urllib2, json
webmap="http://www.arcgis.com/sharing/rest/content/items/95562b6a24994641bbbaa3d1f2b7cf4f/data?f=pjson"
WebMap_as_JSON = urllib.urlopen(webmap).read()
result = arcpy.mapping.ConvertWebMapToMapDocument(WebMap_as_JSON)
mxd = result.mapDocument
mxd.saveACopy(r"C:\Temp\myMap.mxd")
print("Map document saved")

I'm not sure the resulting MXD is really giving you a backup of the webmap though.  A backup implies that you can restore it in case there was any problems.  In the case of turning a webmap into an MXD, you're simply taking the operational layers within the webmap and creating layers in a map document that point to their original source.  For example, the feature service layer in the webmap is still simply a feature service in the map document.  You can't then publish the map document back to ArcGIS Online.  You can backup your webmaps by creating another folder in your My Content called "Backups" or something and save a copy of the webmap in that folder.

Nicole_Ueberschär
Esri Regular Contributor

Thank you Jonathan! Now it makes sense to me how to use the ConvertWebMapToMapDocument.

My intention is to have a local copy of the map definition, that is what I call a Backup. Saving a second copy in the same location might help if someone is spoiling the original one by accident. But if for example my subscription expires I can't access my AGOL items anymore. 

I agree that I won't be able to restore my original webmap with this with one click but at least I can save the map composition. 

0 Kudos