# Using ArcPy to create polygon from coordinates in txt file?

Currently I am having trouble creating polygons from .txt files using Python. I am uncertain about the method(s) I should use to create the geometry of the polygons. The text file is a list of coordinates separated by commas.

Here is my script:

``````import arcpy
from arcpy import env
env.workspace = r'C:\Users\....\data'
env.overwriteOutput = 1
arcpy.CheckOutExtension("Spatial")

txt_file = env.workspace + r'\districts\district01.txt'

xs = []
ys = []

with open(txt_file) as f:
big_str = lines[0]

pairs =  big_str.split('/r')[1:]

for pair in pairs:
pair_list = pair.split(',')
xs.append(pair_list[0])
ys.append (pair_list[1])

point = arcpy.Point()
array = arcpy.Array()

for integer in range(0, len(xs)):
print 'x', xs[integer], 'y', ys[integer]
point.X = float(xs[integer])
point.Y = float(ys[integer])
array.append(point)
``````
• I believe you would need to pass it into the the arcpy.polygon class – ziggy Sep 21 '16 at 21:54
• I have never created a Polygon from scratch but here is the documentation pro.arcgis.com/en/pro-app/arcpy/classes/polygon.htm – ziggy Sep 21 '16 at 21:55
• What happens when you run the code snippet that you have presented? What do a few rows from your text file look like? Perhaps illustrate using a triangle and a square to minimize the number of coordinates that you need to post. – PolyGeo Sep 21 '16 at 21:59
• The code creates a shapefile, but no geometry. The txt file I have is literally just a list of about 200 numbers (XY coords). – MoreMeowbell Sep 22 '16 at 20:14
• So is it an x field with one value being the longitude?and a y field with one value being the latitude? If so thats just a point and not a polygon... – ziggy Sep 22 '16 at 20:26

a few things

1. I am not exactly sure what your txt file of polygon coordinates looks like so you may have to mess around with some of my code I am about to give you

2. I find it useful when looping through csvs or txt files to use the old arcgisscripting search cursor so I know which fields I am trying to extract from(I am aware of the arcpy.da.searchcursor)

3. you need to pass the arcpy.Point() and arcpy.Array() into the arcpy.Polygon() class to create your geometry

4. Pair.x and Pair.y would be the fields holding your polygon coordinates so switch that around to whatever you have them called

``````txt_file = "yourfile.txt"
cur = gp.SearchCursor(txt_file)
coords = [[[pair.x,pair.y]]for pair in cur]
for x in coords:
for z in x:
m = arcpy.Polygon(arcpy.Array([arcpy.Point(*z)]))
print m
arcpy.CopyFeatures_management(m, "C://Users//polygons.shp")
``````
• Can you please give an example of how you'd expect the txt file to look for your script to run? That would make it easier when trying to "mess around" with the code on a different txt file – Midavalo Sep 21 '16 at 23:01
• @ziggy You should look into the da module. It's a vast improvement over the old cursors. – jbalk Sep 21 '16 at 23:38
• @Midavalo My plan right now is to splice up the numbers in the text file, since it is a long list of about 200 numbers (XY coords). – MoreMeowbell Sep 22 '16 at 20:15
• What does your text file look like? Post a snapshot of the columns u are using @j.pramuk – ziggy Sep 22 '16 at 20:24

This method demonstrates how to create a polygon from an array. You will need to build your coordinates into a list to loop through and create all your polygons.

You need 4 coordinates to create a polygon (LL,LR,UR,UL).

Use the 4 coordinates to create an array and make a polygon from the array. Note that you need to include the first coordinate again at the end of the array to 'close' the polygon:

``````            array = arcpy.Array()
``````            cursor = arcpy.da.InsertCursor(fc,["SHAPE@"])