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