Code source de ensae_teaching_cs.data.data_shape_files

# coding: utf-8
"""
Shapefiles data.


:githublink:`%|py|6`
"""
import os
import pandas
from pyquickhelper.filehelper import get_url_content_timeout, unzip_files


[docs]def data_shape_files(name, cache=".", load=True): """ Downloads shape files. :param name: name of the shape file (see below) :param cache: cache folder :param load: loads the shape files, the function relies on :epkg:`geopandas` :return: shape files List of availables shape files: * `'depfr2018'`: see `Contours des départements français issus d'OpenStreetMap <https://www.data.gouv.fr/en/datasets/contours-des-departements-francais-issus-d-openstreetmap/>`_ :githublink:`%|py|24` """ if name == 'depfr2018': url = 'https://www.data.gouv.fr/en/datasets/r/eb36371a-761d-44a8-93ec-3d728bec17ce' dest = os.path.join(cache, 'departements-20180101-shp.zip') if not os.path.exists(dest): get_url_content_timeout(url, output=dest, encoding=None) res = unzip_files(dest, where_to=cache) shp = [name for name in res if name.endswith('.shp')] if len(shp) == 0: raise FileNotFoundError( # pragma: no cover "Unable to find shp file in '{}'.".format(cache)) import geopandas df = geopandas.read_file(shp[0]) df['centroid'] = df['geometry'].apply(lambda r: r.centroid) df['DEPLONG'] = df['centroid'].apply(lambda r: r.x) df['DEPLAT'] = df['centroid'].apply(lambda r: r.y) return df raise ValueError( "Unpexpected value for shape files: '{}'.".format(name))
[docs]def load_french_departments(): """ Loads a dataframe with the list of French departments and the center of each. :githublink:`%|py|49` """ this = os.path.abspath(os.path.dirname(__file__)) name = os.path.join(this, "data_shp", "departement_french_2018.csv") return pandas.read_csv(name)