I am learning Python right now for ArcGIS Pro. I'm new to object-oriented programming and need some help on understanding the statement order. I'm basically looking for a simple logic breakdown for order of statements. For example:
aprx = arcpy.mp.ArcGISProject("CURRENT")
If I'm reading this correctly, a direct translation is:
variable = object.function(parameters)
So, the current project is a parameter of the function ArcGISProject being called on the object arcpy.mp, which will be stored in the variable aprx?
What about this:
m = aprx.activeMap
activeMap is a method called on the now object aprx and stored in the variable m?
lyrList = m.listLayers()
the listLayer function is being called on the now object m and stored in the variable lyrList?
And this:
for lyr in lyrList:
if lyr.isFeatureLayer:
a for-loop. It reads if a layers in the lyrList is a feature layer it gets stored as Boolean (T/F) in lyr?
Looks good
Light reading .... see the table of contents to the left on this link
ArcGIS Pro Python reference—ArcGIS Pro | Documentation
I think that's a great understanding of how it works,
except I will say that activeMap is actually a property of aprx (the ArcGISProject object), not a method().
You then have associated the Map object returned from that property to 'm' using m = aprx.activeMap
see below link for the properties and methods of the ArcGISProject object/Class
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm
for lyr in lyrList:
if lyr.isFeatureLayer:
a for-loop. It reads if a layers in the lyrList is a feature layer it gets stored as Boolean (T/F) in lyr?
Not really. You need to do something when an if statement evaluates to True or False....
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.activeMap
lyrList = m.listLayers()
len(lyrList)
# returns 11
for i in lyrList:
print(i)
Address Points
Site Addresses
Subdivisions_MSD
MSD.SLCOMSD.AddressGridMSD
RoadCenterline All
CenterlinesMSD
MunicipalitiesMSD
MSD.SLCOMSD.ParcelsMSD
MSD.SLCOMSD.Zones
ZipcodesMSD
World Imagery
for i in lyrList:
if i.isFeatureLayer:
print('yay')
''' returns
yay
yay
yay
yay
yay
yay
yay
yay
yay
yay
'''
Notice that while the length of my layer list = 11, when the if statement evaluates true, 'yay' is only printed 10 times. That's because the entry 'World Imagery' is not a feature layer. Use an else to capture that:
for i in lyrList:
if i.isFeatureLayer:
print('yay')
else:
print(f'{i} is not a feature layer')
''' returns
yay
yay
yay
yay
yay
yay
yay
yay
yay
yay
World Imagery is not a feature layer
'''
And there goes the encouragement before the OP has even had a chance to read the links 😉