generated from CU-ESIIL/Education_OASIS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f52582
commit 28fb814
Showing
5 changed files
with
274 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Version: 1.0 | ||
|
||
RestoreWorkspace: Default | ||
SaveWorkspace: Default | ||
AlwaysSaveHistory: Default | ||
|
||
EnableCodeIndexing: Yes | ||
UseSpacesForTab: Yes | ||
NumSpacesForTab: 2 | ||
Encoding: UTF-8 | ||
|
||
RnwWeave: Sweave | ||
LaTeX: pdfLaTeX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
############################################################################### | ||
######## ######## | ||
######## Developing 40x40km Regions of Interest ######## | ||
######## Around Each BBS Route ######## | ||
############################################################################### | ||
|
||
# Author: Kimberly Thompson | ||
|
||
# This code generates square polygons around each BBS route, using the midpoint | ||
# of each linestring, such that the midpoint is 20 km from each side of the | ||
# square. | ||
|
||
|
||
########## clean workspace and load required packages #################### | ||
|
||
# clean workspace to improve efficiency: # | ||
rm(list = ls() ) | ||
gc() #releases memory | ||
|
||
library(tidyverse) # Data cleaning | ||
library(sf) | ||
|
||
|
||
############################################### | ||
### ### | ||
### Data Loading ### | ||
### ### | ||
############################################### | ||
|
||
# Set path to Bioviewpoint folder (where data resides) | ||
# path <- "~/Library/CloudStorage/[email protected]/.shortcut-targets-by-id/1HhR4gs3fKXyAXBhQom6t69gAqR6SeKhx/BioViewPoint" | ||
# path <- "~/share/groups/MAS/04_personal/Kim_T/BioViewPoint" | ||
path <- "I:/mas/04_personal/Kim_T/BioViewPoint" | ||
|
||
|
||
# Load the spatial linestring file | ||
spatial.data <- sf :: st_read(paste(path, | ||
"/00_Data/Processed/BBS/BBS_Rtes_Linestring.shp", | ||
sep = "")) | ||
|
||
# Load USA and Canada shapefile | ||
usa.shape <- sf :: st_read(paste(path, "/00_Data/Raw/USACANAB.shp", | ||
sep = "")) | ||
|
||
# Define the coordinate system | ||
albers = sp:: CRS("+init=epsg:5070") | ||
|
||
# Define coordintate system as albers: | ||
usa.shape <- sf :: st_set_crs( usa.shape, albers ) | ||
|
||
|
||
############################################### | ||
### ### | ||
### Calculating Midpoints ### | ||
### ### | ||
############################################### | ||
|
||
# Use st_length to get the total length of each linestring | ||
# each is approximately 40km but will vary | ||
# Use st_line_interpolate with half the length of each line | ||
# to find the midpoint | ||
|
||
# Calculate midpoints | ||
midpoints <- spatial.data %>% | ||
# Calculate length of each linestring | ||
mutate(length = st_length(geometry), | ||
# Find midpoint | ||
midpoint = st_line_interpolate(geometry, length / 2)) %>% | ||
select(midpoint) | ||
|
||
############################################### | ||
### ### | ||
### Generating Squares ### | ||
### ### | ||
############################################### | ||
|
||
# Define the side length of the square (40km) | ||
side_length <- 40000 # in meters | ||
|
||
# Define function to create a square polygon around each point | ||
create_square <- function(point, side_length) { | ||
|
||
half_side <- side_length / 2 | ||
|
||
# Create a matrix of corner points for the square | ||
coords <- matrix(c( | ||
st_coordinates(point)[1] - half_side, st_coordinates(point)[2] - half_side, | ||
st_coordinates(point)[1] + half_side, st_coordinates(point)[2] - half_side, | ||
st_coordinates(point)[1] + half_side, st_coordinates(point)[2] + half_side, | ||
st_coordinates(point)[1] - half_side, st_coordinates(point)[2] + half_side, | ||
# Closing the square | ||
st_coordinates(point)[1] - half_side, st_coordinates(point)[2] - half_side), | ||
ncol = 2, byrow = TRUE) | ||
|
||
# Create a polygon from the coordinates | ||
polygon <- st_polygon(list(coords)) | ||
|
||
return(st_sfc(polygon, crs = st_crs(spatial.data))) | ||
|
||
} | ||
|
||
# Apply the function to create squares for each point | ||
squares <- midpoints %>% | ||
rowwise() %>% | ||
mutate(square = list(create_square(midpoint, side_length))) %>% | ||
unnest(square) %>% | ||
select(square) | ||
|
||
# Convert squares to sf object | ||
squares_sf <- st_sf(squares) | ||
|
||
|
||
############################################### | ||
### ### | ||
### Housekeeping ### | ||
### ### | ||
############################################### | ||
|
||
|
||
# Create an ID column for both dataframes | ||
squares_sf <- squares_sf %>% | ||
mutate(id = row_number()) # Create an ID for squares_sf | ||
|
||
spatial.data <- spatial.data %>% | ||
mutate(id = row_number()) %>% # Create an ID for spatial.data | ||
select(1:20, id) # Select only the first 20 columns and the ID | ||
|
||
# Combine the dataframes by binding columns | ||
combined_data <- bind_cols(squares_sf, spatial.data) | ||
|
||
# Set squares as the active geometry | ||
st_geometry(combined_data) <- combined_data$square | ||
|
||
# Remove unwanted columns (e.g., LINESTRING and geometry.1) | ||
combined_data <- combined_data %>% | ||
select(-c(2, 3, 24, 25)) | ||
|
||
|
||
# Write the new sf object | ||
st_write(combined_data, | ||
paste(path, "/00_Data/Processed/BBS/40km ROIs.shp", | ||
sep = "")) | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
############################################################################### | ||
######## ######## | ||
######## Calculating Amount of Land-use Change ######## | ||
######## Around Each BBS Route's 40km Region of Interest ######## | ||
############################################################################### | ||
|
||
# Author: Kimberly Thompson | ||
|
||
# This code reclassfies land-use change from type of change (e.g., habitat | ||
# type 1 to habitat type 2) to a presence/absences of change and then | ||
# the sum of presences from 2001 to 2021 to identify regions of interest | ||
# with the highest and loweest change. | ||
|
||
|
||
########## clean workspace and load required packages #################### | ||
|
||
# clean workspace to improve efficiency: # | ||
rm(list = ls() ) | ||
gc() #releases memory | ||
|
||
library(tidyverse) # Data cleaning | ||
library(sf) | ||
library(terra) | ||
|
||
############################################### | ||
### ### | ||
### Data Loading ### | ||
### ### | ||
############################################### | ||
|
||
# Set path to Bioviewpoint folder (where data resides) | ||
path <- "~/Library/CloudStorage/[email protected]/.shortcut-targets-by-id/1HhR4gs3fKXyAXBhQom6t69gAqR6SeKhx/BioViewPoint" | ||
# path <- "~/share/groups/MAS/04_personal/Kim_T/BioViewPoint" | ||
# path <- "I:/mas/04_personal/Kim_T/BioViewPoint" | ||
|
||
# Load the square polygons (ROIs) | ||
bbs.roi <- sf :: st_read(paste(path, | ||
"/00_Data/Processed/BBS/40km ROIs.shp", | ||
sep = "")) | ||
|
||
# Load the National Land Cover Database Change | ||
nlcd <- terra :: rast(paste(path, | ||
"/00_Data/Raw/National Land Cover Database/nlcd_2001_2021_land_cover_change_index_l48_20230630.img", | ||
sep = "")) | ||
|
||
|
||
############################################### | ||
### ### | ||
### Reclassify Land Use Change Raster ### | ||
### ### | ||
############################################### | ||
|
||
# We want to know primarily, which areas have experienced the most (and least) | ||
# change, as opposed to knowing the specific types of change. | ||
# Because the goal is to filter the BBS routes based on amounts of change. | ||
|
||
# Display categories | ||
levels(nlcd) | ||
|
||
# value Class_Names | ||
# 1 0 | ||
# 2 1 no change | ||
# 3 2 water change | ||
# 4 3 urban change | ||
# 5 4 wetland within class change | ||
# 6 5 herbaceous wetland change | ||
# 7 6 agriculture within class change | ||
# 8 7 cultivated crop change | ||
# 9 8 hay/pasture change | ||
# 10 9 rangeland herbaceous and shrub change | ||
# 11 10 barren change | ||
# 12 11 forest change | ||
# 13 12 woody wetland change | ||
# 14 13 snow change | ||
# And numbers above with no cat name | ||
|
||
# Create a reclassification matrix | ||
# This matrix tells R to: | ||
# Reclassify value 1 to 0 | ||
# Reclassify values 2 through 13 to 1 | ||
# Reclassify values 14 and above to 0 | ||
rcl <- matrix(c(1, 1, 0, | ||
2, 13, 1, | ||
14, Inf, 0), | ||
ncol=3, byrow=TRUE) | ||
|
||
# Apply the reclassification | ||
nlcd_reclass <- classify(nlcd, rcl) | ||
|
||
# Define the levels of the reclassification | ||
levels(nlcd_reclass) <- data.frame(id=c(0,1), class=c("None", "Target")) | ||
|
||
|
||
|
||
|
||
|
||
# Define the coordinate system | ||
albers = sp:: CRS("+init=epsg:5070") | ||
|
||
crs(nlcd) == crs(bbs.roi) | ||
|
||
|