!wget http://osm13.openstreetmap.fr/~cquest/openfla/export/departements-20140306-100m-shp.zip

!unzip -u departements-20140306-100m-shp.zip

import geopandas

df = geopandas.read_file("departements-20140306-100m.shp")
#df = df.iloc[[78, 91, 92, 94]]                 # OK
#df = df.loc[df.code_insee == "78"]             # OK
df = df.loc[df.code_insee.isin(["78", "91"])]   # OK

df

ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')

#df.to_file("departements_orig.geojson", driver="GeoJSON")

"""
Explanation: Departements
http://geopandas.org/gallery/plotting_basemap_background.html#adding-a-background-map-to-plots
https://www.data.gouv.fr/fr/datasets/contours-des-departements-francais-issus-d-openstreetmap/
End of explanation
"""


df2 = df.to_crs(epsg=3857)

df2

ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')

"""
Explanation: Convert the data to Web Mercator
End of explanation
"""


import contextily as ctx

def add_basemap(ax, zoom, url='http://tile.stamen.com/terrain/tileZ/tileX/tileY.png'):
    xmin, xmax, ymin, ymax = ax.axis()
    basemap, extent = ctx.bounds2img(xmin, ymin, xmax, ymax, zoom=zoom, url=url)
    ax.imshow(basemap, extent=extent, interpolation='bilinear')
    # restore original x/y limits
    ax.axis((xmin, xmax, ymin, ymax))

"""
Explanation: Contextily helper function
End of explanation
"""


ax = df2.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
add_basemap(ax, zoom=11, url=ctx.sources.ST_TONER_LITE)
ax.set_axis_off()

"""
Explanation: Add background tiles to plot
End of explanation
"""


import fiona
fiona.supported_drivers

df.to_file("departements.geojson", driver="GeoJSON")

df = geopandas.read_file("departements.geojson")

df

ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')

"""
Explanation: Save selected departments into a GeoJSON file
End of explanation
"""


