.. _geopandasrst: ========= Geopandas ========= .. only:: html **Links:** :download:`notebook `, :downloadlink:`html `, :download:`PDF `, :download:`python `, :downloadlink:`slides `, :githublink:`GitHub|_doc/notebooks/decouverte/geopandas.ipynb|*` `geopandas `__ est devenu le point d’entrée à tout traitement lié aux coordonnées géographiques. .. code:: ipython3 from jyquickhelper import add_notebook_menu add_notebook_menu() .. contents:: :local: Coordonnées géographiques, et geocoding --------------------------------------- .. code:: ipython3 import geopandas import pandas .. code:: ipython3 df = pandas.DataFrame(dict(name=["A", "B", "C", "D", "E"], address=["50 rue du moulin 08000 Charleville-Mézières", "50 rue dumoulin 08000 Charleville-Mézières", "50 rue du moulin 08000 Charleville", "29 rue Ordener 75018 Paris", "Gare Lille"])) df .. raw:: html
name address
0 A 50 rue du moulin 08000 Charleville-Mézières
1 B 50 rue dumoulin 08000 Charleville-Mézières
2 C 50 rue du moulin 08000 Charleville
3 D 29 rue Ordener 75018 Paris
4 E Gare Lille
.. code:: ipython3 from geopy.geocoders import Nominatim .. code:: ipython3 geolocator = Nominatim(timeout=10, user_agent = "myGeolocator") res = geolocator.geocode("50 rue du moulin 08000 Charleville-Mézières") res .. parsed-literal:: Location(50, Rue du Moulin, Charleville-Mézières, Ardennes, Grand Est, France métropolitaine, 08000, France, (49.775247, 4.721863, 0.0)) .. code:: ipython3 res.latitude, res.longitude, res.point .. parsed-literal:: (49.775247, 4.721863, Point(49.775247, 4.721863, 0.0)) .. code:: ipython3 coordinates = df['address'].apply(geolocator.geocode) .. code:: ipython3 import numpy from shapely.geometry import Point df['position'] = [numpy.nan if c is None else Point(c.point.longitude, c.point.latitude) for c in coordinates] df .. raw:: html
name address position
0 A 50 rue du moulin 08000 Charleville-Mézières POINT (4.721863 49.775247)
1 B 50 rue dumoulin 08000 Charleville-Mézières NaN
2 C 50 rue du moulin 08000 Charleville POINT (4.721863 49.775247)
3 D 29 rue Ordener 75018 Paris POINT (2.3539607 48.8906617)
4 E Gare Lille POINT (3.162718387293441 50.69456630000001)
.. code:: ipython3 geodf = geopandas.GeoDataFrame(df) .. code:: ipython3 geodf .. raw:: html
name address position
0 A 50 rue du moulin 08000 Charleville-Mézières POINT (4.721863 49.775247)
1 B 50 rue dumoulin 08000 Charleville-Mézières NaN
2 C 50 rue du moulin 08000 Charleville POINT (4.721863 49.775247)
3 D 29 rue Ordener 75018 Paris POINT (2.3539607 48.8906617)
4 E Gare Lille POINT (3.162718387293441 50.69456630000001)
.. code:: ipython3 geodf.dropna() .. raw:: html
name address position
0 A 50 rue du moulin 08000 Charleville-Mézières POINT (4.721863 49.775247)
2 C 50 rue du moulin 08000 Charleville POINT (4.721863 49.775247)
3 D 29 rue Ordener 75018 Paris POINT (2.3539607 48.8906617)
4 E Gare Lille POINT (3.162718387293441 50.69456630000001)
.. code:: ipython3 gdf = geodf.set_geometry('position') gdf .. raw:: html
name address position
0 A 50 rue du moulin 08000 Charleville-Mézières POINT (4.72186 49.77525)
1 B 50 rue dumoulin 08000 Charleville-Mézières None
2 C 50 rue du moulin 08000 Charleville POINT (4.72186 49.77525)
3 D 29 rue Ordener 75018 Paris POINT (2.35396 48.89066)
4 E Gare Lille POINT (3.16272 50.69457)
Carte ----- .. code:: ipython3 gdf.plot(); .. image:: geopandas_15_0.png .. code:: ipython3 import cartopy.crs as ccrs import cartopy.feature as cfeature import matplotlib.pyplot as plt .. code:: ipython3 fig = plt.figure(figsize=(7,7)) ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree()) ax.set_extent([-5, 10, 42, 52]) ax.add_feature(cfeature.OCEAN) ax.add_feature(cfeature.COASTLINE) ax.add_feature(cfeature.RIVERS) ax.add_feature(cfeature.LAKES) ax.add_feature(cfeature.LAND) ax.add_feature(cfeature.BORDERS, linestyle=':') ax.plot([2.35, 2.20], [48.85, 48.71], '.') ax.text(2.35, 48.85, "Paris") ax.text(2.20, 48.71, "Saclay", ha="right") ax.set_title('France'); .. image:: geopandas_17_0.png .. code:: ipython3 fig = plt.figure(figsize=(7,7)) ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree()) ax = gdf.plot(ax=ax) ax.add_feature(cfeature.OCEAN) ax.add_feature(cfeature.COASTLINE) ax.add_feature(cfeature.RIVERS) ax.add_feature(cfeature.LAKES) ax.add_feature(cfeature.LAND) ax.add_feature(cfeature.BORDERS, linestyle=':') for i in range(gdf.shape[0]): if gdf.geometry[i] is None: continue ax.text(gdf.geometry[i].x, gdf.geometry[i].y, gdf.name[i]) ax; .. image:: geopandas_19_0.png Régions administratives -----------------------