-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?

1

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, 
  • 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 Feb 6 at 19:39
  • 1
    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 Feb 6 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).

  • 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 Sep 19 '18 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 Sep 19 '18 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 Sep 21 '18 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 Sep 21 '18 at 19:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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