Skip to content

Commit

Permalink
Deploy commit: ch04 intro 6758ede
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldorman committed Sep 26, 2023
1 parent 37edf33 commit 029b001
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 491 deletions.
250 changes: 125 additions & 125 deletions 01-spatial-data.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion 02-attribute-operations.html
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ <h2 id="toc-title">On this page</h2>

<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title"><span id="attr" class="quarto-section-identifier"><span class="chapter-number">2</span>&nbsp; <span class="chapter-title">Attribute data operations</span></span></h1>
<h1 class="title"><span id="sec-attr" class="quarto-section-identifier"><span class="chapter-number">2</span>&nbsp; <span class="chapter-title">Attribute data operations</span></span></h1>
</div>

<!--
Expand Down
4 changes: 2 additions & 2 deletions 03-spatial-operations.html
Original file line number Diff line number Diff line change
Expand Up @@ -2313,7 +2313,7 @@ <h3 data-number="3.4.1" class="anchored" data-anchor-id="sec-spatial-subsetting-
<div class="cell" data-execution_count="59">
<div class="sourceCode cell-code" id="cb69"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a>src_elev.sample([(<span class="fl">0.1</span>, <span class="fl">0.1</span>)])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="59">
<pre><code>&lt;generator object sample_gen at 0x7f9f9b715640&gt;</code></pre>
<pre><code>&lt;generator object sample_gen at 0x7f40a6de5640&gt;</code></pre>
</div>
</div>
<p>In case we want all values at once, we can apply <code>list</code>. Since there was just one point, the result is just one extracted value, in this case <code>16</code>:</p>
Expand Down Expand Up @@ -2730,7 +2730,7 @@ <h3 data-number="3.4.4" class="anchored" data-anchor-id="sec-focal-operations"><
<span id="cb106-8"><a href="#cb106-8" aria-hidden="true" tabindex="-1"></a>grain_mode[[<span class="dv">0</span>, <span class="op">-</span><span class="dv">1</span>], :] <span class="op">=</span> np.nan</span>
<span id="cb106-9"><a href="#cb106-9" aria-hidden="true" tabindex="-1"></a>grain_mode</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>/tmp/ipykernel_256/45878578.py:3: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
<pre><code>/tmp/ipykernel_253/45878578.py:3: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
lambda x: scipy.stats.mode(x.flatten())[0],</code></pre>
</div>
<div class="cell-output cell-output-display" data-execution_count="82">
Expand Down
13 changes: 7 additions & 6 deletions 04-geometry-operations.html
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ <h2 id="toc-title">On this page</h2>
<li><a href="#sec-geo-ras" id="toc-sec-geo-ras" class="nav-link" data-scroll-target="#sec-geo-ras"><span class="header-section-number">4.4</span> Geometric operations on raster data</a>
<ul>
<li><a href="#geometric-intersections" id="toc-geometric-intersections" class="nav-link" data-scroll-target="#geometric-intersections"><span class="header-section-number">4.4.1</span> Geometric intersections</a></li>
<li><a href="#extent-and-origin" id="toc-extent-and-origin" class="nav-link" data-scroll-target="#extent-and-origin"><span class="header-section-number">4.4.2</span> Extent and origin</a></li>
<li><a href="#sec-extent-and-origin" id="toc-sec-extent-and-origin" class="nav-link" data-scroll-target="#sec-extent-and-origin"><span class="header-section-number">4.4.2</span> Extent and origin</a></li>
<li><a href="#sec-raster-agg-disagg" id="toc-sec-raster-agg-disagg" class="nav-link" data-scroll-target="#sec-raster-agg-disagg"><span class="header-section-number">4.4.3</span> Aggregation and disaggregation</a></li>
<li><a href="#sec-raster-resampling" id="toc-sec-raster-resampling" class="nav-link" data-scroll-target="#sec-raster-resampling"><span class="header-section-number">4.4.4</span> Resampling</a></li>
</ul></li>
Expand Down Expand Up @@ -311,13 +311,14 @@ <h2 data-number="4.1" class="anchored" data-anchor-id="prerequisites"><span clas
</section>
<section id="introduction" class="level2" data-number="4.2">
<h2 data-number="4.2" class="anchored" data-anchor-id="introduction"><span class="header-section-number">4.2</span> Introduction</h2>
<p>So far the book has explained the structure of geographic datasets (Chapter 2), and how to manipulate them based on their non-geographic attributes (Chapter 3) and spatial relations (Chapter 4). This chapter focusses on manipulating the geographic elements of geographic objects, for example by simplifying and converting vector geometries, cropping raster datasets, and converting vector objects into rasters and from rasters into vectors. After reading it—and attempting the exercises at the end—you should understand and have control over the geometry column in vector layers and the extent and geographic location of pixels represented in rasters in relation to other geographic objects.</p>
<p>So far the book has explained the structure of geographic datasets (<a href="01-spatial-data.html"><span>Chapter&nbsp;1</span></a>), and how to manipulate them based on their non-geographic attributes (<a href="02-attribute-operations.html"><span>Chapter&nbsp;2</span></a>) and spatial relations (<a href="03-spatial-operations.html"><span>Chapter&nbsp;3</span></a>). This chapter focusses on manipulating the geographic elements of geographic objects, for example by simplifying and converting vector geometries, and by cropping raster datasets. After reading it you should understand and have control over the geometry column in vector layers and the extent and geographic location of pixels represented in rasters in relation to other geographic objects.</p>
<p><a href="#sec-geo-vec"><span>Section&nbsp;4.3</span></a> covers transforming vector geometries with ‘unary’ and ‘binary’ operations. Unary operations work on a single geometry in isolation, including simplification (of lines and polygons), the creation of buffers and centroids, and shifting/scaling/rotating single geometries using ‘affine transformations’ (<a href="#sec-simplification"><span>Section&nbsp;4.3.1</span></a> to <a href="#sec-affine-transformations"><span>Section&nbsp;4.3.4</span></a>). Binary transformations modify one geometry based on the shape of another, including clipping and geometry unions, covered in <a href="#sec-clipping"><span>Section&nbsp;4.3.5</span></a> and <a href="#sec-geometry-unions"><span>Section&nbsp;4.3.7</span></a>, respectively. Type transformations (from a polygon to a line, for example) are demonstrated in Section <a href="#sec-type-transformations"><span>Section&nbsp;4.3.8</span></a>.</p>
<p><a href="#sec-geo-ras"><span>Section&nbsp;4.4</span></a> covers geometric transformations on raster objects. This involves changing the size and number of the underlying pixels, and assigning them new values. It teaches how to change the resolution (also called raster aggregation and disaggregation), the extent and the origin of a raster. These operations are especially useful if one would like to align raster datasets from diverse sources. Aligned raster objects share a one-to-one correspondence between pixels, allowing them to be processed using map algebra operations, described in Section 4.3.2. The final Section 6 connects vector and raster objects. It shows how raster values can be ‘masked’ and ‘extracted’ by vector geometries. Importantly it shows how to ‘polygonize’ rasters and ‘rasterize’ vector datasets, making the two data models more interchangeable.</p>
<p><a href="#sec-geo-ras"><span>Section&nbsp;4.4</span></a> covers geometric transformations on raster objects. This involves changing the size and number of the underlying pixels, and assigning them new values. It teaches how to change the extent and the origin of a raster “manually” (<a href="#sec-extent-and-origin"><span>Section&nbsp;4.4.2</span></a>), how to change the resolution in fixed “steps” through aggregation and disaggregation (<a href="#sec-raster-agg-disagg"><span>Section&nbsp;4.4.3</span></a>), and finally how to resample a raster into any existing template, which is the most general and often most practical approach (<a href="#sec-raster-resampling"><span>Section&nbsp;4.4.4</span></a>). These operations are especially useful if one would like to align raster datasets from diverse sources. Aligned raster objects share a one-to-one correspondence between pixels, allowing them to be processed using map algebra operations (<a href="03-spatial-operations.html#sec-raster-local-operations"><span>Section&nbsp;3.4.3</span></a>).</p>
<p>In the next chapter (<a href="05-raster-vector.html"><span>Chapter&nbsp;5</span></a>), we deal with the special case of geometry operations that involve both a raster and a vector layer together. It shows how raster values can be ‘masked’ and ‘extracted’ by vector geometries. Importantly it shows how to ‘polygonize’ rasters and ‘rasterize’ vector datasets, making the two data models more interchangeable.</p>
</section>
<section id="sec-geo-vec" class="level2" data-number="4.3">
<h2 data-number="4.3" class="anchored" data-anchor-id="sec-geo-vec"><span class="header-section-number">4.3</span> Geometric operations on vector data</h2>
<p>This section is about operations that in some way change the geometry of vector layers. It is more advanced than the spatial data operations presented in the previous chapter (in <a href="03-spatial-operations.html#sec-spatial-vec"><span>Section&nbsp;3.3</span></a>), because here we drill down into the geometry: the functions discussed in this section work on the geometric (<code>GeoSeries</code>) part, either as standalone object or as part of a <code>GeoDataFrame</code>.</p>
<p>This section is about operations that in some way change the geometry of vector layers. It is more advanced than the spatial data operations presented in the previous chapter (in <a href="03-spatial-operations.html#sec-spatial-vec"><span>Section&nbsp;3.3</span></a>), because here we drill down into the geometry: the functions discussed in this section work on the geometric part (the geometry column, which is a <code>GeoSeries</code> object), either as standalone object or as part of a <code>GeoDataFrame</code>.</p>
<section id="sec-simplification" class="level3" data-number="4.3.1">
<h3 data-number="4.3.1" class="anchored" data-anchor-id="sec-simplification"><span class="header-section-number">4.3.1</span> Simplification</h3>
<p>Simplification is a process for generalization of vector objects (lines and polygons) usually for use in smaller scale maps. Another reason for simplifying objects is to reduce the amount of memory, disk space and network bandwidth they consume: it may be wise to simplify complex geometries before publishing them as interactive maps. The <code>geopandas</code> package provides the <code>.simplify</code> method, which uses the GEOS implementation of the Douglas-Peucker algorithm to reduce the vertex count. <code>.simplify</code> uses the <code>tolerance</code> to control the level of generalization in map units (see Douglas and Peucker 1973 for details).</p>
Expand Down Expand Up @@ -1392,8 +1393,8 @@ <h3 data-number="4.4.1" class="anchored" data-anchor-id="geometric-intersections
</div>
</div>
</section>
<section id="extent-and-origin" class="level3" data-number="4.4.2">
<h3 data-number="4.4.2" class="anchored" data-anchor-id="extent-and-origin"><span class="header-section-number">4.4.2</span> Extent and origin</h3>
<section id="sec-extent-and-origin" class="level3" data-number="4.4.2">
<h3 data-number="4.4.2" class="anchored" data-anchor-id="sec-extent-and-origin"><span class="header-section-number">4.4.2</span> Extent and origin</h3>
<p>When merging or performing map algebra on rasters, their resolution, projection, origin and/or extent have to match. Otherwise, how should we add the values of one raster with a resolution of 0.2 decimal degrees to a second raster with a resolution of 1 decimal degree? The same problem arises when we would like to merge satellite imagery from different sensors with different projections and resolutions. We can deal with such mismatches by aligning the rasters. Typically, raster alignment is done through resampling—that way, it is guaranteed that the rasters match exactly (<a href="#sec-raster-resampling"><span>Section&nbsp;4.4.4</span></a>). However, sometimes it can be useful to modify raster placement and extent “manually”, by adding or removing rows and columns, or by modifying the origin, that is, shifting the raster. For example, it may be useful to add extra rows and columns to a raster prior to focal operations, so that it is easier to operate on the edges.</p>
<p>Let’s demostrate the first operation, raster padding. First, we will read the array with the <code>elev.tif</code> values:</p>
<div class="cell" data-execution_count="76">
Expand Down
6 changes: 3 additions & 3 deletions 05-raster-vector.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ <h2 id="toc-title">On this page</h2>

<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title"><span id="raster-vector" class="quarto-section-identifier"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Raster-vector interactions</span></span></h1>
<h1 class="title"><span id="sec-raster-vector" class="quarto-section-identifier"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Raster-vector interactions</span></span></h1>
</div>

<!--
Expand Down Expand Up @@ -952,7 +952,7 @@ <h3 data-number="5.5.1" class="anchored" data-anchor-id="sec-rasterizing-points"
<div class="cell" data-execution_count="36">
<div class="sourceCode cell-code" id="cb51"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a>((g, <span class="dv">1</span>) <span class="cf">for</span> g <span class="kw">in</span> cycle_hire_osm_projected.geometry.to_list())</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="36">
<pre><code>&lt;generator object &lt;genexpr&gt; at 0x7f4b706f9490&gt;</code></pre>
<pre><code>&lt;generator object &lt;genexpr&gt; at 0x7f225de05080&gt;</code></pre>
</div>
</div>
<p>Therefore, the rasterizing expression is:</p>
Expand Down Expand Up @@ -1181,7 +1181,7 @@ <h3 data-number="5.6.1" class="anchored" data-anchor-id="raster-to-polygons"><sp
<span id="cb69-4"><a href="#cb69-4" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb69-5"><a href="#cb69-5" aria-hidden="true" tabindex="-1"></a>shapes</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="49">
<pre><code>&lt;generator object shapes at 0x7f4b7071edf0&gt;</code></pre>
<pre><code>&lt;generator object shapes at 0x7f225bc42df0&gt;</code></pre>
</div>
</div>
<p>We can generate all shapes at once, into a <code>list</code> named <code>pol</code>, as follows:</p>
Expand Down
Binary file modified 05-raster-vector_files/figure-html/fig-raster-crop-output-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions 06-reproj.html
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ <h2 data-number="6.5" class="anchored" data-anchor-id="sec-geometry-operations-o
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>lnd_layer_buff <span class="op">=</span> lnd_layer.<span class="bu">buffer</span>(<span class="dv">1</span>)</span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>lnd_layer_buff</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>/tmp/ipykernel_368/855451079.py:1: UserWarning: Geometry is in a geographic CRS. Results from 'buffer' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.
<pre><code>/tmp/ipykernel_365/855451079.py:1: UserWarning: Geometry is in a geographic CRS. Results from 'buffer' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

lnd_layer_buff = lnd_layer.buffer(1)</code></pre>
</div>
Expand Down Expand Up @@ -644,10 +644,10 @@ <h2 data-number="6.6" class="anchored" data-anchor-id="sec-when-to-reproject"><s
<div class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a>lnd_layer.distance(lnd_layer_proj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>/tmp/ipykernel_368/2145313019.py:1: UserWarning: Geometry is in a geographic CRS. Results from 'distance' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.
<pre><code>/tmp/ipykernel_365/2145313019.py:1: UserWarning: Geometry is in a geographic CRS. Results from 'distance' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

lnd_layer.distance(lnd_layer_proj)
/tmp/ipykernel_368/2145313019.py:1: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries.
/tmp/ipykernel_365/2145313019.py:1: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries.
Use `to_crs()` to reproject one of the input geometries to match the CRS of the other.

Left CRS: EPSG:4326
Expand Down
Loading

0 comments on commit 029b001

Please sign in to comment.