4

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?

A77ak
  • 57
  • 1
  • 6
  • Duplicate to http://gis.stackexchange.com/questions/119846/calculating-distance-between-latitude-and-longitude-points-using-python ? – Marc Pfister Aug 23 '16 at 01: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 01:17
  • 1
    Please [edit] your question rather than trying to change it via comments on an answer. – PolyGeo Aug 28 '16 at 02:41

1 Answers1

9

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.
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
  • I try and still getting the same distance. I'm using QGIS 2.8 Wien and Python 2.7 – A77ak Aug 23 '16 at 01: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 02:23
  • sourceCrs returns 0 and ellipsoidalEnabled returns true – A77ak Aug 23 '16 at 20:24
  • Let's try with this line to set a source CRS: distance.setSourceCrs(iface.mapCanvas().mapRenderer().destinationCrs().srsid()) right after you create the QgsDistanceArea() object; and then follow what I wrote in the answer. – Germán Carrillo Aug 23 '16 at 20:37
  • It gives me invalid syntax – A77ak Aug 23 '16 at 20:55
  • Try typing the whole line, since there seems to be a problem with Copy&Paste... – Germán Carrillo Aug 23 '16 at 21:19
  • Now it returns global name 'iface' is not defined – A77ak Aug 23 '16 at 23:23
  • I've updated my answer. I guess (because you haven't told us about it) that you're working out of QGIS... – Germán Carrillo Aug 24 '16 at 02:53
  • In my local PC it works, but when I use it in a Unix server without GUI it returns Cannot connect to x server. Exists another method? Thanks for the help – A77ak Aug 24 '16 at 21:47
  • Try changing 2nd and 3rd lines of the script by from PyQt4.QtCore import QCoreApplication and app = QCoreApplication([]) – Germán Carrillo Aug 24 '16 at 22:24
  • It returns TypeError: QgsDistanceArea.setSourceCrs(int): argument 1 has unexpected type 'QgsCoordinateReferenceSystem'. Maybe the QGIS version? Is a bit old and maybe has a different interface.. Im using 2.0.1 Dufour – A77ak Aug 24 '16 at 23:36
  • 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
  • for anyone struggling: distance.setEllipsoidalMode(True) should not be used in QGIS 3, setting the ellipsoid is sufficient, and the method no longer exists. – Sane_or_not Apr 02 '20 at 18:29