Select to view content in your preferred language

Create Polygon.shp from Point.shp using Python Code

778
2
Jump to solution
04-10-2014 03:48 AM
JamesFitzgerald
Frequent Contributor
Hello, I have a question about the code below. Do I create default point and polygon shapefiles or do I neet to add attributes to the point and polygon shapefiles. I have been trying to work on this code and have not been able to get it to work. Not sure about shortList and pointList. This code has been copied from another post.

import arcpy pointFC =  r"C:\Users\whitley-wayne\Desktop\summary\data.gdb\funds" polygonFC = r"C:\Users\whitley-wayne\Desktop\summary\data.gdb\hab60" pointList = []  with arcpy.da.SearchCursor(pointFC, "SHAPE@XY") as cursor:     for row in cursor:         shortList = list(row[0])         pointList.append(shortList)  with arcpy.da.InsertCursor(polygonFC, ["SHAPE@"]) as c:     for pnt in pointList:         #   start with the lower left         lowX = pnt[0]         lowY = pnt[1]         #   find the upper right         highX = lowX + 20         highY = lowY + 20         array = arcpy.Array([arcpy.Point(lowX, lowY),                              arcpy.Point(lowX, highY),                              arcpy.Point(highX, highY),                              arcpy.Point(highX, lowY),                              arcpy.Point(lowX, lowY)])         polygon = arcpy.Polygon(array)         c.insertRow([polygon])
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesFitzgerald
Frequent Contributor
Here is the correct way...http://resources.arcgis.com/en/help/main/10.1/index.html#/Reading_geometries/002z0000001t000000/

import arcpy  fc= r"U:\\Tax\\Special Projects\\JEFitzgerald\\CUVA\\Test\\Points.shp" polygonFC= r"U:\\Tax\\Special Projects\\JEFitzgerald\\CUVA\Test\\Polygon.shp" pointList=[]  ####CODE VERSION#### #for row in arcpy.da.SearchCursor (fc, [ "OID@", "SHAPE@XY"]):     #print ("feature {0}:".format(row[0]))     #print row[1]    #****  CODE    ****# with arcpy.da.SearchCursor (fc, ["SHAPE@XY"]) as cursor:     for row in cursor:         xy = row[0]         pointList.append(xy)        #print ("{0}, {1}".format(x,y))          with arcpy.da.InsertCursor (polygonFC, ["SHAPE@"]) as U:     for pnt in pointList:         lowX = pnt[0]         lowY = pnt[1]         #   find the upper right         highX = lowX + 163.1935         highY = lowY + 183.831         array = arcpy.Array([arcpy.Point(lowX, lowY),                              arcpy.Point(lowX, highY),                              arcpy.Point(highX, highY),                              arcpy.Point(highX, lowY),                              arcpy.Point(lowX, lowY)])         polygon = arcpy.Polygon(array)         U.insertRow([polygon])

View solution in original post

0 Kudos
2 Replies
JamesFitzgerald
Frequent Contributor
Here is the correct way...http://resources.arcgis.com/en/help/main/10.1/index.html#/Reading_geometries/002z0000001t000000/

import arcpy  fc= r"U:\\Tax\\Special Projects\\JEFitzgerald\\CUVA\\Test\\Points.shp" polygonFC= r"U:\\Tax\\Special Projects\\JEFitzgerald\\CUVA\Test\\Polygon.shp" pointList=[]  ####CODE VERSION#### #for row in arcpy.da.SearchCursor (fc, [ "OID@", "SHAPE@XY"]):     #print ("feature {0}:".format(row[0]))     #print row[1]    #****  CODE    ****# with arcpy.da.SearchCursor (fc, ["SHAPE@XY"]) as cursor:     for row in cursor:         xy = row[0]         pointList.append(xy)        #print ("{0}, {1}".format(x,y))          with arcpy.da.InsertCursor (polygonFC, ["SHAPE@"]) as U:     for pnt in pointList:         lowX = pnt[0]         lowY = pnt[1]         #   find the upper right         highX = lowX + 163.1935         highY = lowY + 183.831         array = arcpy.Array([arcpy.Point(lowX, lowY),                              arcpy.Point(lowX, highY),                              arcpy.Point(highX, highY),                              arcpy.Point(highX, lowY),                              arcpy.Point(lowX, lowY)])         polygon = arcpy.Polygon(array)         U.insertRow([polygon])
0 Kudos
T__WayneWhitley
Honored Contributor
If interested, Xander takes this type thing a step further in adding the option of rotating your geometries!
http://forums.arcgis.com/threads/96637-Create-diamond-around-point#10

Cleverly written and is a further lesson in manipulating geometry via Python.