Joris Van den Bossche, FOSS4G Belgium, October 24, 2019
https://github.com/jorisvandenbossche/talks/
Joris Van den Bossche
https://github.com/jorisvandenbossche Twitter: @jorisvdbossche
Example from https://postgis.net/workshops/postgis-intro/:
SELECT subways.name AS subway_name, neighborhoods.name AS neighborhood_name, neighborhoods.boroname AS boroughFROM nyc_neighborhoods AS neighborhoodsJOIN nyc_subway_stations AS subwaysON ST_Contains(neighborhoods.geom, subways.geom)WHERE subways.name = 'Broad St';
Example from https://postgis.net/workshops/postgis-intro/:
SELECT subways.name AS subway_name, neighborhoods.name AS neighborhood_name, neighborhoods.boroname AS boroughFROM nyc_neighborhoods AS neighborhoodsJOIN nyc_subway_stations AS subwaysON ST_Contains(neighborhoods.geom, subways.geom)WHERE subways.name = 'Broad St';
Snippets from presentation last year (https://pokyah.shinyapps.io/foss4GBXL2018):
library(sf) belgium = sf::st_as_sf( rnaturalearth::ne_states(country = 'belgium'))wallonia = belgium %>% dplyr::filter(region == "Walloon")grid = sf::st_intersection( grid, sf::st_transform(wallonia, crs = 3812))
Snippets from presentation last year (https://pokyah.shinyapps.io/foss4GBXL2018):
library(sf) belgium = sf::st_as_sf( rnaturalearth::ne_states(country = 'belgium'))wallonia = belgium %>% dplyr::filter(region == "Walloon")grid = sf::st_intersection( grid, sf::st_transform(wallonia, crs = 3812))
Using Shapely and GeoPandas:
import geopandasimport shapely.geometrydistricts = geopandas.read_file("paris_districts.gpkg")notre_dame = shapely.geometry.Point(452321, 5411311)# filter districts that contain the pointdistricts[districts.contains(notre_dame)]
Using Shapely and GeoPandas:
import geopandasimport shapely.geometrydistricts = geopandas.read_file("paris_districts.gpkg")notre_dame = shapely.geometry.Point(452321, 5411311)# filter districts that contain the pointdistricts[districts.contains(notre_dame)]
Used under the hood by many applications (GDAL, QGIS, PostGIS, MapServer, GRASS, GeoDjango, ...)
JTS itself also used in GeoServer, GeoTools, ...
Simple feature access - OGC / ISO standard:
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Used under the hood by many applications (GDAL, QGIS, PostGIS, MapServer, GRASS, GeoDjango, ...)
Python package for the manipulation and analysis of geometric objects
Pythonic interface to GEOS
Python package for the manipulation and analysis of geometric objects
Pythonic interface to GEOS
>>> from shapely.geometry import Point, LineString, Polygon>>> point = Point(1, 1)>>> line = LineString([(0, 0), (1, 2), (2, 2)])>>> poly = line.buffer(1)
>>> poly.contains(point)True
Python package for the manipulation and analysis of geometric objects
Pythonic interface to GEOS
>>> from shapely.geometry import Point, LineString, Polygon>>> point = Point(1, 1)>>> line = LineString([(0, 0), (1, 2), (2, 2)])>>> poly = line.buffer(1)
>>> poly.contains(point)True
Nice interface to GEOS, but: single objects, no attributes
typical predicates and operations
Make working with tabular geospatial data in python easier by combining Shapely and pandas
Documentation: http://geopandas.readthedocs.io/
What Postgis is for databases/postgresql, GeoPandas is for Python/pandas
make working with geospatial data like working with any other kind of data in python (data stack, numpy, pandas and other tools around those) analysis for which you otherwise would need desktop GIS applications (QGIS, ArcGIS) or geospatial databases (PostGIS)
makes pandas objects geometry aware
Make working with tabular geospatial data in python easier by combining Shapely and pandas
>>> df = geopandas.read_file("ne_110m_admin_0_countries.shp")>>> df pop_est continent name iso_a3 gdp_md_est geometry0 920938 Oceania Fiji FJI 8374.0 MULTIPOLYGON (((180.00000 ...1 53950935 Africa Tanzania TZA 150600.0 POLYGON ((33.90371 -0.9500...2 603253 Africa W. Sahara ESH 906.5 POLYGON ((-8.66559 27.6564...3 35623680 North America Canada CAN 1674000.0 MULTIPOLYGON (((-122.84000..... ... ... ... ... ... ...>>> df = df.to_crs(epsg=3857) >>> df.geometry.area / 1e9 0 21.2833371 952.2551752 117.1023383 52166.480440.. ...
class GeoSeries: ... def distance(self, other): result = [geom.distance(other) for geom in self.geometry] return pd.Series(result)
New library that exposes geospatial operations from GEOS into Python:
New library that exposes geospatial operations from GEOS into Python:
Started by Casper van der Wel:
https://caspervdw.github.io/Introducing-Pygeos/
GitHub repo:
https://github.com/pygeos/pygeos/
Instead of (using Shapely)
[poly.contains(point) for point in points]
you can do
pygeos.contains(poly, points)
Benchmark for 1M points: contained in or distance to a polygon
Significant performance increase: 80x (contains) to 5x (distance) for this example
Numpy universal functions (ufuncs) are vectorized functions that work on arrays element-by-element supporting numpy features such as broadcasting
Demo!
Possibility to run in parallel (releasing the GIL)
Combination with Dask (https://dask.org/):
# with pygeos, single coreres1 = pygeos.distance(points, poly)
# chunked using dask, multi-threadedpoints_chunked = dask.array.from_array(points, chunks=100_000)res2 = points_chunked.map_blocks(pygeos.distance, poly, dtype=float)
Possibility to run in parallel (releasing the GIL)
Combination with Dask (https://dask.org/):
# with pygeos, single coreres1 = pygeos.distance(points, poly)
# chunked using dask, multi-threadedpoints_chunked = dask.array.from_array(points, chunks=100_000)res2 = points_chunked.map_blocks(pygeos.distance, poly, dtype=float)
-> 3x speed-up on my 4 core laptop
pygeos.Geometry
Python C extension type holding pointer to GEOS Geometry objecthttps://github.com/pygeos/pygeos/issues
Docs: https://pygeos.readthedocs.io
Install using conda:
$ conda install --channel conda-forge pygeos
Contribute: https://github.com/pygeos/pygeos/
Docs: https://pygeos.readthedocs.io
Install using conda:
$ conda install --channel conda-forge pygeos
Contribute: https://github.com/pygeos/pygeos/
Joris Van den Bossche
https://github.com/jorisvandenbossche Twitter: @jorisvdbossche
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |