Array to polygon-Python Script

5443
6
01-06-2016 03:52 AM
Srinivasa_MurthyNemani
New Contributor II

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.

0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

There is a code example at the end of the arcpy.Polygon class help topic Polygon—ArcPy Classes | ArcGIS for Desktop

Srinivasa_MurthyNemani
New Contributor II

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.

0 Kudos
LukeSturtevant
Occasional Contributor III

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
DarrenWiens2
MVP Honored Contributor

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")
DanPatterson_Retired
MVP Emeritus

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.

0 Kudos
Srinivasa_MurthyNemani
New Contributor II

Hi Dan & Darren,

Could able to resolve with little R&D.

Thanks for the support.

0 Kudos