AnsweredAssumed Answered

Python create  shapefile line by txt file

Question asked by DEVAPPGIS on Aug 24, 2017
Latest reply on Aug 28, 2017 by DEVAPPGIS

Hi at all,

I have a txt file that has the coordinate of points that create a line.

The txt file has more line with all coordinate points. This is an example of txt

Spatial Reference: 102100
Name: Street 1
488597.653655, 4134910.76248
488813.848952, 4134609.01192
488904.303214, 4134480.54842
488938.462756, 4134422.3471
Name: Road 2
496198.193041, 4134565.19994
496312.413827, 4134568.14182
496433.652036, 4134568.08923
496559.933558, 4134547.91561
496782.196397, 4134527.70636
496923.636101, 4134512.56252

i have written a script python to read txt file line by line and i have created array with a coordinate for each line, this is the result.

[[1, 495793.75484, 4136019.7269, u'N_1_3', u'7_11_2014'], 
[1, 495807.176907, 4135867.38434, u'N_1_3', u'7_11_2014'],
[1, 495798.121445, 4135755.3755, u'N_1_3', u'7_11_2014'],
[1, 495748.534282, 4135576.17606, u'N_1_3', u'7_11_2014'],
[2, 488489.093623, 4134784.63412, u'W_3_1', u'7_11_2014'],
[2, 488300.441633, 4134703.96516, u'W_3_1', u'7_11_2014'],
[2, 488056.638959, 4134616.0752, u'W_3_1', u'7_11_2014']]

1 or 2 represent the Id of line.

I have used this code to write the 2 geometry into shapefile

fc = "test.shp"

cur = None
try:
    #
    cur = arcpy.da.InsertCursor(fc, ["SHAPE@","Name"])

    # Create an array object needed to create features
    #
    array = arcpy.Array()

    # Initialize a variable for keeping track of a feature's ID.
    #
    ID = -1
    for coords in output:
        if ID == -1:
            ID = coords[0]
        # Add the point to the feature's array of points
        #   If the ID has changed, create a new feature
        #
        if ID != coords[0]:
            cur.insertRow([arcpy.Polyline(array))
            array.removeAll()
        array.add(arcpy.Point(coords[1], coords[2], ID=coords[0]))
        ID = coords[0]
    # Add the last feature
    #
    polyline = arcpy.Polyline(array)
    cur.insertRow([polyline,date])


except Exception as e:
   print(e)
finally:
    # Cleanup the cursor if necessary
    #
    if cur:
        del cur

the geometry are write correct but i would like also add for each geometry the name that in the array is index 3.

How i can write also this value for each geometry write?

Outcomes