add new geoprocessing tools based on ogr2ogr and sqlite sql dialect

This commit is contained in:
Giovanni Manghi 2015-02-05 22:55:44 +00:00
parent fb344ff1b6
commit a6af682b8b
5 changed files with 512 additions and 1 deletions

View File

@ -70,6 +70,10 @@ from ogr2ogrclip import Ogr2OgrClip
from ogr2ogrclipextent import Ogr2OgrClipExtent
from ogr2ogrtopostgis import Ogr2OgrToPostGis
from ogr2ogrtopostgislist import Ogr2OgrToPostGisList
from ogr2ogrpointsonlines import Ogr2OgrPointsOnLines
from ogr2ogrbuffer import Ogr2OgrBuffer
from ogr2ogrdissolve import Ogr2OgrDissolve
from ogr2ogronesidebuffer import Ogr2OgrOneSideBuffer
from ogrinfo import OgrInfo
from ogrsql import OgrSql
@ -124,7 +128,8 @@ class GdalOgrAlgorithmProvider(AlgorithmProvider):
GridDataMetrics(),
# ----- OGR tools -----
OgrInfo(), Ogr2Ogr(), Ogr2OgrClip(), Ogr2OgrClipExtent(),
Ogr2OgrToPostGis(), Ogr2OgrToPostGisList(), OgrSql(),
Ogr2OgrToPostGis(), Ogr2OgrToPostGisList(), Ogr2OgrPointsOnLines(),
Ogr2OgrBuffer(), Ogr2OgrDissolve(), Ogr2OgrOneSideBuffer(), OgrSql(),
]
# And then we add those that are created as python scripts

View File

@ -0,0 +1,122 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
ogr2ogrbuffer.py
---------------------
Date : Janaury 2015
Copyright : (C) 2015 by Giovanni Manghi
Email : giovanni dot manghi at naturalgis dot pt
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Giovanni Manghi'
__date__ = 'January 2015'
__copyright__ = '(C) 2015, Giovanni Manghi'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputVector
from processing.tools.system import isWindows
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
class Ogr2OgrBuffer(OgrAlgorithm):
OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT_LAYER = 'INPUT_LAYER'
GEOMETRY = 'GEOMETRY'
DISTANCE = 'DISTANCE'
DISSOLVEALL = 'DISSOLVEALL'
FIELD = 'FIELD'
MULTI = 'MULTI'
OPTIONS = 'OPTIONS'
def defineCharacteristics(self):
self.name = 'Buffer vectors'
self.group = '[OGR] Geoprocessing'
self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
[ParameterVector.VECTOR_TYPE_ANY], False))
self.addParameter(ParameterString(self.GEOMETRY, 'Geometry column name ("geometry" for Shapefiles, may be different for other formats)',
'geometry', optional=False))
self.addParameter(ParameterString(self.DISTANCE,'Buffer distance', '1000', optional=False))
self.addParameter(ParameterBoolean(self.DISSOLVEALL,
'Dissolve all results?', False))
self.addParameter(ParameterTableField(self.FIELD, 'Dissolve by attribute',
self.INPUT_LAYER, optional=True))
self.addParameter(ParameterBoolean(self.MULTI,
'Output as singlepart geometries (only used when dissolving by attribute)?', False))
self.addParameter(ParameterString(self.OPTIONS, 'Additional creation options (see ogr2ogr manual)',
'', optional=True))
self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
def processAlgorithm(self, progress):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = self.ogrConnectionString(inLayer)[1:-1]
layername = "'" + self.ogrLayerName(inLayer) + "'"
geometry = unicode(self.getParameterValue(self.GEOMETRY))
distance = unicode(self.getParameterValue(self.DISTANCE))
dissolveall = self.getParameterValue(self.DISSOLVEALL)
field = unicode(self.getParameterValue(self.FIELD))
multi = self.getParameterValue(self.MULTI)
output = self.getOutputFromName(self.OUTPUT_LAYER)
outFile = output.value
output = self.ogrConnectionString(outFile)
options = unicode(self.getParameterValue(self.OPTIONS))
arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(self.ogrLayerName(inLayer))
if dissolveall or field != 'None':
arguments.append('-dialect sqlite -sql "SELECT ST_Union(ST_Buffer(')
else:
arguments.append('-dialect sqlite -sql "SELECT ST_Buffer(')
arguments.append(geometry)
arguments.append(',')
arguments.append(distance)
if dissolveall or field != 'None':
arguments.append(')),*')
else:
arguments.append('),*')
arguments.append('FROM')
arguments.append(layername)
if field != 'None':
arguments.append('GROUP')
arguments.append('BY')
arguments.append(field)
arguments.append('"')
if field != 'None' and multi:
arguments.append('-explodecollections')
if len(options) > 0:
arguments.append(options)
commands = []
if isWindows():
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
GdalUtils.runGdal(commands, progress)

View File

@ -0,0 +1,141 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
ogr2ogrdissolve.py
---------------------
Date : Janaury 2015
Copyright : (C) 2015 by Giovanni Manghi
Email : giovanni dot manghi at naturalgis dot pt
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Giovanni Manghi'
__date__ = 'January 2015'
__copyright__ = '(C) 2015, Giovanni Manghi'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputVector
from processing.tools.system import isWindows
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
class Ogr2OgrDissolve(OgrAlgorithm):
OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT_LAYER = 'INPUT_LAYER'
GEOMETRY = 'GEOMETRY'
FIELD = 'FIELD'
MULTI = 'MULTI'
COUNT = 'COUNT'
STATS = 'STATS'
STATSATT = 'STATSATT'
AREA = 'AREA'
FIELDS = 'FIELDS'
OPTIONS = 'OPTIONS'
def defineCharacteristics(self):
self.name = 'Dissolve polygons'
self.group = '[OGR] Geoprocessing'
self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
[ParameterVector.VECTOR_TYPE_POLYGON], False))
self.addParameter(ParameterString(self.GEOMETRY, 'Geometry column name ("geometry" for Shapefiles, may be different for other formats)',
'geometry', optional=False))
self.addParameter(ParameterTableField(self.FIELD, 'Dissolve field',
self.INPUT_LAYER))
self.addParameter(ParameterBoolean(self.MULTI,
'Output as multipart geometries?', True))
self.addParameter(ParameterBoolean(self.FIELDS,
'Keep input attributes?', False))
self.addParameter(ParameterBoolean(self.COUNT,
'Count dissolved features?', False))
self.addParameter(ParameterBoolean(self.AREA,
'Compute area and perimeter of dissolved features?', False))
self.addParameter(ParameterBoolean(self.STATS,
'Compute min/max/sum/mean for the following numeric attribute?', False))
self.addParameter(ParameterTableField(self.STATSATT, 'Numeric attribute to compute dissolved features stats',
self.INPUT_LAYER))
self.addParameter(ParameterString(self.OPTIONS, 'Additional creation options (see ogr2ogr manual)',
'', optional=True))
self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
def processAlgorithm(self, progress):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = self.ogrConnectionString(inLayer)[1:-1]
layername = "'" + self.ogrLayerName(inLayer) + "'"
geometry = unicode(self.getParameterValue(self.GEOMETRY))
field = unicode(self.getParameterValue(self.FIELD))
statsatt = unicode(self.getParameterValue(self.STATSATT))
stats = self.getParameterValue(self.STATS)
area = self.getParameterValue(self.AREA)
multi = self.getParameterValue(self.MULTI)
count = self.getParameterValue(self.COUNT)
fields = self.getParameterValue(self.FIELDS)
#dsUri = QgsDataSourceURI(self.getParameterValue(self.INPUT_LAYER))
#geomColumn = dsUri.geometryColumn()
querystart = '-dialect sqlite -sql "SELECT ST_Union(' + geometry + ')'
queryend = ' FROM ' + layername + ' GROUP BY ' + field + '"'
if fields:
queryfields = ",*"
else:
queryfields = "," + field
if count:
querycount = ", COUNT(" + geometry + ") AS count"
else:
querycount = ""
if stats:
querystats = ", SUM(" + statsatt + ") AS sum_diss, MIN(" + statsatt + ") AS min_diss, MAX(" + statsatt + ") AS max_diss, AVG(" + statsatt + ") AS avg_diss"
else:
querystats = ""
if area:
queryarea = ", SUM(ST_area(" + geometry + ")) AS area_diss, ST_perimeter(ST_union(" + geometry + ")) AS peri_diss"
else:
queryarea = ""
query = querystart + queryfields + querycount + querystats + queryarea + queryend
output = self.getOutputFromName(self.OUTPUT_LAYER)
outFile = output.value
output = self.ogrConnectionString(outFile)
options = unicode(self.getParameterValue(self.OPTIONS))
arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(self.ogrLayerName(inLayer))
arguments.append(query)
if not multi:
arguments.append('-explodecollections')
if len(options) > 0:
arguments.append(options)
commands = []
if isWindows():
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
GdalUtils.runGdal(commands, progress)

View File

@ -0,0 +1,145 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
ogr2ogronesidebuffer.py
---------------------
Date : Janaury 2015
Copyright : (C) 2015 by Giovanni Manghi
Email : giovanni dot manghi at naturalgis dot pt
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Giovanni Manghi'
__date__ = 'January 2015'
__copyright__ = '(C) 2015, Giovanni Manghi'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
from processing.tools.system import isWindows
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
class Ogr2OgrOneSideBuffer(OgrAlgorithm):
OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT_LAYER = 'INPUT_LAYER'
OPERATION = 'OPERATION'
OPERATIONLIST = ['Single Side Buffer','Offset Curve']
GEOMETRY = 'GEOMETRY'
RADIUS = 'RADIUS'
LEFTRIGHT = 'LEFTRIGHT'
LEFTRIGHTLIST = ['Right','Left']
DISSOLVEALL = 'DISSOLVEALL'
FIELD = 'FIELD'
MULTI = 'MULTI'
OPTIONS = 'OPTIONS'
def defineCharacteristics(self):
self.name = 'Single sided buffers (and offset lines) for lines'
self.group = '[OGR] Geoprocessing'
self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
[ParameterVector.VECTOR_TYPE_LINE], False))
self.addParameter(ParameterSelection(self.OPERATION, 'Buffer or Offset Curve?',self.OPERATIONLIST, 0))
self.addParameter(ParameterString(self.GEOMETRY, 'Geometry column name ("geometry" for Shapefiles, may be different for other formats)',
'geometry', optional=False))
self.addParameter(ParameterString(self.RADIUS,'Buffer distance', '1000', optional=False))
self.addParameter(ParameterSelection(self.LEFTRIGHT, 'Left or Right buffer',self.LEFTRIGHTLIST, 0))
self.addParameter(ParameterBoolean(self.DISSOLVEALL,
'Dissolve all results?', False))
self.addParameter(ParameterTableField(self.FIELD, 'Dissolve by attribute',
self.INPUT_LAYER, optional=True))
self.addParameter(ParameterBoolean(self.MULTI,
'Output as singlepart geometries (only used when dissolving by attribute)?', False))
self.addParameter(ParameterString(self.OPTIONS, 'Additional creation options (see ogr2ogr manual)',
'', optional=True))
self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
def processAlgorithm(self, progress):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = self.ogrConnectionString(inLayer)[1:-1]
layername = "'" + self.ogrLayerName(inLayer) + "'"
operation = self.OPERATIONLIST[self.getParameterValue(self.OPERATION)]
geometry = unicode(self.getParameterValue(self.GEOMETRY))
distance = unicode(self.getParameterValue(self.RADIUS))
leftright = self.LEFTRIGHTLIST[self.getParameterValue(self.LEFTRIGHT)]
dissolveall = self.getParameterValue(self.DISSOLVEALL)
field = unicode(self.getParameterValue(self.FIELD))
multi = self.getParameterValue(self.MULTI)
output = self.getOutputFromName(self.OUTPUT_LAYER)
outFile = output.value
output = self.ogrConnectionString(outFile)
options = unicode(self.getParameterValue(self.OPTIONS))
arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(self.ogrLayerName(inLayer))
if dissolveall or field != 'None':
if operation == 'Single Side Buffer':
arguments.append('-dialect sqlite -sql "SELECT ST_Union(ST_SingleSidedBuffer(')
else:
arguments.append('-dialect sqlite -sql "SELECT ST_Union(ST_OffsetCurve(')
else:
if operation == 'Single Side Buffer':
arguments.append('-dialect sqlite -sql "SELECT ST_SingleSidedBuffer(')
else:
arguments.append('-dialect sqlite -sql "SELECT ST_OffsetCurve(')
arguments.append(geometry)
arguments.append(',')
arguments.append(distance)
if dissolveall or field != 'None':
if leftright == 'Left':
arguments.append(',0)),*')
else:
arguments.append(',1)),*')
else:
if leftright == 'Left':
arguments.append(',0),*')
else:
arguments.append(',1),*')
arguments.append('FROM')
arguments.append(layername)
if field != 'None':
arguments.append('GROUP')
arguments.append('BY')
arguments.append(field)
arguments.append('"')
if field != 'None' and multi:
arguments.append('-explodecollections')
if len(options) > 0:
arguments.append(options)
commands = []
if isWindows():
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
GdalUtils.runGdal(commands, progress)

View File

@ -0,0 +1,98 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
ogr2ogrpointsonlines.py
---------------------
Date : Janaury 2015
Copyright : (C) 2015 by Giovanni Manghi
Email : giovanni dot manghi at naturalgis dot pt
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = 'Giovanni Manghi'
__date__ = 'January 2015'
__copyright__ = '(C) 2015, Giovanni Manghi'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean
from processing.core.outputs import OutputVector
from processing.tools.system import isWindows
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
class Ogr2OgrPointsOnLines(OgrAlgorithm):
OUTPUT_LAYER = 'OUTPUT_LAYER'
INPUT_LAYER = 'INPUT_LAYER'
DISTANCE = 'DISTANCE'
GEOMETRY = 'GEOMETRY'
OPTIONS = 'OPTIONS'
def defineCharacteristics(self):
self.name = 'Create points along lines'
self.group = '[OGR] Geoprocessing'
self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
[ParameterVector.VECTOR_TYPE_LINE], False))
self.addParameter(ParameterString(self.GEOMETRY, 'Geometry column name ("geometry" for Shapefiles, may be different for other formats)',
'geometry', optional=False))
self.addParameter(ParameterNumber(self.DISTANCE, 'Distance from line start represented as fraction of line length', 0, 1, 0.5))
self.addParameter(ParameterString(self.OPTIONS, 'Additional creation options (see ogr2ogr manual)',
'', optional=True))
self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
def processAlgorithm(self, progress):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = self.ogrConnectionString(inLayer)[1:-1]
layername = "'" + self.ogrLayerName(inLayer) + "'"
distance = unicode(self.getParameterValue(self.DISTANCE))
geometry = unicode(self.getParameterValue(self.GEOMETRY))
output = self.getOutputFromName(self.OUTPUT_LAYER)
outFile = output.value
output = self.ogrConnectionString(outFile)
options = unicode(self.getParameterValue(self.OPTIONS))
arguments = []
arguments.append(output)
arguments.append(ogrLayer)
arguments.append(self.ogrLayerName(inLayer))
arguments.append('-dialect sqlite -sql "SELECT ST_Line_Interpolate_Point(')
arguments.append(geometry)
arguments.append(',')
arguments.append(distance)
arguments.append('),*')
arguments.append('FROM')
arguments.append(layername)
arguments.append('"')
if len(options) > 0:
arguments.append(options)
commands = []
if isWindows():
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
GdalUtils.runGdal(commands, progress)