Wednesday, November 9, 2011

Rendering Feature Sets in ArcMap from Python

If your creating a python script that will run inside ArcMap, and the feature sets that are generated from the script must be then displayed, the basic script code is below.
Read further to see how to setup your script in an ArcToolbox.


import arcpy
from arcpy import env
import sys

#The set that I will query, it has two polygons. I will select one and then render it.
queryFset = r'C:\Development\2Test_QueryPolygon.shp'

arcpy.env.overwriteOutput = True # So the script can be repeated
arcpy.env.workspace = "in_memory" # Faster have your environment in_memory
result = "in_memory/featureSetOut" # Faster to have your result in_memory

try:
selectPoly = sys.argv[1] # Argument 1, not 0, 0 is the program itself
except:
print "\nNo arguments" # Incase your testing in the command line
selectPoly = 1

arcpy.MakeFeatureLayer_management(queryFset, "lyr") # Feature layer to do work on
queryStringAll = '\"Id\" =' + str(selectPoly) # Query String with dynamic variable
print queryStringAll
selectionOption = "NEW_SELECTION" # Select by attribute option
arcpy.SelectLayerByAttribute_management("lyr", selectionOption,queryStringAll) # The actual select by attribute method

arcpy.CopyFeatures_management("lyr", result) # Put the layer in memory (see above declaration)
featSet = arcpy.FeatureSet ("featureSet") # Geoprocessing record set object arcobject.Featureset
featSet.load(result) # Put whats it memory into the feature set

arcpy.SetParameter(1,featSet) # This is confusing, its works with '1'
# Even though 1 is the sys.argv[1]
print "exited"
print arcpy.GetMessages()



No comments:

Post a Comment