#!/usr/bin/python # -*- coding: utf-8 -*- class Proximite: ''' Function calculating the proximity indice. The purpose of this indice is to reflect the proximity between productions site and consommation site. We are calculating it with a comparison of the area of patches cultivating fruits and vegetables and the population of living area (bassin de vie) in the PAT's territory A living area with a low population will require a lower cultivating area to get a good value. ''' def __init__(self, initial_patches, surf_totale_cible) : Population_PAT = initial_patches.groupby('INSEE_COM')['POPULATION'].first().sum() #498873 popByBdv = initial_patches.groupby('Bdv').apply(lambda x:x.groupby('INSEE_COM')['POPULATION'].first().sum()) # OK but slower # popByBdv = patches.groupby(['Bdv','INSEE_COM']).first().groupby('Bdv')['POPULATION'].sum() self.targetSurfByBdv = surf_totale_cible * popByBdv/Population_PAT def compute_indicator(self, patches, affichage): ''' :param shape: The scenario to analyse as a list :param fruit_legume: The proportion of area we want in the PAT for the "fruits and vegetables" type :param affichage: True if we want the display in the console, False is the other case ''' # get area of "fruits et légumes" in the scenario flSurfByBdv = patches[patches['cultgeopat']=='Fruits et légumes'].groupby('Bdv').agg({'SURF_PARC':sum}) result = flSurfByBdv.div(self.targetSurfByBdv,axis=0) # put 0 where target is reached result = result.where(result<1,0).sum() / result.where(result<1).count() return result.values[0] if __name__ == '__main__': import geopandas as gpd patches = gpd.GeoDataFrame.from_file("../output/PAT_patches/PAT_patches.shp", encoding='utf-8') prox = Proximite(patches, 20165939.605135553) print(prox.compute_indicator(patches, True))