20- How to Calculate the Distance Between Two Points in PyQGIS

Play this article
# 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)

# 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")

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