I created a dataset with the capacity and production of a product per country.
I am interested in calculating the balances between these different countries. For example, France has a positive balance of 50, Belgium a balance of 10, and Germany a balance of -50.
I am interested in creating a PyQGIS function that calculates balances between these countries based on the distance to optimize these flows; E.G: Germany to Belgium 10, Germany to France 40, equilibrium reached! Are there any existing functions of which I am not familiar with?
output_folder = 'C:/Users/*/Desktop/Extr-act/Output/'
layer5 = QgsVectorLayer(output_folder + "Distance matrix.shp", "Distance matrix", "ogr")
layer6 = QgsVectorLayer(output_folder + "Centroids.shp", "Centroids", "ogr")
with edit(layer6):
for f in layer6.getFeatures():
if f["BC_Balance"] < 0:
for i in layer5.getFeatures():
if f["CNTR_CODE"] == i["InputID"]:
f["Dest"] = ["InputID"]
f["Amount"] = 0
layer5.updateFeature(f)
I have added the question with the code I came up with so far.
https://drive.google.com/drive/folders/1WCllWC2dgSCsqQ3TokB9obO90jd4ZAoT?usp=sharing