TransWikia.com

Writing script to convert coordinates in .txt file into polygons using ArcPy

Geographic Information Systems Asked by BrandDev on March 26, 2021

I’m a student in my first year of learning Python and am 100% stuck at the moment. I by accident created a script that converts shapefile coordinates into a .txt file; but I need to do the opposite.

Using https://desktop.arcgis.com/en/arcmap/latest/analyze/python/writing-geometries.htm as a guide

I am attempting to take a .txt document (displayed below) and turn the coordinates into Polygons. This is an assignment, and I’m currently in my first year of coding; so I’m still trying to progressively learn. This course doesn’t really provide any guidance, so everything I know I’ve learned from Esri; I just really need general guidance, I don’t want someone to just give me the answers, I really need to learn this.

My current code and thoughts on what I need to do are below (New error being received)

Traceback (most recent call last):
File "", line 67, in
NameError: name ‘coords’ is not defined

# Import arcpy
import arcpy

# Specify the location of the file geodatabase containing the imported table using the environment class in arcpy
from arcpy import env
arcpy.env.workspace = "C:UsersdevotDesktopJustin_Rains_Module_7"
outworkspace = "C:UsersdevotDesktopJustin_Rains_Module_7Module 7_Justin_Rains.gdb"




# Turn on overwriting of outputs using the environment class in arcpy
arcpy.env.overwriteOutput = True

# List feature classes in the specified workspace
fclist = arcpy.ListFeatureClasses()

# Define object that extracts the spatial reference information from the shapefile provided in the Module 7 data download
dataset = "C:UsersdevotDesktopJustin_Rains_Module_7Property.shp"
spatial_ref = arcpy.Describe(dataset).spatialReference
print("Obtained spatial reference.")

#Create a polygon feature class in which the property parcel polygons will be later written (e.g. properties)
arcpy.CreateFeatureclass_management(outworkspace, "Properties", "POLYGON", "", "DISABLED", "DISABLED", spatial_ref)
print("Created new feature class to store property parcel polygons.")

# Define object for newly created polygon feature class
inTable = "C:UsersdevotDesktopJustin_Rains_Module_7Module 7_Justin_Rains.gdbmodule7table"

# Add field to table in which the property name will be later written
fieldName = "Name"
fieldLength = 25

arcpy.AddField_management(inTable, fieldName, "TEXT", fieldLength)
print("Added Name field to Properties FC.")

# Open, as read-only, the text file called 'Property_Module_7_Modified_No_Headers.txt'
infile = open("C:UsersdevotDesktopJustin_Rains_Module_7Property_Module_7_Modified_No_Header (2).txt", mode="r")

# Define object that adds coordinate data from text file into a nested list. (hint: incorporate line.split() method.
array = arcpy.Array()
point = arcpy.Point()
for line in infile:
    Segment = line.split(", ")
array.add(point)

# Define object that employs an InsertCursor from arcpy that can modify the newly created polygon feature class.
cursor = arcpy.InsertCursor(inTable, [fieldName, "SHAPE@"])
print("Insert cursor set.")

# Define object that assigns the arcpy.Array() method so that all the coordinates of each polygon have a place to be
# stored, before the polygon is created in the feature class
polygon = arcpy.Polygon(array)
print("Polygon array set.")

# Initialize a variable for keeping track of a feature's ID
coords_list = [line.split() for line in infile]
print(coords_list)

ID = -1 # Define 'ID' object equal to -1

for coords in coords_list: # FOR loop applied to coords in nested list of coordinates
        if ID == -1:
            ID = coords_list[0] # associate ID object to track the ID value in the nested list of coordinates
# Add the point to the feature's array of points
# If the ID has changed, create a new feature
if ID != coords[0]:
    cursor.insertRow([arcpy.Polygon(array)])
array.removeAll()
array.add(arcpy.Point(coords[2], coords[3], ID=coords[0]))
ID = coords[0]
# Add the last feature
polygon = arcpy.Polygon(array, spatial_ref)# Define object that employs an InsertCursor from arcpy that can modify the newly created polygon feature class.

cursor = arcpy.InsertCursor(inTable, [fieldName, "SHAPE@"])

cursor.insertRow(arcpy.Polygon(array))

infile.close()

print("Input file closed.")

del cursor

print("Task complete.")

My .txt document read likes this

1 Jones_Tract 424564.6207 4396443.553
1 Jones_Tract 425988.3089 4395630.017

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP