[Qgis-developer] calculate distance from two points along line in pyqgis

gene martin.laloux at gmail.com
Wed Jan 14 10:20:40 PST 2015


Therefore there is a quicker solution iterating through the segments of the
line until a segment contains the point

The Spatial predicate is *point.within(line)* but as there are floating
point precision errors when finding a point on a line, I use the distance
with an appropriate threshold.

def pair(list):
    '''Iterate over pairs in a list '''
    for i in range(1, len(list)):
        yield list[i-1], list[i]

def distance(line,point):
    sum = 0
    for seg_start, seg_end in pair(line.asPolyline()):
        if QgsGeometry.fromPolyline([seg_start, seg_end]).distance(point) >
1e-8 : # correction of the floating point precision errors
            sum = sum + QgsGeometry.fromPolyline([seg_start,
seg_end]).length()
        if QgsGeometry.fromPolyline([seg_start, seg_end]).distance(point)<
1e-8 :
            return sum + QgsGeometry.fromPolyline([seg_start,
point.asPoint()]).length()

print distance(polyline, point)
247.25827728910002

<http://osgeo-org.1560.x6.nabble.com/file/n5181538/segments.jpg> 





--
View this message in context: http://osgeo-org.1560.x6.nabble.com/calculate-distance-from-two-points-along-line-in-pyqgis-tp5181313p5181538.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.


More information about the Qgis-developer mailing list