How do we load Rasters stored in PostgreSQL-PostGIS or Oracle Spatial 11g into QGIS using PyQGIS?

I am able to load vector layers from Oracle 11g with below code. What changes are necessary to add Raster layers? The files are in tif format.

#Connect to Oracle and Fetch Table Names 
con = cx_Oracle.connect('myoradb/myoradb@')
print con.version
cur = con.cursor()
cur.execute(u"select TABLE_NAME from user_tab_columns where data_type='SDO_GEOMETRY'")

tables = cur.fetchall()

uri = QgsDataSourceURI()
uri.setConnection("", "1521", "newdev", "myoradb", "myoradb")

QgsProject.instance().writeEntry('WFSLayers', "/", [])

for t in tables :
   print str(t[0])

   uri.setDataSource('',  str(t[0]), "GEOMETRY")
   vlayer = QgsVectorLayer(uri.uri(),str(t[0]), 'oracle')
   lst = [ vlayer.id() ]
   pWfsLayer.append(u'%s' % vlayer.id())
   pLayers.append(u'%s' % vlayer.name())

   rect = QgsRectangle(render.fullExtent())            

If your raster is stored in PostGIS, you can load it using PyQGIS (e.g., from the QGIS Python console) this way:

connString = "PG: dbname=myDBname host=localhost user=myUser password=myPassword port=5432 mode=2 schema=public column=rast table=myRasterTable" 
layer = QgsRasterLayer( connString, "My PostGIS Raster" )

if layer.isValid():
    layer.setContrastEnhancement( QgsContrastEnhancement.StretchToMinimumMaximum )

QgsMapLayerRegistry.instance().addMapLayer( layer )

You would need to adjust all parameters in the connection string that start with "my," like "myDBname."

Additionally, as you may have noticed, I'm passing a column parameter with value rast. The value of the parameter column is taken from the view raster_columns in your database, namely, from the field r_raster_column as you see in the picture below:

enter image description here

If necessary, you could of course get such value by executing a SQL query before setting your connection string. Something like:

SELECT r_raster_column FROM raster_columns \
    WHERE r_table_schema = 'public' AND \
    r_table_name = 'myRasterTable' 
  • @gccarillo...Brilliant, it is working with PostGIS when I used raster2pgsql to load raster files.Thanks much.I hope this works for 11g too.
    – addcolor
    Feb 2 '15 at 15:14
  • @Durga Great! Please consider marking my answer as accepted if it was useful to you. That's how you reward answerers in GIS.StackExchange. Feb 2 '15 at 15:17
  • Thanks! That's a very important part of how GIS.SE works. You could also take the GIS.SE tour, which explains other aspects of GIS.SE. Feb 2 '15 at 15:43
  • @gccarillo ,what is the connection string for Oracle Spatial..In place of PG in the above connection string,what is the keyword for Oracle Spatial?
    – addcolor
    Feb 3 '15 at 9:09
  • @Durga I've never worked with Oracle Spatial, sorry. Feb 3 '15 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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