[GIS] Distance between two points on QGIS 3.4.7 and Python 3.7

distancepointpythonqgis

I am under the 2154 CRS (RGF 93).
I want to calculate the distance in meters between two point knowing their coordinates.

Let's consider two points : A(474828.85, 6756169.31) and B(874895.75, 6756159.5)
If I measure the distance manualy I get 69.6m.

I want to get this result through the Python console. I have read two ways of doing it: QGIS Python Distance between points

Nevertheless, it fails on my case.

>>>point1 = QgsPoint(474828.85, 6756169.31)
>>>point2 = QgsPoint(874895.75, 6756159.5)
>>>distance = QgsDistanceArea()

This is ok, but the next line sends an error message:

>>>m = distance.measureLine(point1, point2)
Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: QgsDistanceArea.measureLine(): arguments did not match any overloaded call:
  overload 1: argument 1 has unexpected type 'QgsPoint'
  overload 2: argument 1 has unexpected type 'QgsPoint'

The other method presented in the link fails too:
enter image description here
My question is : "How can I get the distance given in meters ?"

Best Answer

This method expect point in latitude/longitude.

https://qgis.org/api/classQgsDistanceArea.html

Your coordinates are projected, so you can just do

point1 = QgsPoint(474828.85, 6756169.31)
point2 = QgsPoint(874895.75, 6756159.5)
distance = point1.distance(point2)

https://qgis.org/api/classQgsPoint.html

Related Question