1

I am looking for a way to access a .mxd file's layer names and data sources.

In ArcGIS Desktop, I could do:

def print_layer_info(mxd_path):
    print(mxd_path + ":")
    mxd = arcpy.mapping.MapDocument(mxd_path)

    for df in arcpy.mapping.ListDataFrames(mxd, "*"):
        lyr = arcpy.mapping.ListLayers(mxd, "", df)[0]
        if lyr.supports("dataSource"):
            print "\t" + lyr.name
            print "\t\t" + lyr.dataSource
    del mxd

According to http://pro.arcgis.com/en/pro-app/arcpy/mapping/migratingfrom10xarcpymapping.htm, I could do:

p = arcpy.mp.ArcGISProject("file.mxd")

But that doesn't work. When I do that I get a OSError.

Since the arcpy.mapping.MapDocument is gone in ArcPy for ArcGIS Pro, what is the equivalent way of reading layer name and datasource of an .mxd file?

2 Answers 2

3

You can list the layers and the datasources of an MXD (ArcMap document) from ArcGIS Pro by simply importing the MXD into a project, and then following the same workflow you would do from ArcMap. (The syntax may vary a little).

  • Load up a project (like current, as the following code is run from the Python window)
  • Load the MXD into it
  • Find the map
  • List the layers

code:

p = arcpy.mp.ArcGISProject("CURRENT")
p.importDocument(r"C:\data\data.mxd")
for m in p.listMaps(): print(m.name)
> Layers
mxd = p.listMaps("Layers")[0]
for l in mxd.listLayers():
    if l.supports("dataSource"):
        print("{}, {}".format(l.name, l.dataSource))
> World Ocean Reference, 
> foo1, source1
> foo2, source2
> World Ocean Base, 
2
  • I was asking for a ArcPy-only solution. I need to process thousands of mxds, so I don't want to manually open ArcGIS Pro and then do an import for every single MXD.
    – sdMarth
    Commented Feb 6, 2019 at 19:39
  • 3
    This is an arcpy only solution. If you mean you want to run the code entirely outside ArcGIS Pro, then make 1 dummy/template Project, reference that instead of current and just import MXDs into that. Since there is no map "remove" option from the project class, trash your p reference for each run and re-establish it, thus importing into a "new-blank" project.
    – KHibma
    Commented Feb 6, 2019 at 19:51
0

It is not possible to use the arcpy.mp module that comes with ArcGIS Pro to create a MapDocument object from an *.mxd.

To do that you use arcpy.mapping from the arcpy site-package that comes with the ArcGIS Desktop 10.x architecture and works against the ArcMap application.

To work with the content of an *.mxd in ArcGIS Pro (or the arcpy which it installs) you would first need to import the *.mxd into an ArcGIS Pro project (and then create ArcGISProject and Map objects from that).

4
  • Can I programmatically create a ArcGIS Pro project or would I have to manually do it? Ultimately, I'm writing a script to automate over many .mxd files.
    – sdMarth
    Commented Sep 19, 2018 at 21:48
  • Welcome to GIS SE! As a new user be sure to take the Tour to learn about our focussed Q&A format. That should be asked as a new question.
    – PolyGeo
    Commented Sep 19, 2018 at 22:04
  • I would also like to know if there's a way to create a virtual ArcGIS Pro Project using arcpy. I am only interested in being able to read information about layers in an .mxd
    – sdMarth
    Commented Sep 21, 2018 at 15:30
  • What do you mean by a "virtual ArcGIS Pro Project"? In any event it sounds like you may need to ask a new question about that.
    – PolyGeo
    Commented Sep 21, 2018 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.