Creating Polyline feature from points using for loop in Python: No error but does not create the features either

Question:

I am new to Python and trying to create a Polyline feature from Lat longs in a text file. Using answers to similar questions, below is the code I came up with. It doesn’t throw an error but doesn’t create polyline feature either. The LatLongs.txt contains three columns 1) unique identifier, 2) Latitude, 3) Longitude. Any help is greatly appreciated.

import arcpy
arcpy.env.overwriteOutput=1
array = arcpy.Array()

outFolder=r'C:Temp'
fc = 'PolylineCSV.shp'
spatRef = arcpy.SpatialReference(4326)
arcpy.CreateFeatureclass_management(out_path=outFolder, out_name=fc, 
                                   geometry_type='POLYLINE', 
                                   spatial_reference=spatRef)

featureList = []
cursor = arcpy.InsertCursor("C:\Temp\PolylineCSV.shp")
feat = cursor.newRow()

coordinateList="C:\Temp\LatLongs.txt"
with open(coordinateList) as f:
    for line in f:           
        SplitLine = line.split(',') 
        x = float(SplitLine[2]) #Get longitude
        y = float(SplitLine[1]) #Get latitude
        point = arcpy.Point(x,y)
        array.add(point)
        try:
            nextline=next(f)
            SplitnextLine = nextline.split(',')
            x = float(SplitnextLine[2])
            y = float(SplitnextLine[1])
            point = arcpy.Point(x,y)
            array.add(point)
            polyline = arcpy.Polyline(array)                     
            array.removeAll()
            # Append to the list of Polyline objects
            featureList.append(polyline)
            feat.shape = polyline
            # Insert the feature
            cursor.insertRow(feat)
        except StopIteration as e:
            print("error handled")
del feat
del cursor
Asked By: asmi

||

Answers:

ArcGIS for Desktop

Your issue is most likely this line: feat.shape = polyline. feat has no shape property. You may use feat.setValue("shape", polyline). Here is an example based on your code (but without looping through a CSV file):

import os
import arcpy

output_folder = r"c:temp"
output_file = "trips.shp"

sr = arcpy.SpatialReference(4326)

arcpy.CreateFeatureclass_management(
    output_folder, output_file, "POLYLINE", spatial_reference=sr)

cursor = arcpy.InsertCursor(os.path.join(output_folder, output_file))

array = arcpy.Array()

array.add(arcpy.Point(8.541, 47.374))
array.add(arcpy.Point(-63.716, 44.867))

polyline = arcpy.Polyline(array, spatial_reference=sr)

feature = cursor.newRow()

# set the geometry
feature.setValue("shape", polyline)

cursor.insertRow(feature)

del cursor

It looks like you are using ArcGIS for Desktop. If you are using 10.1 or newer, then you should use arcpy.da.InsertCursor instead arcpy.InsertCursor.

[arcpy.InsertCursor] was superceded by arcpy.da.InsertCursor at ArcGIS 10.1. For faster performance, use arcpy.da.InsertCursor.

https://desktop.arcgis.com/en/arcmap/latest/analyze/python/data-access-using-cursors.htm


ArcGIS Pro

If I understand your code correctly, then every two points (lines) in your CSV file should be a line. Here are two working examples:

import os
import csv
import arcpy, arcpy.da, arcpy.management

Use arcpy.da.InsertCursor:

arcpy.env.overwriteOutput = True

output_folder = r"c:temp"
output_file_name = "trips.shp"

input_file = r"d:projectsplaygroundpythonstackgisdatatrips.csv"

sr = arcpy.SpatialReference(4326)

output_feature_class = arcpy.management.CreateFeatureclass(
    output_folder, output_file_name, geometry_type="POLYLINE", spatial_reference=sr)

with open(input_file) as csv_file, 
        arcpy.da.InsertCursor(output_feature_class, ["SHAPE@"]) as cursor:

    reader = csv.reader(csv_file)
    next(reader)  # skip header line

    for row in reader:
        
        first_point = arcpy.Point(row[2], row[3])

        next_row = next(reader, None)

        if next_row:

            second_point = arcpy.Point(next_row[2], next_row[3])

            line = arcpy.Polyline(
                arcpy.Array([first_point, second_point]), spatial_reference=sr)

            cursor.insertRow([line])

Or, use arcpy.management.CopyFeatures:

arcpy.env.overwriteOutput = True

input_file = r"d:projectsplaygroundpythonstackgisdatatrips.csv"
output_file = r"c:temptrips.shp"

features = []

with open(input_file) as csv_file:

    reader = csv.reader(csv_file)
    next(reader)  # skip header line

    for row in reader:
        
        first_point = arcpy.Point(row[2], row[3])

        next_row = next(reader, None)

        if next_row:

            second_point = arcpy.Point(next_row[2], next_row[3])

            line = arcpy.Polyline(
                arcpy.Array([first_point, second_point]), spatial_reference=sr)

            features.append(line)

arcpy.management.CopyFeatures(features, output_file)
Answered By: Thomas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.