-1

This is carrying forward from the previously asked Creating Polyline shapefile from text file using ArcPy?

With further insight from a colleague, I have the following code that I feel is a good start to creating a polyline file from a text file. However; I need to alter it, so it will break up the text file by line and populate the shapefile with the attributes.

import arcpy

arcpy.env.workspace = "C:\\Data"

outFolder = "C:\\_Data"
fc = "Paths.shp"

spatRef = arcpy.SpatialReference(26913)
arcpy.CreateFeatureclass_management(outFolder, fc, "POLYLINE", "", "", "", spatRef)

pointList = arcpy.Array()

with open("C:\\Paths.txt",'r') as coordinateList:
    with arcpy.da.InsertCursor(fc, ["SHAPE@"]) as cursor:
        for line in coordinateList:
            SplitLine = line.split(",") 
            x = float(SplitLine[1])
            y = float(SplitLine[2])

            point = arcpy.Point(x,y)
            pointList.add(point)
    polyline = arcpy.Polyline(pointList)
    cursor.insertRow([polyline])

del cursor
coordinateList.close()

A sample of the text file is below (Name, coordx, coordy).

Ranch1_1, 529018.125025, 4108038.05548
Ranch1_1, 529005.718792, 4108028.20659
Ranch1_1, 528993.340503, 4108018.73931
Ranch1_1, 528980.990158, 4108009.65364
Ranch1_1, 528968.667757, 4108000.94958
Ranch1_1, 528956.373301, 4107992.62713
Ranch1_1, 528944.106788, 4107984.68629
Ranch1_1, 528931.86822, 4107977.12705
Ranch1_1, 528919.657595, 4107969.94943
Ranch1_1, 528907.474915, 4107963.15341
Ranch1_2, 529018.125025, 4108038.05548
Ranch1_2, 528994.381279, 4108036.02086
Ranch1_2, 528971.341496, 4108034.79222
Ranch1_2, 528949.005677, 4108034.36954
Ranch1_2, 528938.101754, 4108034.46044
Ranch1_2, 528927.373822, 4108034.75283
Ranch1_2, 528916.82188, 4108035.24671
Ranch1_2, 528906.44593, 4108035.94208
Ranch1_2, 528896.24597, 4108036.83895
Ranch1_2, 528886.222001, 4108037.93731
4
  • 2
    Possible duplicate of Creating Polyline shapefile from text file using ArcPy? – ahmadhanb Oct 19 '18 at 11:54
  • This not a duplicate. It's taking what I acquired from that question and moving forward with it. – user129835 Oct 19 '18 at 12:17
  • Yes, I get a single polyline with no attribution populated into the shapefile. – user129835 Oct 19 '18 at 13:23
  • looks likes its a csv....... conmvert to .csv much easier but if you insist with .txt read other answers – NULL.Dude Oct 19 '18 at 16:06
1

Try code below. I use pandas module to read the textfile and collections.defaultdict to store the rows as dictionaries like 'Ranch1_1': [<Point (529018.125025, 4108038.05548, #, #)>, <Point (529005.718792, 4108028.20659, #, #)>, ...}. With pandas you dont have to convert data types, it will understand that the coordinates are numbers and not text.

import arcpy
from collections import defaultdict
import pandas as pd

outFolder=r'C:\Test'
arcpy.env.workspace = outFolder

#Read textfile and store in dictionary
df = pd.read_csv(r"C:\Test\ranchlines.txt",header=None,names=['Ranch','X','Y']) #I have no header in my textfile only values so I specify column names here
d = defaultdict(list)
for index, row in df.iterrows():
    d[row['Ranch']].append(arcpy.Point(row['X'],row['Y']))

#Create feature class
fc = 'Ranchlines.shp'
spatRef = arcpy.SpatialReference(26913)
arcpy.CreateFeatureclass_management(out_path=outFolder, out_name=fc, 
                                   geometry_type='POLYLINE', 
                                   spatial_reference=spatRef)
arcpy.AddField_management(in_table=fc, field_name='RanchName', field_type='TEXT')

#Insert polylines and attribute
icur = arcpy.da.InsertCursor(fc,['SHAPE@','RanchName'])
for ranch, points in d.iteritems(): #d.items() in python 3
    icur.insertRow([arcpy.Polyline(arcpy.Array(points)),ranch])
del icur

enter image description here

8
  • Thank you! Where would I adjust this, so the coordX and coordY atrributes get input into the shapefile attribute table? – user129835 Oct 19 '18 at 13:28
  • As a long string of coordinates? Why would you want that, it will be almost unreadable? – BERA Oct 19 '18 at 13:58
  • No. Attribute1: RanchName (Name of the feature). Attribute2: CoordX (the x coordinate value). Attribute3: CoordY ( the y coordintae value). This would make it very readable and useful. – user129835 Oct 19 '18 at 14:04
  • Each row is many coordinate pairs so that is not possible. For example the purple line is 10 x and y pairs – BERA Oct 19 '18 at 14:07
  • You cant create a line with one x y pair – BERA Oct 19 '18 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy