No geometry values when importing rasters to PostGIS using raster2pgsql - postgresql

System software...
RHEL 6.6, PostgreSQL 9.3.5, POSTGIS 2.1.5, GEOS 3.4.2, GDAL 1.9.2
I'm trying to get my first PostGIS database setup, but am having an issues getting rasters imported to the db with any geometry values. The database does have the PostGIS and adminpack extension enabled. Issuing the to following to import the raster (raster.tif) to my database (testdb) with superuser role (benh)...
$ raster2pgsql -I -C -F -t 100x100 -s 4326 raster.tif public.raster > raster.sql
$ psql -d testdb -U benh -f raster.sql
... which does seem to successfully create the table (EPSG value verified with gdalinfo), but I can't get either QGIS or GeoServer to recognize the table as a raster. Issuing...
$ gdalinfo PG":host=localhost port=5432 dbname=testdb user=benh schema='public' table='raster'"
... returns ...
...
SUBDATASET_1253_NAME=PG:host=localhost port=5432 dbname=testdb user=benh schema=
public table=raster column=rast where='rid = 1253'
SUBDATASET_1253_DESC=PostGIS Raster at
public.raster (rast), rid = 1253
SUBDATASET_1254_NAME=PG:host=localhost port=5432 dbname=testdb user=benh schema=
public table=raster column=rast where='rid = 1254'
SUBDATASET_1254_DESC=PostGIS Raster at
public.raster (rast), rid = 1254
Corner Coordinates:
Upper Left ( 0.0000000, 0.0000000)
Lower Left ( 0.0000000, 0.0000000)
Upper Right ( 0.0000000, 0.0000000)
Lower Right ( 0.0000000, 0.0000000)
Center ( 0.0000000, 0.0000000)
... which would explain why QGIS and GeoServer are having trouble. Can somebody offer a suggestion here? I've already completely uninstalled and reinstalled PostgreSQL, PostGIS, and GDAL without success. I'm at the bottom of my shallow bag of tricks so any tips would be great. Thanks.

Related

raster2pgsql: "Could not allocate memory for INSERT statement"

I'm very new to raster2pgsql so please bear with me. I'm trying to load a 60mb .tif (from the High-Resolution Settlements Layer project) to my postgis-enabled database with the following code:
raster2pgsql -s 5235 -C -F [path to the .tif] public.hrsl_lka | psql
-h localhost -U postgres -p 5432 -d project
However, I get the following error:
ERROR: insert_records: Could not allocate memory for INSERT statement
ERROR: process_rasters: Could not convert raster tiles into INSERT or
COPY statements ERROR: Unable to process rasters
Loading smaller .tifs of around 3mb to the same database but from other sources works fine, however.
Is there a size limit with raster2pgsql? I'm on PostgreSQL 12.4.
With many thanks,
Gregor
Have you tried setting the tile size -t?
According to the documentation:
-t: Tile size - expressed as width x height. If not provided, a default is worked out automatically in the range of 32-100 so it best
matches the raster dimensions. It is worth remembering that when
importing multiple files, tiles will be computed for the first raster
and then applied to others.
Alternatively you can let the script compute it for you by means of setting -t to auto e.g.
raster2pgsql -s 5235 -t auto -C -F file.tif public.hrsl_lka | psql -d db
Related answer: Are there limitations using a PostGIS out-db raster?

shp2pgsql gsid conversion issues

I'm fairly new to PostGIS. I've imported a shp file countless times in different ways. Here's my latest attempt.
shp2pgsql -d -s 4269 /var/www/Shape/Hamilton_City/Election_Poll_Boundaries.shp city_hamilton | psql -U postgres -d shape
I'm importing to srid 4269 because the .prj file says:
GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
When I do any type of search on the geometry of the polygons as in:
select ST_Astext(geom) AS coordinates FROM city_hamilton limit 1;
I get odd coordinates like:
MULTIPOLYGON(((574380.4001 4808575.0399,574434.7803 4808545.44,574496.2521 4808512.3351,.....
What am I doing wrong here?

error "illegal option --t" occus when import OSM map into postgis [windows environment]

I want to import OSM map into postgis, so used the following command in windows cmd console:
osm2pgsql -U postgres -d osm -hstore -s _S ./default.style ./xxxx.osm
But "illegal option --t" error occus:
osm2pgsql illegal option --t
Usage error.
I don't know what's the meaning of "illegal option --t" and don't know how to handle it.
I've looked at several articles but haven't find any answer.
thanks.
p.s I've installed postgis 2.1.7 and postgresql 9.4 and hstore.
There are at least 2 mistakes in your command -hstore instead of --hstore and _S instead of -S .
So:
osm2pgsql -U postgres -d osm --hstore -s -S ./default.style ./xxxx.osm

How to query stored PostGIS Raster data from within the IPython Notebook

Test data
1 TIF file (159KB)
Goal threefold:
Load raster into PostGIS using raster2pgsql and visualize in QGIS
In my IPython Notebook connect to PostGIS and load raster into NumPy array
In my IPyhton Notebook use Pandas to load a time-series of one pixel of rasters with different time step stored in PostGIS
Process so far
I've managed to get one raster image into PostGIS Raster using the raster2pgsql command and visualize it in QGIS using the DB Manager:
raster2pgsql -s 4326 -d -I -C -M -R -l 4 D:\Downloads\raster//modis.tif -F -t 100x100 public.ndvi | psql -U postgres -d rastertest -h localhost -p 5432
But how to access/query this raster from within IPython Notebook?
I found this presentation, which is going about SQLALchemy and GeoAlchemy2. And where it is mentioned that it support PostGIS Raster as well. It seems to be very interesting! But using the documentation I don't see how I can apply this to Raster data
I think I can make a connection to my PostGIS database using the following code, where postgres=user, password=admin and database=rastertest:
from sqlalchemy import create_engine
engine = create_engine('postgresql://postgres:admin#localhost/rastertest', echo=True)
But then..
any advice is very much appreciated.
You should use the psycopg module to connect to a postgres db from python. Some code samples:
import psycopg2
def connect_db():
try:
conn = psycopg2.connect("dbname='rastertest' user='admin' host='localhost' password='password'")
conn.set_session(autocommit=True) #if you want your updates to take effect without being in a transaction and requiring a commit, for a beginner, I would set this to True
return conn
except:
print "I am unable to connect to the database"
return None
def get_raster(raster_id,conn):
query= "SELECT ST_AsText(geom) from raster_table where id={}".format(raster_id)
conn.cursor().execute(query)
res = cur.fetchall()
return res[0][0]
Maybe the text representation of the raster is something you can use.
Alternatively, take a look here http://postgis.net/docs/RT_reference.html to see if any of the functions return what you want for your numpy array and replace the query in get_raster accordingly. (Possibly this http://postgis.net/docs/RT_ST_DumpValues.html)

importing osm file into a postgres/postgis database

Im using the tool osm2pgsql to import an osm file into a postgres database using phppgadmin as the administrative tool. i have only downloaded a small town from osm in xml format and im having trouble importing it using the terminal on mac. Im a bit of a noob at this so any help would be greatly appreciated. Ive looked at several articles but none provide the clarity i need in order to fulfill my needs.
Thanks
im getting this error could be something small not sure..
Using projection SRS 900913 (Spherical Mercator)
Setting up table: planet_osm_point
NOTICE: table "planet_osm_point" does not exist, skipping
NOTICE: table "planet_osm_point_tmp" does not exist, skipping
SELECT AddGeometryColumn('planet_osm_point', 'way', 900913, 'POINT', 2 );
failed: ERROR: function addgeometrycolumn(unknown, unknown, integer, unknown, integer) does not exist
LINE 1: SELECT AddGeometryColumn('planet_osm_point', 'way', 900913, ...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
You need to install the postgis extensions into your postgres database. Assuming that you have postgis installed on your machine you need to do something like:
psql <database> < /usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql
The exact path to postgis.sql will depend on the distribution you are using and how it has packaged postgis. You may also want/need to install the spatial reference systems table with a command like this:
psql <database> < /usr/share/postgresql/8.4/contrib/postgis-1.5/spatial_ref_sys.sql
This is an example command:
osm2pgsql -c -d your_country -U postgres -W -H localhost -P 5432 -s -k -x -p osm -S default.style ..\your_country.osm.bz2
Here are some useful weblinks:
http://wiki.openstreetmap.org/wiki/Osm2pgsql
http://www.gis.hsr.ch/wiki/Osm2pgsql
-S.