View source: R/04-crop-analysis.R
| create_crop_mask | R Documentation |
Create binary or classified crop mask from USDA CDL data for specified crops. Fixed to handle terra operations properly.
create_crop_mask(
cdl_data,
crop_codes,
region_boundary = NULL,
mask_type = "binary"
)
cdl_data |
CDL raster data (file path or SpatRaster) |
crop_codes |
Vector of CDL codes or crop names |
region_boundary |
Optional region boundary for clipping |
mask_type |
Type of mask: "binary" (1/0) or "preserve" (keep original codes) |
SpatRaster with crop mask
## Not run:
# These examples require actual CDL data files
# Create corn mask
corn_mask <- create_crop_mask("cdl_2023.tif", "corn", "Iowa")
# Create grain crops mask
grain_mask <- create_crop_mask(cdl_raster, "grains", mask_type = "preserve")
## End(Not run)
# Example with mock data (this can run)
mock_cdl <- terra::rast(nrows = 5, ncols = 5, crs = "EPSG:4326")
terra::values(mock_cdl) <- c(1, 1, 5, 5, 24, 1, 5, 5, 24, 24,
1, 1, 5, 24, 24, 5, 5, 24, 24, 1,
1, 5, 5, 24, 1) # corn, soy, wheat
# Create corn mask from mock data
corn_mask <- create_crop_mask(mock_cdl, "corn")
print(terra::values(corn_mask)) # Should show 1s and 0s
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.