Skip to content

Commit

Permalink
added postgis tools to sextante
Browse files Browse the repository at this point in the history
edited some grass descriptions and renamed postproc folder (now it is not only for postproc, but also for pre execution checks)
  • Loading branch information
volaya committed Dec 15, 2012
1 parent eed0bc0 commit 33e3925
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 162 deletions.
21 changes: 11 additions & 10 deletions python/plugins/sextante/admintools/AdminToolsAlgorithmProvider.py
Expand Up @@ -16,14 +16,17 @@
* *
***************************************************************************
"""
from sextante.admintools.PostGISExecuteSQL import PostGISExecuteSQL
import os
from PyQt4 import QtGui

__author__ = 'Victor Olaya'
__date__ = 'October 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


import os
from sextante.admintools.ImportIntoPostGIS import ImportIntoPostGIS
from sextante.admintools.ImportVectorIntoGeoServer import ImportVectorIntoGeoServer
from sextante.admintools.CreateWorkspace import CreateWorkspace
from sextante.admintools.ImportRasterIntoGeoServer import ImportRasterIntoGeoServer
Expand All @@ -38,7 +41,7 @@ def __init__(self):
AlgorithmProvider.__init__(self)
self.alglist = [ImportVectorIntoGeoServer(), ImportRasterIntoGeoServer(),
CreateWorkspace(), DeleteWorkspace(), DeleteDatastore(),
CreateStyleGeoServer()]#] PostGISExecuteSQL(), ImportIntoPostGIS()]#, TruncateSeedGWC()]
CreateStyleGeoServer(), ImportIntoPostGIS(), PostGISExecuteSQL()]#, TruncateSeedGWC()]

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
Expand All @@ -49,18 +52,16 @@ def unload(self):


def getName(self):
return "admintools"
return "gspg"

def getDescription(self):
return "Administration tools"
return "GeoServer/PostGIS tools"

#===========================================================================
# def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/../images/geoserver.png")
#===========================================================================
def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/database.png")

def _loadAlgorithms(self):
self.algs = self.alglist

def supportsNonFileBasedOutput(self):
return True
return False
62 changes: 55 additions & 7 deletions python/plugins/sextante/admintools/ImportIntoPostGIS.py
Expand Up @@ -16,8 +16,9 @@
* *
***************************************************************************
"""
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.parameters.ParameterBoolean import ParameterBoolean


__author__ = 'Victor Olaya'
__date__ = 'October 2012'
Expand All @@ -26,32 +27,79 @@
__revision__ = '$Format:%H$'

import os
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.parameters.ParameterString import ParameterString
from sextante.admintools import postgis_utils
import PyQt4

class ImportIntoPostGIS(GeoAlgorithm):

DATABASE = "DATABASE"
TABLENAME = "TABLENAME"
INPUT = "INPUT"
OVERWRITE = "OVERWRITE"
CREATEINDEX = "CREATEINDEX"

def getIcon(self):
return QIcon(os.path.dirname(__file__) + "/../images/postgis.png")

def processAlgorithm(self, progress):
pass
connection = self.getParameterValue(self.DATABASE)
overwrite = self.getParameterValue(self.OVERWRITE)
createIndex = self.getParameterValue(self.CREATEINDEX)
settings = QSettings()
mySettings = "/PostgreSQL/connections/"+ connection
try:
database = settings.value(mySettings+"/database").toString()
username = settings.value(mySettings+"/username").toString()
host = settings.value(mySettings+"/host").toString()
port = int(settings.value(mySettings+"/port").toString())
password = settings.value(mySettings+"/password").toString()
except Exception, e:
raise GeoAlgorithmExecutionException("Wrong database connection name: " + connection)

table = self.getParameterValue(self.TABLENAME);
table.replace(" ", "")
providerName = "postgres"

try:
db = postgis_utils.GeoDB(host=host, port=port, dbname=database, user=username, passwd=password)
except postgis_utils.DbError, e:
raise GeoAlgorithmExecutionException("Couldn't connect to database:\n"+e.message)

uri = QgsDataSourceURI()
uri.setConnection(host, str(port), database, username, password)
uri.setDataSource("public", table, "the_geom", "")

options = {}
if overwrite:
options['overwrite'] = True

layerUri = self.getParameterValue(self.INPUT);
layer = QGisLayers.getObjectFromUri(layerUri)
ret, errMsg = QgsVectorLayerImport.importLayer(layer, uri.uri(), providerName, self.crs, False, False, options)
if ret != 0:
raise GeoAlgorithmExecutionException(u"Error importing to PostGIS\n%s" % errMsg)

if createIndex:
db.create_spatial_index(table, "public", "the_geom")

db.vacuum_analyze(table, "public")

def defineCharacteristics(self):
self.name = "Import into PostGIS"
self.group = "PostGIS management tools"
self.addParameter(ParameterVector(self.INPUT, "Layer to import"))
self.addParameter(ParameterString(self.DATABASE, "Database"))
self.addParameter(ParameterString(self.TABLENAME, "Name for new table"))

self.addParameter(ParameterString(self.DATABASE, "Database (connection name)"))
self.addParameter(ParameterString(self.TABLENAME, "Table to import to"))
self.addParameter(ParameterBoolean(self.OVERWRITE, "Overwrite", True))
self.addParameter(ParameterBoolean(self.CREATEINDEX, "Create spatial index", True))





91 changes: 12 additions & 79 deletions python/plugins/sextante/admintools/PostGISExecuteSQL.py
Expand Up @@ -36,7 +36,6 @@ class PostGISExecuteSQL(GeoAlgorithm):

DATABASE = "DATABASE"
SQL = "SQL"
TABLENAME = "TABLENAME"

def getIcon(self):
return QIcon(os.path.dirname(__file__) + "/../images/postgis.png")
Expand All @@ -46,96 +45,30 @@ def processAlgorithm(self, progress):
connection = self.getParameterValue(self.DATABASE)
settings = QSettings()
mySettings = "/PostgreSQL/connections/"+ connection
database = settings.value(mySettings+"/database").toString()
username = settings.value(mySettings+"/username").toString()
host = settings.value(mySettings+"/host").toString()
port = settings.value(mySettings+"/port").toString()
password = settings.value(mySettings+"/password").toString()

# connect to DB
try:
database = settings.value(mySettings+"/database").toString()
username = settings.value(mySettings+"/username").toString()
host = settings.value(mySettings+"/host").toString()
port = int(settings.value(mySettings+"/port").toString())
password = settings.value(mySettings+"/password").toString()
except Exception, e:
raise GeoAlgorithmExecutionException("Wrong database connection name: " + connection)
try:
self.db = postgis_utils.GeoDB(host=host, port=port, dbname=database, user=username, passwd=password)
except postgis_utils.DbError, e:
raise GeoAlgorithmExecutionException("Couldn't connect to database:\n"+e.message)

## Set up sql statement for geoprocess
newTbl = self.getParameterValue(self.TABLENAME);
newTbl.replace(" ", "")
sqlNewTbl = str("CREATE TABLE ") + str(newTbl) + " as "
txtSQL = self.getParameterValue(self.SQL)
txtSQL = sqlNewTbl + txtSQL
sqlArray = txtSQL.split("\n")
sqlString = ""
for i in range(0,sqlArray.count()):
sqlString = sqlString + sqlArray[i] + " "

## Run query
sql = self.getParameterValue(self.SQL).replace("\n", " ")
try:
self.db._exec_sql_and_commit(str(sqlString))
except postgis_utils.DbError, e:
raise GeoAlgorithmExecutionException("Couldn't connect to database:\n"+e.message)

try: #first try
dbGeoTbls = self.db.list_geotables(self.cmbSchema.currentText())
self.db._exec_sql_and_commit(str(sql))
except postgis_utils.DbError, e:
raise GeoAlgorithmExecutionException("Couldn't connect to database:\n"+e.message)

for i in range(0,len(dbGeoTbls)):
if dbGeoTbls[i][0]==newTbl:
if dbGeoTbls[i][7]=="geometry":
geocol = str(dbGeoTbls[i][6])

schName = self.cmbSchema.currentText()

## Update the geometry_columns table
geocolSQL = "SELECT ST_Dimension(" + geocol + "), ST_SRID(" + geocol + "), GeometryType(" + geocol + ") FROM " + newTbl + ";"
c = self.db.con.cursor()
self.db._exec_sql(c, str(geocolSQL))
geomPar = c.fetchone()

## Setup sql statement to update geometry_columns
geocolupdate = "INSERT INTO geometry_columns "
geocolupdate = geocolupdate + "(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, type) VALUES"
geocolupdate = geocolupdate + "('', 'public', '" + newTbl + "', '" + geocol + "', '" + str(geomPar[0]) + "', '" + str(geomPar[1]) + "', '" + str(geomPar[2])
geocolupdate = geocolupdate + "');"
self.db._exec_sql_and_commit(str(geocolupdate))

## Add remaining constraints for dimension, geometry type and srid
pkeySQL = "ALTER TABLE " + schName + "." + newTbl + " ADD CONSTRAINT " + newTbl + "_pkey PRIMARY KEY(pgid);"
dimSQL = "ALTER TABLE " + schName + "." + newTbl + " ADD CONSTRAINT enforce_dims_" + geocol + " CHECK (ndims(" + geocol + ") = " + str(geomPar[0]) + ");"
gtypSQL = "ALTER TABLE " + schName + "." + newTbl + " ADD CONSTRAINT enforce_geotype_" + geocol + " CHECK (geometrytype(" + geocol + ") = '" + str(geomPar[2]) + "'::text OR " + geocol + " IS NULL);"
sridSQL = "ALTER TABLE " + schName + "." + newTbl + " ADD CONSTRAINT enforce_srid_" + geocol + " CHECK (srid(" + geocol + ") = " + str(geomPar[1]) + ");"

try: #second try
self.db._exec_sql_and_commit(str(pkeySQL))
self.db._exec_sql_and_commit(str(dimSQL))
self.db._exec_sql_and_commit(str(gtypSQL))
self.db._exec_sql_and_commit(str(sridSQL))
except postgis_utils.DbError, e:
QMessageBox.critical(self, "error", "Couldn't connect to database:\n"+e.message)
return

#=======================================================================
# ## ## Add resulting data set if Add layer checkbox is selected
# if self.actionAddData.isChecked():
# ## Set up data source and add to QGIS view
# uri = QgsDataSourceURI()
# uri.setConnection(str(self.db.host), str(self.db.port), str(self.db.dbname), str(self.db.user), str(self.db.passwd))
# uri.setDataSource(schName, newTbl, geocol)
# dbSchemas = self.db.list_schemas()
# for s in range(0,len(dbSchemas)):
# dbSchema = dbSchemas[s][1]
# if dbSchema==schName:
# dbOwner = dbSchemas[s][2]
# self.iface.addVectorLayer(uri.uri(), newTbl, dbOwner)
#=======================================================================
raise GeoAlgorithmExecutionException("Error executing SQL:\n"+e.message)

def defineCharacteristics(self):
self.name = "PostGIS execute SQL"
self.group = "PostGIS management tools"
self.addParameter(ParameterString(self.DATABASE, "Database"))
self.addParameter(ParameterString(self.TABLENAME, "Name for new table"))
self.addParameter(ParameterString(self.SQL, "SQL query", True))
self.addParameter(ParameterString(self.SQL, "SQL query", "", True))



8 changes: 4 additions & 4 deletions python/plugins/sextante/admintools/postgis_utils.py
Expand Up @@ -5,7 +5,7 @@
postgis_utils.py
---------------------
Date : November 2012
Copyright : (C) 2012 by Victor Olaya
Copyright : (C) 2012 by Martin Dobias
Email : volayaf at gmail dot com
***************************************************************************
* *
Expand All @@ -17,9 +17,9 @@
***************************************************************************
"""

__author__ = 'Victor Olaya'
__author__ = 'Martin Dobias'
__date__ = 'November 2012'
__copyright__ = '(C) 2012, Victor Olaya'
__copyright__ = '(C) 2012, Martin Dobias'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

Expand Down Expand Up @@ -524,7 +524,7 @@ def create_index(self, table, name, column, schema=None):
def create_spatial_index(self, table, schema=None, geom_column='the_geom'):
table_name = self._table_name(schema, table)
idx_name = self._quote("sidx_"+table)
sql = "CREATE INDEX %s ON %s USING GIST(%s GIST_GEOMETRY_OPS)" % (idx_name, table_name, self._quote(geom_column))
sql = "CREATE INDEX %s ON %s USING GIST(%s)" % (idx_name, table_name, self._quote(geom_column))
self._exec_sql_and_commit(sql)

def delete_index(self, name, schema=None):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/grass/CMakeLists.txt
Expand Up @@ -2,7 +2,7 @@ FILE(GLOB PY_FILES *.py)
FILE(GLOB OTHER_FILES grass.txt)
FILE(GLOB DESCR_FILES description/*.txt)

ADD_SUBDIRECTORY(postproc)
ADD_SUBDIRECTORY(ext)

PLUGIN_INSTALL(sextante grass ${PY_FILES} ${OTHER_FILES})
PLUGIN_INSTALL(sextante grass/description ${DESCR_FILES})
8 changes: 4 additions & 4 deletions python/plugins/sextante/grass/description/r.describe.txt
@@ -1,11 +1,11 @@
r.describe
r.describe - Prints terse list of category values found in a raster map layer.
r.describe - Prints terse list of category values found in a raster layer.
Raster (r.*)
ParameterRaster|map|Name of input raster map|False
ParameterString|nv|String representing no data cell value|*
ParameterRaster|map|input raster layer|False
ParameterNumber|nv|No-data cell value|None|None|0
ParameterNumber|nsteps|Number of quantization steps|1.0|None|255
ParameterBoolean|-1|Print the output one value per line|False
ParameterBoolean|-r|Only print the range of the data|False
ParameterBoolean|-n|Suppress reporting of any NULLs|False
ParameterBoolean|-d|Use the current region|False
ParameterBoolean|-i|Read fp map as integer|False
OutputHTML|html|Output report
7 changes: 0 additions & 7 deletions python/plugins/sextante/grass/description/r.distance.txt

This file was deleted.

8 changes: 4 additions & 4 deletions python/plugins/sextante/grass/description/r.drain.txt
@@ -1,10 +1,10 @@
r.drain
r.drain - Traces a flow through an elevation model on a raster map.
Raster (r.*)
ParameterRaster|input|Name of elevation raster map|False
ParameterString|coordinate|Map coordinates of starting point(s) (E,N)|
ParameterMultipleInput|vector_points|Name of vector map(s) containing starting point(s)|0.0|False
ParameterRaster|input|Elevatio|False
ParameterString|coordinate|Map coordinates of starting point(s) (E,N)|(0,0)
ParameterMultipleInput|vector_points|Vector layer(s) containing starting point(s)|0|False
ParameterBoolean|-c|Copy input cell values on output|False
ParameterBoolean|-a|Accumulate input values along the path|False
ParameterBoolean|-n|Count cell numbers along the path|False
OutputRaster|output|Name for output raster map
OutputRaster|output|Result
11 changes: 5 additions & 6 deletions python/plugins/sextante/grass/description/r.fill.dir.txt
@@ -1,9 +1,8 @@
r.fill.dir
r.fill.dir - Filters and generates a depressionless elevation map and a flow direction map from a given elevation raster map.
r.fill.dir - Filters and generates a depressionless elevation layer and a flow direction layer from a given elevation raster layer.
Raster (r.*)
ParameterRaster|input|Name of existing raster map containing elevation surface|False
ParameterRaster|input|Elevation|False
ParameterSelection|type|Output aspect direction format|grass;agnps;answers
ParameterBoolean|-f|Find unresolved areas only|False
OutputRaster|elevation|Output elevation raster map after filling
OutputRaster|direction|Output direction raster map
OutputRaster|areas|Output raster map of problem areas
OutputRaster|elevation|Depressionless DEM
OutputRaster|direction|Flow direction
OutputRaster|areas|Problem areas
6 changes: 3 additions & 3 deletions python/plugins/sextante/grass/description/r.fillnulls.txt
@@ -1,8 +1,8 @@
r.fillnulls
r.fillnulls - Fills no-data areas in raster maps using v.surf.rst splines interpolation or v.surf.bspline interpolation
r.fillnulls - Fills no-data areas in a raster layer using v.surf.rst splines interpolation or v.surf.bspline interpolation
Raster (r.*)
ParameterRaster|input|Name of input raster map in which to fill nulls|False
ParameterRaster|input|Input raster layer to fill|False
ParameterSelection|method|Method|bilinear;bicubic;rst|2
ParameterNumber|tension|Spline tension parameter|None|None|40.0
ParameterNumber|smooth|Spline smoothing parameter|None|None|0.1
OutputRaster|output|Name for output raster map with nulls filled by interpolation
OutputRaster|output|Filled layer
14 changes: 7 additions & 7 deletions python/plugins/sextante/grass/description/r.flow.txt
@@ -1,14 +1,14 @@
r.flow
r.flow - Construction of slope curves (flowlines), flowpath lengths, and flowline densities (upslope areas) from a raster digital elevation model (DEM).
Raster (r.*)
ParameterRaster|elevin|Input elevation raster map|False
ParameterRaster|aspin|Input aspect raster map|False
ParameterRaster|barin|Input barrier raster map|False
ParameterRaster|elevin|Elevation|False
ParameterRaster|aspin|Aspect|False
ParameterRaster|barin|Barriers|False
ParameterNumber|skip|Number of cells between flowlines|None|None|1.0
ParameterNumber|bound|Maximum number of segments per flowline|None|None|5.0
ParameterBoolean|-u|Compute upslope flowlines instead of default downhill flowlines|False
ParameterBoolean|-3|3-D lengths instead of 2-D|False
ParameterBoolean|-m|Use less memory, at a performance penalty|False
OutputRaster|flout|Output flowline vector map
OutputRaster|lgout|Output flowpath length raster map
OutputRaster|dsout|Output flowline density raster map
*ParameterBoolean|-m|Use less memory, at a performance penalty|False
OutputRaster|flout|Output flowline vector layer
OutputRaster|lgout|Output flowpath length raster layer
OutputRaster|dsout|Output flowline density raster layer
3 changes: 3 additions & 0 deletions python/plugins/sextante/grass/ext/CMakeLists.txt
@@ -0,0 +1,3 @@
FILE(GLOB PY_FILES *.py)

PLUGIN_INSTALL(sextante grass/ext ${PY_FILES})

0 comments on commit 33e3925

Please sign in to comment.