Introd u ction to the dataset W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON Joris Van den Bossche Open so u rce so �w are de v eloper and teacher , GeoPandas maintainer
Artisanal mining site data from IPIS IPIS : International Peace Information Ser v ice Image : Connormah , CC BY - SA 3.0 , from Wikimedia Commons WORKING WITH GEOSPATIAL DATA IN PYTHON
Artisanal mining site data from IPIS IPIS : International Peace Information Ser v ice Image : G . A . O , p u blic domain , from Wikimedia Commons WORKING WITH GEOSPATIAL DATA IN PYTHON
Artisanal mining site data from IPIS More anal y sis ( re . social & sec u rit y) WORKING WITH GEOSPATIAL DATA IN PYTHON
Geospatial file formats Reading � les : geopandas.read_file("path/to/file.geojson") S u pported formats : ESRI Shape � le One "� le " consists of m u ltiple � les ! ( .shp , .dbf , .shx , .prj , ...) GeoJSON GeoPackage ( .gpkg ) ... & PostGIS databases ! WORKING WITH GEOSPATIAL DATA IN PYTHON
Writing to geospatial file formats Writing a GeoDataFrame to a � le w ith the to_file() method : # Writing a Shapefile file geodataframe.to_file("mydata.shp", driver='ESRI Shapefile') # Writing a GeoJSON file geodataframe.to_file("mydata.geojson", driver='GeoJSON') # Writing a GeoPackage file geodataframe.to_file("mydata.gpkg", driver='GPKG') WORKING WITH GEOSPATIAL DATA IN PYTHON
Let ' s practice ! W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON
Additional spatial operations W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON Joris Van den Bossche Open so u rce so �w are de v eloper and teacher , GeoPandas maintainer
O v er v ie w of spatial operations Spatial relationships : Geometr y operations : intersects intersection within union contains difference ... ... Join a � rib u tes based on spatial relation : Combine datasets based on geometr y operation : geopandas.sjoin geopandas.overlay WORKING WITH GEOSPATIAL DATA IN PYTHON
Unar y u nion Con v ert a series of geometries to a single u nion geometr y WORKING WITH GEOSPATIAL DATA IN PYTHON
Unar y u nion Con v ert a series of geometries to a single u nion geometr y: WORKING WITH GEOSPATIAL DATA IN PYTHON
B u ffer operation WORKING WITH GEOSPATIAL DATA IN PYTHON
B u ffer operation WORKING WITH GEOSPATIAL DATA IN PYTHON
B u ffer operation WORKING WITH GEOSPATIAL DATA IN PYTHON
Let ' s practice ! W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON
Appl y ing c u stom spatial operations W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON Joris Van den Bossche Open so u rce so �w are de v eloper and teacher , GeoPandas maintainer
WORKING WITH GEOSPATIAL DATA IN PYTHON
Total ri v er length w ithin 50 km of each cit y? For a single point ( cairo ): area = cairo.buffer(50000) rivers_within_area = rivers.intersection(area) print(rivers_within_area.length.sum() / 1000) 186.397219642 WORKING WITH GEOSPATIAL DATA IN PYTHON
The appl y() method Series.apply() : call a f u nction on each of the v al u es of the Series Series.apply(function, **kwargs) function : the f u nction being called on each v al u e ; the v al u e is passed as the � rst arg u ment **kwargs : additional arg u ments passed to the f u nction For a GeoSeries , the f u nction is called as function(geom, **kwargs) for each geom in the GeoSeries WORKING WITH GEOSPATIAL DATA IN PYTHON
Appl y ing a c u stom spatial operation The f u nction to appl y: def river_length(geom, rivers): area = geom.buffer(50000) rivers_within_area = rivers.intersection(area) return rivers_within_area.length.sum() / 1000 Call f u nction on the single geometr y: river_length(cairo, rivers=rivers) 186.3972196423455 A l i ll iti WORKING WITH GEOSPATIAL DATA IN PYTHON
Appl y ing a c u stom spatial operation Appl y ing on all cities : cities.geometry.apply(river_length, rivers=rivers) 0 0.000000 1 0.000000 2 106.072198 ... WORKING WITH GEOSPATIAL DATA IN PYTHON
Appl y ing a c u stom spatial operation Appl y ing on all cities and assigning res u lt to ne w col u mn : cities['river_length'] = cities.geometry.apply(river_length, rivers=rivers) cities.head() name geometry river_length 0 Vatican City POINT (1386304.6 5146502.5) 0.000000 1 San Marino POINT (1385011.5 5455558.1) 0.000000 2 Vaduz POINT (1059390.7 5963928.5) 106.072198 .. ... ... ... WORKING WITH GEOSPATIAL DATA IN PYTHON
Let ' s practice ! W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON
Working w ith raster data W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON Joris Van den Bossche Open so u rce so �w are de v eloper and teacher , GeoPandas maintainer
Image so u rce : QGIS doc u mentation WORKING WITH GEOSPATIAL DATA IN PYTHON
Raster data WORKING WITH GEOSPATIAL DATA IN PYTHON
Raster data w ith m u ltiple bands WORKING WITH GEOSPATIAL DATA IN PYTHON
The rasterio package import rasterio " P y thonic " bindings to GDAL Reading and w riting raster � les Processing tools ( masking , reprojection , resampling , ..) h � ps :// rasterio . readthedocs . io / en / latest / WORKING WITH GEOSPATIAL DATA IN PYTHON
Opening a raster file import rasterio src = rasterio.open("DEM_world.tif") Metadata : src.count src.width, src.height 1 (4320, 2160) WORKING WITH GEOSPATIAL DATA IN PYTHON
Raster data = n u mp y arra y array = src.read() Standard numpy arra y: array array([[[-4290, -4290, -4290, ..., -4290, -4290, -4290], [-4278, -4278, -4278, ..., -4278, -4278, -4278], [-4269, -4269, -4269, ..., -4269, -4269, -4269], ..., [ 2804, 2804, 2804, ..., 2804, 2804, 2804], [ 2804, 2804, 2804, ..., 2804, 2804, 2804], [ 2804, 2804, 2804, ..., 2804, 2804, 2804]]], dtype=int16) WORKING WITH GEOSPATIAL DATA IN PYTHON
Plotting a raster dataset Using the rasterio.plot.show() method : import rasterio.plot rasterio.plot.show(src, cmap='terrain') WORKING WITH GEOSPATIAL DATA IN PYTHON
E x tracting information based on v ector data rasterstats : S u mmar y statistics of geospatial raster datasets based on v ector geometries ( h � ps :// gith u b . com / perr y geo / p y thon - rasterstats ) WORKING WITH GEOSPATIAL DATA IN PYTHON
E x tract raster v al u es w ith rasterstats For point v ectors : rasterstats.point_query(geometries, "path/to/raster", interpolation='nearest'|'bilinear') For pol y gon v ectors : rasterstats.zonal_stats(geometries, "path/to/raster", stats=['min', 'mean', 'max']) WORKING WITH GEOSPATIAL DATA IN PYTHON
E x tract raster v al u es w ith rasterstats result = rasterstats.zonal_stats(countries.geometry, "DEM_gworld.tif", stats=['mean']) countries['mean_elevation'] = pd.DataFrame(result) countries.sort_values('mean_elevation', ascending=False).head() name continent geometry mean_elevation 157 Tajikistan Asia POLYGON ((74.98 37.41, ... 3103.231105 85 Kyrgyzstan Asia POLYGON ((80.25 42.34, ... 2867.717142 24 Bhutan Asia POLYGON ((91.69 27.77, ... 2573.559846 119 Nepal Asia POLYGON ((81.11 30.18, ... 2408.907816 6 Antarctica Antarctica (POLYGON ((-59.57 -80.04... 2374.075028 .. ... ... ... ... WORKING WITH GEOSPATIAL DATA IN PYTHON
Let ' s practice ! W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON
The end W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON Instr u ctors Joris Van den Bossche & Dani Arribas - Bel
Taking the ne x t steps ... More on GeoPandas : GeoPandas docs and e x ample galler y: h � ps :// geopandas . readthedocs . io / Other online so u rces , e . g .: h � ps :// a u tomating - gis - processes . gith u b . io /2018/ Looking for spatial statistics ? Check P y SAL Working w ith m u lti - dimensional gridded data ? Check x arra y Want to create interacti v e w eb maps ? Check foli u m , ip y lea � et or geo v ie w s Make matplotlib plots w ith projection s u pport ? Check cartop y WORKING WITH GEOSPATIAL DATA IN PYTHON
Good l u ck ! W OR K IN G W ITH G E OSPATIAL DATA IN P YTH ON
Recommend
More recommend