3

I know that one can import GeoTIFF into PostGIS via raster2pgsql. I was wondering if there is any easy way (as a sequence of SQL statements) to do the same without resorting to the command line tool?

Essentially, I am interested in a PostgreSQL function that is the inverse of ST_AsTIFF, which given a pathname to a GeoTIFF, creates a pgraster table or a row in such a table.

The reason I ask is that I need such a function in a SQL stored procedure to import and process GeoTIFFs (not known in advance).

1 Answer 1

4

Take a look at ST_FromGDALRAster.

http://postgis.net/docs/manual-2.1/RT_ST_FromGDALRaster.html

Though that function assumes you imported the GTIFF as a bytea in PostgreSQL. If you want to keep the file out of the database and you have out-db enabled for your raster, you can use ST_AddBand. There is an example of an out-db tiff on the reference of this page:

http://postgis.net/docs/manual-2.1/RT_ST_AddBand.html

3
  • Thanks a lot. Do you know of some example code for the first approach? I tried the second approach in PostGIS 2.1, but it didn't work. I think there is a bug in ST_AddBand which prevented it from correctly figuring out the size of an external geotiff.
    – tinlyx
    Jan 12, 2015 at 23:53
  • Yah I have a feeling there is no logic in that for figuring out extent of geotiff, which is why the example fills it in. Regarding the first. Not sure if this helps much - but another example: bostongis.com/blog/index.php?/archives/…
    – Regina Obe
    Jan 13, 2015 at 8:15
  • I forgot to add, which may be your issue -- pulling the geotiff into the db with just stored proc is tricky. You can try PL/Python or PL/Perl or some lang that can access the OS - doing reverse of this: postgis.net/docs/manual-2.1/… or use pg_read_binary_file (essentially repurposing it) stackoverflow.com/questions/16048649/…
    – Regina Obe
    Jan 13, 2015 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.