Skip to content

Commit

Permalink
update reprojection sec
Browse files Browse the repository at this point in the history
  • Loading branch information
vzrg-tamu committed Jan 31, 2024
1 parent 5cce6da commit 0cb7f94
Show file tree
Hide file tree
Showing 180 changed files with 18,798 additions and 55 deletions.
Binary file modified awesome_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed ch1_files/figure-html/Data types-1.png
Binary file not shown.
Binary file removed ch1_files/figure-html/Data types-2.png
Binary file not shown.
Binary file removed ch1_files/figure-html/Multivariate data frame-1.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed ch1_files/figure-html/Multivariate data frame-4.png
Binary file not shown.
Binary file removed ch1_files/figure-html/Univariate data-1.png
Binary file not shown.
Binary file removed ch1_files/figure-html/Univariate data-2.png
Binary file not shown.
Binary file removed ch1_files/figure-html/data frame and list-1.png
Binary file not shown.
Binary file removed ch1_files/figure-html/data frame and list-2.png
Binary file not shown.
Binary file removed ch1_files/figure-html/data frame and list-3.png
Binary file not shown.
Binary file removed ch1_files/figure-html/data frame and list-4.png
Binary file not shown.
Binary file removed ch1_files/figure-html/data frame and list-5.png
Binary file not shown.
Binary file removed ch1_files/figure-html/data frame and list-6.png
Binary file not shown.
Binary file removed ch1_files/figure-html/density plot-1.png
Binary file not shown.
Binary file not shown.
Binary file removed ch1_files/figure-html/histogram-1.png
Binary file not shown.
Binary file removed ch1_files/figure-html/line plot-1.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed ch1_files/figure-html/time series-1.png
Binary file not shown.
Binary file removed ch1_files/figure-html/unnamed-chunk-19-1.png
Binary file not shown.
Binary file removed ch1_files/figure-html/unnamed-chunk-19-2.png
Binary file not shown.
Binary file removed ch1_files/figure-html/unnamed-chunk-19-3.png
Binary file not shown.
Binary file removed ch1_files/figure-html/using layout-1.png
Diff not rendered.
Binary file removed ch1_files/figure-html/using layout-2.png
Diff not rendered.
14 changes: 0 additions & 14 deletions ch2.rmarkdown

This file was deleted.

55 changes: 27 additions & 28 deletions ch2_a.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ list.files("./SampleData-master") # List folder contents. Do you see sample da
```

### Using R ad GIS
### Using R as GIS

A `geographic information system`, or `GIS` refers to a platform which can map, analyzes and manipulate **geographically referenced** dataset. A geographically referenced data (or geo-referenced data) is a spatial dataset which can be related to a point on Earth with the help of geographic coordinates. Types of geo-referenced spatial data include: rasters (grids of regularly sized pixels) and vectors (polygons, lines, points).

Expand Down Expand Up @@ -218,7 +218,7 @@ mapview(sm2, # RasterLayer
)
```

## Plotting raster data using `tidyterra`
#### Plotting raster data using `tidyterra`

`tidyterra` is a package that add common methods from the tidyverse for SpatRaster and SpatVectors objects created with the `terra` package. It also adds specific `geom_spat*()` functions for plotting rasters with `ggplot2`.

Expand Down Expand Up @@ -262,7 +262,7 @@ sm_conus= ggplot() +
print(sm_conus)
```

## Plotting vector data
### Plotting vector data

Importing and plotting shapefiles is equally easy in R. We will import the shapefile of the updated global `IPCC climate reference regions` and global `coastline`as Simple Feature (`sf`) Object. Terra can also be used to import shapefiles as vectors using the function `vect`. However, `sf` is more versatile, especially for shapefiles. Notice how the attributes of the `sf` objects resemble an Excel data sheet.

Expand Down Expand Up @@ -326,7 +326,7 @@ points(cbind(Long,Lat), # Lat-Long as Spatial Points
col="blue", pch=16, cex=1.2) # Shape, size and color of point
```

## Reprojection of rasters using `terra::project`
### Reprojection of rasters using `terra::project`

A coordinate reference system (CRS) is used to relate locations on Earth (which is a 3-D spheroid) to a 2-D projected map using coordinates (for example latitude and longitude). Projected CRSs are usually expressed in Easting and Northing (x and y) values corresponding to long-lat values in Geographic CRS.

Expand All @@ -344,21 +344,20 @@ A good description of coordinate reference systems and their importance can be f

In R, the coordinate reference systems or `CRS` are commonly specified in `EPSG` (European Petroleum Survey Group) or PROJ4 format (See: <https://epsg.io/>). Few commonly used projection systems and their codes are summarized below:

+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+------------+
| Projection system | PROJ.4 code | EPSG code |
+===================================================================================================+==============================================================================================================================+============+
| NAD83 (North American Datum 1983) | "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs +type=crs" | EPSG:4269 |
+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+------------+
| Mercator | "+proj=merc +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +R=6371000 +units=m +no_defs +type=crs" | ESRI:53004 |
+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+------------+
| WGS 84 (World Geodetic System 1984) | "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs" | EPSG:3395 |
+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+------------+
| WGS 84 / Pseudo-Mercator -- Spherical Mercator ( used in Google Maps, OpenStreetMap) | "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs" | EPSG:3857 |
+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+------------+
| Robinson (better for plotting global data due to minimal distortion, except for higher latitudes) | "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs" | ESRI:54030 |
+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+------------+

: The `SpatRaster` reprojection process is done with `project()` from the `terra` package.
------------------------------------------------------------------------

| | | |
|---------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------|
| *Projection system* | *PROJ.4 code* | *EPSG code* |
| NAD83 (North American Datum 1983) | "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs +type=crs" | EPSG:4269 |
| Mercator | "+proj=merc +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +R=6371000 +units=m +no_defs +type=crs" | ESRI:53004 |
| WGS 84 (World Geodetic System 1984) | "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs" | EPSG:3395 |
| WGS 84 / Pseudo-Mercator -- Spherical Mercator ( used in Google Maps, OpenStreetMap) | "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=`@null` +wktext +no_defs +type=crs" \| | EPSG:3857 |
| Robinson (better for plotting global data due to minimal distortion, except for higher latitudes) | "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs" | ESRI:54030 |

------------------------------------------------------------------------

The `SpatRaster` reprojection process is done with `project()` from the `terra` package.

```{r, message = FALSE, warning = FALSE,results='asis'}
# Importing SMAP soil moisture data
Expand Down Expand Up @@ -417,18 +416,18 @@ RobinsonPlot <- ggplot() +
print(RobinsonPlot)
# Save high resolution plot to disk
ggsave(
"globalSM.png", # Name of the file to be created
plot = RobinsonPlot, # Plot to be exported
bg = "white", # Plot background
height = 6, # Height
width = 10, # Width
units = c("in") # Units ("in", "cm", "mm" or "px")
)
# ggsave(
# "globalSM.png", # Name of the file to be created
# plot = RobinsonPlot, # Plot to be exported
# bg = "white", # Plot background
# height = 6, # Height
# width = 10, # Width
# units = c("in") # Units ("in", "cm", "mm" or "px")
# )
```

Let us view the exported plot saved on the disk.
Run the commented script above, and view the exported plot saved on the disk.

**Other resources:**

Expand Down
26 changes: 13 additions & 13 deletions ch1.html → docs/ch1.html
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,13 @@ <h3 data-number="2.1.3" class="anchored" data-anchor-id="data-types"><span class
<span id="cb86-8"><a href="#cb86-8" aria-hidden="true" tabindex="-1"></a>out[[<span class="dv">2</span>]] <span class="ot">=</span> data2</span>
<span id="cb86-9"><a href="#cb86-9" aria-hidden="true" tabindex="-1"></a>out[[<span class="dv">1</span>]] <span class="co"># Contains data1 at this location</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-stdout">
<pre><code> [1] 25.42772 29.18901 25.48689 29.03675 26.95134 22.81134 27.13496 20.82484
[9] 20.45778 27.15116 22.15038 27.13918 24.84337 22.05860 27.02549 22.00617
[17] 23.91949 29.00458 26.12511 22.17041 25.64904 23.62808 27.23896 24.77415
[25] 22.89046 25.18251 29.38348 21.34865 22.70105 23.00579 20.93994 27.69340
[33] 21.31076 23.77950 25.41240 29.26983 23.38066 23.83741 27.28841 21.66094
[41] 29.24796 24.89167 25.24554 25.89850 29.06924 25.31701 26.74200 22.79233
[49] 29.94284 20.00816</code></pre>
<pre><code> [1] 27.11756 28.68166 29.86901 26.28753 25.28627 24.00654 29.89054 22.37843
[9] 21.85850 21.36041 20.25304 22.18784 24.24517 27.52384 23.89215 28.42054
[17] 28.24869 23.03340 20.12828 27.27353 27.32140 25.28569 27.57224 23.66329
[25] 26.56781 21.45258 24.02670 24.29696 26.98421 26.07914 23.56593 25.70500
[33] 24.99104 24.12146 26.56791 20.89452 27.42495 20.88284 21.90780 20.82813
[41] 26.17926 25.82521 21.80011 29.14641 26.02670 24.44777 22.62200 25.04881
[49] 25.64245 22.97770</code></pre>
</div>
<div class="sourceCode cell-code" id="cb88"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb88-1"><a href="#cb88-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Data frame</span></span>
<span id="cb88-2"><a href="#cb88-2" aria-hidden="true" tabindex="-1"></a>out<span class="ot">=</span><span class="fu">data.frame</span>(<span class="at">x=</span>data1, <span class="at">y=</span>data2)</span>
Expand Down Expand Up @@ -844,12 +844,12 @@ <h3 data-number="2.3.3" class="anchored" data-anchor-id="multivariate-plots"><sp
<pre><code># A tibble: 6 × 4
Year January Month Value
&lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;dbl&gt;
1 1913 78.4 Jan -6.35
2 1913 78.4 Feb -15.6
3 1913 78.4 Mar -6.51
4 1914 40.6 Jan -3.69
5 1914 40.6 Feb -4.96
6 1914 40.6 Mar -5.17</code></pre>
1 1913 65.1 Jan -7.94
2 1913 65.1 Feb -4.96
3 1913 65.1 Mar -8.86
4 1914 73.4 Jan -14.7
5 1914 73.4 Feb -2.37
6 1914 73.4 Mar -10.6 </code></pre>
</div>
</div>
<p><strong>Line plot</strong></p>
Expand Down
Binary file added docs/ch1_files/figure-html/Data types-1.png
Binary file added docs/ch1_files/figure-html/Data types-2.png
Binary file added docs/ch1_files/figure-html/Univariate data-1.png
Binary file added docs/ch1_files/figure-html/Univariate data-2.png
Binary file added docs/ch1_files/figure-html/density plot-1.png
File renamed without changes
File renamed without changes
Binary file added docs/ch1_files/figure-html/histogram-1.png
Binary file added docs/ch1_files/figure-html/line plot-1.png
Binary file added docs/ch1_files/figure-html/time series-1.png
Binary file added docs/ch1_files/figure-html/unnamed-chunk-19-1.png
Binary file added docs/ch1_files/figure-html/unnamed-chunk-19-2.png
Binary file added docs/ch1_files/figure-html/unnamed-chunk-19-3.png
Binary file added docs/ch1_files/figure-html/using layout-1.png
Binary file added docs/ch1_files/figure-html/using layout-2.png
Loading

0 comments on commit 0cb7f94

Please sign in to comment.