Skip to content

Commit

Permalink
restructured admintools (was geoservertools)
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Nov 7, 2012
1 parent 6a2fdf5 commit 3c5de39
Show file tree
Hide file tree
Showing 35 changed files with 839 additions and 110 deletions.
2 changes: 1 addition & 1 deletion python/plugins/sextante/CMakeLists.txt
@@ -1,5 +1,6 @@
SET (SEXTANTE_PLUGIN_DIR ${QGIS_DATA_DIR}/python/plugins/sextante)

ADD_SUBDIRECTORY(admintools)
ADD_SUBDIRECTORY(algs)
ADD_SUBDIRECTORY(core)
ADD_SUBDIRECTORY(ftools)
Expand All @@ -18,7 +19,6 @@ ADD_SUBDIRECTORY(pymorph)
ADD_SUBDIRECTORY(r)
ADD_SUBDIRECTORY(saga)
ADD_SUBDIRECTORY(script)
ADD_SUBDIRECTORY(servertools)
ADD_SUBDIRECTORY(taudem)
#ADD_SUBDIRECTORY(tests)

Expand Down
Expand Up @@ -2,7 +2,7 @@

"""
***************************************************************************
GeoServerToolsAlgorithmProvider.py
AdminToolsAlgorithmProvider.py
---------------------
Date : October 2012
Copyright : (C) 2012 by Victor Olaya
Expand All @@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from sextante.admintools.PostGISExecuteSQL import PostGISExecuteSQL

__author__ = 'Victor Olaya'
__date__ = 'October 2012'
Expand All @@ -25,22 +26,22 @@


import os
from sextante.servertools.CreateWorkspace import CreateWorkspace
from sextante.servertools.ImportVectorIntoGeoServer import ImportVectorIntoGeoServer
from sextante.servertools.ImportRasterIntoGeoServer import ImportRasterIntoGeoServer
from sextante.servertools.DeleteWorkspace import DeleteWorkspace
from sextante.servertools.DeleteDatastore import DeleteDatastore
from sextante.servertools.StyleGeoServerLayer import StyleGeoServerLayer
from sextante.admintools.ImportVectorIntoGeoServer import ImportVectorIntoGeoServer
from sextante.admintools.CreateWorkspace import CreateWorkspace
from sextante.admintools.ImportRasterIntoGeoServer import ImportRasterIntoGeoServer
from sextante.admintools.DeleteWorkspace import DeleteWorkspace
from sextante.admintools.DeleteDatastore import DeleteDatastore
from sextante.admintools.CreateStyleGeoServer import CreateStyleGeoServer
from sextante.core.AlgorithmProvider import AlgorithmProvider
from PyQt4 import QtGui

class GeoServerToolsAlgorithmProvider(AlgorithmProvider):
class AdminToolsAlgorithmProvider(AlgorithmProvider):

def __init__(self):
AlgorithmProvider.__init__(self)
self.alglist = [ImportVectorIntoGeoServer(), ImportRasterIntoGeoServer(),
CreateWorkspace(), DeleteWorkspace(), DeleteDatastore(),
StyleGeoServerLayer()]#, TruncateSeedGWC()]
CreateStyleGeoServer(), PostGISExecuteSQL()]#, TruncateSeedGWC()]

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
Expand All @@ -51,13 +52,15 @@ def unload(self):


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

def getDescription(self):
return "Geoserver management tools"
return "Administration 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/geoserver.png")
#===========================================================================

def _loadAlgorithms(self):
self.algs = self.alglist
Expand Down
Expand Up @@ -3,4 +3,4 @@ FILE(GLOB PY_FILES *.py)
ADD_SUBDIRECTORY(geoserver)
ADD_SUBDIRECTORY(httplib2)

PLUGIN_INSTALL(sextante servertools ${PY_FILES})
PLUGIN_INSTALL(sextante admintools ${PY_FILES})
Expand Up @@ -2,7 +2,7 @@

"""
***************************************************************************
DeleteDatastore.py
CreateStyleGeoServer.py
---------------------
Date : October 2012
Copyright : (C) 2012 by Victor Olaya
Expand All @@ -25,12 +25,12 @@

from qgis.core import *
from sextante.parameters.ParameterString import ParameterString
from sextante.servertools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm
from sextante.admintools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm
from sextante.parameters.ParameterFile import ParameterFile
from sextante.parameters.ParameterBoolean import ParameterBoolean


class StyleGeoServerLayer(GeoServerToolsAlgorithm):
class CreateStyleGeoServer(GeoServerToolsAlgorithm):

STYLE = "STYLE"
OVERWRITE = "OVERWRITE"
Expand Down
Expand Up @@ -16,8 +16,6 @@
* *
***************************************************************************
"""
from sextante.servertools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm
from sextante.outputs.OutputString import OutputString

__author__ = 'Victor Olaya'
__date__ = 'October 2012'
Expand All @@ -29,15 +27,14 @@
from qgis.core import *
from PyQt4 import QtGui
from sextante.parameters.ParameterString import ParameterString
from sextante.admintools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm
from sextante.outputs.OutputString import OutputString

class CreateWorkspace(GeoServerToolsAlgorithm):

WORKSPACE = "WORKSPACE"
WORKSPACEURI = "WORKSPACEURI"

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

def processAlgorithm(self, progress):
self.createCatalog()
workspaceName = self.getParameterValue(self.WORKSPACE)
Expand Down
Expand Up @@ -25,7 +25,7 @@

from qgis.core import *
from sextante.parameters.ParameterString import ParameterString
from sextante.servertools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm
from sextante.admintools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm

class DeleteDatastore(GeoServerToolsAlgorithm):

Expand Down
Expand Up @@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from sextante.servertools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm

__author__ = 'Victor Olaya'
__date__ = 'October 2012'
Expand All @@ -26,6 +25,7 @@

from qgis.core import *
from sextante.parameters.ParameterString import ParameterString
from sextante.admintools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm

class DeleteWorkspace(GeoServerToolsAlgorithm):

Expand Down
Expand Up @@ -17,7 +17,7 @@
***************************************************************************
"""
from sextante.parameters.ParameterString import ParameterString
from sextante.servertools.geoserver.catalog import Catalog
from sextante.admintools.geoserver.catalog import Catalog

__author__ = 'Victor Olaya'
__date__ = 'October 2012'
Expand Down
Expand Up @@ -25,7 +25,7 @@

from qgis.core import *
from sextante.parameters.ParameterString import ParameterString
from sextante.servertools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm
from sextante.admintools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm
from sextante.parameters.ParameterRaster import ParameterRaster


Expand All @@ -44,7 +44,7 @@ def processAlgorithm(self, progress):
name = self.getParameterValue(self.NAME)
workspaceName = self.getParameterValue(self.WORKSPACE)
filename = self.exportRasterLayer(inputFilename)
workspace = self.catalog.get_workspace(workspaceName)
workspace = self.catalog.get_workspace(workspaceName)
ds = self.catalog.create_coveragestore2(name, workspace)
ds.data_url = "file:" + filename;
self.catalog.save(ds)
Expand Down
Expand Up @@ -29,7 +29,7 @@
from sextante.core.QGisLayers import QGisLayers
from sextante.core.LayerExporter import LayerExporter
from sextante.parameters.ParameterString import ParameterString
from sextante.servertools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm
from sextante.admintools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm

class ImportVectorIntoGeoServer(GeoServerToolsAlgorithm):

Expand Down
143 changes: 143 additions & 0 deletions python/plugins/sextante/admintools/PostGISExecuteSQL.py
@@ -0,0 +1,143 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
PostGISExecuteSQL.py
---------------------
Date : October 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from sextante.admintools import postgis_utils
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
import PyQt4

__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 qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.parameters.ParameterString import ParameterString
from sextante.admintools.GeoServerToolsAlgorithm import GeoServerToolsAlgorithm

class PostGISExecuteSQL(GeoServerToolsAlgorithm):

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

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

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:
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
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())
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)
#=======================================================================

def defineCharacteristics(self):
self.addBaseParameters()
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"))



3 changes: 3 additions & 0 deletions python/plugins/sextante/admintools/geoserver/CMakeLists.txt
@@ -0,0 +1,3 @@
FILE(GLOB PY_FILES *.py)

PLUGIN_INSTALL(sextante admintools/geoserver ${PY_FILES})
@@ -1,17 +1,17 @@
from datetime import datetime, timedelta
import logging
from sextante.servertools.geoserver.layer import Layer
from sextante.servertools.geoserver.store import coveragestore_from_index, datastore_from_index, \
from sextante.admintools.geoserver.layer import Layer
from sextante.admintools.geoserver.store import coveragestore_from_index, datastore_from_index, \
UnsavedDataStore, UnsavedCoverageStore
from sextante.servertools.geoserver.style import Style
from sextante.servertools.geoserver.support import prepare_upload_bundle, url
from sextante.servertools.geoserver.layergroup import LayerGroup, UnsavedLayerGroup
from sextante.servertools.geoserver.workspace import workspace_from_index, Workspace
from sextante.admintools.geoserver.style import Style
from sextante.admintools.geoserver.support import prepare_upload_bundle, url
from sextante.admintools.geoserver.layergroup import LayerGroup, UnsavedLayerGroup
from sextante.admintools.geoserver.workspace import workspace_from_index, Workspace
from os import unlink
from xml.etree.ElementTree import XML
from xml.parsers.expat import ExpatError
from urlparse import urlparse
from sextante.servertools import httplib2
from sextante.admintools import httplib2

logger = logging.getLogger("gsconfig.catalog")

Expand Down
@@ -1,5 +1,5 @@
from sextante.servertools.geoserver.support import ResourceInfo, xml_property, write_bool, url
from sextante.servertools.geoserver.style import Style
from sextante.admintools.geoserver.support import ResourceInfo, xml_property, write_bool, url
from sextante.admintools.geoserver.style import Style

class _attribution(object):
def __init__(self, title, width, height):
Expand Down
@@ -1,4 +1,4 @@
from sextante.servertools.geoserver.support import ResourceInfo, bbox, write_bbox, \
from sextante.admintools.geoserver.support import ResourceInfo, bbox, write_bbox, \
write_string, xml_property, url

def _maybe_text(n):
Expand Down
@@ -1,4 +1,4 @@
from sextante.servertools.geoserver.support import ResourceInfo, xml_property, write_string, bbox, \
from sextante.admintools.geoserver.support import ResourceInfo, xml_property, write_string, bbox, \
write_bbox, string_list, write_string_list, attribute_list, write_bool, url

def md_link(node):
Expand Down

0 comments on commit 3c5de39

Please sign in to comment.