3

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/… ? – Marc Pfister Aug 23 '16 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. – artwork21 Aug 23 '16 at 1:17
  • 1
    Please edit your question rather than trying to change it via comments on an answer. – PolyGeo Aug 28 '16 at 2:41
7

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.setPrefixPath("/usr", True) # Adjust it to your path
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 – A77ak Aug 23 '16 at 1:28
  • Can you please tell me the result of running distance.sourceCrs() and distance.ellipsoidalEnabled() in your environment? – Germán Carrillo Aug 23 '16 at 2:23
  • sourceCrs returns 0 and ellipsoidalEnabled returns true – A77ak Aug 23 '16 at 20:24
  • 1
    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. – Germán Carrillo Aug 24 '16 at 23:52
  • 1
    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. – user35594 Sep 30 '16 at 19:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.