Hi,
I wann a create a python script, for creating polygon feature from coordinate array, what should be the data type for accessing the array in my paramater list.
Thanks & Regards,
Sri.
There is a code example at the end of the arcpy.Polygon class help topic Polygon—ArcPy Classes | ArcGIS for Desktop
Hi Dan,
Thank you for your quick reply, I looked into the example.
and here is my code below.
import arcpy
array = arcpy.GetParameterAsText(0)
# A list of features and coordinate pairs
feature_info = [array]
# A list that will hold each of the Polygon objects
features = []
for feature in feature_info:
# Create a Polygon object based on the array of points
# Append to the list of Polygon objects
features.append(
arcpy.Polygon(
arcpy.Array([arcpy.Point(*coords) for coords in feature])))
# Persist a copy of the Polyline objects using CopyFeatures
arcpy.CopyFeatures_management(features, "c:/geometry/polygons.shp")
As my array is a parameter that expects the value at runtime, what would be the datatype to assing in the tool.
You could try a few things to see if they work for you. The Cell Size parameter type with multivalue might work, but it is quite finicky.
You could also try to use tool validation and set up a value table parameter like this:
paste this code into the Validation and see if this works for you.
import arcpy class ToolValidator(object): """Class for validating a tool's parameter values and controlling the behavior of the tool's dialog.""" def __init__(self): """Setup arcpy and the list of tool parameters.""" self.params = arcpy.GetParameterInfo() def initializeParameters(self): """Refine the properties of a tool's parameters. This method is called when the tool is opened.""" param = self.params[0] param.datatype = "Value Table" param.columns = [["Double","X"],["Double","Y"]] return def updateParameters(self): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.""" return def updateMessages(self): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return
The data type you use depends on what format you reasonably expect and require from your user. If you can rely on your user to type in or (copy and paste) a full polygon array, like:
[[[6,5],[7,8],[8,7],[6,5]]]
...then you could simply use a string. Similarly, you could input a table of coordinates, or type and parse an input table like Luke mentions.
If you go with the above array option, the following slightly modified script should work:
import arcpy, ast array = arcpy.GetParameterAsText(0) # a simple string # A list of features and coordinate pairs feature_info = ast.literal_eval(array) # A list that will hold each of the Polygon objects features = [] for feature in feature_info: # Create a Polygon object based on the array of points # Append to the list of Polygon objects features.append( arcpy.Polygon( arcpy.Array([arcpy.Point(*coords) for coords in feature]))) # Persist a copy of the Polyline objects using CopyFeatures arcpy.CopyFeatures_management(features, r"C:\junk\poly.shp")
I really don't see a point in developing a tool to do that over simply running a script. People are not going to want to copy a list of lists of lists into a dialog to make a polygon. If your intent is to make an extent polygon (ie a rectangle) that is easily done using an extent object and constructing the polygon from that. There are far easier ways to construct a polygon or polygons that through a dialog interface.
Hi Dan & Darren,
Could able to resolve with little R&D.
Thanks for the support.