Commit b8cdcfde authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

optimization of function: speedup 2.86

parent 399a6a45
......@@ -11,6 +11,13 @@ class Resilience:
:param grille: Grid divising the PAT in squares
self.grid = gpd.GeoDataFrame.from_file(grille)
intersection = gpd.sjoin(
how='inner', op='intersects')
self.grouped_by_cell = intersection.groupby('id')
self.size = len(self.grouped_by_cell)
self.surfaces = intersection[['id','SURF_PARC']].groupby('id').sum()
# The parameters are : the grid layer, a list corresponding to a scenario and a boolean value True or False signifying if we want to see the printing lines or not
def compute_indicator(self, patches, affichage=False):
......@@ -28,21 +35,21 @@ class Resilience:
:param patches: The scenario to analyse as a list
:param affichage: True if we want the display in the console, False is the other case
intersection = gpd.sjoin(
how='inner', op='intersects')
resilience = 0
grouped_by_cell = intersection.groupby('id')
for cell,group_by_cell in grouped_by_cell:
surf_tot = group_by_cell['SURF_PARC'].sum()
prop_cult = group_by_cell.groupby('cultgeopat')['SURF_PARC'].sum() / surf_tot
for cell,group_by_cell in self.grouped_by_cell:
surf_tot = self.surfaces.loc[cell].values[0]
intersection = patches[['ID_PARCEL','cultgeopat','SURF_PARC']].loc[group_by_cell['index_right']]
prop_cult = intersection.groupby('cultgeopat')['SURF_PARC'].sum() / surf_tot
res_cell = - (prop_cult * prop_cult.apply(math.log2)).sum()
resilience += res_cell
return resilience / len(grouped_by_cell)
return resilience / self.size
if __name__ == '__main__':
import geopandas as gpd
patches = gpd.GeoDataFrame.from_file("../output/PAT_patches/PAT_patches.shp", encoding='utf-8')
import time
res = Resilience("Parcelle_PAT/Grille_resilience.shp")
start = time.time()
elapsed = time.time()-start
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment