Python definition query - script created in ArcMap to be used in Pro

2784
2
Jump to solution
11-03-2017 11:47 AM
PeteCoventry3
Occasional Contributor

My goal is to create a script that applies a definition query to multiple layers in ArcGIS Pro. I was able to successfully create a script (not the most elegant) in ArcMap and not knowing that the GUI is different in Pro and causing errors to my script.

I'm new to Python and doing my best to recreate this script in ArcGIS Pro, but having difficulty especially using the listLayer.    

Any help would be greatly appreciated!

Here's my script created in ArcMap:

     

mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_1ST_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_2ND_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_3RD_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_4TH_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_5TH_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Here's my attempt to convert the script to version 3 to use in ArcGIS Pro:

aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_1ST_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)
aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_2ND_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)
aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_3RD_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)
aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_4TH_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)
aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_5TH_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Have you tried the code?  If so, what was the first error?  (I am guessing Line 02).

Your question/issue doesn't involve any Python 2 to Python 3 issues.  The issue you are running into has to do with the ArcGIS Pro GUI being significantly different than ArcMap, and the ArcGIS Pro mapping module being completely new to address the restructured Pro GUI.  In short, it is an ArcMap to ArcGIS Pro ArcPy conversion issue, not a Python version conversion issue.

Since the ArcGIS Pro GUI is so different than ArcMap GUI, I suggest you read through the ArcGIS Pro  documentation to get familiar with how the new ArcPy mapping module works.  Updating the query definition itself is the same between Pro and ArcMap, it is getting access to the layers that is different between the two:

map_name = # name of Pro map tab containing layers to be updated

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps(map_name)[0]
lyr = map.listLayers("CONDO_1ST_LEVEL*")[0]
lyr.definitionQuery = '"SUBCODE" =\'1172\''

View solution in original post

2 Replies
JoshuaBixby
MVP Esteemed Contributor

Have you tried the code?  If so, what was the first error?  (I am guessing Line 02).

Your question/issue doesn't involve any Python 2 to Python 3 issues.  The issue you are running into has to do with the ArcGIS Pro GUI being significantly different than ArcMap, and the ArcGIS Pro mapping module being completely new to address the restructured Pro GUI.  In short, it is an ArcMap to ArcGIS Pro ArcPy conversion issue, not a Python version conversion issue.

Since the ArcGIS Pro GUI is so different than ArcMap GUI, I suggest you read through the ArcGIS Pro  documentation to get familiar with how the new ArcPy mapping module works.  Updating the query definition itself is the same between Pro and ArcMap, it is getting access to the layers that is different between the two:

map_name = # name of Pro map tab containing layers to be updated

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps(map_name)[0]
lyr = map.listLayers("CONDO_1ST_LEVEL*")[0]
lyr.definitionQuery = '"SUBCODE" =\'1172\''
PeteCoventry3
Occasional Contributor

Hi Joshua

Thanks so much for the reply and clarification that it's more of a GUI issue than a Python 2 to Python 3 issue.  Again, I'm very new to this and appreciate the clarification.  

I've updated my question so that it more accurately addresses the issue. 

Yes, when I ran the script created in ArcMap in Pro it was line 2 that I was receiving the following error: 

    Traceback (most recent call last):
     File "<string>", line 2, in <module>
      NameError: name 'm' is not defined

So I ran the script you've provided and it worked perfectly!!  Thanks so much!!

0 Kudos