Create Polygons from Lat and Long coordinates through Python

1407
5
Jump to solution
04-08-2014 01:28 PM
JamesFitzgerald
Occasional Contributor II
Hello,
I have a problem. I want to create polygons by using the X and Y values from a CSV file. I have borrowed a script and edited to work with my data. The only problem is the script runs for only one set of values. Not both. I have a location and from the location it draws a polygon that is 20000 square feet and it does not recognise the second set of coordinates.

I believe I am missing a split line? Or not.


import arcpy, csv, os
from arcpy import env

env.overwriteOutput = True
infile = "U:\Tax\Special Projects\JEFitzgerald\CUVA\Test\XY_EVENT_.csv"



point = arcpy.Point()
array = arcpy.Array()
featureList= []

curXY = arcpy.da.SearchCursor(infile, ["X","Y"])
   

for row in curXY:
    X = float (row[1])
    Y = float (row[0])
   
    highX = X+ 148.7
    highY = Y + 134.5
    array = arcpy.Array([arcpy.Point(X, Y), arcpy.Point(highX, Y),
    arcpy.Point(highX, highY), arcpy.Point(X, highY), arcpy.Point(X, Y)])

polygon = arcpy.Polygon(array)
featureList.append(polygon)

arcpy.CopyFeatures_management(featureList, "U:\Tax\Special Projects\JEFitzgerald\CUVA\Test\Test_CUVA.gdb\Poly33")


   

print arcpy.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
WilliamCraft
MVP Regular Contributor
From looking at your code, I've made my suggested changes below in bold:

import arcpy, csv, os from arcpy import env  env.overwriteOutput = True infile = "U:\Tax\Special Projects\JEFitzgerald\CUVA\Test\XY_EVENT_.csv"  point = arcpy.Point() array = arcpy.Array() featureList= []  curXY = arcpy.da.SearchCursor(infile, ["X","Y"])  for row in curXY:     X = float (row[1])     Y = float (row[0])          highX = X+ 148.7     highY = Y + 134.5     array = arcpy.Array([arcpy.Point(X, Y), arcpy.Point(highX, Y),     arcpy.Point(highX, highY), arcpy.Point(X, highY), arcpy.Point(X, Y)])      polygon = arcpy.Polygon(array)     featureList.append(polygon)  arcpy.CopyFeatures_management(featureList, "U:\Tax\Special Projects\JEFitzgerald\CUVA\Test\Test_CUVA.gdb\Poly33")  print arcpy.GetMessages()


I think if you indent the two bolded lines above,then each time you construct your array to generate a polygon you will append it to the feature list.  After iterating through all of the curXY rows in your CSV, you can execute one CopyFeatures operation (as you're already doing) to push everything from the feature list to the file geodatabase.

View solution in original post

0 Kudos
5 Replies
WilliamCraft
MVP Regular Contributor
Can you re-post your code using the CODE wrap button (once you highlight all the code, press the button with the pound sign) so that we can see the indentation you're using?  Thanks!
0 Kudos
JamesFitzgerald
Occasional Contributor II
import arcpy, csv, os
from arcpy import env

env.overwriteOutput = True
infile = "U:\Tax\Special Projects\JEFitzgerald\CUVA\Test\XY_EVENT_.csv"



point = arcpy.Point()
array = arcpy.Array()
featureList= []

curXY = arcpy.da.SearchCursor(infile, ["X","Y"])





for row in curXY:
    X = float (row[1])
    Y = float (row[0])
    
    highX = X+ 148.7
    highY = Y + 134.5
    array = arcpy.Array([arcpy.Point(X, Y), arcpy.Point(highX, Y),
    arcpy.Point(highX, highY), arcpy.Point(X, highY), arcpy.Point(X, Y)])

polygon = arcpy.Polygon(array)
featureList.append(polygon)

arcpy.CopyFeatures_management(featureList, "U:\Tax\Special Projects\JEFitzgerald\CUVA\Test\Test_CUVA.gdb\Poly33")


    

print arcpy.GetMessages()
0 Kudos
WilliamCraft
MVP Regular Contributor
From looking at your code, I've made my suggested changes below in bold:

import arcpy, csv, os from arcpy import env  env.overwriteOutput = True infile = "U:\Tax\Special Projects\JEFitzgerald\CUVA\Test\XY_EVENT_.csv"  point = arcpy.Point() array = arcpy.Array() featureList= []  curXY = arcpy.da.SearchCursor(infile, ["X","Y"])  for row in curXY:     X = float (row[1])     Y = float (row[0])          highX = X+ 148.7     highY = Y + 134.5     array = arcpy.Array([arcpy.Point(X, Y), arcpy.Point(highX, Y),     arcpy.Point(highX, highY), arcpy.Point(X, highY), arcpy.Point(X, Y)])      polygon = arcpy.Polygon(array)     featureList.append(polygon)  arcpy.CopyFeatures_management(featureList, "U:\Tax\Special Projects\JEFitzgerald\CUVA\Test\Test_CUVA.gdb\Poly33")  print arcpy.GetMessages()


I think if you indent the two bolded lines above,then each time you construct your array to generate a polygon you will append it to the feature list.  After iterating through all of the curXY rows in your CSV, you can execute one CopyFeatures operation (as you're already doing) to push everything from the feature list to the file geodatabase.
0 Kudos
JamesFitzgerald
Occasional Contributor II
Thank you very much! Your suggestion worked! It also will save me a great deal of time!

Thanks again,
J
0 Kudos
WilliamCraft
MVP Regular Contributor
Great, glad to hear it!  Please mark the correct answer with the green check.
0 Kudos