This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
data_shape_files.py
52 lines (46 loc) · 1.85 KB
/
data_shape_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# coding: utf-8
"""
@file
@brief Shapefiles data.
"""
import os
import pandas
from pyquickhelper.filehelper import get_url_content_timeout, unzip_files
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/>`_
"""
if name == 'depfr2018':
url = 'https://github.com/sdpython/data/raw/master/shapefiles/france_departements/departements-20180101-shp.zip'
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
f"Unable to find shp file in '{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(
f"Unpexpected value for shape files: '{name}'.")
def load_french_departments():
"""
Loads a dataframe with the list of French
departments and the center of each.
"""
this = os.path.abspath(os.path.dirname(__file__))
name = os.path.join(this, "data_shp", "departement_french_2018.csv")
return pandas.read_csv(name)