I am trying to save the result of a distance matrix on two layers in CSV (or in shapefile could be okay as well)
You will find the code used for the distance matrix on below:
sLayerName = "Centroids_site"
tLayerName = "Centroids_lotiss"
sLayers = QgsProject.instance().mapLayersByName(sLayerName)
sLayer = sLayers[0]
# Distance between all features from two different layers
tLayers = QgsProject.instance().mapLayersByName(tLayerName)
tLayer = tLayers[0]
sFeats = sLayer.getFeatures()
print(sLayer.featureCount())
for sfeat in sFeats:
sgeom = sfeat.geometry()
tFeats = tLayer.getFeatures()
for tfeat in tFeats:
tgeom = tfeat.geometry()
dist_pl = sgeom.distance(tgeom)
print(sfeat.id(), tfeat.id(), dist_pl)
print(sfeat.id(), 'done')
I have the output on console, but I would like to have it in a file (CSV, preferentially)
The code for the distance matrix come from "OpenSourceOptions" https://opensourceoptions.com/blog/pyqgis-measure-the-distance-between-geometries/