proximite.py 1.78 KB
 Seneque Colin committed Dec 19, 2018 1 2 3 ``````#!/usr/bin/python # -*- coding: utf-8 -*- `````` Dumoulin Nicolas committed Mar 26, 2019 4 ``````class Proximite: `````` Dumoulin Nicolas committed Mar 27, 2019 5 6 7 8 9 10 11 12 13 `````` ''' 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 `````` 14 `````` popByBdv = initial_patches.groupby('Bdv').apply(lambda x:x.groupby('INSEE_COM')['POPULATION'].first().sum()) `````` Dumoulin Nicolas committed Mar 27, 2019 15 16 17 18 `````` # OK but slower # popByBdv = patches.groupby(['Bdv','INSEE_COM']).first().groupby('Bdv')['POPULATION'].sum() self.targetSurfByBdv = surf_totale_cible * popByBdv/Population_PAT `````` 19 `````` def compute_indicator(self, patches, affichage): `````` Dumoulin Nicolas committed Mar 27, 2019 20 21 22 23 24 25 26 27 28 29 30 `````` ''' :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] `````` Dumoulin Nicolas committed Jan 21, 2019 31 `````` `````` Dumoulin Nicolas committed Mar 26, 2019 32 33 34 ``````if __name__ == '__main__': import geopandas as gpd patches = gpd.GeoDataFrame.from_file("../output/PAT_patches/PAT_patches.shp", encoding='utf-8') `````` Dumoulin Nicolas committed Mar 27, 2019 35 `````` prox = Proximite(patches, 20165939.605135553) `````` 36 `` print(prox.compute_indicator(patches, True))``