1

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)
  • What is the full error including line number that you get from running the code snippet that you have posted? – PolyGeo Oct 13 '16 at 18:40
  • One quick thing I noticed is that if you look at the documentation (pro.arcgis.com/en/pro-app/tool-reference/data-management/…) I think your argument order is a bit off. You might have provided a percentage when the tool was asking for a distance. Not sure if that will fix it or not, but try arcpy.GeneratePointsAlongLines_management(Input_Features, Output_features, "PERCENTAGE","",percentage) instead – rumski20 Oct 13 '16 at 18:42
  • rumski20, I get the same error message when I change it to be this... – confused_coder Oct 13 '16 at 19:05
  • 1
    I believe the Sampling Toolset is new at 10.4. – Evan Oct 13 '16 at 19:24
  • Ohhhhh that would make sense. I only have 10.3, although I couldn't seem to find that in the documentation funny enough. Bummer, this would be so convenient! – confused_coder Oct 13 '16 at 19:26
7

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.

  • Wow, I just copied and pasted this in and it worked like a charm. Thank you so much! The only problem is that for one of the lines it created 6 points along the line rather than just 1, spaced evenly apart. It's a detail that I can probably find a way to deal with, but any thoughts as to why that might have happened? Thank you again! – confused_coder Oct 14 '16 at 13:22
  • This code is designed to add points along the line at a percentage based on a field. It would put 6 points on a line if you used a value such as 15%, because 15 goes into 100 6.667 times (the script will round down otherwise the final point would go past the line). So if your value is not around 15, my only other guess is that there may be two lines drawn on top of each other? It would only make one point on the line if the field value is >= 50%. Does that make sense? This is using the percentage of the total distance along the lines. – crmackey Oct 15 '16 at 19:34

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.