Skip to main content
replaced http://gis.stackexchange.com/ with https://gis.stackexchange.com/
Source Link

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 prefixadjust 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.

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.

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.
added 1073 characters in body
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

You need to setIf you're working into the ellipsoidalMode to TrueQGIS Python Console, do this:

distance.setEllipsoidalMode(True)
  • 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)
    

And thenIf, on the contrary, you're working out of QGIS, i.e., in a PyQGIS standalone script, you might need to setsetup the ellipsoid overwhole QGIS environment, which means that you need to initialize QGIS will perform calculationsresources, e.gamong which are the reference systems used by QGIS., WGS84 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')

Now you can measure the distance once again:

m = distance.measureLine(point1, point2) # ~322.48m.

Which will give you ~322.475m.

You need to set the ellipsoidalMode to True:

distance.setEllipsoidalMode(True)

And then you might need to set the ellipsoid over which QGIS will perform calculations, e.g., WGS84:

distance.setEllipsoid('WGS84')

Now you can measure the distance once again:

m = distance.measureLine(point1, point2)

Which will give you ~322.475m.

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.
Source Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180

You need to set the ellipsoidalMode to True:

distance.setEllipsoidalMode(True)

And then you might need to set the ellipsoid over which QGIS will perform calculations, e.g., WGS84:

distance.setEllipsoid('WGS84')

Now you can measure the distance once again:

m = distance.measureLine(point1, point2)

Which will give you ~322.475m.