Skip to content

Commit

Permalink
Récupération et restructuration des bassins de vie (#15)
Browse files Browse the repository at this point in the history
* utilise safe_download

* enchainement final BV - a mettre dans une fonction

* req

* ajoute clé BV2012

* download_pb

* Add files via upload

* version fonctionnelle get_BV

* retire BV doublon

* retire BV doublon

* retour de la bonne clé

Co-authored-by: vattia <vincent.attia@sante.gouv.fr>
  • Loading branch information
linogaliana and vattia authored Nov 22, 2022
1 parent 3d7aefb commit 9c47fc8
Show file tree
Hide file tree
Showing 7 changed files with 592 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cartiflette/download/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .dev import get_vectorfile_ign, get_administrative_level_available_ign
from .dev import get_vectorfile_ign, get_administrative_level_available_ign, get_BV

__all__ = ["get_vectorfile_ign", "get_administrative_level_available_ign"]
__all__ = ["get_vectorfile_ign", "get_administrative_level_available_ign", "get_BV"]
52 changes: 49 additions & 3 deletions cartiflette/download/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
import re
import typing
import tempfile
import zipfile
import py7zr
import pandas as pd
import geopandas as gpd


from cartiflette.utils import download_pb, download_pb_ftp, import_yaml_config


def safe_download_write(
url: str, location: str = None, param_ftp: dict = None, ext: str = "7z"
) -> str:
url: str,
location: str = None,
param_ftp: dict = None,
ext: str = "7z",
verify: bool = True, force = True) -> str :
"""
Download data given URL and additional parameters.
Expand Down Expand Up @@ -51,7 +56,7 @@ def safe_download_write(
)
download_pb_ftp(ftp, url, fname=location)
else:
download_pb(url, location)
download_pb(url, location, verify = verify, force = force)

return location

Expand Down Expand Up @@ -415,6 +420,47 @@ def get_vectorfile_ign(
return data_ign



def get_BV(
year: int = 2022):
"""
Import and Unzip Bassins de vie (Insee, format 2012)
Args:
year
Returns:
A DataFrame
"""

dict_open_data = import_yaml_config()

url = dict_open_data['Insee']\
['BV2012'][year]["file"]

#from dev import safe_download_write
out_name = safe_download_write(
url,
location=None,
param_ftp=None,
ext = ".zip", verify = False, force = True)

tmp = tempfile.TemporaryDirectory()
location = tmp.name
# unzip in location directory

archive = zipfile.ZipFile(out_name, 'r')
archive.extractall(path=location)
archive.close()

df=pd.read_excel(location+"/"+dict_open_data['Insee']['BV2012'][year]["excel_name"],
sheet_name="Composition_communale",skiprows=5)
df=df.loc[df['BV2012'] != "ZZZZZ"][['CODGEO','BV2012']]
#ZZZZZ à Mayotte

return df


# def get_bv(location):
# dict_open_data = import_yaml_config()
# url = dict_open_data['Insee']\
Expand Down
15 changes: 12 additions & 3 deletions cartiflette/utils/_download_pb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from tqdm import tqdm


def download_pb(url: str, fname: str, total: int = None, verify: bool = True):
def download_pb(
url: str,
fname: str,
total: int = None,
force: bool = True,
verify: bool = True):
"""Useful function to get request with a progress bar
Borrowed from
Expand All @@ -24,9 +29,13 @@ def download_pb(url: str, fname: str, total: int = None, verify: bool = True):
except KeyError:
proxies = {"http": "", "https": ""}

resp = requests.get(url, proxies=proxies, stream=True, verify=verify)
resp = requests.get(
url,
proxies=proxies,
stream=True,
verify=verify)

if total is None:
if total is None and force is False:
total = int(resp.headers.get("content-length", 0))

with open(fname, "wb") as file, tqdm(
Expand Down
41 changes: 23 additions & 18 deletions cartiflette/utils/sources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,26 +173,31 @@ Insee:
file: https://www.insee.fr/fr/statistiques/fichier/2028028/table-appartenance-geo-communes-08.zip
documentation: https://www.insee.fr/fr/information/2028028
BV2012:
2022: #Pour appariement sur code commune : au sein du zip, fichier XLSX "BV2012_au_01-01-2022.xlsx", onglet "Composition_communale", variables CODGEO LIBGEO BV2012 LIBBV2012 DEP REG (début du tab, ligne 6)
id: 2115016-2022
2022:
id: 2115016
file: https://www.insee.fr/fr/statistiques/fichier/2115016/BV2012_au_01-01-2022.zip
documentation: https://www.insee.fr/fr/information/2115016
2021: #Pour appariement sur code commune : au sein du zip, fichier XLSX "BV2012_au_01-01-2021.xlsx", onglet "Composition_communale", variables CODGEO LIBGEO BV2012 LIBBV2012 DEP REG (début du tab, ligne 6)
id: 2115016-2021
file: https://www.insee.fr/fr/statistiques/fichier/2115016/BV2012_au_01-01-2021.zip
documentation: https://www.insee.fr/fr/information/2115016
2020: #Pour appariement sur code commune : au sein du zip, fichier XLSX "BV2012_au_01-01-2020_v1.xlsx", onglet "Composition_communale", variables CODGEO LIBGEO BV2012 LIBBV2012 DEP REG (début du tab, ligne 6)
id: 2115016-2020
file: https://www.insee.fr/fr/statistiques/fichier/2115016/BV2012_au_01-01-2020_v1.zip
documentation: https://www.insee.fr/fr/information/2115016
2019: #Pour appariement sur code commune : au sein du zip, fichier XLS "BV2012_au_01-01-2019.xls", onglet "Composition_communale", variables CODGEO LIBGEO BV2012 LIBBV2012 DEP REG (début du tab, ligne 6)
id: 2115016-2019
file: https://www.insee.fr/fr/statistiques/fichier/2115016/BV2012_au_01-01-2019.zip
documentation: https://www.insee.fr/fr/information/2115016
2018: #Pour appariement sur code commune : au sein du zip, fichier XLS "BV2012_au_01-01-2018_V2.xls", onglet "Composition_communale", variables CODGEO LIBGEO BV2012 LIBBV2012 DEP REG (début du tab, ligne 6)
id: 2115016-2018
file: https://www.insee.fr/fr/statistiques/fichier/2115016/BV2012_au_01-01-2018_V2.zip
documentation: https://www.insee.fr/fr/information/2115016
excel_name: BV2012_au_01-01-2022.xlsx
2021:
id: 2115016
file: https://www.insee.fr/fr/statistiques/fichier/2115016/BV2012_au_01-01-2021.zip
documentation: https://www.insee.fr/fr/information/2115016
excel_name: BV2012_au_01-01-2021.xlsx
2020:
id: 2115016
file: https://www.insee.fr/fr/statistiques/fichier/2115016/BV2012_au_01-01-2020_v1.zip
documentation: https://www.insee.fr/fr/information/2115016
excel_name: BV2012_au_01-01-2020_v1.xlsx
2019:
id: 2115016
file: https://www.insee.fr/fr/statistiques/fichier/2115016/BV2012_au_01-01-2019.zip
documentation: https://www.insee.fr/fr/information/2115016
excel_name: BV2012_au_01-01-2019.xls
2018:
id: 2115016
file: https://www.insee.fr/fr/statistiques/fichier/2115016/BV2012_au_01-01-2018_V2.zip
documentation: https://www.insee.fr/fr/information/2115016
excel_name: BV2012_au_01-01-2018_V2.xls
UU2020:
2022: #Pour appariement sur code commune : au sein du zip, fichier XLSX "UU2020_au_01-01-2022_v2022-09-27.xlsx", onglet "Composition_communale", variables CODGEO LIBGEO UU2020 LIBUU2020 TYPE_COM STATUT_2017 DEP REG (début du tab, ligne 6)
id: 4802589-2022
Expand Down
Loading

0 comments on commit 9c47fc8

Please sign in to comment.