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

2015-01-13 Thread Ing. Pierluigi De Rosa
Hi all,

I have a line geometry (ALine) and a point(Apoint) belonging to the
ALine.
Apoint have been evaluated from a second line geometry Bline that cross
the Aline using the QgsGeometry intersection function as:
Apoint =Bline.intersection(Aline)

I need to calculate distance along Aline from the starting point of
Aline up to Apoint.

In a certain way this problem could be seen as the opposite of function
interpolate of QgsGeometry
point = geom.interpolate(currentdistance)
where it is known the distance from the beginning and it provide the
point at specific distance.
In my case I have the point but I need the distance.

Thanks
Pierluigi
-- 



Ing. Pierluigi De Rosa (PhD)
Studio Associato GFOSSERVICES


___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2015-01-13 Thread G. Allegri
Hi Pierluigi,
I don't know of such a function available from Python. I've translated a
function from GEOS (C++) to Python some time ago. If you wish I can share
it.

giovanni

2015-01-13 18:16 GMT+01:00 Ing. Pierluigi De Rosa <
pierluigi.der...@gfosservices.it>:

> Hi all,
>
> I have a line geometry (ALine) and a point(Apoint) belonging to the
> ALine.
> Apoint have been evaluated from a second line geometry Bline that cross
> the Aline using the QgsGeometry intersection function as:
> Apoint =Bline.intersection(Aline)
>
> I need to calculate distance along Aline from the starting point of
> Aline up to Apoint.
>
> In a certain way this problem could be seen as the opposite of function
> interpolate of QgsGeometry
> point = geom.interpolate(currentdistance)
> where it is known the distance from the beginning and it provide the
> point at specific distance.
> In my case I have the point but I need the distance.
>
> Thanks
> Pierluigi
> --
>
>
> 
> Ing. Pierluigi De Rosa (PhD)
> Studio Associato GFOSSERVICES
>
>
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>



-- 
Giovanni Allegri
http://about.me/giovanniallegri
Twitter: https://twitter.com/_giohappy_
blog: http://blog.spaziogis.it
GEO+ geomatica in Italia http://bit.ly/GEOplus
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2015-01-14 Thread gene
It is a pure problem of analytical geometry or vector calculus (look at 
PyQGIS: geometry, vectors, vector algebra or direction cosines...(in French

 
))

Is the line is straight the cartesian distance between two points is given
by

def distance(point1,point2):
#Theoretical cartesian Euclidean distance between 2 points (x,y)
return math.sqrt((point2.x()-point1.x())**2 +
(point2.y()-point1.y())**2)

As PyQGIS has the sqrDist function, you can replace by

def distance(point1,point2):
return math.sqrt(point1.sqrDist(point2))

By definition, the point of intersection between two lines lies on the two
lines. It is easy to extract the start point or the end point of the lines
and calculate the distance.

If you want a confirmation, there are many algorithms in Python to verify
that 3 points are collinear

If the line has many segments, work at the straight segments level 



--
View this message in context: 
http://osgeo-org.1560.x6.nabble.com/calculate-distance-from-two-points-along-line-in-pyqgis-tp5181313p5181376.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2015-01-14 Thread Ing. Pierluigi De Rosa
Dear Gene,

Maybe my have not been well explained but I don't need the straight
cartesian distance but the distance following the line (as a path).
I think the code posted by Giovanni should be fine for my case.
I will test it right now.

Thanks
Pierluigi


Il giorno mer, 14/01/2015 alle 00.05 -0800, gene ha scritto:
> It is a pure problem of analytical geometry or vector calculus (look at 
> PyQGIS: geometry, vectors, vector algebra or direction cosines...(in French
> 
>  
> ))
> 
> Is the line is straight the cartesian distance between two points is given
> by
> 
> def distance(point1,point2):
> #Theoretical cartesian Euclidean distance between 2 points (x,y)
> return math.sqrt((point2.x()-point1.x())**2 +
> (point2.y()-point1.y())**2)
> 
> As PyQGIS has the sqrDist function, you can replace by
> 
> def distance(point1,point2):
> return math.sqrt(point1.sqrDist(point2))
> 
> By definition, the point of intersection between two lines lies on the two
> lines. It is easy to extract the start point or the end point of the lines
> and calculate the distance.
> 
> If you want a confirmation, there are many algorithms in Python to verify
> that 3 points are collinear
> 
> If the line has many segments, work at the straight segments level 
> 
> 
> 
> --
> View this message in context: 
> http://osgeo-org.1560.x6.nabble.com/calculate-distance-from-two-points-along-line-in-pyqgis-tp5181313p5181376.html
> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer

-- 



Ing. Pierluigi De Rosa (PhD)
Studio Associato GFOSSERVICES

Presso CAMPUS Via F.lli Cairoli 25 - 06127 Perugia (PG) 
fax: 075 7823038
cel: 3497558268
web: www.gfosservices.it
skype: pierluigi.derosa 

___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2015-01-14 Thread gene
I don't have the code posted by Giovanni but this is very easy to do with 
Shapely    (a Python wrapper of
GEOS).

With PyQGIS, I do it in this way:

1) by definition a polyline is composed of  straight  segments between
points (nodes)
2) after computing the intersection between the polylines , the intersecting
point lies on a particular segment, easy to determine.
3) Then a simple loop through the segments (length) allows to compute the
distance between the first point of the line and the intersection point (=
sum of segments length + the length of the segment between the first point
of the segment where the intersecting point lies and the intersection
point).



--
View this message in context: 
http://osgeo-org.1560.x6.nabble.com/calculate-distance-from-two-points-along-line-in-pyqgis-tp5181313p5181413.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2015-01-14 Thread G. Allegri
https://gist.github.com/giohappy/f2abde1549aa2775b5aa

2015-01-14 10:40 GMT+01:00 gene :

> I don't have the code posted by Giovanni but this is very easy to do with
> Shapely    (a Python wrapper of
> GEOS).
>
> With PyQGIS, I do it in this way:
>
> 1) by definition a polyline is composed of  straight  segments between
> points (nodes)
> 2) after computing the intersection between the polylines , the
> intersecting
> point lies on a particular segment, easy to determine.
> 3) Then a simple loop through the segments (length) allows to compute the
> distance between the first point of the line and the intersection point (=
> sum of segments length + the length of the segment between the first point
> of the segment where the intersecting point lies and the intersection
> point).
>
>
>
> --
> View this message in context:
> http://osgeo-org.1560.x6.nabble.com/calculate-distance-from-two-points-along-line-in-pyqgis-tp5181313p5181413.html
> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>



-- 
Giovanni Allegri
http://about.me/giovanniallegri
Twitter: https://twitter.com/_giohappy_
blog: http://blog.spaziogis.it
GEO+ geomatica in Italia http://bit.ly/GEOplus
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2015-01-14 Thread gene
= sum of segments length before the one where the intersecting point lies +
the length between the first point of this segment and the intersection
point



--
View this message in context: 
http://osgeo-org.1560.x6.nabble.com/calculate-distance-from-two-points-along-line-in-pyqgis-tp5181313p5181416.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


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

2015-01-14 Thread G. Allegri
Yes gene. Plus, this script considers a point not intersecting the line.

2015-01-14 10:49 GMT+01:00 gene :

> = sum of segments length before the one where the intersecting point lies +
> the length between the first point of this segment and the intersection
> point
>
>
>
> --
> View this message in context:
> http://osgeo-org.1560.x6.nabble.com/calculate-distance-from-two-points-along-line-in-pyqgis-tp5181313p5181416.html
> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>



-- 
Giovanni Allegri
http://about.me/giovanniallegri
Twitter: https://twitter.com/_giohappy_
blog: http://blog.spaziogis.it
GEO+ geomatica in Italia http://bit.ly/GEOplus
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

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

2015-01-14 Thread gene
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

 





--
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.
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer