Python 2 to 3 Upgrade - Help with listLayers

360
2
06-10-2019 09:07 PM
AdamRice
New Contributor II

I was wondering if anyone could help me today...

My organisation has recently realised that Python 2.7 is dying this year and are looking at updating any old scripts to Python 3 and/or Pro, and I've been having some issues with the listLayers function.

Caveat: I've run the Python 2to3 script.

Basically I have a script that creates images from a .MXD pointed at a .GDB. Everything else seems to be working/has been converted or I already fixed but for the life of me, I can't get the ListLayers function to read layers from the newer .APRX files... I think it's something with the ArcGISProject.listMaps.ListLayers but I'd love some help/advice on howe people resolved this

CODE:

Here is where I read the layers in from the .APRX and query the ID fields.

    for lyr in arcpy.mp.ArcGISProject.listMaps.ListLayers(aprx, "*", data_frame):
        if (lyr.name in list(config_file['layers'].keys())) and (lyr.supports("DEFINITIONQUERY")):
            lyr.definitionQuery = "{} = '{}'".format(config_file['layers'][lyr.name]['projectIDField'], project_id)

Here is where I validate the .APRX by checking the current layers against the config file.

   # Validate the aprx file
    try:
        check_aprx = arcpy.mp.ArcGISProject(arcpy.GetParameterAsText(0))
        arc_layer_list = [layer.name for layer in arcpy.mp.ListLayers(check_aprx)]
        config_layers = list(config_file['layers'].keys())

        if set(config_layers).issubset(arc_layer_list):
            logger.log_msg("aprx File = {} --- OK".format(arcpy.GetParameterAsText(0)))
            aprx_valid = True
        else:
            logger.log_error("aprx File = {} --- INVALID. aprx File does not contain the correct layers as indicated"
                             " in the config file. Please review.".format(arcpy.GetParameterAsText(0)))

Here is the output/ERROR:

0 Kudos
2 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Adam, I suggest you have a slow read through the new package info for 'mapping' which is not 'mp'

Introduction to arcpy.mp—ArcPy | ArcGIS Desktop 

Migrating from arcpy.mapping to ArcGIS Pro—ArcPy | ArcGIS Desktop 

Some things have move, most stayed the same, obvious things like projects are now *.aprx, but all the classes and methods are in the Table of Contents section to the left of the help topic.

eg. Alphabetical list of arcpy.mp classes—ArcPy | ArcGIS Desktop 

2 to 3 only helps with the python stuff, there isn't a mapping to mp equivalent, except tracking down the functionality... but the help topics are great and easy to follow

AdamRice
New Contributor II

Hey Dan,

Sorry for the late reply but your response wasn't helpful at all. I thought it was clear from the screenshots that I was using arcpy.mp but regardless, for those experiencing this issue the problem is you need to stipulate each method call separately.

So something like:

for lyr in arcpy.mp.ArcGISProject.listMaps.ListLayers

Should be:

aprx = arcpy.mp.ArcGISProject(arcpy.GetParameterAsText(0))
mapView = aprx.listMaps()[0]
for layer in mapView.listLayers():
 if (layer.name in list(config_file['layers'].keys())) and (layer.supports("DEFINITIONQUERY")):
 layer.definitionQuery = "{} = '{}'".format(config_file['layers'][layer.name]['projectIDField'], project_id)


Furthermore the main issue actually stemmed from the inability to set map extents (which turns out hasn't been replicated in arcpy.mp). Do you happen to know when this will be added? It's a bit hard to update code when you don't update the package...

0 Kudos