Can someone help me understand what I am doing wrong in my calculation of the distance between two points?

My code is as follows:

import qgis

crs    = qgis.core.QgsCoordinateReferenceSystem()
crs.createFromSrsId(3452) # EPSG:4326 
disCur = qgis.core.QgsDistanceArea()

point1 = qgis.core.QgsPoint(35.895392, -78.758094)
point2 = qgis.core.QgsPoint(35.898154, -78.680271)
disTot = disCur.measureLine(point1, point2)
print disTot, disCur.lengthUnits()

It is my expectation to get an answer of 4.36 mi or 7.02 km (from GoogleMaps) but my code gives me a distance of 0.077871997361 and a value of 3 for the lengthUnits.

  1. What should I change in my code to the length in either meters, km or miles?
  2. What are the units of the distance as given my code above?
  3. Why do I get an integer value of 3 for the lengthUnits()?

It looks like you are using latitudes and longitudes for you input point geometry but you are using Louisiana South State Plane (US Feet) for the CRS. QGIS is using the coordinate reference system units of measure to calculate distances. Since you are using lat/longs for your inputs the script is returning the distance between these coordinates in feet. If you apply the distance formula to your data you will see that the distance between your two points is 0.07 feet. See the screen grab below.

Use Louisiana South State Plane (US Feet) values for your points shown below and not the lat/long values displayed.

point1 = qgis.core.QgsPoint(35.895392, -78.758094)
point2 = qgis.core.QgsPoint(35.898154, -78.680271)

enter image description here

  • Thanks I'll look at either projecting the original points. – user997836 Jan 5 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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