Skip to content

Commit 80c1dda

Browse files
author
Giovanni Manghi
committedOct 21, 2014
add Processing clip using ogr2ogr to compensate for the bad Clip performances of the native QGIS Clip tool
1 parent fdf0436 commit 80c1dda

File tree

3 files changed

+199
-1
lines changed

3 files changed

+199
-1
lines changed
 

‎python/plugins/processing/algs/gdal/GdalOgrAlgorithmProvider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
from GridDataMetrics import GridDataMetrics
6767

6868
from ogr2ogr import Ogr2Ogr
69+
from ogr2ogrclip import Ogr2OgrClip
70+
from ogr2ogrclipextent import Ogr2OgrClipExtent
6971
from ogrinfo import OgrInfo
7072
from ogrsql import OgrSql
7173

@@ -119,7 +121,7 @@ def createAlgsList(self):
119121
ColorRelief(), GridInvDist(), GridAverage(), GridNearest(),
120122
GridDataMetrics(),
121123
# ----- OGR tools -----
122-
OgrInfo(), Ogr2Ogr(), OgrSql(),
124+
OgrInfo(), Ogr2Ogr(), Ogr2OgrClip(), Ogr2OgrClipExtent(), OgrSql(),
123125
]
124126

125127
# And then we add those that are created as python scripts
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ogr2ogrclip.py
6+
---------------------
7+
Date : November 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
Email : volayaf at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Victor Olaya'
21+
__date__ = 'November 2012'
22+
__copyright__ = '(C) 2012, Victor Olaya'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
from PyQt4.QtCore import *
31+
from PyQt4.QtGui import *
32+
33+
from qgis.core import *
34+
35+
from processing.core.parameters import ParameterVector
36+
from processing.core.parameters import ParameterString
37+
from processing.core.parameters import ParameterSelection
38+
from processing.core.outputs import OutputVector
39+
40+
from processing.tools.system import *
41+
42+
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
43+
from processing.algs.gdal.GdalUtils import GdalUtils
44+
45+
class Ogr2OgrClip(OgrAlgorithm):
46+
47+
OUTPUT_LAYER = 'OUTPUT_LAYER'
48+
INPUT_LAYER = 'INPUT_LAYER'
49+
CLIP_LAYER = 'CLIP_LAYER'
50+
OPTIONS = 'OPTIONS'
51+
52+
def defineCharacteristics(self):
53+
self.name = 'Clip vectors by polygon'
54+
self.group = '[OGR] Miscellaneous'
55+
56+
self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
57+
[ParameterVector.VECTOR_TYPE_ANY], False))
58+
self.addParameter(ParameterVector(self.CLIP_LAYER, 'Clip layer',
59+
[ParameterVector.VECTOR_TYPE_POLYGON], False))
60+
self.addParameter(ParameterString(self.OPTIONS, 'Creation Options',
61+
'', optional=True))
62+
63+
self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
64+
65+
def processAlgorithm(self, progress):
66+
inLayer = self.getParameterValue(self.INPUT_LAYER)
67+
ogrLayer = self.ogrConnectionString(inLayer)
68+
clipLayer = self.getParameterValue(self.CLIP_LAYER)
69+
ogrClipLayer = self.ogrConnectionString(clipLayer)
70+
71+
output = self.getOutputFromName(self.OUTPUT_LAYER)
72+
outFile = output.value
73+
74+
output = self.ogrConnectionString(outFile)
75+
options = unicode(self.getParameterValue(self.OPTIONS))
76+
77+
arguments = []
78+
arguments.append('-clipsrc')
79+
arguments.append(ogrClipLayer)
80+
if len(options) > 0:
81+
arguments.append(options)
82+
83+
arguments.append(output)
84+
arguments.append(ogrLayer)
85+
86+
commands = []
87+
if isWindows():
88+
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
89+
GdalUtils.escapeAndJoin(arguments)]
90+
else:
91+
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
92+
93+
GdalUtils.runGdal(commands, progress)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ogr2ogrclipextent.py
6+
---------------------
7+
Date : November 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
Email : volayaf at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Victor Olaya'
21+
__date__ = 'November 2012'
22+
__copyright__ = '(C) 2012, Victor Olaya'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
from PyQt4.QtCore import *
31+
from PyQt4.QtGui import *
32+
33+
from qgis.core import *
34+
35+
from processing.core.parameters import ParameterVector
36+
from processing.core.parameters import ParameterString
37+
from processing.core.parameters import ParameterSelection
38+
from processing.core.parameters import ParameterExtent
39+
from processing.core.outputs import OutputVector
40+
41+
from processing.tools.system import *
42+
43+
from processing.algs.gdal.OgrAlgorithm import OgrAlgorithm
44+
from processing.algs.gdal.GdalUtils import GdalUtils
45+
46+
class Ogr2OgrClipExtent(OgrAlgorithm):
47+
48+
OUTPUT_LAYER = 'OUTPUT_LAYER'
49+
INPUT_LAYER = 'INPUT_LAYER'
50+
CLIP_EXTENT = 'CLIP_EXTENT'
51+
OPTIONS = 'OPTIONS'
52+
53+
def defineCharacteristics(self):
54+
self.name = 'Clip vectors by extent'
55+
self.group = '[OGR] Miscellaneous'
56+
57+
self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
58+
[ParameterVector.VECTOR_TYPE_ANY], False))
59+
self.addParameter(ParameterExtent(self.CLIP_EXTENT,
60+
'Clip extent'))
61+
#self.addParameter(ParameterString(self.CLIP_EXTENT, 'Clip extent',
62+
# '', optional=False))
63+
self.addParameter(ParameterString(self.OPTIONS, 'Creation Options',
64+
'', optional=True))
65+
66+
self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
67+
68+
def processAlgorithm(self, progress):
69+
inLayer = self.getParameterValue(self.INPUT_LAYER)
70+
ogrLayer = self.ogrConnectionString(inLayer)
71+
clipExtent = self.getParameterValue(self.CLIP_EXTENT)
72+
ogrclipExtent = self.ogrConnectionString(clipExtent)
73+
74+
output = self.getOutputFromName(self.OUTPUT_LAYER)
75+
outFile = output.value
76+
77+
output = self.ogrConnectionString(outFile)
78+
options = unicode(self.getParameterValue(self.OPTIONS))
79+
80+
arguments = []
81+
regionCoords = ogrclipExtent.split(',')
82+
arguments.append('-spat')
83+
arguments.append(regionCoords[0])
84+
arguments.append(regionCoords[2])
85+
arguments.append(regionCoords[1])
86+
arguments.append(regionCoords[3])
87+
#arguments.append('-spat')
88+
#arguments.append(ogrclipExtent)
89+
90+
if len(options) > 0:
91+
arguments.append(options)
92+
93+
arguments.append(output)
94+
arguments.append(ogrLayer)
95+
96+
commands = []
97+
if isWindows():
98+
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
99+
GdalUtils.escapeAndJoin(arguments)]
100+
else:
101+
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
102+
103+
GdalUtils.runGdal(commands, progress)

0 commit comments

Comments
 (0)
Please sign in to comment.