Spacially Referencing a series of polygons

3581
2
Jump to solution
03-06-2015 08:45 AM
SheenaBerry
New Contributor II

Hello, I am working on a script that produces a polygon grid from a series of irregularly spaced points (hence why I can't use fishnet).

I have managed to produce the desired grid but am unable to assign a spactial reference to the grid.

I have tried a number of ways such as assigning a Spactial Reference. I've tried to assign it to the individual points using a for loop:

PointsList=[]       

for n in coordList:

    array=[]

    for coords in n:

        point=arcpy.Point(coords[0],coords[1])

        ptGeometry=arcpy.PointGeometry(point,arcpy.SpatialReference(factoryCode))

        array.append(ptGeometry)

    PointsList.append(array)

features = []

for feature in PointsList:
    #Create a Polygon object based on the array of points
    #Append to the list of Polygon objects
    features.append(
        arcpy.Polygon(
            arcpy.Array(*corner)) for corner in feature)

arcpy.CopyFeatures_management(features, outputName)

Or to the polygon durring its construction:

features = []

for feature in coordList:

    # 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]), arcpy.SpatialReference(factoryCode)))

arcpy.CopyFeatures_management(features, outputName)

But both ways are giving me a runtime error.

I was wondering if it would be best to first create a feature class and then creat my polygons but I don't know how to write polygons to feature classes in python.

If someone could provide guidance it would be much apprechiated.

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Below an extract from another post:

import arcpy

arcpy.env.overwriteOutput = True

sr = arcpy.SpatialReference(4326) # WGS 1984

# coords

lon_min = 175

lon_max = 185

lat_min = 0

lat_max = 10

polygon = arcpy.Polygon(arcpy.Array([arcpy.Point(lon_min, lat_min),

                                    arcpy.Point(lon_min, lat_max),

                                    arcpy.Point(lon_max, lat_max),

                                    arcpy.Point(lon_max, lat_min),

                                    arcpy.Point(lon_min, lat_min)]),sr)

fc_out = r"D:\Xander\GeoNet\AroundTheWorld\polygon01.shp"

arcpy.CopyFeatures_management([polygon], fc_out)

Notice that the Polygon is created from an Array of a list of Point objects.

In your post I see you are using an "array"  which is in your case a list and you use PointGeometry objects.

View solution in original post

0 Kudos
2 Replies
XanderBakker
Esri Esteemed Contributor

Below an extract from another post:

import arcpy

arcpy.env.overwriteOutput = True

sr = arcpy.SpatialReference(4326) # WGS 1984

# coords

lon_min = 175

lon_max = 185

lat_min = 0

lat_max = 10

polygon = arcpy.Polygon(arcpy.Array([arcpy.Point(lon_min, lat_min),

                                    arcpy.Point(lon_min, lat_max),

                                    arcpy.Point(lon_max, lat_max),

                                    arcpy.Point(lon_max, lat_min),

                                    arcpy.Point(lon_min, lat_min)]),sr)

fc_out = r"D:\Xander\GeoNet\AroundTheWorld\polygon01.shp"

arcpy.CopyFeatures_management([polygon], fc_out)

Notice that the Polygon is created from an Array of a list of Point objects.

In your post I see you are using an "array"  which is in your case a list and you use PointGeometry objects.

View solution in original post

0 Kudos
SheenaBerry
New Contributor II

Thanks Xander Bakker,

I guess my main issue was in the line:

arcpy.Array([arcpy.Point(*coords) for coords in feature]),arcpy.SpatialReference(factoryCode)))

where factoryCode was an input Coordinate system from the user of this script. I was hoping to have the code so I can create the polygons with a different spatial without having to open the scrip everytime. Here is a better example of what I am triing to do.

import arcpy

arcpy.env.overwriteOutput = True

inputWorkspace = arcpy.GetParameterAsText(0)

outputName     = arcpy.GetParameterAsText(1)

factoryCode    = arcpy.GetParameterAsText(2) #input Coordinate System

coordList = [[[1, 2], [2, 4], [3, 7]],

                [[6, 8], [5, 7], [7, 2], [9, 5]]]

features = []

sr = arcpy.SpatialReference(factoryCode)

for feature in coordList:

features.append(

     arcpy.Polygon(

          arcpy.Array([arcpy.Point(*coords) for coords in feature]), sr))

arcpy.CopyFeatures_management(features, outputName)

0 Kudos