2014-10-21 16:48:19 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ogr2ogrclipextent.py
|
|
|
|
---------------------
|
|
|
|
Date : November 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2014-10-21 16:48:19 +01:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'November 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
|
|
|
|
|
|
|
# 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 ParameterExtent
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
|
2016-01-22 15:45:09 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
2014-10-21 16:48:19 +01:00
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
|
|
|
|
2016-01-22 15:45:09 +02:00
|
|
|
from processing.tools.system import isWindows
|
|
|
|
from processing.tools.vector import ogrConnectionString, ogrLayerName
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2016-01-22 15:45:09 +02:00
|
|
|
class Ogr2OgrClipExtent(GdalAlgorithm):
|
2014-10-21 16:48:19 +01:00
|
|
|
|
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
CLIP_EXTENT = 'CLIP_EXTENT'
|
|
|
|
OPTIONS = 'OPTIONS'
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2014-10-21 16:48:19 +01:00
|
|
|
self.addParameter(ParameterExtent(self.CLIP_EXTENT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Clip extent')))
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterString(self.OPTIONS,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Additional creation options'), '', optional=True))
|
2014-10-21 16:48:19 +01:00
|
|
|
|
2015-05-18 09:29:41 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Clipped (extent)')))
|
2014-10-21 16:48:19 +01:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'clipvectorsbyextent'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Clip vectors by extent')
|
|
|
|
|
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector geoprocessing')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def getConsoleCommands(self, parameters):
|
2014-10-21 16:48:19 +01:00
|
|
|
inLayer = self.getParameterValue(self.INPUT_LAYER)
|
2016-01-22 15:45:09 +02:00
|
|
|
ogrLayer = ogrConnectionString(inLayer)[1:-1]
|
2014-10-21 16:48:19 +01:00
|
|
|
clipExtent = self.getParameterValue(self.CLIP_EXTENT)
|
2017-05-17 07:41:15 +10:00
|
|
|
if not clipExtent:
|
|
|
|
clipExtent = QgsProcessingUtils.combineLayerExtents([inLayer])
|
2014-10-21 16:48:19 +01:00
|
|
|
|
|
|
|
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
|
|
|
outFile = output.value
|
|
|
|
|
2016-01-22 15:45:09 +02:00
|
|
|
output = ogrConnectionString(outFile)
|
2016-09-21 18:24:26 +02:00
|
|
|
options = str(self.getParameterValue(self.OPTIONS))
|
2014-10-21 16:48:19 +01:00
|
|
|
|
|
|
|
arguments = []
|
2015-09-22 23:03:12 +01:00
|
|
|
regionCoords = clipExtent.split(',')
|
2014-10-21 16:48:19 +01:00
|
|
|
arguments.append('-spat')
|
|
|
|
arguments.append(regionCoords[0])
|
|
|
|
arguments.append(regionCoords[2])
|
|
|
|
arguments.append(regionCoords[1])
|
2014-11-13 23:13:13 +01:00
|
|
|
arguments.append(regionCoords[3])
|
2015-02-05 18:37:59 +00:00
|
|
|
arguments.append('-clipsrc spat_extent')
|
2014-10-21 16:48:19 +01:00
|
|
|
|
2017-02-08 11:22:21 +00:00
|
|
|
if options is not None and len(options.strip()) > 0:
|
2014-10-21 16:48:19 +01:00
|
|
|
arguments.append(options)
|
|
|
|
|
|
|
|
arguments.append(output)
|
|
|
|
arguments.append(ogrLayer)
|
2016-01-22 15:45:09 +02:00
|
|
|
arguments.append(ogrLayerName(inLayer))
|
2014-10-21 16:48:19 +01:00
|
|
|
|
|
|
|
commands = []
|
|
|
|
if isWindows():
|
|
|
|
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
|
|
|
|
GdalUtils.escapeAndJoin(arguments)]
|
|
|
|
else:
|
|
|
|
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
|
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
return commands
|
2015-05-21 15:43:47 +02:00
|
|
|
|
|
|
|
def commandName(self):
|
2015-08-22 14:29:41 +02:00
|
|
|
return "ogr2ogr"
|