[Qgis-developer] Is it possible to read a OGR datasource with EXecuteSQL statement?

Régis Haubourg regis.haubourg at eau-adour-garonne.fr
Thu Mar 27 06:19:02 PDT 2014


Hi, 
I'm trying to developp a processing script that would save a lot of
complexity if I could open in qgis a ogr datasource using ExecuteSQL
statement. 
I'm reading my datasources with OGR python API with no problem, and a simple
SQL allows me to keep only one fields, and calculate on the fly area, maybe
intersections between ogr layers. 
I would like to know if it is possible to dynamically load that OGR
datasource as a QGIS layer, or if I have no other choice than writing a temp
file somewhere.. 

see that snippet inside a processing script (for automated GUI) where I
would like to load layersqlpg as a qgis layer:
--------------------
import os
import tempfile
from osgeo import ogr
ogr.UseExceptions()         # if something goes wrong, we want to know about
it
drivershp = ogr.GetDriverByName('ESRI Shapefile')
# dataSourceshp = driver.Open('three_points.shp', 0)#0--read-only

databaseServer = "vslin147"
databaseName = "refgeo2	"
databaseUser = "visu"
databasePW = "visu"

connString = "PG: host=%s dbname=%s user=%s password=%s"
%(databaseServer,databaseName,databaseUser,databasePW)

conn = ogr.Open(connString)
layerpg = conn.GetLayerByName('ref.admin_departement_ag')
layersqlpg= conn.ExecuteSQL("SELECT a.insee_departement, st_area(geoml93),
geoml93 from ref.admin_departement_ag a")
tempdir = tempfile.gettempdir()
outShapefile = tempfile.gettempdir()+'\output_ogrexecute.shp' 
outDriver = ogr.GetDriverByName("ESRI Shapefile")


Cheers, 
Régis



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Is-it-possible-to-read-a-OGR-datasource-with-EXecuteSQL-statement-tp5131553.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.


More information about the Qgis-developer mailing list