RPostGIS – work with GeoSpatial rasters

In a previous post, we showed you how to use the rpostgis library to load and write vector data to a PostgreSQL/PostGIS database. You can also use this library to archive raster data in the database. Let’s load the library first:

For the exercise, we will use the Digital Terrain Model generated in the previous post. Let’s load it:

Let’s define the connection to our database:

Before proceeding, we need to check if our database has the postgis_raster extension installed. If not, we can initiate the extension from the database with a SQL query:

From within R, we can use the dbSendQuery command from the RPostgreSQL library to send queries:

If the extension exists, the dbSendQuery function will return an error. This can be used to check if the extension has already been added to our database.

The raster is written to the database using the pgWriteRaster function, defining the connection to the database (conn), the table name and the raster to load (raster):

We load the raster from the database with the function pgGetRaster, specifying the connection to the database (conn), the name of the table (name), the geometry for which the range is to be loaded, a fragment of the raster (boundary):

Our downloaded raster looks like this:

Leave a Reply

Your email address will not be published.

Translate using Google Translate»
Social media & sharing icons powered by UltimatelySocial

Podoba Ci się nasza strona? Odwiedź nasz profil