-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombine.R
More file actions
176 lines (142 loc) · 5.27 KB
/
combine.R
File metadata and controls
176 lines (142 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# combine.R
# combine scaled and cropped geotiffs together, in a single plot, applying a consistent CRS.
# Load required libraries
library(terra)
library(sf)
library(ggplot2)
library(maps)
library(dplyr)
library(tidyr)
library(terrainr)
# Create images directory if it doesn't exist
if (!dir.exists("images")) {
dir.create("images")
}
# Get all _scaled_cropped.tif files from the output folder
tif_files <- list.files("output", pattern = "_scaled_cropped\\.tif$", full.names = TRUE)
# Check if any files were found
if (length(tif_files) == 0) {
stop("No _scaled_cropped.tif files found in the output folder")
}
cat("Found", length(tif_files), "raster files\n")
# Load all raster files
raster_list <- lapply(tif_files, rast)
# Extract file names for labels
file_labels <- basename(tif_files)
file_labels <- gsub("_scaled_cropped\\.tif$", "", file_labels)
# Set CRS to UTM Zone 11N for all rasters (appropriate for California)
utm_crs <- "EPSG:32611"
cat("Setting CRS to UTM Zone 11N for all rasters...\n")
for (i in 1:length(raster_list)) {
crs(raster_list[[i]]) <- utm_crs
}
# Convert to WGS84 for mapping
wgs84_crs <- "EPSG:4326"
cat("Projecting all rasters to WGS84...\n")
projected_rasters <- list()
for (i in 1:length(raster_list)) {
cat("Projecting", file_labels[i], "...\n")
projected_rasters[[i]] <- project(raster_list[[i]], wgs84_crs, method = "near")
}
# Convert rasters to data frames for ggplot
cat("Converting rasters to data frames...\n")
raster_dfs <- list()
is_rgb_list <- logical(length(projected_rasters))
for (i in 1:length(projected_rasters)) {
cat("Converting", file_labels[i], "...\n")
# Convert raster to data frame
df <- as.data.frame(projected_rasters[[i]], xy = TRUE)
# Check the number of bands and set appropriate column names
num_bands <- ncol(df) - 2 # subtract x and y columns
cat(" Number of bands:", num_bands, "\n")
if (num_bands == 3) {
# RGB data
colnames(df) <- c("x", "y", "r", "g", "b")
is_rgb_list[i] <- TRUE
# Remove NA values for RGB data
df <- df[!is.na(df$r) & !is.na(df$g) & !is.na(df$b), ]
} else if (num_bands == 1) {
# Single band data
colnames(df) <- c("x", "y", "value")
is_rgb_list[i] <- FALSE
# Remove NA values
df <- df[!is.na(df$value), ]
} else {
# Multiple bands but not RGB - use first band
colnames(df)[1:2] <- c("x", "y")
colnames(df)[3] <- "value"
is_rgb_list[i] <- FALSE
# Remove NA values
df <- df[!is.na(df$value), ]
cat(" Warning: Multiple bands detected, using first band for visualization\n")
}
# Add dataset name
df$dataset <- file_labels[i]
raster_dfs[[i]] <- df
}
# No coastline background needed
# Calculate the combined extent of all bounding boxes for zooming
all_extents <- lapply(projected_rasters, ext)
combined_extent <- ext(
min(sapply(all_extents, function(x) x[1])), # xmin (longitude)
max(sapply(all_extents, function(x) x[2])), # xmax (longitude)
min(sapply(all_extents, function(x) x[3])), # ymin (latitude)
max(sapply(all_extents, function(x) x[4])) # ymax (latitude)
)
# Add some buffer around the bounding boxes for better visualization
buffer <- 0.02 # degrees
xlim <- c(combined_extent[1] - buffer, combined_extent[2] + buffer)
ylim <- c(combined_extent[3] - buffer, combined_extent[4] + buffer)
# Create the plot
cat("Creating plot with separate raster layers...\n")
p <- ggplot()
# Add each raster as a separate layer
for (i in 1:length(raster_dfs)) {
cat("Adding raster layer for", file_labels[i], "...\n")
if (is_rgb_list[i]) {
# RGB data - use geom_spatial_rgb
p <- p + geom_spatial_rgb(data = raster_dfs[[i]],
mapping = aes(x = x, y = y, r = r, g = g, b = b))
} else {
# Single band data - use geom_tile
p <- p + geom_tile(data = raster_dfs[[i]],
aes(x = x, y = y, fill = value))
}
}
# Continue building the plot
p <- p +
# Add color scale only if we have non-RGB data
{if (any(!is_rgb_list)) {
scale_fill_gradientn(colors = terrain.colors(100),
name = "Raster\nValue",
na.value = "transparent")
}} +
# Set coordinate system and zoom to Santa Barbara region
coord_sf(crs = st_crs(4326),
xlim = xlim,
ylim = ylim) +
# Add titles and labels
labs(title = "Combined Dibblee Maps -- Santa Barbara Region",
x = "Longitude",
y = "Latitude") +
# Theme
theme_minimal() +
theme(panel.grid = element_line(color = "white", linewidth = 0.2),
panel.background = element_rect(fill = "white"),
legend.position = "bottom",
plot.title = element_text(face = "bold", size = 14),
plot.subtitle = element_text(size = 12))
# Save the plot
ggsave("images/combined.png", plot = p, width = 12, height = 10, dpi = 300)
# Print summary information
cat("Bounding box summary:\n")
cat("====================\n")
for (i in 1:length(projected_rasters)) {
e <- ext(projected_rasters[[i]])
cat("Dataset:", file_labels[i], "\n")
cat(" Longitude:", round(e[1], 4), "to", round(e[2], 4), "\n")
cat(" Latitude:", round(e[3], 4), "to", round(e[4], 4), "\n")
cat("\n")
}
cat("Multiple raster layers plot saved to: images/combined.png\n")
cat("Successfully processed", length(raster_list), "raster files as separate layers\n")