Select to view content in your preferred language

Save mapx file using python

6468
15
09-20-2019 08:02 AM
JohnSawicki1
Emerging Contributor

I am looking to save some but not all of my  Maps in an ArcGIS Pro document as templates. I know I can save the layers out using python but can't find a way to save the "mapx" file using python. I am saving these pieces tp put them into source control. 

Thanks 

John 

0 Kudos
15 Replies
Jeremy_Moore
MVP Alum

When you say ArcGIS Pro Documents are you referring to an ArcGIS Pro project file (.aprx) that contains multiple maps?

EDIT:

      I'm pretty sure you are talking about .aprx files because I see where you can export the maps as a .mapx file in ArcGIS Pro. However I have not been able to find any way to save a mapx in arcpy. I took a look at the arcpy Mapping Module which was designed to access the information inside of a .aprx file:

Introduction to arcpy.mp—ArcPy | ArcGIS Desktop 

I also looked in the Map Class for the Mapping Module:

Map—ArcPy | ArcGIS Desktop 

I saw no where in the documentation about saving or exporting as a .mapx. I Also tried to see if saveACopy() would work and it did not. Maybe someone else has more insight?

JohnSawicki1
Emerging Contributor

Thanks for figuring out what I meant. I also tried saveACopy on a map but no luck. Seems silly that you can save a map "mapx" externally using from the user interface but there appears to be no way to do so in python.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

ArcPy has never been a complete SDK for ArcGIS Desktop.  If saving *.mapx files is important, log an enhancement request with Esri Support and submit an ArcGIS Ideas‌.

0 Kudos
Luke_Pinner
MVP Regular Contributor

I don't know if this will work and can't test*, but you could try getting the CIM definition (>= ArcGIS 2.4) from the map and then use json.dump / json.dumps to write the .mapx file manually.

* corporate IT moves slooowly and we're still on 2.3

Something like:

import arcpy
import json

aprx_path = "path\\to\\your.aprx"
mapx_path = "path\\to\\{}.mapx"

aprx = arcpy.mp.ArcGISProject(aprx_path)
for map in aprx.listMaps():
    print("Map: " + map.name)
    map_def = map.getDefinition()  # <-- Get the CIM definition
    with open(mapx_path.format(map.name), 'w') as map_file:
         map_file.write(json.dumps(map_def))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

From the documentation - Python CIM access—ArcPy | ArcGIS Desktop 

Python CIM access

The arcpy.mp module ... does not provide access to all properties, settings, and capabilities available in ArcGIS Pro. One reason is to keep the API streamlined, simple, and manageable. Another reason is that ArcGIS Pro is being developed at such a rapid pace that the APIs can't keep up. It may take a release or more before you have access through the managed APIs. Starting with ArcGIS Pro 2.4, Python developers will have fine-grained access to the Cartographic Information Model (CIM) and can access many more settings, properties, and capabilities that are persisted in a project or document.

...

What is the CIM

The CIM is the Esri Cartographic Information Model. It is a map content specification used to document how information that describes various project components is persisted when saved, read, referenced, or opened. The specification is represented as JSON and is used for maps, scenes, layouts, layers, symbols, and styles in ArcGIS applications and APIs.

Luke_Pinner
MVP Regular Contributor

John Sawicki‌ did you try this? I'm interested to see if it actually worked or not since I can't test.

0 Kudos
JohnSawicki1
Emerging Contributor

I can't seem to make it work ... This line

map_file.write(json.dumps(map_def))

Give me this error:

TypeError: Object of type 'CIMMap' is not JSON serializable

I have tried a few different ways to output the object but so far no luck ...

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

map.getDefinition doesn't return JSON or an object that can be directly serialized to JSON, hence the error.  The function returns a CIMMap object that you will have to serialize by hand (not by hand by hand, but write your own function to do it).

0 Kudos
KimSundeen2
New Contributor

Hey John. Did you ever solve this? I addressed a similar JSON serializable error with the following code:

 import arcpy import json aprx_path = "path\\to\\your.aprx" 
 mapx_path = "path\\to\\{}.mapx" 
 aprx = arcpy.mp.ArcGISProject(aprx_path) 
for map in aprx.listMaps(): 
     print("Map: " + map.name) 
     map_def = map.getDefinition() # <-- Get the CIM definition 
 
 with open(mapx_path.format(map.name), 'w'as map_file: 
    json_mapx_data = json.load(map_file) # <-- Needed serialize and load mapx as JSON 
    json_mapx_data .write(json.dumps(map_def)) 
JohnSawicki1
Emerging Contributor

Kim

Thanks ! I will give it a try! I also found this:

exportToMAPX (out_mapx)
ParameterExplanationData Type
out_mapx

A string used to save a Map to a map file (.mapx).

String

This method is useful if you want to save a map to a map file that can be imported later into a project using the ArcGISProject importDocument method.

Map—ArcGIS Pro | Documentation 

I think it is new but I am stuck on a old version of pro.

John

0 Kudos