[GRASS-user] Export raster to PostGIS

Hello

Is it possible to export a raster map created using r.water.outlet to PostGIS in GRASS 78? The documentation indicates using r.out.gdal with PostGISRaster as format. The command I’m trying is the following:

r.out.gdal input=mywatershed format=PostGISRaster output=mywatershed createopt=“PG:dbname=‘mydb’”

The postgresql server is running with locally and the database “mydb” can be accessed without having to enter a password using psql client. However, this command fails with the error:

ERROR 6: PostGISRasterDataset::CreateCopy() only works on source datasets that are PostGISRaster

Is something wrong in the command? The error message seems to suggest that it is impossible to export the raster map created with r.water.outlet to PostGIS because it is not sourced from PostGIS. Can someone please confirm?

Asim

As r.out.gdal uses GDAL library, it is bound to GDAL limitations.
According to the GDAL PG documentation, PG rasters are read-only:
https://gdal.org/drivers/raster/postgisraster.html#raster-postgisraster
Thus you can read but can not write PG rasters with GDAL.

Māris.

piektd., 2022. g. 26. aug., plkst. 10:48 — lietotājs Asim via
grass-user (<grass-user@lists.osgeo.org>) rakstīja:

Hello

Is it possible to export a raster map created using r.water.outlet to PostGIS in GRASS 78? The documentation indicates using r.out.gdal with PostGISRaster as format. The command I'm trying is the following:

   r.out.gdal input=mywatershed format=PostGISRaster output=mywatershed createopt="PG:dbname='mydb'"

The postgresql server is running with locally and the database "mydb" can be accessed without having to enter a password using psql client. However, this command fails with the error:

   ERROR 6: PostGISRasterDataset::CreateCopy() only works on source datasets that are PostGISRaster

Is something wrong in the command? The error message seems to suggest that it is impossible to export the raster map created with r.water.outlet to PostGIS because it is not sourced from PostGIS. Can someone please confirm?

Asim

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

Hi Māris

Thank you for pointing out the read-only limitation of GDAL library. A natural workaround is to convert the raster to vector with r.to.vect and then use v.out.postgis. However, there are some hurdles with regards to the type of resulting geometries (boundary, centroid and area) arising after the conversion to vector. I will start a separate thread if I can articulate the problem somewhat precisely.

Asim

Hi Asim,

You could try to use raster2pgsql after making sure you have the GDAL-GRASS plugin installed.

Then you should be able to load GRASS GIS raster maps directly into PostGIS by pointing to the header of your GRASS GIS maps. Something like:
raster2pgsql -s 4326 -C -l 2,4 -I -F -t 2700x2700 /your_grassdb/location/mapset/cellhd/your_raster_map output_pg_shema_name. output_table_name | psql -h your_host -p 5432 -U postgres -d your_pg_database
(obviously you have to adjust options and flags of raster2pgsql to your case...).

Hope that helps.

Cheers
Stefan

-----Original Message-----
From: grass-user <grass-user-bounces@lists.osgeo.org> On Behalf Of Asim via grass-user
Sent: lørdag 27. august 2022 11:38
To: Maris Nartiss <maris.gis@gmail.com>
Cc: grass-user@lists.osgeo.org
Subject: Re: [GRASS-user] Export raster to PostGIS

Hi Māris

Thank you for pointing out the read-only limitation of GDAL library. A natural workaround is to convert the raster to vector with r.to.vect and then use v.out.postgis. However, there are some hurdles with regards to the type of resulting geometries (boundary, centroid and area) arising after the conversion to vector. I will start a separate thread if I can articulate the problem somewhat precisely.

Asim

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fgrass-user&amp;data=05|01|Stefan.Blumentrath%40nina.no|e039842dbdef478ee35408da880fe04f|6cef373021314901831055b3abf02c73|0|0|637971899037805938|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D|3000|||&amp;sdata=4KmNrDMDYpVNlHGVghYR86DjRvNf%2BMtT3tMOZ3FFiSs%3D&amp;reserved=0

Hi,

Stefan Blumentrath <Stefan.Blumentrath@nina.no> schrieb am So., 28. Aug. 2022, 15:27:

Hi Asim,

You could try to use raster2pgsql after making sure you have the GDAL-GRASS plugin installed.

FYI:
This GDAL-GRASS driver you find here:

https://github.com/OSGeo/gdal-grass

Markus

Then you should be able to load GRASS GIS raster maps directly into PostGIS by pointing to the header of your GRASS GIS maps. Something like:
raster2pgsql -s 4326 -C -l 2,4 -I -F -t 2700x2700 /your_grassdb/location/mapset/cellhd/your_raster_map output_pg_shema_name. output_table_name | psql -h your_host -p 5432 -U postgres -d your_pg_database
(obviously you have to adjust options and flags of raster2pgsql to your case…).

Hope that helps.

Cheers
Stefan