The workflow for geographic data typically involves:
The goal of the sigugr
package is to provide a
comprehensive set of functions that simplify the processes of
transforming, storing, and publishing geographic data.
You can install the released version of sigugr
from CRAN with:
install.packages("sigugr")
And the development version from GitHub with:
# install.packages("pak")
::pak("josesamos/sigugr") pak
The satellite bands for a specific area were downloaded from GloVis
(USGS Global Visualization Viewer). They were integrated and
initially transformed using the satres
package. To reduce their file size and enable inclusion here, their
resolution was adjusted using the aggregate_rasters()
function provided by this package.
Figure 1 below shows the original satellite bands that we started with.
library(sigugr)
<- system.file("extdata", "sat.tif", package = "sigugr")
tif <- terra::rast(tif)
sat ::plot(sat) terra
We are interested only in the area defined by a polygon representing
a municipality in the region. Using the clip_raster()
function, we extract the area of interest. We can either preserve the
original CRS of the bands or reproject them to the CRS of the clipping
polygon, which is controlled by the keep_crs
parameter.
The obtained result is shown in Figure 2.
<- system.file("extdata", "sigugr.gpkg", package = "sigugr")
gpkg <- sf::st_read(gpkg, layer = "lanjaron", quiet = TRUE)
polygon
<- clip_raster(sat, polygon, keep_crs = FALSE)
sat2
::plot(sat2) terra
We save the result in a working file, which will be used for database storage and publication.
<- tempfile(fileext = ".tif")
sat_file ::writeRaster(sat2, sat_file, filetype = "GTiff", overwrite = TRUE) terra
We store the result in a PostGIS database using the
store_raster()
function. The database must have the
postgis
and postgis_raster
extensions
enabled.
<- DBI::dbConnect(
conn ::Postgres(),
RPostgresdbname = "sigugr",
host = "localhost",
user = "postgres",
password = "postgres"
)
<- store_bands(sat_file, conn)
tables
::dbDisconnect(conn) DBI
The following Figure 3 shows the access from QGIS to one of the tables included in the database.
To publish the raster bands in GeoServer, we must use the version
stored in the file, as GeoServer does not support PostGIS as a source
for raster data. We publish them using the publish_bands()
function.
<- geoserver(
gso url = "http://localhost:8080/geoserver",
user = "admin",
password = "geoserver",
workspace = "sigugr"
)
|>
gso publish_bands(sat_file)
The result can also be viewed from QGIS by accessing the GeoServer instance via WMS, as shown in Figure 4.
Except for the functions used to connect to PostGIS and GeoServer, as well as those for accessing layers in files, the following function calls have been used to clip and reproject the satellite bands, store them in PostGIS, and publish them in GeoServer:
# Clip and reproject
<- clip_raster(sat, polygon, keep_crs = FALSE)
sat2
# Store in PostGIS
<- store_bands(sat_file, conn)
tables
# Publish in GeoServer
|>
gso publish_bands(sat_file)
Using data obtained from the web, we can easily transform, store, and publish it using the functions implemented in the package, as demonstrated in this example.