Python extract point by line every specific lenght

4028
6
Jump to solution
02-10-2016 08:10 AM
DEVAPP
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

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
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') 

View solution in original post

6 Replies
WesMiller
Regular Contributor III
0 Kudos
DarrenWiens2
MVP Honored Contributor

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

JeffBigos
Esri Contributor

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

The function is called positionAlongLine:

PolyLine

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:

Reading geometries

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:

2. Built-in Functions — Python 2.7.11 documentation

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

0 Kudos
DarrenWiens2
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') 

JeffBigos
Esri Contributor

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

0 Kudos
DEVAPP
by
New Contributor III

Hi Darren and Jeff,

thanks for your help.

I will follow this way and script published by Darren.

Thanks guys

Regards

0 Kudos