Departements


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

In [ ]:
!unzip -u departements-20140306-100m-shp.zip

In [ ]:
import geopandas

In [ ]:
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

In [ ]:
df

In [ ]:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')

In [ ]:
#df.to_file("departements_orig.geojson", driver="GeoJSON")

Convert the data to Web Mercator


In [ ]:
df2 = df.to_crs(epsg=3857)

In [ ]:
df2

In [ ]:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')

Contextily helper function


In [ ]:
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))

Add background tiles to plot


In [ ]:
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()

Save selected departments into a GeoJSON file


In [ ]:
import fiona
fiona.supported_drivers

In [ ]:
df.to_file("departements.geojson", driver="GeoJSON")

In [ ]:
df = geopandas.read_file("departements.geojson")

In [ ]:
df

In [ ]:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')