DEV Community

Dr. Azad Rasul
Dr. Azad Rasul

Posted on

20- Computing the distance between a pair of points using PyQGIS

# import the necessary module
from qgis.core import QgsDistanceArea

# create a new QgsDistanceArea instance
d = QgsDistanceArea()

# set the ellipsoid to use for the measurement (in this case, WGS84)
d.setEllipsoid('WGS84')

# create two QgsPointXY instances representing the coordinates of Erbil and Duhok
Erbil = QgsPointXY(44.01, 36.19)
Duhok = QgsPointXY(42.99, 36.86)

# calculate the distance between the two points using the QgsDistanceArea instance
distance = d.measureLine([Erbil, Duhok])

# print the result of the distance calculation, converted from meters to kilometers
print("The distance between Erbil and Huhok is", distance / 1000, "KM")
Enter fullscreen mode Exit fullscreen mode

If you like the content, please SUBSCRIBE to my channel for the future content

Top comments (0)