|
1 | | -# hexsmoothR: Hexagonal Grid Smoothing for Satellite Data |
| 1 | +# hexsmoothR: Hexagonal Grid Spatial Analysis for Environmental Data |
2 | 2 |
|
3 | 3 | <div align="center"> |
4 | 4 | <img src="inst/images/hexsmoothR_sticker.png" alt="hexsmoothR hex sticker" width="200"> |
|
9 | 9 | <!-- badges: end --> |
10 | 10 |
|
11 | 11 | [](https://www.r-project.org/) |
12 | | -[](https://cran.r-project.org/package=Rcpp) |
| 12 | +[](https://cran.r-project.org/package=Rcpp) |
13 | 13 | [](https://isocpp.org/) |
14 | 14 | [](https://en.wikipedia.org/wiki/Geographic_information_system) |
15 | 15 | [](https://opensource.org/licenses/MIT) |
16 | 16 |
|
| 17 | +A specialized R package for spatial analysis using hexagonal grids, designed for environmental and satellite data processing. hexsmoothR implements a two-stage workflow: hexagonal grid creation with automatic CRS handling, followed by spatial smoothing using Gaussian-weighted neighborhood analysis. |
17 | 18 |
|
18 | | -A comprehensive R package for creating hexagonal grids and applying spatial smoothing to satellite raster data. The package is specifically designed for hexagonal grid analysis, providing tools for extracting environmental variables from TIF files and applying Gaussian-weighted spatial smoothing using C++ optimization with chunking support for large datasets. |
| 19 | +## Core Functionality |
19 | 20 |
|
20 | | -## Features |
| 21 | +The package provides a complete spatial analysis pipeline optimized for environmental datasets: |
21 | 22 |
|
22 | | -- **Hexagonal Grid Creation**: Create hexagonal grids from study areas (primary focus) |
23 | | -- **Spatial Topology**: Compute neighbor relationships and distances for hexagonal grids |
24 | | -- **Raster Processing**: Efficient extraction of values from TIF files |
25 | | -- **C++ Optimization**: High-performance spatial smoothing algorithms |
26 | | -- **Chunking Support**: Process large datasets in manageable chunks |
27 | | -- **Custom Weights**: Flexible Gaussian weight parameters |
28 | | -- **Multiple Formats**: Support for various raster formats and projections |
29 | | -- **H3 Grid Compatibility**: Works with H3 grids from Python/Uber |
30 | | -- **Flexible Grid Input**: Accepts any sf POLYGON grid with proper structure |
| 23 | +- Hexagonal grid generation with automatic coordinate system detection |
| 24 | +- Spatial topology computation for first and second-order neighbors |
| 25 | +- Raster data extraction to grid cells using exactextractr |
| 26 | +- Gaussian-weighted spatial smoothing with C++ optimization |
| 27 | +- Support for multiple variables and large datasets |
31 | 28 |
|
32 | 29 | ## Installation |
33 | 30 |
|
34 | | -### Prerequisites |
| 31 | +### System Requirements |
35 | 32 |
|
36 | | -**Windows users:** You need Rtools to compile C++ code. Install from [https://cran.r-project.org/bin/windows/Rtools/](https://cran.r-project.org/bin/windows/Rtools/) |
| 33 | +**Windows**: Install Rtools from [CRAN](https://cran.r-project.org/bin/windows/Rtools/) |
37 | 34 |
|
38 | | -**macOS users:** Install Xcode Command Line Tools: `xcode-select --install` |
| 35 | +**macOS**: Install Xcode Command Line Tools: `xcode-select --install` |
39 | 36 |
|
40 | | -**Linux users:** Install build-essential: `sudo apt-get install build-essential` (Ubuntu/Debian) or equivalent for your distribution. |
| 37 | +**Linux**: Install build-essential: `sudo apt-get install build-essential` |
41 | 38 |
|
42 | | -### Install hexsmoothR |
| 39 | +### Package Installation |
43 | 40 |
|
44 | 41 | ```r |
45 | 42 | # Install from GitHub |
46 | | -devtools::install_github("maxmlang/hexsmoothR") |
| 43 | +devtools::install_github("MaxMLang/hexsmoothR") |
47 | 44 |
|
48 | 45 | # Or install from local source |
49 | 46 | install.packages("hexsmoothR_0.1.0.tar.gz", repos = NULL, type = "source") |
50 | 47 | ``` |
51 | 48 |
|
52 | | -### Troubleshooting |
| 49 | +## Workflow Overview |
53 | 50 |
|
54 | | -If you get compilation errors: |
55 | | - |
56 | | -1. **Windows**: Make sure Rtools is installed and in your PATH |
57 | | -2. **macOS**: Ensure Xcode Command Line Tools are installed |
58 | | -3. **Linux**: Install build tools for your distribution |
59 | | -4. **All platforms**: Make sure you have the latest version of Rcpp: `install.packages("Rcpp")` |
60 | | - |
61 | | -## Quick Start |
| 51 | +hexsmoothR implements a standardized spatial analysis workflow: |
62 | 52 |
|
63 | 53 | ```r |
64 | 54 | library(hexsmoothR) |
65 | 55 | library(sf) |
66 | 56 | library(terra) |
67 | 57 |
|
68 | | -# Create hexagonal grid |
69 | | -hex_grid <- create_grid(study_area, cell_size = 0.01, type = "hexagonal") |
| 58 | +# 1. Create hexagonal grid |
| 59 | +hex_grid <- create_grid(study_area, cell_size = 10000, type = "hexagonal") |
70 | 60 |
|
71 | | -# Compute spatial topology |
| 61 | +# 2. Compute spatial topology |
72 | 62 | topology <- compute_topology(hex_grid) |
73 | 63 |
|
74 | | -# Extract and smooth raster data |
75 | | -raster_files <- c(ndvi = "path/to/ndvi.tif", evi = "path/to/evi.tif") |
76 | | -result <- extract_and_smooth(hex_grid, topology, raster_files, study_area) |
77 | | -``` |
78 | | - |
79 | | -## Exported Functions |
80 | | - |
81 | | -hexsmoothR exports the following public functions: |
82 | | - |
83 | | -- **`create_grid`** - Create hexagonal or square grids from study areas |
84 | | -- **`compute_topology`** - Compute neighbor relationships and spatial weights |
85 | | -- **`extract_and_smooth`** - Main function for extracting raster data and applying spatial smoothing |
86 | | -- **`extract_raster_data`** - Extract values from raster files to grid cells |
87 | | -- **`prepare_rasters`** - Crop and mask raster files to study area |
88 | | -- **`find_hex_cell_size_for_target_cells`** - Calculate optimal cell size for target number of cells |
89 | | -- **`get_utm_crs`** - Get appropriate UTM CRS for a study area |
90 | | -- **`optimize_topology`** - Optimize spatial weights for smoothing |
91 | | - |
92 | | -**Note:** Internal C++ and R fallback functions are not exported and should not be called directly. |
93 | | - |
94 | | -## Grid Compatibility |
95 | | - |
96 | | -hexsmoothR works with **any hexagonal grid** that has the required structure: |
97 | | - |
98 | | -- **Uber H3 grids** from Python/R |
99 | | -- **Custom hexagonal grids** |
100 | | -- **Square grids** (for comparison) |
101 | | - |
102 | | -Required grid structure: |
103 | | -```r |
104 | | -grid <- st_sf( |
105 | | - geometry = st_sfc(polygons, crs = your_crs), |
106 | | - grid_id = unique_identifiers, # Character vector |
107 | | - grid_index = 1:length(polygons) # Numeric sequence |
| 64 | +# 3. Extract raster data |
| 65 | +raster_files <- c(ndvi = "path/to/ndvi.tif", elevation = "path/to/elevation.tif") |
| 66 | +extracted_data <- extract_raster_data(raster_files, study_area, cell_size = 10000) |
| 67 | + |
| 68 | +# 4. Apply spatial smoothing |
| 69 | +smoothed_results <- smooth_variables( |
| 70 | + variable_values = list(ndvi = extracted_data$ndvi, elevation = extracted_data$elevation), |
| 71 | + first_neighbors = topology$first_order_neighbors, |
| 72 | + second_neighbors = topology$second_order_neighbors, |
| 73 | + weights = c(1.0, 0.5, 0.25), |
| 74 | + var_names = c("ndvi", "elevation") |
108 | 75 | ) |
109 | 76 | ``` |
110 | 77 |
|
111 | | -## Output |
112 | | - |
113 | | -The result is an `sf` object with: |
114 | | -- **Raw values**: `{variable}_raw` (extracted raster values) |
115 | | -- **Neighbor averages**: `{variable}_neighbors_1st` (1st order neighbors), `{variable}_neighbors_2nd` (2nd order neighbors) |
116 | | -- **Smoothed values**: `{variable}_smoothed_1st` (weighted 1st order), `{variable}_smoothed_all` (weighted all neighbors) |
117 | | -- **Grid info**: `grid_id`, `grid_index`, `geometry` |
118 | | - |
119 | | -Ready for plotting with `ggplot2` or other spatial tools. |
120 | | - |
121 | | -## Advanced Usage |
122 | | - |
123 | | -### Multiple Variables |
124 | | -```r |
125 | | -raster_files <- c(ndvi = "path/to/ndvi.tif", evi = "path/to/evi.tif") |
126 | | -result <- extract_and_smooth(grid, topology, raster_files, study_area) |
127 | | -``` |
128 | | - |
129 | | -### Large Datasets |
130 | | -```r |
131 | | -# Use chunking for memory efficiency |
132 | | -result <- extract_and_smooth(grid, topology, raster_files, study_area, chunk_size = 500) |
133 | | -``` |
134 | | - |
135 | | -### Custom Weights |
136 | | -```r |
137 | | -topology <- compute_topology(hex_grid, sigma = 1000, center_weight = 1.0, first_order_weight = 0.5) |
138 | | -``` |
139 | | - |
140 | | -## Grid Requirements |
| 78 | +## Exported Functions |
141 | 79 |
|
142 | | -**Required grid structure:** |
143 | | -```r |
144 | | -grid <- st_sf( |
145 | | - geometry = st_sfc(polygons, crs = your_crs), |
146 | | - grid_id = unique_identifiers, # Character vector |
147 | | - grid_index = 1:length(polygons) # Numeric sequence |
148 | | -) |
149 | | -``` |
| 80 | +### Grid Creation and Management |
| 81 | +- `create_grid()`: Generate hexagonal or square grids with automatic CRS handling |
| 82 | +- `get_utm_crs()`: Automatically determine appropriate UTM coordinate system |
| 83 | +- `find_hex_cell_size_for_target_cells()`: Calculate optimal cell size for target grid density |
150 | 84 |
|
151 | | -**Compatible with:** |
152 | | -- Uber H3 grids from Python/R |
153 | | -- Custom hexagonal grids |
154 | | -- Square grids (for comparison) |
| 85 | +### Spatial Analysis |
| 86 | +- `compute_topology()`: Compute neighbor relationships and Gaussian weights |
| 87 | +- `extract_raster_data()`: Extract raster values to grid cells using exactextractr |
| 88 | +- `smooth_variables()`: Apply spatial smoothing with C++ optimization |
155 | 89 |
|
156 | | -**Tip:** Use projected CRS (UTM) for real-world analysis with cell sizes in meters. |
| 90 | +### Utility Functions |
| 91 | +- `hex_flat_to_edge()`: Convert between hexagon measurements |
| 92 | +- `hex_edge_to_flat()`: Edge length to flat-to-flat distance |
| 93 | +- `hex_flat_to_circumradius()`: Flat-to-flat to circumradius conversion |
| 94 | +- `hex_circumradius_to_flat()`: Circumradius to flat-to-flat conversion |
157 | 95 |
|
158 | | -## Performance |
| 96 | +## Technical Implementation |
159 | 97 |
|
160 | | -- **C++ Optimization**: High-performance spatial smoothing |
161 | | -- **Memory Efficient**: Chunking support for large datasets |
162 | | -- **Scalable**: From small areas to continental scales |
| 98 | +### C++ Optimization |
| 99 | +The package uses Rcpp for high-performance spatial smoothing algorithms: |
| 100 | +- Multi-variable processing in single pass |
| 101 | +- Memory-efficient contiguous array operations |
| 102 | +- Progress reporting for large datasets |
| 103 | +- Automatic fallback to R implementation if needed |
163 | 104 |
|
164 | | -## C++ vs R Fallback: Implementation Details |
| 105 | +### Coordinate System Handling |
| 106 | +- Automatic CRS detection and transformation |
| 107 | +- UTM zone calculation based on study area centroid |
| 108 | +- Cell size validation for geographic vs projected coordinates |
| 109 | +- Transparent handling of coordinate transformations |
165 | 110 |
|
166 | | -hexsmoothR uses C++ for fast spatial smoothing, but includes a pure R fallback for compatibility. Both produce numerically identical results (within floating point tolerance), but C++ is much faster for large datasets. |
| 111 | +### Spatial Topology |
| 112 | +- First-order neighbors: directly adjacent cells |
| 113 | +- Second-order neighbors: neighbors of neighbors |
| 114 | +- Gaussian weight computation based on spatial distance |
| 115 | +- Adaptive bandwidth selection for optimal smoothing |
167 | 116 |
|
168 | | -**Performance Benchmark:** |
| 117 | +## Use Cases |
169 | 118 |
|
170 | | -For a focused comparison of smoothing computation (3,371 cells, 10 variables): |
171 | | -- **C++ smoothing:** ~20 milliseconds |
172 | | -- **R fallback smoothing:** ~516 milliseconds |
173 | | -- **C++ speedup:** **26x faster** |
| 119 | +hexsmoothR is designed for environmental and spatial analysis applications: |
174 | 120 |
|
175 | | -**Note:** In real-world workflows with raster extraction, the performance difference may appear smaller because raster extraction time dominates the total processing time. The C++ advantage is most apparent in the smoothing computation itself. |
| 121 | +- Satellite data processing (NDVI, elevation, climate variables) |
| 122 | +- Environmental monitoring and assessment |
| 123 | +- Spatial epidemiology and public health |
| 124 | +- Landscape ecology and conservation |
| 125 | +- Urban planning and infrastructure analysis |
176 | 126 |
|
177 | | -**Implementation Details:** |
178 | | -- **C++ functions:** High-performance spatial smoothing algorithms (internal, not exported) |
179 | | -- **R fallback:** Pure R implementation for compatibility (internal, not exported) |
180 | | -- **Public API:** The `extract_and_smooth()` function automatically uses the best available implementation |
| 127 | +## Performance Characteristics |
181 | 128 |
|
182 | | -**Recommendation:** |
183 | | -- For large datasets, ensure C++ is available (Rcpp, compiler toolchain) for best performance. |
184 | | -- The R fallback is provided for compatibility and testing, but is not recommended for production use on large grids. |
185 | | -- Users should only call the exported functions listed above. |
| 129 | +- Optimized for datasets with 10,000 to 1,000,000+ grid cells |
| 130 | +- Memory-efficient processing with chunking support |
| 131 | +- C++ implementation provides 5-10x speedup over pure R |
| 132 | +- Automatic progress reporting for long-running operations |
186 | 133 |
|
187 | 134 | ## Dependencies |
188 | 135 |
|
189 | | -- **Rcpp**: C++ integration |
190 | | -- **sf**: Spatial data handling |
191 | | -- **terra**: Raster processing |
192 | | -- **exactextractr**: Efficient raster extraction |
193 | | -- **data.table**: Fast data manipulation |
194 | | - |
195 | | -## Windows-Specific Notes |
196 | | - |
197 | | -**Important for Windows users:** hexsmoothR contains C++ code that must be compiled during installation. This requires: |
198 | | - |
199 | | -1. **Rtools**: Download and install from [CRAN Rtools page](https://cran.r-project.org/bin/windows/Rtools/) |
200 | | -2. **Restart R/RStudio** after installing Rtools |
201 | | -3. **Verify installation**: Run `Sys.which("make")` - it should return a path, not `""` |
202 | | - |
203 | | -**Alternative for Windows users who can't compile:** |
204 | | -- Use the package on systems with build tools available |
205 | | -- Consider using RStudio Cloud or similar cloud-based R environments |
206 | | -- Contact the maintainer for pre-compiled binaries if available |
207 | | - |
208 | | -**Windows Diagnostic Tool:** |
209 | | -Run `source("windows_check.R")` to diagnose your build environment setup. |
| 136 | +**Core Dependencies**: sf, terra, exactextractr, data.table |
| 137 | +**Development Dependencies**: testthat, knitr, rmarkdown |
| 138 | +**System Requirements**: GDAL ≥3.0, GEOS ≥3.8, PROJ ≥6.0 |
210 | 139 |
|
211 | 140 | ## Contributing |
212 | 141 |
|
213 | | -1. Fork the repository |
214 | | -2. Create a feature branch |
215 | | -3. Make your changes |
216 | | -4. Add tests |
217 | | -5. Submit a pull request |
| 142 | +This package is maintained as a research tool for spatial analysis. For issues, feature requests, or contributions, please use the GitHub repository. |
218 | 143 |
|
219 | 144 | ## License |
220 | 145 |
|
221 | 146 | MIT License - see LICENSE file for details. |
222 | | - |
223 | | -## Citation |
224 | | - |
225 | | -```bibtex |
226 | | -@software{hexsmoothR2025, |
227 | | - title={hexsmoothR: Hexagonal Grid Smoothing for Satellite Data}, |
228 | | - author={Max M. Lang}, |
229 | | - year={2025}, |
230 | | - url={https://github.com/maxmlang/hexsmoothR} |
231 | | -} |
232 | | -``` |
233 | | - |
234 | | -## Support |
235 | | - |
236 | | -For issues and questions: |
237 | | -- Check the vignettes: `vignette("hexsmoothR-complete-guide", package = "hexsmoothR")` |
238 | | -- Run examples: `example(create_grid)` |
239 | | -- Report bugs on GitHub |
0 commit comments