diff --git a/04-spatial-operations.qmd b/04-spatial-operations.qmd index 22a7ca4f..d8c4b2a6 100644 --- a/04-spatial-operations.qmd +++ b/04-spatial-operations.qmd @@ -454,7 +454,7 @@ nz_height4.plot( ); ``` -### Joining incongruent layers +### Joining incongruent layers {#sec-joining-incongruent-layers} Spatial congruence is an important concept related to spatial aggregation. An aggregating object (which we will refer to as `y`) is congruent with the target object (`x`) if the two objects have shared borders. Often this is the case for administrative boundary data, whereby larger units---such as Middle Layer Super Output Areas (MSOAs) in the UK or districts in many other European countries---are composed of many smaller units. diff --git a/05-geometry-operations.qmd b/05-geometry-operations.qmd index bb66ba7a..856d8c35 100644 --- a/05-geometry-operations.qmd +++ b/05-geometry-operations.qmd @@ -283,7 +283,7 @@ The next lines of code demonstrate how this works for the `.difference` (@fig-di ```{python} #| label: fig-difference -#| fig-cap: Difference between `x` and `y` (namely, `x` "minus" `y`) +#| fig-cap: Difference between `x` and `y` (namely, `x` "minus" `y`) x.difference(y) ``` @@ -307,6 +307,46 @@ Keep in mind that `x` and `y` are interchangeable in all predictes except for `. * `x.difference(y)` means `x` minus `y`, whereas * `y.difference(x)` means `y` minus `x`. +The latter examples demontrate pairwise operations between individual `shapely` geometries. The **geopandas** package, as is often the case, contains wrappers of these **shapely** functions to be applied to multiple, or pairwise, use cases. For example, applying either of the pairwise methods on a `GeoSeries` or `GeoDataFrame` combined with a `shapely` geometry returns the pairwise (many-to-one) results. + +Let's demonstrate by calculating the difference of each geometry in a `GeoSeries` and a "fixed" `shapely` geometry. To creare the latter, let's take `x` and combine it with itself translated (@sec-affine-transformations) to a distance of `1` or `2` units "upwards" on the y-axis: + +```{python} +geom1 = gpd.GeoSeries([x]) +geom2 = geom1.translate(0, 1) +geom3 = geom1.translate(0, 2) +geom = pd.concat([geom1, geom2, geom3]) +``` + +Here is a plot of the `GeoSeries`, with the `shapely` geometry (in red) that we will intersect with it (@fig-geom-intersection): + +```{python} +#| label: fig-geom-intersection +#| fig-cap: A `GeoSeries` with two circles, and a `shapely` geometry that we will "subtract" from it (in red) + +fig, ax = plt.subplots() +geom.plot(color='none', ax=ax) +gpd.GeoSeries(y).plot(color='#FF000040', edgecolor='black', ax=ax); +``` + +Now, using `.intersection` automatically applies the `shapely` method of the same name on each geometry in `geom`, returning a new `GeoSeries`, which we name `geom_inter_y`, with the pairwise "intersections". Note the empty third geometry (can you explain the meaning of this result?): + +```{python} +geom_inter_y = geom.intersection(y) +geom_inter_y +``` + +Here is a plot of the result (@fig-geom-intersection2): + +```{python} +#| label: fig-geom-intersection2 +#| fig-cap: The output `GeoSeries`, after subtracting a `shapely` geometry using `.intersection` + +geom_inter_y.plot(color='none'); +``` + +The `.overlay` method (see @sec-joining-incongruent-layers) further extends this technique, making it possible to apply many-to-many pairwise geometry generations between all pairs of two `GeoDataFrame`s. The output is a new `GeoDataFrame` with the pairwise outputs, plus the attributes of both inputs which were the inputs of the particular pairwise output geometry. See the [Set operations with overlay](https://geopandas.org/en/stable/docs/user_guide/set_operations.html) article in the **geopandas** documentation for examples of `.overlay`. + ### Subsetting and clipping Clipping objects can change their geometry but it can also subset objects, returning only features that intersect (or partly intersect) with a clipping/subsetting object. diff --git a/05-geometry-operations_files/figure-html/cell-46-output-1.svg b/05-geometry-operations_files/figure-html/cell-46-output-1.svg deleted file mode 100644 index a179a7a1..00000000 --- a/05-geometry-operations_files/figure-html/cell-46-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/cell-47-output-1.svg b/05-geometry-operations_files/figure-html/cell-47-output-1.svg deleted file mode 100644 index 37bf8308..00000000 --- a/05-geometry-operations_files/figure-html/cell-47-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/cell-48-output-1.svg b/05-geometry-operations_files/figure-html/cell-48-output-1.svg deleted file mode 100644 index a916b122..00000000 --- a/05-geometry-operations_files/figure-html/cell-48-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/cell-50-output-1.svg b/05-geometry-operations_files/figure-html/cell-50-output-1.svg deleted file mode 100644 index a179a7a1..00000000 --- a/05-geometry-operations_files/figure-html/cell-50-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/cell-51-output-1.svg b/05-geometry-operations_files/figure-html/cell-51-output-1.svg deleted file mode 100644 index 27270596..00000000 --- a/05-geometry-operations_files/figure-html/cell-51-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/cell-52-output-1.svg b/05-geometry-operations_files/figure-html/cell-52-output-1.svg deleted file mode 100644 index fb937988..00000000 --- a/05-geometry-operations_files/figure-html/cell-52-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/cell-61-output-1.svg b/05-geometry-operations_files/figure-html/cell-61-output-1.svg deleted file mode 100644 index e61e6138..00000000 --- a/05-geometry-operations_files/figure-html/cell-61-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/fig-affine-transformations-output-1.png b/05-geometry-operations_files/figure-html/fig-affine-transformations-output-1.png deleted file mode 100644 index 00b62a22..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-affine-transformations-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-buffers-output-1.png b/05-geometry-operations_files/figure-html/fig-buffers-output-1.png deleted file mode 100644 index eeb81a0b..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-buffers-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-casting1-output-1.png b/05-geometry-operations_files/figure-html/fig-casting1-output-1.png deleted file mode 100644 index 0a789f09..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-casting1-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-centroid-pnt-on-surface-output-1.png b/05-geometry-operations_files/figure-html/fig-centroid-pnt-on-surface-output-1.png deleted file mode 100644 index 636fd171..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-centroid-pnt-on-surface-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-difference-output-1.svg b/05-geometry-operations_files/figure-html/fig-difference-output-1.svg deleted file mode 100644 index f75e7127..00000000 --- a/05-geometry-operations_files/figure-html/fig-difference-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/fig-dissolve-output-1.png b/05-geometry-operations_files/figure-html/fig-dissolve-output-1.png deleted file mode 100644 index d09335ab..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-dissolve-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-dissolve2-output-1.svg b/05-geometry-operations_files/figure-html/fig-dissolve2-output-1.svg deleted file mode 100644 index cf347a52..00000000 --- a/05-geometry-operations_files/figure-html/fig-dissolve2-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/fig-dissolve3-output-1.svg b/05-geometry-operations_files/figure-html/fig-dissolve3-output-1.svg deleted file mode 100644 index 592332a8..00000000 --- a/05-geometry-operations_files/figure-html/fig-dissolve3-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/fig-intersection-output-1.svg b/05-geometry-operations_files/figure-html/fig-intersection-output-1.svg deleted file mode 100644 index 5c6b36d5..00000000 --- a/05-geometry-operations_files/figure-html/fig-intersection-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/fig-intersection-points-output-1.png b/05-geometry-operations_files/figure-html/fig-intersection-points-output-1.png deleted file mode 100644 index 68e84f72..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-intersection-points-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-multilinestring-to-linestring-output-1.png b/05-geometry-operations_files/figure-html/fig-multilinestring-to-linestring-output-1.png deleted file mode 100644 index f598d271..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-multilinestring-to-linestring-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-overlapping-circles-output-1.svg b/05-geometry-operations_files/figure-html/fig-overlapping-circles-output-1.svg deleted file mode 100644 index 27e8724d..00000000 --- a/05-geometry-operations_files/figure-html/fig-overlapping-circles-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/fig-random-points-output-1.png b/05-geometry-operations_files/figure-html/fig-random-points-output-1.png deleted file mode 100644 index 60b33038..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-random-points-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-raster-aggregate-output-1.png b/05-geometry-operations_files/figure-html/fig-raster-aggregate-output-1.png deleted file mode 100644 index 42872247..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-raster-aggregate-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-raster-disaggregate-output-1.png b/05-geometry-operations_files/figure-html/fig-raster-disaggregate-output-1.png deleted file mode 100644 index 011ace08..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-raster-disaggregate-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-raster-intersection-output-1.png b/05-geometry-operations_files/figure-html/fig-raster-intersection-output-1.png deleted file mode 100644 index 5f7d1b6b..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-raster-intersection-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-raster-intersection2-output-1.png b/05-geometry-operations_files/figure-html/fig-raster-intersection2-output-1.png deleted file mode 100644 index 8ec20c2f..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-raster-intersection2-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-raster-resample-output-1.png b/05-geometry-operations_files/figure-html/fig-raster-resample-output-1.png deleted file mode 100644 index 2822f7f4..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-raster-resample-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-raster-shift-origin-output-1.png b/05-geometry-operations_files/figure-html/fig-raster-shift-origin-output-1.png deleted file mode 100644 index 268adbd1..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-raster-shift-origin-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-raster-shift-origin2-output-1.png b/05-geometry-operations_files/figure-html/fig-raster-shift-origin2-output-1.png deleted file mode 100644 index 2c86903a..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-raster-shift-origin2-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-simplify-lines-output-1.png b/05-geometry-operations_files/figure-html/fig-simplify-lines-output-1.png deleted file mode 100644 index 75454dfa..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-simplify-lines-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-simplify-polygons-output-1.png b/05-geometry-operations_files/figure-html/fig-simplify-polygons-output-1.png deleted file mode 100644 index d0660b19..00000000 Binary files a/05-geometry-operations_files/figure-html/fig-simplify-polygons-output-1.png and /dev/null differ diff --git a/05-geometry-operations_files/figure-html/fig-symmetric-difference-output-1.svg b/05-geometry-operations_files/figure-html/fig-symmetric-difference-output-1.svg deleted file mode 100644 index f1e2067e..00000000 --- a/05-geometry-operations_files/figure-html/fig-symmetric-difference-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/05-geometry-operations_files/figure-html/fig-union-output-1.svg b/05-geometry-operations_files/figure-html/fig-union-output-1.svg deleted file mode 100644 index 31501dd9..00000000 --- a/05-geometry-operations_files/figure-html/fig-union-output-1.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file