Python create shapefile line by txt file

3456
7
Jump to solution
08-24-2017 01:23 PM
DEVAPP
by
New Contributor III

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?

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

It looked as if ID wasn't being properly adjusted.  I also did not see where "date" in your insertRow code (renamed to refDate/feature_date in code below) was being set to anything.  For testing, I omitted the try/except code; you will probably want that back in.  Check the field names in line 18, and make sure they are properly defined in the shapefile you are using.

import arcpy

output = [
    [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']
    ]

fc = r"C:\Path\To\test.shp"

ID = -1

# assumes names of fields are properly defined in shapefile
cur = arcpy.da.InsertCursor(fc, ["SHAPE@", "Name", "refDate"]) 

array = arcpy.Array([]) # array needs to be created after InsertCursor

for coords in output:
    if coords[0] == ID:
        array.append(arcpy.Point(coords[1], coords[2]))

    else: # next feature
        # process previous array
        if ID > 0:
            polyline = arcpy.Polyline(array)
            cur.insertRow([polyline, feature_name, feature_date])

        # begin new array
        array.removeAll()
        ID = coords[0]
        array.append(arcpy.Point(coords[1], coords[2]))
        feature_name = coords[3]
        feature_date = coords[4]

# add the last feature
polyline = arcpy.Polyline(array)
cur.insertRow([polyline, feature_name, feature_date])

del cur

print 'Done.'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hope this helps.

Update:  I was having some problems with "array.removeAll()" in line 33.  It didn't seem to be always clearing the array. After following Dan Patterson‌'s advice to move the array declaration to follow InsertCursor, line 33 appears to be working correctly.

View solution in original post

7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Have you read the arcpy documentation?

What do you have so far?

It is kind of hard to help otherwise... perhaps getting the source program to put it out in a better format or more widely recognized format would be better.

DEVAPP
by
New Contributor III

Hi Dan i have update my reques with code and more info

Thanks

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

If I understand correctly, you have the lines, but now want an additional field with a id/name, so line 1 has a name "N_1_3",  line 2 is "W_3_1", etc?

you may just want to create a separate table

1, "N_1_3"

2, "W_3_1"

...

and then use join item to add the new field to your feature class.  Unless you have to have it in a script (because of multiple datasets).

0 Kudos
RandyBurton
MVP Regular Contributor

It looked as if ID wasn't being properly adjusted.  I also did not see where "date" in your insertRow code (renamed to refDate/feature_date in code below) was being set to anything.  For testing, I omitted the try/except code; you will probably want that back in.  Check the field names in line 18, and make sure they are properly defined in the shapefile you are using.

import arcpy

output = [
    [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']
    ]

fc = r"C:\Path\To\test.shp"

ID = -1

# assumes names of fields are properly defined in shapefile
cur = arcpy.da.InsertCursor(fc, ["SHAPE@", "Name", "refDate"]) 

array = arcpy.Array([]) # array needs to be created after InsertCursor

for coords in output:
    if coords[0] == ID:
        array.append(arcpy.Point(coords[1], coords[2]))

    else: # next feature
        # process previous array
        if ID > 0:
            polyline = arcpy.Polyline(array)
            cur.insertRow([polyline, feature_name, feature_date])

        # begin new array
        array.removeAll()
        ID = coords[0]
        array.append(arcpy.Point(coords[1], coords[2]))
        feature_name = coords[3]
        feature_date = coords[4]

# add the last feature
polyline = arcpy.Polyline(array)
cur.insertRow([polyline, feature_name, feature_date])

del cur

print 'Done.'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hope this helps.

Update:  I was having some problems with "array.removeAll()" in line 33.  It didn't seem to be always clearing the array. After following Dan Patterson‌'s advice to move the array declaration to follow InsertCursor, line 33 appears to be working correctly.

DanPatterson_Retired
MVP Esteemed Contributor

Randy... if you are having trouble with the array thing... try moving line 14 to line 20.5 ... it will create the empty array where it should be... Also, I am not sure with cursors whether you need the empty list inside the array creation, that may be what is causing the removal to have 'issues'

RandyBurton
MVP Regular Contributor

Thanks, Dan.  It does appear that the array declaration needs to happen after the InsertCursor line for the removeAll method to work.  I have modified the code in my previous post to show this correction.

0 Kudos
DEVAPP
by
New Contributor III

Thanks Randy for your help.

The output now is ok.

Great

Thanks

0 Kudos