Skip to content

Instantly share code, notes, and snippets.

@YannBouyeron
Last active July 25, 2019 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YannBouyeron/5f0d3d6b76bbea030a471f26cf16c99d to your computer and use it in GitHub Desktop.
Save YannBouyeron/5f0d3d6b76bbea030a471f26cf16c99d to your computer and use it in GitHub Desktop.
GeoPandas SIG SVT

Geopandas

Geopandas permet de créer des objets GeoDataFrame; ces objets très similaires aux DataFrame de Pandas, ont cependant la particularité de posséder une série géométrique qualifiée de GeoSeries contenant des coordonnées spatiales.

Importer geopandas

>>> import geopandas as gp 

Les GeoSeries

Un objet GeoSeries est une séries constituées d’éléments représentant des coordonnées spatiales.

Les GeoSeries peuvent représenter des Point, des LineString, ou des Polygon

Des petits points...

Un GeoSeries avec un seul point:

>>> pt = (2,6)

>>> gsp = gp.GeoSeries(Point(pt))

>>> gsp
0    POINT (2 6)
dtype: object

>>> gsp.plot()
<matplotlib.axes._subplots.AxesSubplot object at 0x66dac5d0>

>>> plt.show()

Un GeoSeries avec 3 points:

>>> gsmp = gp.GeoSeries([Point(2,4), Point(8,1), Point(13,15)])

>>> gsmp
0      POINT (2 4)
1      POINT (8 1)
2    POINT (13 15)
dtype: object

>>> gsmp.plot()
<matplotlib.axes._subplots.AxesSubplot object at 0x66de5150>

>>> plt.show()	

Si vous voulez sauvegarder le graphique, utilisez l’objet plt (matplotlib):

>>> plt.savefig("multipoints.png")

Des lignes...

Un GeoSeries de une ligne:

>>> ln =((2,2),(3,3),(4,4))

>>> gsl = gp.GeoSeries(LineString(ln))

>>> gsl
0    LINESTRING (2 2, 3 3, 4 4)
dtype: object	

>>> gsl.plot()
<matplotlib.axes._subplots.AxesSubplot object at 0x66c4b7b0>

>>> plt.show()

Il est aussi possible de faire des GeoSeries contenant plusieurs lignes gp.GeoSeries([LineString(ln), LineString(ln2), LineString(ln3)])

Des polygones...

>>> crd = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.))

>>> poly = gp.GeoSeries(Polygon(crd))

>>> poly
0    POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
dtype: object

>>> poly.plot()
<matplotlib.axes._subplots.AxesSubplot object at 0x66ad9310>

>>> plt.show()

>>> poly.plot(color="r")
<matplotlib.axes._subplots.AxesSubplot object at 0x669654d0>

>>> plt.show()

Il est aussi possible de faire des GeoSeries contenant plusieurs polygones: gp.GeoSeries([Polygon(crd), Polygon(crd2), Polygon(crd3)])

Les GeoDataFrame

Un objet GeoDataFrame contient nécessairement une série géométrique de type GeoSeries; il contient en plus d’autres Series "classiques" représentant des données qui se trouvent ainsi géolocalisées.

>>> world = gp.read_file(gp.datasets.get_path('naturalearth_lowres'))

>>> cities = gp.read_file(gp.datasets.get_path('naturalearth_cities'))

>>> world.head()
 pop_est      continent                      name iso_a3  gdp_md_est                                           geometry
0     920938        Oceania                      Fiji    FJI      8374.0  (POLYGON ((180 -16.06713266364245, 180 -16.555...
1   53950935         Africa                  Tanzania    TZA    150600.0  POLYGON ((33.90371119710453 -0.950000000000000...
2     603253         Africa                 W. Sahara    ESH       906.5  POLYGON ((-8.665589565454809 27.65642588959236...
3   35623680  North America                    Canada    CAN   1674000.0  (POLYGON ((-122.84 49.00000000000011, -122.974...
4  326625791  North America  United States of America    USA  18560000.0  (POLYGON ((-122.84 49.00000000000011, -120 49....

>>> cities.head()
           name                                     geometry
0  Vatican City  POINT (12.45338654497177 41.90328217996012)
1    San Marino    POINT (12.44177015780014 43.936095834768)
2         Vaduz  POINT (9.516669472907267 47.13372377429357)
3    Luxembourg  POINT (6.130002806227083 49.61166037912108)
4       Palikir  POINT (158.1499743237623 6.916643696007725)

>>> world.keys()
Index(['pop_est', 'continent', 'name', 'iso_a3', 'gdp_md_est', 'geometry'], dtype='object')

>>> cities.keys()
Index(['name', 'geometry'], dtype='object')

Il est possible de filtrer un GeoDataFrame:

>>> senegal = world[(world.name == "Senegal")]

>>> senegal
     pop_est continent     name iso_a3  gdp_md_est                                           geometry
51  14668522    Africa  Senegal    SEN     39720.0  POLYGON ((-16.71372880702347 13.59495860437985...
 

>>> petit_pays_afrique = world[(world.continent == "Africa") & (world.pop_est < 800000)]
 
>>> petit_pays_afrique
    pop_est continent        name iso_a3  gdp_md_est                                           geometry
2    603253    Africa   W. Sahara    ESH       906.5  POLYGON ((-8.665589565454809 27.65642588959236...
69   778358    Africa  Eq. Guinea    GNQ     31770.0  POLYGON ((9.649158155972628 2.283866075037736,...	

Créer des cartes à partir d’un GeoDataFrame

>>> world.plot()
>>> plt.show()

Ajouter un code de couleur relatif à une colonne de GeoDataFrame

>>> world.plot(column='pop_est')
>>> plt.show()

Ajouter une légende (Cet exemple est extrait de la documentation officielle de GeoPandas):

>>> from mpl_toolkits.axes_grid1 import make_axes_locatable

>>> fig, ax = plt.subplots(1, 1)

>>> divider = make_axes_locatable(ax)

>>> cax = divider.append_axes("right", size="5%", pad=0.1)

>>> world.plot(column='pop_est', ax=ax, legend=True, cax=cax)
>>> plt.show()

Modifier le code couleurs:

>>> plt.colormaps()
['Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'G�nBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', '�Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', �'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', '�Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', �'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r�', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'fla�g', 'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow�', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gra�y_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'icefire', 'icefire_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'mako', 'mako_r', '�nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'r�ocket', 'rocket_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'ta�b20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'viridis', 'viridis_r', 'vlag', 'vlag_r', 'winter', 'winter_r']

On va choisir le code couleurs PRGn:

>>> world.plot(column='pop_est', ax=ax, legend=True, cax=cax, cmap="PRGn")
<matplotlib.axes._subplots.AxesSubplot object at 0x67756050>

>>> plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment