---
title: "iemiscdata: Map of Southeastern USA Engineering Weather Sites"
author: "Irucka Embry, E.I.T. (EcoC²S)"
date: "`r Sys.Date()`"
lang: en-us
urlcolor: blue
output:
  rmarkdown::html_document:
    highlight: kate
    toc: true
    theme: readable
vignette: >
  %\VignetteIndexEntry{iemiscdata Map of Southeastern USA Engineering Weather Sites}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

<br />
<br />

# Creating a ggplot2 Map of the Southeastern USA Engineering Weather Sites

```{r, warning = FALSE, message = FALSE, out.width = "100%", out.height = "100%"}
install.load::load_package("ie2miscdata", "USA.state.boundaries", "data.table", "ggplot2", "sf", "spatstat", "maptools")
# load needed packages using the load_package function from the install.load package (it is assumed that you have already installed these packages)


data(weather_results)
# load the weather_results data (containing the site information for USA weather stations)

data(state_boundaries_wgs84)
# load the state_boundaries_wgs84 data from USA.state.boundaries (for the USA map)


## Weather Results

weather_results_map <- copy(weather_results)
# copy the weather_results using data.table

setnames(weather_results_map, 3:4, c("lat", "lon"))
# set the names of columns 3 and 4 using data.table

weather_results_map_sf <- st_as_sf(weather_results_map, coords = c("lon", "lat"), crs = "+proj=longlat +datum=NAD83")
# set the initial projection to longlat using sf

weather_results_map_sf_projected <- st_transform(weather_results_map_sf, "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0")
# transform the coordinates to match those of the USA_state_boundaries_map data from USA.state.boundaries.data (formerly in USA.state.boundaries)


## Southeast region

# subset all southeastern states from state_boundaries_wgs84
southeast <- subset(state_boundaries_wgs84, NAME %in% c("Kentucky", "Florida", "North Carolina", "South Carolina", "Alabama", "Mississippi", "Louisiana", "Georgia", "Virginia", "Tennessee"))
# Source 1

southeast_projected <- st_transform(southeast, "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0")
# transform the coordinates to match those of the USA_state_boundaries_map data from USA.state.boundaries.data (formerly in USA.state.boundaries)


# As different methods using sf failed to subset the weather map points within the Southeast, the weather results were transformed back to a sp Spatial object so that spatstat could be used to determine this information

weather_results_map_spatial <- as(weather_results_map_sf_projected, "Spatial")
# transform to Spatial

weather_results_map_dt <- as.data.table(as.data.frame(weather_results_map_spatial))
# transform to a data.table

setnames(weather_results_map_dt, c("coords.x1", "coords.x2"), c("lon", "lat"))
# set the names of columns coords.x1 and coords.x2 using data.table


southeast_spatial <- as(southeast_projected, "Spatial")
# transform to Spatial

southeast_owin <- as.owin(southeast_spatial)
# transform to Window


inside_southeast <- which(inside.owin(weather_results_map_dt$lon, weather_results_map_dt$lat, southeast_owin))
# determine which locations are within the borders of the Southeastern USA
# Source 2


weather_results_keep <- weather_results[inside_southeast, ]
# keep only the locations within the Southeastern USA

weather_results_map_keep <- copy(weather_results_keep)
# create a data.table copy of the sites that are kept

setnames(weather_results_map_keep, 3:4, c("lat", "lon"))
# set the names of columns 3 and 4 using data.table

weather_results_map_keep_sf <- st_as_sf(weather_results_map_keep, coords = c("lon", "lat"), crs = "+proj=longlat +datum=WGS84 +ellps=WGS84")
# set the projection to longlat using sf

# plot the map using ggplot2
p <- ggplot() + geom_sf(data = southeast, colour = "black", fill = "white")
p <- p + geom_sf(data = weather_results_map_keep_sf, colour = "#3591d1")
p <- p + labs(x = "", y = "", title = "Southeast USA Engineering Weather Sites Map")
print(p)
```

<br />
<br />

# Sources

Latitude Longitude Coordinates to State Code in R - Stack Overflow answered by Josh O'Brien on Jan 6 2012 and edited by Josh O'Brien on Jun 18, 2020. See https://stackoverflow.com/questions/8751497/latitude-longitude-coordinates-to-state-code-in-r.  

R-sig-geo - Problem in converting SpatialPolygonsDataFrame to owin object Answer by Roger Bivand on Sep 15, 2006. See https://stat.ethz.ch/pipermail/r-sig-geo/2006-September/001313.html.  

<br />
<br />

# EcoC^2^S Links

[EcoC^2^S Home](https://www.ecoccs.com/index.html)  
<br />
[About EcoC^2^S](https://www.ecoccs.com/about_ecoc2s.html)  
<br />
[Products](https://www.questionuniverse.com/products.html)  
<br />
[EcoC^2^S Media](https://www.ecoccs.com/media.html)  
<br />
[EcoC^2^S Resources](https://www.ecoccs.com/resources.html)  
<br />
[R Trainings and Resources provided by EcoC^2^S (Irucka Embry, E.I.T.)](https://www.ecoccs.com/rtraining.html)  

<br />
<br />

# Copyright and License

All R code written by Irucka Embry is distributed under the GPL-3 (or later) license, see the [GNU General Public License (GPL) page](https://www.gnu.org/licenses/gpl-3.0.html).  

All written content originally created by Irucka Embry is copyrighted under the Creative Commons Attribution-ShareAlike 4.0 International License. All other written content retains the copyright of the original author(s).  

<a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.  
