AnsweredAssumed Answered

Using Python to create line segments from coordinates

Question asked by GEOlson86 on Jan 30, 2015
Latest reply on Feb 1, 2015 by xander_bakker

I'm working on manipulating point coordinates into line segments.  I have a series of GPS track points that I'm trying to get into individual links.  Each line will only have a starting vertex and and ending vertex.  I was successful in loading each point, starting with the second point, in copying the X,Y coordinates from the prior point.  For example, point 2 has its GPS location in and X and Y column and it has the coordinate from point 1 in two other X and Y columns, point 3 has its X,Y values and the values from point 2.  This has given me both the starting and ending coordinates.


I tried using the sample Python script from the help file, but it seems to skip segments.  The only thing I can think is happening is that the X1 and Y1 and loading in as the same as X2 and Y2, so there is no line to be drawn, yet a feature is created.  The other problem I've run into is that the vertices of the lines don't overlap the points. 


mxd = arcpy.mapping.MapDocument("Current")# Define mxd to current file.

mapLyr1 = arcpy.mapping.ListLayers(mxd, "test_points") [0]

feature_info = list()

with arcpy.da.UpdateCursor(mapLyr1,["FID","Latitude", "Longitude", "E_Lat", "E_Long"]) as sCursor:
    for row in sCursor:
  if row[0] == 0:
  lastLat = row[1]
  lastLong = row[2]
  thisLat = row[1]
  thisLong = row[2]
  row[3] = lastLat
  row[4] = lastLong
  Coords = [[thisLong, thisLat], [lastLong, lastLat]]#creates the X1, Y1 and X2, Y2 pairs
  print Coords
  feature_info.append(Coords)#append the X,Y pairs
  lastLat = thisLat#turn current Y value into past Y value
  lastLong = thisLong#turn current X value into past X value

#Loops through layer to create list of X,Y Pairs
with arcpy.da.SearchCursor(mapLyr1, ["Longitude", "Latitude", "E_Long", "E_Lat"]) as lineMaker:
  for points in lineMaker:
  p1, p2, p3, p4 = points[0], points[1], points[2], points[3]
  pairs = [[p1, p2], [p3, p4]]

features = []
#Snippet from Help/ESRI Resources
for feature in feature_info:
    # Create a Polyline object based on the array of points
    # Append to the list of Polyline objects
            arcpy.Array([arcpy.Point(*coords) for coords in feature])))

# Persist a copy of the Polyline objects using CopyFeatures
arcpy.CopyFeatures_management(features, "c:/temp/polylines.shp")


I end up with the features looking like this:



I'm not sure if this is a scripting error alone, or if projections are playing a part too.  If it were a projection issue, then all the lines would show up.  I expect that if it were only a scripting error, all the points and lines would coincide.