<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Struggling to write a script that will convert coordinates in a .txt file into polygons (Using Python) in ArcMap Questions</title>
    <link>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030789#M2048</link>
    <description>&lt;P&gt;Thanks so much for the reference. I went ahead and made some adjustments to my code and actually have it almost complete.&lt;BR /&gt;&lt;BR /&gt;My final issue is that I'm running into this error based off the updated code&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 67, in &amp;lt;module&amp;gt;&lt;BR /&gt;NameError: name 'coords' is not defined&lt;/P&gt;</description>
    <pubDate>Thu, 25 Feb 2021 22:37:21 GMT</pubDate>
    <dc:creator>JustinRains</dc:creator>
    <dc:date>2021-02-25T22:37:21Z</dc:date>
    <item>
      <title>Struggling to write a script that will convert coordinates in a .txt file into polygons (Using Python)</title>
      <link>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030678#M2044</link>
      <description>&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Hello Everyone,&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;I'm a student in my first year of learning Python and am 100% stuck at the moment. I by accident created a scrip that converts shapefile coordinates into a .txt file; but I need to do the opposite..&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Using&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://desktop.arcgis.com/en/arcmap/latest/analyze/python/writing-geometries.htm" target="_blank" rel="noopener nofollow ugc"&gt;https://desktop.arcgis.com/en/arcmap/latest/analyze/python/writing-geometries.htm&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;as a guide&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;I am attempting to take a .txt document (displayed below) and turn the coordinates into Polgyons. 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,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;I don't want someone to just give me the answers&lt;/STRONG&gt;, I really need to learn this.&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;&lt;STRONG&gt;My current code and thoughts on what I need to do are below (EDIT: The error I'm now receiving&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 67, in &amp;lt;module&amp;gt;&lt;BR /&gt;NameError: name 'coords' is not defined&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;# Import arcpy&lt;BR /&gt;import arcpy&lt;/P&gt;&lt;P&gt;# Specify the location of the file geodatabase containing the imported table using the environment class in arcpy&lt;BR /&gt;from arcpy import env&lt;BR /&gt;arcpy.env.workspace = "C:\Users\devot\Desktop\Justin_Rains_Module_7"&lt;BR /&gt;outworkspace = "C:\Users\devot\Desktop\Justin_Rains_Module_7\Module 7_Justin_Rains.gdb"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;# Turn on overwriting of outputs using the environment class in arcpy&lt;BR /&gt;arcpy.env.overwriteOutput = True&lt;/P&gt;&lt;P&gt;# List feature classes in the specified workspace&lt;BR /&gt;fclist = arcpy.ListFeatureClasses()&lt;/P&gt;&lt;P&gt;# Define object that extracts the spatial reference information from the shapefile provided in the Module 7 data download&lt;BR /&gt;dataset = "C:\Users\devot\Desktop\Justin_Rains_Module_7\Property.shp"&lt;BR /&gt;spatial_ref = arcpy.Describe(dataset).spatialReference&lt;BR /&gt;print("Obtained spatial reference.")&lt;/P&gt;&lt;P&gt;#Create a polygon feature class in which the property parcel polygons will be later written (e.g. properties)&lt;BR /&gt;arcpy.CreateFeatureclass_management(outworkspace, "Properties", "POLYGON", "", "DISABLED", "DISABLED", spatial_ref)&lt;BR /&gt;print("Created new feature class to store property parcel polygons.")&lt;/P&gt;&lt;P&gt;# Define object for newly created polygon feature class&lt;BR /&gt;inTable = "C:\Users\devot\Desktop\Justin_Rains_Module_7\Module 7_Justin_Rains.gdb\module7table"&lt;/P&gt;&lt;P&gt;# Add field to table in which the property name will be later written&lt;BR /&gt;fieldName = "Name"&lt;BR /&gt;fieldLength = 25&lt;/P&gt;&lt;P&gt;arcpy.AddField_management(inTable, fieldName, "TEXT", fieldLength)&lt;BR /&gt;print("Added Name field to Properties FC.")&lt;/P&gt;&lt;P&gt;# Open, as read-only, the text file called 'Property_Module_7_Modified_No_Headers.txt'&lt;BR /&gt;infile = open("C:\Users\devot\Desktop\Justin_Rains_Module_7\Property_Module_7_Modified_No_Header (2).txt", mode="r")&lt;/P&gt;&lt;P&gt;# Define object that adds coordinate data from text file into a nested list. (hint: incorporate line.split() method.&lt;BR /&gt;array = arcpy.Array()&lt;BR /&gt;point = arcpy.Point()&lt;BR /&gt;for line in infile:&lt;BR /&gt;Segment = line.split(", ")&lt;BR /&gt;array.add(point)&lt;/P&gt;&lt;P&gt;# Define object that employs an InsertCursor from arcpy that can modify the newly created polygon feature class.&lt;BR /&gt;cursor = arcpy.InsertCursor(inTable, [fieldName, "SHAPE@"])&lt;BR /&gt;print("Insert cursor set.")&lt;/P&gt;&lt;P&gt;# Define object that assigns the arcpy.Array() method so that all the coordinates of each polygon have a place to be&lt;BR /&gt;# stored, before the polygon is created in the feature class&lt;BR /&gt;polygon = arcpy.Polygon(array)&lt;BR /&gt;print("Polygon array set.")&lt;/P&gt;&lt;P&gt;# Initialize a variable for keeping track of a feature's ID&lt;BR /&gt;coords_list = [line.split() for line in infile]&lt;BR /&gt;print(coords_list)&lt;/P&gt;&lt;P&gt;ID = -1 # Define 'ID' object equal to -1&lt;/P&gt;&lt;P&gt;for coords in coords_list: # FOR loop applied to coords in nested list of coordinates&lt;BR /&gt;if ID == -1:&lt;BR /&gt;ID = coords_list[0] # associate ID object to track the ID value in the nested list of coordinates&lt;BR /&gt;# Add the point to the feature's array of points&lt;BR /&gt;# If the ID has changed, create a new feature&lt;BR /&gt;if ID != coords[0]:&lt;BR /&gt;cursor.insertRow([arcpy.Polygon(array)])&lt;BR /&gt;array.removeAll()&lt;BR /&gt;array.add(arcpy.Point(coords[2], coords[3], ID=coords[0]))&lt;BR /&gt;ID = coords[0]&lt;BR /&gt;# Add the last feature&lt;BR /&gt;polygon = arcpy.Polygon(array, spatial_ref)# Define object that employs an InsertCursor from arcpy that can modify the newly created polygon feature class.&lt;/P&gt;&lt;P&gt;cursor = arcpy.InsertCursor(inTable, [fieldName, "SHAPE@"])&lt;/P&gt;&lt;P&gt;cursor.insertRow(arcpy.Polygon(array))&lt;/P&gt;&lt;P&gt;infile.close()&lt;/P&gt;&lt;P&gt;print("Input file closed.")&lt;/P&gt;&lt;P&gt;del cursor&lt;/P&gt;&lt;P&gt;print("Task complete.")&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;&lt;BR /&gt;&lt;BR /&gt;My .txt document read likes this&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;1 Jones_Tract 424564.6207 4396443.553&lt;BR /&gt;1 Jones_Tract 425988.3089 4395630.017&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 22:35:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030678#M2044</guid>
      <dc:creator>JustinRains</dc:creator>
      <dc:date>2021-02-25T22:35:39Z</dc:date>
    </item>
    <item>
      <title>Re: Struggling to write a script that will convert coordinates in a .txt file into polygons (Using Python)</title>
      <link>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030740#M2045</link>
      <description>&lt;P&gt;I don't know why you've set that workspace path, a textfile isn't a workspace (Folder, FGDB etc.), and the path itself is funky. I find raw string formatting handy:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;arcpy.env.wokspace = r'C:\MyFiles\MyFGDB.gdb'&lt;/LI-CODE&gt;&lt;P&gt;, although I guess it is personal preference, but note why are you setting this as an arcpy.env.workspace?&lt;/P&gt;&lt;P&gt;The listfeatureclasses() - why? you have a textfile you want to turn into a FC don't you?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes - read the textfile and turn it into an array as formatted in the link you supplied.&amp;nbsp; Be aware of polygon geometries and how inner rings etc are created, final closing coordinates which repeat the starting coordinate must also be supplied to close the ring, clockwise and anticlockwise ring notations etc..)&lt;/P&gt;&lt;P&gt;I don't quite get the textfile format, is 1 Jones_Tract a single polygon, with each coordinate being supplied per line, if so, are they in the correct clockwise order, and/or are there any inner rings or complexity?&lt;/P&gt;&lt;P&gt;Does that format continue each for i.e.:&lt;/P&gt;&lt;P&gt;2 Jones_Tract .... X Y&lt;/P&gt;&lt;P&gt;2 Jones_Tract .... X Y&lt;/P&gt;&lt;P&gt;2 Jones_Tract .... X Y&lt;/P&gt;&lt;P&gt;2 Jones_Tract .... X Y&lt;/P&gt;&lt;P&gt;3 Jones_Tract .... X Y&lt;/P&gt;&lt;P&gt;3 Jones_Tract .... X Y&lt;/P&gt;&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 21:32:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030740#M2045</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-02-25T21:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: Struggling to write a script that will convert coordinates in a .txt file into polygons (Using Python)</title>
      <link>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030757#M2046</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/455308"&gt;@JustinRains&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me share some code from a couple of years ago when I created a tool to create polygons from a text file. Although the structure of the textfile I worked on is different from yours it should get you started.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#-------------------------------------------------------------------------------
# Name:        TXTpoints2Polygons.py
# Purpose:     covert TXT file with points to polygons
#
# Author:      xbakker
#
# Created:     10/04/2015
#-------------------------------------------------------------------------------

import arcpy
import os

def main():

    import sys
    import traceback

    try:
        arcpy.env.overwriteOutput = True

        # first parameter is txt file
        txt = arcpy.GetParameterAsText(0)

        # second parameter (provide default NAME)
        fld_name = arcpy.GetParameterAsText(1)

        # third parameter is output fc
        fc_out = arcpy.GetParameterAsText(2)

        # forth  parameter is spatial reference
        sr = arcpy.GetParameter(3)


        # create empty output fc
        ws_name, fc_name = os.path.split(fc_out)
        geomtype = "POLYGON"
        arcpy.CreateFeatureclass_management(ws_name, fc_name, geomtype, spatial_reference=sr)

        # add field
        arcpy.AddField_management(fc_out, fld_name, "TEXT", field_length=255)

        # start insert cursor
        flds = ("SHAPE@", fld_name)
        with arcpy.da.InsertCursor(fc_out, flds) as curs:

            # read input file
            cnt = 0
            name = ""
            first_point = None
            lst_pnt = []

            with open(txt, 'r') as f:
                for line in f.readlines():
                    cnt += 1
                    if cnt &amp;gt; 1:
                        line = line.replace('\n','')
                        lst_line = line.split('\t')
                        a = lst_line[0]
                        if a.isdigit():
                            # point
                            if bln_start:
                                first_point = GetPoint(line)
                                pnt = GetPoint(line)
                                lst_pnt.append(pnt)
                            else:
                                pnt = GetPoint(line)
                                lst_pnt.append(pnt)
                            bln_start = False
                        else:
                            # name or header
                            if a[:5].upper() == 'PUNTO':
                                pass
                            else:
                                if first_point != None:
                                    lst_pnt.append(first_point)

                                if len(lst_pnt) &amp;gt; 2:
                                    # create previous polygon and write to fc
                                    polygon = arcpy.Polygon(arcpy.Array(lst_pnt), sr)
                                    curs.insertRow((polygon, name, ))
                                lst_pnt = []
                                name = line.strip()
                                bln_start = True
                                arcpy.AddMessage("Processing  polygon: '{0}'".format(name))

                if first_point != None:
                    lst_pnt.append(first_point)
                    polygon = arcpy.Polygon(arcpy.Array(lst_pnt), sr)
                    curs.insertRow((polygon, name, ))

    except:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "Errores de Python:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        msgs = "Errores de ArcPy:\n" + arcpy.GetMessages(2) + "\n"
        arcpy.AddError(pymsg)
        arcpy.AddError(msgs)


def GetPoint(line):
    line = line.replace(',','.')
    lst_line = line.split('\t')
    x = float(lst_line[1])
    y = float(lst_line[2])
    return arcpy.Point(x, y)

if __name__ == '__main__':
    main()&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 25 Feb 2021 21:36:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030757#M2046</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-02-25T21:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: Struggling to write a script that will convert coordinates in a .txt file into polygons (Using Python)</title>
      <link>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030788#M2047</link>
      <description>&lt;P&gt;I believe I addressed some of your concerns in this updated code; my code is more or less done at this point, but I am running into this error which I for some reason can't resolve.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 22:36:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030788#M2047</guid>
      <dc:creator>JustinRains</dc:creator>
      <dc:date>2021-02-25T22:36:32Z</dc:date>
    </item>
    <item>
      <title>Re: Struggling to write a script that will convert coordinates in a .txt file into polygons (Using Python)</title>
      <link>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030789#M2048</link>
      <description>&lt;P&gt;Thanks so much for the reference. I went ahead and made some adjustments to my code and actually have it almost complete.&lt;BR /&gt;&lt;BR /&gt;My final issue is that I'm running into this error based off the updated code&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 67, in &amp;lt;module&amp;gt;&lt;BR /&gt;NameError: name 'coords' is not defined&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 22:37:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030789#M2048</guid>
      <dc:creator>JustinRains</dc:creator>
      <dc:date>2021-02-25T22:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Struggling to write a script that will convert coordinates in a .txt file into polygons (Using Python)</title>
      <link>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030795#M2049</link>
      <description>&lt;P&gt;You've not formatted the code so I can't see the indentation, although I'd expect that coords is referenced outside the for loop - for coords in... :&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 22:47:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030795#M2049</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-02-25T22:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: Struggling to write a script that will convert coordinates in a .txt file into polygons (Using Python)</title>
      <link>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030843#M2050</link>
      <description>&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - GeoNet, The Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;so people can assess your indentation etc&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2021 00:20:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/struggling-to-write-a-script-that-will-convert/m-p/1030843#M2050</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-26T00:20:09Z</dc:date>
    </item>
  </channel>
</rss>

