Get mapx Data Source Properties In Python

710
2
05-11-2021 02:19 PM
StevenCross
New Contributor III

I am trying to write a python script that will crawl through the arcgisinput folder/subfolders for all the MXD, and .mapx documents that have been published to ArcGIS Enterprise. I would like to get all the Data Source properties (MapDocument Name, Feature Class, Database, Owner etc). I have a working script for MXD's but cannot incorporate the .mapx file. 

Any help with this would be greatly appreciated.

 

0 Kudos
2 Replies
Brian_Wilson
Occasional Contributor III

I've not used mapx files yet. I just created one and looked at it.

A mapx file is just a text file containing JSON data and they are not even minified so you can just list them out. Looks like all the information you want is in the section "layerDefinitions".

Generally I load JSON files into Python objects and then examine them in the Visual Studio Code debugger and pull them apart with Python until I find the bits that I need.

Does that make sense to you or do you want a snippet of Python?

Maybe there is some cool Python API thing, I'm going to look because I want the rich Esri learning experience but generally I'd just hack the JSON personally.

 

0 Kudos
Brian_Wilson
Occasional Contributor III

I did a brief search of arcgis py but did not see anything helpful, so I wrote a few lines of plain old Python (no Esri modules). Here is the code,

"""
Open a mapx file and find the Data Source properties 
"""
import json
file = "k:/e911/e911 map.mapx"
with open(file, 'r') as fp:
    mapx = json.load(fp)

map_name = mapx['mapDefinition']['name']
layers = mapx['layerDefinitions']
for layer in layers: 
    print("-"*72)
    print("type: %s name: \"%s\"" % (layer['type'], layer['name']))
    try:
        data = layer['featureTable']['dataConnection']
        print("dataset: %s" % data['dataset'])
        print("%s\t%s" % (data['workspaceFactory'], data['workspaceConnectionString']))
    except:
        # Uncomment this line for debugging, so you can see what did not 
        # look like a data source. For example, group layers show up here.
        print(json.dumps(layer, indent=2))
        pass
    print()