1

I am creating a shapefile based on three columns in my CSV File - Latitude, Longitude, and Laps. For the most part, I understand how to loop through the data and create the many points based on the latitude and longitude column, but I am having issues with my Laps column. I need to be able to loop through the data but exclude blank columns. Below is a sample of my data from the CSV File. The row that needs to be excluded is the row with the total Lap time for that Lap (i.e. #Lap 0...)

enter image description here enter image description here

Below is my code that I am using. When I run it, I get an Error 99999: Something unexpected has caused the tool to fail.

# This script reads GPS tracks in CSV format and
# writes the geometries (polyline) from the list of coordinate pairs

import csv
import arcpy

def addPolyline(fc, array, sr):
    polyline = arcpy.Polyline(array, sr)
    with arcpy.da.InsertCursor(fc, ("SHAPE@",)) as cursor:
      cursor.insertRow((polyline,))
      array.removeAll()

# setup workpace to save the file
arcpy.env.workspace = r"C:\\"

# setup the feature class and spatial reference 
polylineFC = "carpath.shp"
sr = arcpy.SpatialReference("GCS_WGS_1984")
arcpy.CreateFeatureclass_management(arcpy.env.workspace, polylineFC, "POLYLINE", spatial_reference = sr)
spatialref = arcpy.Describe(polylineFC).spatialReference


# open the input file
wakefieldRaceWay = open (arcpy.env.workspace + "\WakefieldParkRaceway_20160421.csv", "r") 
# setup csv reader and process the header
csvReader = csv.reader(wakefieldRaceWay)
header = next(csvReader)

latIndex = header.index("Latitude")
lonIndex = header.index("Longitude")
lapIndex = header.index("Lap")

# create an array
vertices = arcpy.Array()

# create a loop to iterate over lines but 
# exclude lines that have zero data in them
for row in csvReader:
    isLap = row[lapIndex]
    if isLap == '\n':
        if vertices.count> 0 :
            addPolyline(polylineFC, vertices, spatialref)
     
    # Loop through the lines in the file and get each coordinate 
    # put the coordinates into a tuple and add it to the list
    Latitude = row[latIndex]
    Longitude = row[lonIndex]
    vertex = arcpy.Point(Latitude, Longitude)
    vertices.add(vertex)
    
# add the last segment manually    
addPolyline(polylineFC, vertices, spatialref)
1
arcpy.env.workspace = r"C:\\"

using a raw string, the interpreter will escape backslashes, so this will result in 4 backslashes. Remove the "r" as you have to escape to form a valid string.

>>> dir = r"c:\\"
>>> dir
'c:\\\\'

here you are also prepending a backslash to the file name, so this will end up with 5 slashes.

wakefieldRaceWay = open (arcpy.env.workspace + "**\**WakefieldParkRaceway_20160421.csv", "r") 



>>> wakefieldRaceWay = dir + "\WakefieldParkRaceway_20160421.csv"
>>> wakefieldRaceWay
'c:\\\\\\WakefieldParkRaceway_20160421.csv'

an empty value in a csv will be blank, not a new line character

if isLap == '\n':

so use

 if isLap in (None, "") 

or better yet

if not isLap 

EDIT: You need some logic to skip attempting to create points from the blank lines lat/long.

# Loop through the lines in the file and get each coordinate 
# put the coordinates into a tuple and add it to the list
if isLap:
  Latitude = row[latIndex]
  Longitude = row[lonIndex]
  vertex = arcpy.Point(Latitude, Longitude)
  vertices.add(vertex)

Also you need to clear your vertices array after creating a new line or every line will include the vertices of every previous line.

if not isLap:
    if vertices.count> 0 :
        addPolyline(polylineFC, vertices, spatialref)
        vertices.removeAll()
 
3
  • Thanks the problem was with the way I was calling the spatial reference. The problem I am not having and I get a RuntimeError: Point: Input value is not numeric. My coordinate system is decimal in degrees Apr 21 at 0:34
  • I think the problem is you are adding points using the lat/long from blank rows.
    – BlueTalon
    Apr 21 at 1:52
  • When I incorporate your changes, I am getting a new runtime error: Object: CreateObject cannot create geometry from inputs. Do you understand why or where? I have used the debugger and nothing. Even with the error, the shapefile is still created with the laps. Apr 22 at 2:51
0

I think you should look at this line of code:

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

It does not look like a valid workspace so as a test try using:

arcpy.env.workspace = r"C:\temp"
3
  • It didn't work. My professor believes the problems lie in line 42: if isLap == '\n'. I told python to look for an empty cell instead of a cell that contains a blank, and that's why I am getting the error. He sent me this site: tutorialspoint.com/python/python_strings.htm. Do you know the proper string manipulation to use? Apr 20 at 22:56
  • Please always provide the full error message, including line number, along with the precise code you tested, in the body of your question.
    – PolyGeo
    Apr 20 at 22:59
  • 1
    I second using a directory in C: rather than the root, unless absolutely necessary. You will run into trouble eventually, if that isn't already an issue. Good habit to get into.
    – BlueTalon
    Apr 20 at 23:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.