Saturday, 30 April 2016

How to import a raster into PostGIS?


I'm trying to follow the instructions here to load a raster into a PostGIS database:


python raster2pgsql.py -s 4269 -I -r *.tif -F myschema.demelevation -o elev.sql

I understand that I replace * with the path to my raster, but I don't understand the part myschema.demelevation or elev.sql. Should I have my own schema for this file? And what does the elev.sql part mean?


I also read through the gdal PostGIS raster driver to try and understand this with more examples. Similarly, they suggest loading a raster katrina


python raster2pgsql.py -r /path/to/katrina.tif -t katrina -l 1 -k 64x64 -o katrina.sql -s 4326 -I -M


Using my current setup, I tried loading the katrina raster in:


    python2.6 ~/src/postgis-2.0.0SVN/raster/scripts/python/raster2pgsql.py -r ~/tmp/katrina.tif -t katrina -l 1 -k 64x64 -o katrina.sql -s 4326 -I -M

However, I got the following errors:


Traceback (most recent call last):
File "/home/src/postgis-2.0.0SVN/raster/scripts/python/raster2pgsql.py", line 34, in
from osgeo import gdal
File "/home/lib/python2.6/GDAL-1.8.1-py2.6-linux-i686.egg/osgeo/__init__.py", line 21, in
_gdal = swig_import_helper()

File "/home/lib/python2.6/GDAL-1.8.1-py2.6-linux-i686.egg/osgeo/__init__.py", line 17, in swig_import_helper
_mod = imp.load_module('_gdal', fp, pathname, description)
ImportError: /home/lib/python2.6/GDAL-1.8.1-py2.6-linux-i686.egg/osgeo/_gdal.so: undefined symbol: GDALSetRasterUnitType

I don't fully understand these errors mean; when I compiled gdal should I have specified an argument for GDALSetRasterUnitType ?


More generally, I'm having difficulty understanding why I am not specifying the database that I am trying to load this data into.




After following MerseViking's advice I ran:


python /usr/lib/postgresql/8.4/bin/raster2pgsql.py -r /home/celenius/Downloads/katrina.tif -t katrina -l 1 -k 64x64 -o katrina.sql -s 4326 -I -M


which returned the following output:


------------------------------------------------------------
Summary of GDAL to PostGIS Raster processing:
------------------------------------------------------------
Number of processed raster files: 1 (/home/celenius/Downloads/katrina.tif)
List of generated tables (number of tiles):
1 katrina (256)

Then I ran:


psql -d test -f katrina.sql - U postgres -W


which returned the following:


    addrastercolumn                                                                                                                                                                                        
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public.katrina.rast srid:4326 pixel_types:{8BUI,8BUI,8BUI} out_db:false regular_blocking:true nodata_values:NULL scale_x:'0.029325513196481' scale_y:'-0.029325513196481' blocksize_x:'64' blocksize_y:'64' extent:POLYGON((-100.014662756598 9.98533724340176,-100.014662756598 40.0146627565982,-69.9853372434018 40.0146627565982,-69.9853372434018 9.98533724340176,-100.014662756598 9.98533724340176))
(1 row)

(END)

This message and a blinking cursor appear on screen. I assume that it is loading into the database, but I'm not sure. The tif file is just 3MB - I had assumed that it would not take long to load a file of this size but the blinking cursor has already been on the screen for ~1 hour. Has this crashed or do I just need to wait a long time? I have 4GB of RAM and a dual-processor of 2.5 GHz.




Answer



It seems there's a typo on that page; in the line:


python raster2pgsql.py -s 4269 -I -r *.tif -F myschema.demelevation -o elev.sql

The -F parameter should be -t which specifies the table name to import the data into. The part before the . is the optional schema name if you want your table in a schema other than public. The -o parameter specifies the file that is generated by the Python script. This file is the SQL representation of your table definition and the actual data from the source raster, so it can get pretty large. Once this file is generated, you need to run it through psql -d -f elev.sql to actually populate the database, after which it can be deleted.


However, what strikes me as odd is that you're calling raster2pgsql.py from the PostGIS 2.0 source directory. Have you actually compiled (by running make) and installed (by running make install as root) PostGIS 2.0? Because it should be on your path, and IIRC the installer automatically updates your PYTHON_PATH environment variable.


As for the missing GDALSetRasterUnitType, the thing I would first check for is that you don't have an earlier version of GDAL libraries installed that Python is picking up instead of 1.8.1. Try gdalinfo --version This site may shed some light on your problem.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...