# Python extract point by line every specific lenght

2845
6
02-10-2016 08:10 AM
Highlighted
by
New Contributor III

Hi at all,

i could extract a point by a lines.

I have write a script for extract the start and end point of line, but i could exctract point by line every 500 meters respect the total lenght of line.

Is there in python some script to do this or some example (way) to see?

Thanks

Regards

Tags (3)
1 Solution

Accepted Solutions
Highlighted
MVP Honored Contributor

Jeff, you've pretty well described the exact process I linked to...

`... perpLines = []  ... fc = "line"  ... sr = arcpy.Describe(fc).spatialReference  ... perpLineSpacing = 1000  ... perpLineLength = 1000  ... with arcpy.da.SearchCursor(fc,"SHAPE@",spatial_reference=sr) as cursor:  ...     for row in cursor:   ...         for part in row[0]: # part = a line array  ...             for i in range(len(part)):  ...                 if i==0: # first vertex  ...                     perpLineCounter = 0  ...                 else:  ...                     dy = part.Y - part[i-1].Y  ...                     dx = part.X - part[i-1].X  ...                     segmentAngle = math.degrees(math.atan2(dy,dx))  ...                     segmentLength = math.sqrt(math.pow(dy,2)+math.pow(dx,2))  ...                     linesOnSegment = int(segmentLength/perpLineSpacing)  ...                     for line in range(linesOnSegment+1😞  ...                         point = row[0].positionAlongLine(perpLineCounter * perpLineSpacing)  ...                         left = arcpy.Point(point.centroid.X - (math.cos(math.radians(segmentAngle-90))*perpLineLength), point.centroid.Y - (math.sin(math.radians(segmentAngle-90))*perpLineLength))  ...                         right = arcpy.Point(point.centroid.X + (math.cos(math.radians(segmentAngle-90))*perpLineLength), point.centroid.Y + (math.sin(math.radians(segmentAngle-90))*perpLineLength))  ...                         perpLines.append(arcpy.Polyline(arcpy.Array([left,right]),sr))  ...                         perpLineCounter += 1  ... arcpy.CopyFeatures_management(perpLines ,r'in_memory\lines') `

6 Replies
Highlighted
Regular Contributor III
Highlighted
MVP Honored Contributor

See this thread for an example. You can ignore the part about constructing perpendicular lines and simply place points.

Highlighted
Esri Contributor

I would take a look at a useful function available to you directly from a geometry object.

The function is called positionAlongLine:

To get to that function you will first need to get access to the polyline geometry.

That can be done using some of the examples here:

Once you have the access to Polyline geometry you will be able to use the positionAlongLine function.

The value needed will depend on the spatial reference of the geometry, more to the point the linear unit.

So if the unit is meters the value pass to the function will be in meters.

You could look at the python range function to give you a list of the values to pass in:

The function will return a point geometry, so you can pass that geometry to the next step in your process.

Highlighted
MVP Honored Contributor

Jeff, you've pretty well described the exact process I linked to...

`... perpLines = []  ... fc = "line"  ... sr = arcpy.Describe(fc).spatialReference  ... perpLineSpacing = 1000  ... perpLineLength = 1000  ... with arcpy.da.SearchCursor(fc,"SHAPE@",spatial_reference=sr) as cursor:  ...     for row in cursor:   ...         for part in row[0]: # part = a line array  ...             for i in range(len(part)):  ...                 if i==0: # first vertex  ...                     perpLineCounter = 0  ...                 else:  ...                     dy = part.Y - part[i-1].Y  ...                     dx = part.X - part[i-1].X  ...                     segmentAngle = math.degrees(math.atan2(dy,dx))  ...                     segmentLength = math.sqrt(math.pow(dy,2)+math.pow(dx,2))  ...                     linesOnSegment = int(segmentLength/perpLineSpacing)  ...                     for line in range(linesOnSegment+1😞  ...                         point = row[0].positionAlongLine(perpLineCounter * perpLineSpacing)  ...                         left = arcpy.Point(point.centroid.X - (math.cos(math.radians(segmentAngle-90))*perpLineLength), point.centroid.Y - (math.sin(math.radians(segmentAngle-90))*perpLineLength))  ...                         right = arcpy.Point(point.centroid.X + (math.cos(math.radians(segmentAngle-90))*perpLineLength), point.centroid.Y + (math.sin(math.radians(segmentAngle-90))*perpLineLength))  ...                         perpLines.append(arcpy.Polyline(arcpy.Array([left,right]),sr))  ...                         perpLineCounter += 1  ... arcpy.CopyFeatures_management(perpLines ,r'in_memory\lines') `

Highlighted
Esri Contributor

I love the output from that script, Nice work on that one!!

Highlighted
by
New Contributor III

Hi Darren and Jeff,