Skip to content

Commit

Permalink
updates and ROIs
Browse files Browse the repository at this point in the history
  • Loading branch information
kimberlylthompson committed Oct 14, 2024
1 parent 7f52582 commit 28fb814
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 4 deletions.
Binary file modified .DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions BioViewPoint.Rproj
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
15 changes: 11 additions & 4 deletions Code/01_Creating spatial BBS routes.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,15 @@ bbs_route.data_sf.point$geometry.1 <- NULL
# Create a dataframe with all the information in both the bbs routes df and the
# updated.route df but with the route shape (linestring) geometry only
bbs_route.data_sf.line <- cbind(bbs_route.data_sf, updated.route.shapefile)
bbs_route.data_sf.line$geometry <- NULL
names(bbs_route.data_sf.line)[21] <- "geometry"

# Set the geometry column to the linestring geometry
st_geometry(bbs_route.data_sf.line) <- bbs_route.data_sf.line$geometry.1

# Remove the old geometry column
bbs_route.data_sf.line$geometry.1 <- NULL

# Ensure that 'geometry' is now the active geometry column
st_geometry(bbs_route.data_sf.line) <- st_geometry(bbs_route.data_sf.line)


###############################################
Expand Down Expand Up @@ -281,10 +288,10 @@ plot(bbs_route.data_sf.point$geometry,
# Write each sf dataframe as a geo json file

st_write(bbs_route.data_sf.point,
paste(path, "/00_Data/Processed/BBS_Rtes_Point.geojson",
paste(path, "/00_Data/Processed/BBS/BBS_Rtes_Point.shp",
sep = ""))
st_write(bbs_route.data_sf.line,
paste(path, "/00_Data/Processed/BBS_Rtes_Linestring.geojson",
paste(path, "/00_Data/Processed/BBS/BBS_Rtes_Linestring.shp",
sep = ""))


148 changes: 148 additions & 0 deletions Code/03_Generating 40km ROIs.R
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 = ""))






102 changes: 102 additions & 0 deletions Code/04_LUC in ROIs.R
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)


0 comments on commit 28fb814

Please sign in to comment.