Introduzione a GeoPandas

Lavorare con i dati geospaziali in Python

Joris Van den Bossche

Open source software developer and teacher, GeoPandas maintainer

Formati di dati specifici per lo spazio

restaurants = pd.read_csv("datasets/paris_restaurants.csv")
restaurants.head()
                                             type         x          y
0                             Restaurant européen  259641.6  6251867.4
1                Restaurant traditionnel français  259572.3  6252030.2
2                Restaurant traditionnel français  259657.2  6252143.8
3  Restaurant indien, pakistanais et Moyen Orient  259684.4  6252203.6
4                Restaurant traditionnel français  259597.9  6252230.0

Nel resto del corso:

  • formati di file spaziali (Shapefile, GeoJSON, GeoPackage, ...)
  • GeoPandas: DataFrame di pandas con supporto ai dati spaziali
Lavorare con i dati geospaziali in Python

Importare dati geospaziali con GeoPandas

import geopandas
countries = geopandas.read_file("countries.geojson")
countries.head()
          name      continent       gdp                            geometry
0  Afghanistan           Asia   64080.0  POLYGON ((61.21 35.65, 62.23 35...
1       Angola         Africa  189000.0  MULTIPOLYGON (((23.90 -11.72, 2...
2      Albania         Europe   33900.0  POLYGON ((21.02 40.84, 21.00 40...
4    Argentina  South America  879400.0  MULTIPOLYGON (((-66.96 -54.90, ...
5      Armenia           Asia   26300.0  POLYGON ((43.58 41.09, 44.97 41...
Lavorare con i dati geospaziali in Python

Visualizzare velocemente dati spaziali con GeoPandas

countries.plot()

Lavorare con i dati geospaziali in Python

Il GeoDataFrame

countries.head()
          name      continent       gdp                            geometry
0  Afghanistan           Asia   64080.0  POLYGON ((61.21 35.65, 62.23 35...
1       Angola         Africa  189000.0  MULTIPOLYGON (((23.90 -11.72, 2...
2      Albania         Europe   33900.0  POLYGON ((21.02 40.84, 21.00 40...
...
type(countries)
geopandas.geodataframe.GeoDataFrame
Lavorare con i dati geospaziali in Python

Il GeoDataFrame

countries.head()
          name      continent       gdp                            geometry
0  Afghanistan           Asia   64080.0  POLYGON ((61.21 35.65, 62.23 35...
1       Angola         Africa  189000.0  MULTIPOLYGON (((23.90 -11.72, 2...
2      Albania         Europe   33900.0  POLYGON ((21.02 40.84, 21.00 40...
...

Un GeoDataFrame rappresenta un insieme di dati vettoriali geospaziali in forma tabellare:

  • una colonna "geometry": contiene le informazioni geometriche
  • altre colonne: attributi che descrivono ciascuna geometria
Lavorare con i dati geospaziali in Python

L'attributo "geometry"

countries.geometry
0      POLYGON ((61.21 35.65, 62.23 35...
1      MULTIPOLYGON (((23.90 -11.72, 2...
                      ...                
175    POLYGON ((23.22 -17.52, 22.56 -...
176    POLYGON ((29.43 -22.09, 28.79 -...
Name: geometry, Length: 176, dtype: object
type(countries.geometry)
geopandas.geoseries.GeoSeries
Lavorare con i dati geospaziali in Python

DataFrame consapevole dello spazio

countries.geometry.area
0       63.593500
1      103.599439
2        3.185163
          ...    
174    112.718524
175     62.789498
176     32.280371
Length: 177, dtype: float64
Lavorare con i dati geospaziali in Python

Riepilogo

Un GeoDataFrame è come un DataFrame di pandas:

  • tutte le funzionalità dei normali DataFrame di pandas funzionano ancora

ma potenziato con funzionalità spaziali:

  • metodo plot()
  • attributo geometry (GeoSeries)
  • attributi e metodi specifici spaziali (es. area)
Lavorare con i dati geospaziali in Python

Esercitiamoci!

Lavorare con i dati geospaziali in Python

Preparing Video For Download...