@@ -94,7 +94,7 @@ Spatial topology determines smoothing algorithm behavior. Each hexagon receives
9494
9595## Step 1: Library Loading and Data Preparation
9696
97- ``` {r load-libraries}
97+ ``` {r load-libraries, eval = TRUE }
9898library(hexsmoothR)
9999library(sf)
100100library(terra)
@@ -151,11 +151,9 @@ if (file.exists(sentinel_file)) {
151151 true_coastal_vegetation[true_coastal_vegetation > 0.8] <- 0.8
152152}
153153
154-
155154# Use original data for hexagonal smoothing demonstration
156155observed_coastal_vegetation <- true_coastal_vegetation
157156
158-
159157# Data summary
160158if (file.exists(sentinel_file)) {
161159 cat("Sentinel-2 L2A NDVI data loaded\n")
@@ -168,7 +166,6 @@ cat("CRS:", crs(observed_coastal_vegetation, proj = TRUE), "\n")
168166missing_pct <- round(global(is.na(observed_coastal_vegetation), "mean")$mean * 100, 1)
169167cat("Missing values:", missing_pct, "%\n")
170168
171-
172169# Create color palette for NDVI visualization
173170plot_colors <- colorRampPalette(c("darkred", "red", "orange", "yellow", "green", "darkgreen"))(100)
174171cat("Color palette: 100 colors (red to green)\n")
@@ -192,7 +189,7 @@ if (file.exists(sentinel_file)) {
192189 plot_title_2 <- "Observed Coastal Vegetation Data (NDVI-like)\nSimulated data"
193190}
194191
195- plot(observed_coastal_vegetation$sentinel2_ndvi_2 ,
192+ plot(observed_coastal_vegetation$sentinel2_ndvi_3 ,
196193 main = plot_title_2,
197194 col = plot_colors,
198195 axes = FALSE)
@@ -218,7 +215,7 @@ Use projected CRS (UTM) for accurate measurements.
218215
219216Calculate cell size to match target hexagon count and raster area.
220217
221- ``` {r create-grid}
218+ ``` {r create-grid, eval = TRUE }
222219# Create study area polygon from raster extent
223220study_area_wgs <- st_sf(geometry = st_sfc(
224221 st_polygon(list(matrix(
@@ -272,7 +269,7 @@ plot(st_geometry(hex_grid_wgs84), main = paste("Hexagonal Grid -", nrow(hex_grid
272269
273270### Helper Function for Optimal Cell Size
274271
275- ``` {r optimal-cell-size, eval =FALSE }
272+ ``` {r optimal-cell-size, eval = TRUE }
276273# Package suggests optimal cell size for target hexagon count
277274target_cells <- 5000
278275optimal_cell_size <- find_hex_cell_size_for_target_cells(
@@ -302,7 +299,7 @@ hex_grid <- existing_grid
302299
303300Extract raster values into hexagon cells using existing grid.
304301
305- ``` {r extract-raster}
302+ ``` {r extract-raster, eval = TRUE }
306303# Extract raster values into hexagon cells
307304# Function handles CRS transformations and pixel aggregation
308305
@@ -442,28 +439,26 @@ extracted_auto <- extract_raster_data(
442439
443440### Standard 2-Order Topology (Backward Compatible)
444441
445- ``` {r configure-topology, eval = FALSE }
442+ ``` {r configure-topology, eval = TRUE }
446443# Compute spatial topology for smoothing (default: 2 orders)
447- # Note: This example assumes you have a hex_grid object from previous steps
448- # topology <- compute_topology(hex_grid)
444+ topology <- compute_topology(hex_grid)
449445
450- # Example output structure (not executable in vignette):
451- cat("Topology computed: 100 cells\n")
452- cat("Average distance: 15000 m\n")
453- cat("Sigma (bandwidth): 7500 m\n")
446+ cat("Topology computed:", length(topology$neighbors[[1]]), "cells\n")
447+ cat("Average distance:", round(topology$avg_distance, 0), "m\n")
448+ cat("Sigma (bandwidth):", round(topology$sigma, 0), "m\n")
454449
455450cat("Gaussian weights:\n")
456- cat("Center: 0.667 \n")
457- cat("First-order: 0.222 \n")
458- cat("Second-order: 0.111 \n")
451+ cat("Center:", round(topology$weights$center_weight, 3), " \n")
452+ cat("First-order:", round(topology$weights$neighbor_weights[1], 3), " \n")
453+ cat("Second-order:", round(topology$weights$neighbor_weights[2], 3), " \n")
459454
460455# Weights applied to individual neighbors, not averages
461456cat("Weights applied to each neighbor individually\n")
462457
463458# Neighbor examples
464- cat("Cell 1: 6 1st, 12 2nd neighbors\n")
465- cat("Cell 2: 6 1st, 12 2nd neighbors\n")
466- cat("Cell 3: 6 1st, 12 2nd neighbors\n")
459+ cat("Cell 1:", length(topology$neighbors[[1]][[1]]), " 1st,", length(topology$neighbors[[2]][[1]]), " 2nd neighbors\n")
460+ cat("Cell 2:", length(topology$neighbors[[1]][[2]]), " 1st,", length(topology$neighbors[[2]][[2]]), " 2nd neighbors\n")
461+ cat("Cell 3:", length(topology$neighbors[[1]][[3]]), " 1st,", length(topology$neighbors[[2]][[3]]), " 2nd neighbors\n")
467462
468463# Note: Visualization code would go here but requires actual grid objects
469464cat("Neighborhood visualization would show:\n")
@@ -474,7 +469,7 @@ cat("- 2nd order neighbors (yellow)\n")
474469
475470### Advanced: N-Order Topology for Enhanced Smoothing
476471
477- ``` {r configure-n-order-topology, eval = FALSE }
472+ ``` {r configure-n-order-topology, eval = TRUE }
478473# Compute topology with 3 neighbor orders for more extensive smoothing
479474topology_3 <- compute_topology(hex_grid, neighbor_orders = 3)
480475
@@ -505,7 +500,7 @@ for (i in 1:min(3, length(topology_3$neighbors[[1]]))) {
505500topology_custom <- compute_topology(
506501 hex_grid,
507502 neighbor_orders = 4,
508- neighbor_weights = list(0.5, 0.3, 0.15, 0.05) # Custom decay pattern
503+ neighbor_weights_param = list(0.5, 0.3, 0.15, 0.05) # Custom decay pattern
509504)
510505
511506cat("Custom 4-order topology with manual weights\n")
@@ -528,28 +523,31 @@ Higher orders provide more extensive smoothing but increase computation time and
528523
529524### Standard 2-Order Smoothing (Backward Compatible)
530525
531- ``` {r apply-smoothing, eval = FALSE }
526+ ``` {r apply-smoothing, eval = TRUE }
532527# Apply spatial smoothing using C++-optimized function (2 orders)
533- # Note: This example assumes you have topology and extracted_values from previous steps
534- # smoothing_results <- smooth_variables(
535- # variable_values = list(coastal_vegetation = extracted_values),
536- # neighbors = topology$neighbors,
537- # weights = topology$weights,
538- # var_names = c("coastal_vegetation")
539- # )
528+ smoothing_results <- smooth_variables(
529+ variable_values = list(coastal_vegetation = extracted_values),
530+ neighbors = topology$neighbors,
531+ weights = topology$weights,
532+ var_names = c("coastal_vegetation")
533+ )
540534
541535cat("Spatial smoothing complete\n")
542- cat("Results: raw, neighbors_1st, neighbors_2nd, weighted_combined \n")
536+ cat("Results:", paste(names(smoothing_results$coastal_vegetation), collapse = ", "), " \n")
543537
544538# Example smoothing effectiveness analysis:
545539cat("Smoothing effectiveness:\n")
546- cat("Original - Mean: 0.456 SD: 0.234 \n")
540+ cat("Original - Mean:", round(mean(extracted_values, na.rm = TRUE), 3), " SD:", round(sd(extracted_values, na.rm = TRUE), 3), " \n")
547541
548542cat("Smoothed coastal vegetation values:\n")
549- cat(" Mean: 0.478\n")
550- cat(" SD: 0.189\n")
543+ cat(" Mean:", round(mean(smoothing_results$coastal_vegetation$weighted_combined, na.rm = TRUE), 3), "\n")
544+ cat(" SD:", round(sd(smoothing_results$coastal_vegetation$weighted_combined, na.rm = TRUE), 3), "\n")
545+
546+ noise_reduction <- (sd(extracted_values, na.rm = TRUE) -
547+ sd(smoothing_results$coastal_vegetation$weighted_combined, na.rm = TRUE)) /
548+ sd(extracted_values, na.rm = TRUE) * 100
551549
552- cat("Coastal vegetation noise reduction: 19.2 %\n")
550+ cat("Coastal vegetation noise reduction:", round(noise_reduction, 1), " %\n")
553551cat("(Lower standard deviation = more consistent coastal vegetation patterns)\n")
554552
555553# The smoothing results contain:
@@ -563,7 +561,7 @@ cat("\n★ weighted_combined is typically the best result for noise reduction\n"
563561
564562### Advanced: N-Order Smoothing for Enhanced Results
565563
566- ``` {r apply-n-order-smoothing, eval = FALSE }
564+ ``` {r apply-n-order-smoothing, eval = TRUE }
567565# Apply 3-order smoothing using the new N-order system
568566smoothing_results_3 <- smooth_variables(
569567 variable_values = list(coastal_vegetation = extracted_values),
@@ -595,9 +593,9 @@ cat("- neighbors_2nd:", round(mean(smoothing_results_3$coastal_vegetation$neighb
595593cat("- neighbors_3rd:", round(mean(smoothing_results_3$coastal_vegetation$neighbors_3rd, na.rm = TRUE), 3), "\n")
596594
597595# Calculate additional noise reduction from 3-order smoothing
598- noise_reduction_3 <- (sd(raw_values , na.rm = TRUE) -
596+ noise_reduction_3 <- (sd(extracted_values , na.rm = TRUE) -
599597 sd(smoothing_results_3$coastal_vegetation$weighted_combined, na.rm = TRUE)) /
600- sd(raw_values , na.rm = TRUE) * 100
598+ sd(extracted_values , na.rm = TRUE) * 100
601599
602600cat("3-Order noise reduction:", round(noise_reduction_3, 1), "%\n")
603601cat("Additional reduction over 2-order:", round(noise_reduction_3 - noise_reduction, 1), "%\n")
@@ -612,20 +610,19 @@ cat("Additional reduction over 2-order:", round(noise_reduction_3 - noise_reduct
612610
613611## Step 6: Combine Results and Analyze
614612
615- ``` {r combine-results, eval = FALSE }
613+ ``` {r combine-results, eval = TRUE }
616614# Combine the hexagonal grid with smoothing results
617- # Note: This example assumes you have hex_grid and smoothing_results from previous steps
618- # hex_grid_with_results <- hex_grid
619- # hex_grid_with_results$coastal_vegetation_raw <- smoothing_results$coastal_vegetation$raw
620- # hex_grid_with_results$coastal_vegetation_neighbors_1st <- smoothing_results$coastal_vegetation$neighbors_1st
621- # hex_grid_with_results$coastal_vegetation_neighbors_2nd <- smoothing_results$coastal_vegetation$neighbors_2nd
622- # hex_grid_with_results$coastal_vegetation_weighted_combined <- smoothing_results$coastal_vegetation$weighted_combined
615+ hex_grid_with_results <- hex_grid
616+ hex_grid_with_results$coastal_vegetation_raw <- smoothing_results$coastal_vegetation$raw
617+ hex_grid_with_results$coastal_vegetation_neighbors_1st <- smoothing_results$coastal_vegetation$neighbors_1st
618+ hex_grid_with_results$coastal_vegetation_neighbors_2nd <- smoothing_results$coastal_vegetation$neighbors_2nd
619+ hex_grid_with_results$coastal_vegetation_weighted_combined <- smoothing_results$coastal_vegetation$weighted_combined
623620
624621# Transform to WGS84 for plotting
625- # hex_grid_with_results_wgs84 <- st_transform(hex_grid_with_results, crs = 4326)
622+ hex_grid_with_results_wgs84 <- st_transform(hex_grid_with_results, crs = 4326)
626623
627624cat("Results combined with grid successfully\n")
628- cat("Available columns: grid_id, grid_index, coastal_vegetation_raw, coastal_vegetation_neighbors_1st, coastal_vegetation_neighbors_2nd, coastal_vegetation_weighted_combined \n")
625+ cat("Available columns:", paste(names(hex_grid_with_results), collapse = ", "), " \n")
629626
630627# Analyze the smoothing effect
631628cat("\n=== SMOOTHING ANALYSIS ===\n")
@@ -636,37 +633,53 @@ cat("- neighbors_2nd: Mean of second-order neighbors only\n")
636633cat("- weighted_combined: Weighted average (center + all neighbors, weights applied to each neighbor)\n\n")
637634
638635# Example analysis output:
639- cat("Original values - Count: 100 Mean: 0.4562 SD: 0.2341 \n")
640- cat("Smoothed values - Count: 100 Mean: 0.4789 SD: 0.1893 \n")
641- cat("Variance reduction: 19.1 %\n")
636+ cat("Original values - Count:", length(extracted_values), " Mean:", round(mean(extracted_values, na.rm = TRUE), 4), " SD:", round(sd(extracted_values, na.rm = TRUE), 4), " \n")
637+ cat("Smoothed values - Count:", length(smoothing_results$coastal_vegetation$weighted_combined), " Mean:", round(mean(smoothing_results$coastal_vegetation$weighted_combined, na.rm = TRUE), 4), " SD:", round(sd(smoothing_results$coastal_vegetation$weighted_combined, na.rm = TRUE), 4), " \n")
638+ cat("Variance reduction:", round(noise_reduction, 1), " %\n")
642639```
643640
644641## Step 7: Visualize the Improvement
645642
646- ``` {r visualize-results, eval = FALSE }
643+ ``` {r visualize-results, eval = TRUE }
647644# Create spectacular before/after visualization showing the power of spatial smoothing
648- # Note: This example assumes you have hex_grid_wgs84 and smoothing_results from previous steps
649-
650645cat("=== CREATING BEFORE/AFTER VISUALIZATION ===\n")
651646
652- # Example visualization workflow (not executable in vignette):
653- cat("Visualization would show:\n")
654- cat("1. Original noisy coastal vegetation raster (NDVI-like)\n")
655- cat("2. True underlying coastal vegetation pattern (for reference)\n")
656- cat("3. Spatially smoothed coastal vegetation result\n")
657- cat("4. Color legend with viridis palette\n")
647+ # Create visualization plots
648+ par(mfrow = c(2, 2))
658649
659- cat("Progression of smoothing would display:\n")
660- cat("- Raw data\n")
661- cat("- Smoothed (1st order)\n")
662- cat("- Smoothed (All orders)\n")
650+ # Plot 1: Original extracted values
651+ plot(st_geometry(hex_grid_wgs84),
652+ col = plot_colors[cut(extracted_values, 100)],
653+ main = "Original Extracted Values",
654+ border = "white", lwd = 0.01)
663655
664- cat("All visualization plots would be created successfully!\n")
665- ```
656+ # Plot 2: Smoothed values (1st order)
657+ plot(st_geometry(hex_grid_wgs84),
658+ col = plot_colors[cut(smoothing_results$coastal_vegetation$neighbors_1st, 100)],
659+ main = "Smoothed (1st Order Neighbors)",
660+ border = "white", lwd = 0.01)
666661
662+ # Plot 3: Smoothed values (2nd order)
663+ plot(st_geometry(hex_grid_wgs84),
664+ col = plot_colors[cut(smoothing_results$coastal_vegetation$neighbors_2nd, 100)],
665+ main = "Smoothed (2nd Order Neighbors)",
666+ border = "white", lwd = 0.01)
667667
668+ # Plot 4: Final weighted combined result
669+ plot(st_geometry(hex_grid_wgs84),
670+ col = plot_colors[cut(smoothing_results$coastal_vegetation$weighted_combined, 100)],
671+ main = "Final Weighted Combined Result",
672+ border = "white", lwd = 0.01)
668673
669- ## Step 9: Working with Multiple Variables
674+ cat("All visualization plots created successfully!\n")
675+ cat("Progression shows:\n")
676+ cat("- Top left: Raw extracted data\n")
677+ cat("- Top right: 1st order neighbor smoothing\n")
678+ cat("- Bottom left: 2nd order neighbor smoothing\n")
679+ cat("- Bottom right: Final weighted combined result\n")
680+ ```
681+
682+ ## Step 8: Working with Multiple Variables
670683
671684``` {r multiple-variables, eval = FALSE}
672685# You can work with multiple raster files and variables
@@ -701,9 +714,9 @@ smoothing_multi <- smooth_variables(
701714cat("Variables processed:", paste(names(smoothing_multi), collapse = ", "), "\n")
702715```
703716
704- ## Step 10 : Hexagon Measurement Utilities
717+ ## Step 9 : Hexagon Measurement Utilities
705718
706- ``` {r hexagon-utilities}
719+ ``` {r hexagon-utilities, eval = TRUE }
707720# The package provides helper functions for hexagon measurements
708721# IMPORTANT: cell_size_flat is in METERS (UTM coordinates)
709722cell_size_flat <- 20000 # 20km flat-to-flat distance in meters
@@ -759,16 +772,19 @@ This vignette demonstrates the complete hexsmoothR workflow:
759772### Understanding Units and CRS
760773
761774** Coordinate Reference Systems:**
775+
762776- ** UTM coordinates** : Use meters for cell sizes (e.g., 20000 for 20km hexagons)
763777- ** WGS84 coordinates** : Use degrees for cell sizes (e.g., 0.1 for ~ 11km hexagons)
764778- ** Functions automatically convert** between CRS when needed
765779
766780** Cell Size Units:**
781+
767782- ** ` cell_size ` parameter** : Always in the units of your study area's CRS
768783- ** UTM zones** : cell_size in meters (recommended for real-world analysis)
769784- ** Geographic coordinates** : cell_size in degrees (use with caution for large areas)
770785
771786** Smoothing Weights:**
787+
772788- ** Weights apply to each individual neighbor** , not to neighbor means
773789- ** Center weight** : Influence of the cell itself
774790- ** First-order weight** : Influence of each immediate neighbor
0 commit comments