src <- system.file("extdata/ynp_fires_1984_2022.gpkg", package="gdalraster")
# Convert GeoPackage to Shapefile
shp_file <- file.path(tempdir(), "ynp_fires.shp")
ogr2ogr(src, shp_file, src_layers = "mtbs_perims")
# Reproject to WGS84
ynp_wgs84 <- file.path(tempdir(), "ynp_fires_wgs84.gpkg")
args <- c("-t_srs", "EPSG:4326")
ogr2ogr(src, ynp_wgs84, cl_arg = args)
# Clip to a bounding box (xmin, ymin, xmax, ymax in the source SRS)
# This will select features whose geometry intersects the bounding box.
# The geometries themselves will not be clipped unless "-clipsrc" is
# specified.
# The source SRS can be overridden with "-spat_srs" ""
ynp_clip <- file.path(tempdir(), "ynp_fires_aoi_clip.gpkg")
bb <- c(469685.97, 11442.45, 544069.63, 85508.15)
args <- c("-spat", bb)
ogr2ogr(src, ynp_clip, cl_arg = args)
# Filter features by a -where clause
ynp_filtered <- file.path(tempdir(), "ynp_fires_2000_2022.gpkg")
sql <- "ig_year >= 2000 ORDER BY ig_year"
args <- c("-where", sql)
ogr2ogr(src, ynp_filtered, src_layers = "mtbs_perims", cl_arg = args)
deleteDataset(shp_file)
deleteDataset(ynp_wgs84)
deleteDataset(ynp_clip)
deleteDataset(ynp_filtered)
Run the code above in your browser using DataLab