if (FALSE) {
# These examples require satellite imagery files (Landsat/Sentinel data etc.)
# =================================================================
# MOST COMMON USE CASE: Extract raster values to CSV points
# =================================================================
# Your typical workflow: CSV file with coordinates + raster file
results <- universal_spatial_join(
source_data = "my_field_sites.csv", # CSV with lon, lat columns
target_data = "satellite_image.tif", # Any raster file
method = "extract", # Extract raster values to points
buffer_distance = 100, # 100m buffer around each point
summary_function = "mean", # Average within buffer
verbose = TRUE # See what's happening
)
# Check results - original data + new columns with raster values
head(results)
# site_id lon lat geometry extracted_satellite_image
# 1 1 -83.12345 40.12345 POINT (-83.1 40.1) 0.752
# 2 2 -83.23456 40.23456 POINT (-83.2 40.2) 0.681
# 3 3 -83.34567 40.34567 POINT (-83.3 40.3) 0.594
# Access the extracted values
results$extracted_satellite_image
# =================================================================
# ZONAL STATISTICS: Calculate statistics by polygon areas
# =================================================================
# Calculate average precipitation by watershed
watershed_precip <- universal_spatial_join(
source_data = "precipitation_raster.tif", # Raster data
target_data = "watershed_boundaries.shp", # Polygon boundaries
method = "zonal", # Calculate zonal statistics
summary_function = "mean", # Average precipitation per watershed
verbose = TRUE
)
# Result: polygons with precipitation statistics
head(watershed_precip)
# watershed_id geometry zonal_mean_precipitation_raster
# 1 1 POLYGON ((-84.2 40.1, ...)) 42.3
# 2 2 POLYGON ((-84.5 40.3, ...)) 38.7
# =================================================================
# RESAMPLE RASTER: Change resolution or align rasters
# =================================================================
# Aggregate 30m Landsat to 250m MODIS resolution
landsat_resampled <- universal_spatial_join(
source_data = "landsat_30m.tif", # High resolution input
target_data = "modis_250m.tif", # Target resolution template
method = "resample", # Resample operation
summary_function = "mean", # Average when aggregating
verbose = TRUE
)
# Check new resolution
terra::res(landsat_resampled)
# [1] 250 250
# Scale by factor instead of template
coarser_raster <- universal_spatial_join(
source_data = "fine_resolution.tif",
target_data = NULL, # No template needed
method = "resample",
scale_factor = 5, # 5x coarser resolution
summary_function = "mean"
)
# =================================================================
# VECTOR OVERLAY: Join two vector datasets
# =================================================================
# Find which counties contain each field site
sites_with_counties <- universal_spatial_join(
source_data = "field_sites.shp", # Point data
target_data = "county_boundaries.shp", # Polygon data
method = "overlay", # Spatial intersection
verbose = TRUE
)
# Result: points with county attributes added
head(sites_with_counties)
# site_id geometry county_name state_name
# 1 1 POINT (-83.1 40.1) Franklin Ohio
# 2 2 POINT (-83.2 40.2) Delaware Ohio
# =================================================================
# AUTO-DETECTION: Let function choose best method
# =================================================================
# Function automatically detects: points + raster = extract method
auto_result <- universal_spatial_join(
source_data = my_points, # Any point data
target_data = my_raster, # Any raster data
method = "auto", # Automatically choose method
verbose = TRUE # See what method was chosen
)
# Output: "Auto-detected method: extract for vector to raster"
# =================================================================
# ERROR HANDLING EXAMPLES
# =================================================================
# Function handles common issues automatically
robust_result <- universal_spatial_join(
source_data = "points_wgs84.csv", # WGS84 coordinate system
target_data = "raster_utm.tif", # UTM coordinate system
method = "extract",
na_strategy = "nearest", # Handle missing values
verbose = TRUE # See CRS handling messages
)
# Output: "CRS mismatch detected. Reprojecting to match raster CRS..."
}
Run the code above in your browser using DataLab