2014-10-21 16:48:19 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ogr2ogrclip.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__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.outputs import OutputVector
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.tools.system import isWindows
|
2014-10-21 16:48:19 +01:00
|
|
|
|
|
|
|
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
|
|
|
|
from processing.algs.gdal.GdalUtils import GdalUtils
|
|
|
|
|
|
|
|
class Ogr2OgrClip(OgrAlgorithm):
|
|
|
|
|
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
CLIP_LAYER = 'CLIP_LAYER'
|
|
|
|
OPTIONS = 'OPTIONS'
|
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
|
|
|
self.name = 'Clip vectors by polygon'
|
2014-11-05 18:44:45 +00:00
|
|
|
self.group = '[OGR] Geoprocessing'
|
2014-10-21 16:48:19 +01:00
|
|
|
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
|
|
|
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY], False))
|
|
|
|
self.addParameter(ParameterVector(self.CLIP_LAYER,
|
|
|
|
self.tr('Clip layer'), [ParameterVector.VECTOR_TYPE_POLYGON], False))
|
|
|
|
self.addParameter(ParameterString(self.OPTIONS,
|
|
|
|
self.tr('Additional creation options'), '', optional=True))
|
2014-10-21 16:48:19 +01:00
|
|
|
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Output layer')))
|
2014-10-21 16:48:19 +01:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
|
|
|
inLayer = self.getParameterValue(self.INPUT_LAYER)
|
2015-02-05 18:37:59 +00:00
|
|
|
ogrLayer = self.ogrConnectionString(inLayer)[1:-1]
|
2014-10-21 16:48:19 +01:00
|
|
|
clipLayer = self.getParameterValue(self.CLIP_LAYER)
|
2015-02-10 08:49:00 +00:00
|
|
|
ogrClipLayer = self.ogrConnectionString(clipLayer)[1:-1]
|
2014-10-21 16:48:19 +01:00
|
|
|
|
|
|
|
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
|
|
|
outFile = output.value
|
|
|
|
|
|
|
|
output = self.ogrConnectionString(outFile)
|
|
|
|
options = unicode(self.getParameterValue(self.OPTIONS))
|
|
|
|
|
|
|
|
arguments = []
|
|
|
|
arguments.append('-clipsrc')
|
2014-11-13 23:13:13 +01:00
|
|
|
arguments.append(ogrClipLayer)
|
2015-02-10 08:49:00 +00:00
|
|
|
arguments.append("-clipsrclayer")
|
|
|
|
arguments.append(self.ogrLayerName(clipLayer))
|
2014-10-21 16:48:19 +01:00
|
|
|
if len(options) > 0:
|
|
|
|
arguments.append(options)
|
|
|
|
|
|
|
|
arguments.append(output)
|
|
|
|
arguments.append(ogrLayer)
|
2015-02-05 18:37:59 +00:00
|
|
|
arguments.append(self.ogrLayerName(inLayer))
|
2015-02-07 13:38:35 +01:00
|
|
|
|
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-01-13 10:49:08 +02:00
|
|
|
GdalUtils.runGdal(commands, progress)
|