TitlePages

Tuesday, 25 November 2014

Working with Group layers through Python

TOOL USED:  arcpy.mapping

Python script will be deployed in the ARC Toolbox to create the Conversion Tool
WORKING OF THE TOOL:
This tool will iterate through each layer in the group layer and perform operation on each layer for every iterations

PROCEDURE:


  • Create a New Script tool in your toolbox of your ArcCatalog 
  • Set any empty python code file as source file in which you are going to write your script
  • Set Output layer as input parameter and group layers as the data Type
  • Copy the following code into your python code file



import arcpy
from arcpy import env
arcpy.env.overwriteOutput=1
outputgrouplayer=arcpy.GetParameter(0)

#Assign the output group layer to a variable, to be used later
lyrs =outputgrouplayer

print lyrs
#Create a Mapping Layer out of the Group Layer
groupLayer = arcpy.mapping.Layer(lyrs)

#Get a list of all the mappping layers, the first return will be the Group Layer itself
newLyrs = arcpy.mapping.ListLayers(groupLayer)

#Iterate through all of the Layers (mapping layers)
for lyr in newLyrs:
    #Because the first return is the group layer itself, we check for this, and move on
    if not lyr.isGroupLayer:
        print "Layer name is: {0}. Count is: {1}.".format(lyr.name, str(arcpy.GetCount_management(lyr)))

  • This will print the layer name and count of feature for every layer in the Group layer inputted
  • Run the script tool and target your group layer as the input in the parameter box. Click on Ok
  • Now it is Party time. Happy Coding

No comments: