# QGIS Python Distance between points [closed]

I'm new in QGIS and in GIS. I want to calculate distance between two points in meters using WGS 84 coordinates. I'm working with QGIS 2.0.1 Dufour and Python 2.7

The code is the following (based on http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html?highlight=measureline#geometry-predicates-and-operations):

``````point1 = QgsPoint(-46.443077,-67.51561)
point2 = QgsPoint(-46.4446,-67.512778)

#Create a measure object
distance = QgsDistanceArea()

#Measure the distance
m = distance.measureLine(point1, point2)
``````

Using this coordinates I'm getting 0.00321554863126 And in Google Maps is aproximately 290m)

Any help?

• Duplicate to gis.stackexchange.com/questions/119846/… ? Aug 23, 2016 at 1:04
• I would recommend calculating distances with data in PCS instead of GCS. Right now your result is returning results in dd (flat earth) and not meters (curved earth). The degree of calculation error will increase in GCS as the distance between to points increases. Aug 23, 2016 at 1:17
– PolyGeo
Aug 28, 2016 at 2:41

If you're working into the QGIS Python Console, do this:

• Set the `ellipsoidalMode` to `True`:

``````distance.setEllipsoidalMode(True)
``````
• Set the ellipsoid over which QGIS will perform calculations, e.g., WGS84:

``````distance.setEllipsoid('WGS84')
``````
• Now you can measure the distance once again (which will give you ~322.48m.):

``````m = distance.measureLine(point1, point2)
``````

If, on the contrary, you're working out of QGIS, i.e., in a PyQGIS standalone script, you need to setup the whole QGIS environment, which means that you need to initialize QGIS resources, among which are the reference systems used by QGIS. Just run the following code (adjust the prefix if you're working on a different environment):

``````from qgis.core import QgsDistanceArea, QgsCoordinateReferenceSystem, QgsPoint, QgsApplication
from PyQt4.QtGui import QApplication
app = QApplication([])
QgsApplication.initQgis()

point1 = QgsPoint(-46.443077,-67.51561)
point2 = QgsPoint(-46.4446,-67.512778)

#Create a measure object
distance = QgsDistanceArea()
crs = QgsCoordinateReferenceSystem()
crs.createFromSrsId(3452) # EPSG:4326
distance.setSourceCrs(crs)
distance.setEllipsoidalMode(True)
distance.setEllipsoid('WGS84')
m = distance.measureLine(point1, point2) # ~322.48m.
``````
• I try and still getting the same distance. I'm using QGIS 2.8 Wien and Python 2.7 Aug 23, 2016 at 1:28
• Can you please tell me the result of running `distance.sourceCrs()` and `distance.ellipsoidalEnabled()` in your environment? Aug 23, 2016 at 2:23
• sourceCrs returns 0 and ellipsoidalEnabled returns true Aug 23, 2016 at 20:24
• There's no way you can get any valuable help if you keep changing the context of the question. I feel like I'm wasting a lot of time trying to help but you don't make that easy. Good luck with your task. Aug 24, 2016 at 23:52
• I think @A77ak, you should accept this answer by ticking the tick and upvoting helpful answers and then ask a new question. There is a limt to how much you can change your original question. Sep 30, 2016 at 19:59