[GIS] Generate Points Along Line

arcgis-10.3arcpylinepoint

I'm trying to find a point along a series of line at a specified percentage of each line (the percentage along the line changes with every point too). I found ArcGIS documentation of the data management tool GeneratePointsAlongLines, but when I try to use it I get the error message:

Traceback (most recent call last):
File "L:\gathr\indonesia\Sara\Scripts\1234.py", line 11, in
arcpy.GeneratePointsAlongLines_management(Input_Features, Output_features, "PERCENTAGE",percentage)
AttributeError: 'module' object has no attribute 'GeneratePointsAlongLines_management'

What am I doing wrong here? Is there an easier way to go about this rather than Generate Points Along Lines. I'm having a hard time figuring out how to use positionAlongLine, but it looks like it could be helpful. Thoughts?

import arcpy
import os
in_data = "L:\\gathr\\indonesia\\Sara\\Lines_withinTimes\\Day_andmmsi\\s20160101fc_lines.gdb\\Detection1_lines_UTM"
rows = arcpy.SearchCursor("L:\\gathr\\indonesia\\Sara\\Lines_withinTimes\\Day_andmmsi\\s20160101fc_lines.gdb\\Detection1_lines_UTM")
for row in rows:
    row_name = str(row)
    Input_Features = in_data
    Output_path = "L:\\gathr\\indonesia\\Sara\\Lines_withinTimes\\Day_andmmsi\\s20160101fc_lines.gdb\\"
    Ouput_features = os.path.join(Output_path, "Detection_" + row_name)
    percentage = row.getValue("duration_fraction")
    arcpy.GeneratePointsAlongLines_management(Input_Features, Output_features, "PERCENTAGE",percentage)

Best Answer

I have always done this with a search cursor and insert cursor. The Polyline geometry object has a getPostitionAlongLine() method that you can leverage:

import arcpy
import os
import math
arcpy.env.overwriteOutput = True

def addPointsAlongLine(lines, out_points, percentage_field):

    def iterPercentages(p):
        """iterate by percentage"""
        p = min([p, 100])
        div = int(math.floor(100.0 / p))
        for i in range(1,div+1):
            yield (0.01 * (p * i))

    path, name = os.path.split(out_points)
    sr = arcpy.Describe(lines).spatialReference
    sm = 'SAME_AS_TEMPLATE'
    arcpy.management.CreateFeatureclass(path, name, 'POINT', lines, sm, sm, sr)
    fields = [f.name for f in arcpy.ListFields(lines)
              if f.type not in ('OID', 'Geometry')
              and not f.name.lower().startswith('shape')]

    # field index for percentage
    fi = fields.index(percentage_field)

    # add points along line
    with arcpy.da.InsertCursor(out_points, fields + ['SHAPE@']) as irows:
        with arcpy.da.SearchCursor(lines, fields + ['SHAPE@']) as rows:
            for row in rows:
                perc = float(row[fi])
                for p in iterPercentages(perc):
                    irows.insertRow(row[:-1] + (row[-1].positionAlongLine(p, True),))
    return out_points

if __name__ == '__main__':

    gdb = r'L:\gathr\indonesia\Sara\Lines_withinTimes\Day_andmmsi\s20160101fc_lines.gdb'
    lines = os.path.join(gdb, 'Detection1_lines_UTM')
    out_points = os.path.join(gdb, 'Direction_along_line')
    perc_field = "duration_fraction"

    addPointsAlongLine(lines, out_points, perc_field)
    print 'done'

I should add in this case, the percentage values from the field should be numbers between 1-100 as integer or float.

Related Question