Skip to content

Commit fd72ae8

Browse files
author
volayaf@gmail.com
committedMar 6, 2012
implemented saga resampling procedure
some ftools algorithms git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@29 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
1 parent 562ca20 commit fd72ae8

File tree

14 files changed

+542
-26
lines changed

14 files changed

+542
-26
lines changed
 

‎src/sextante/core/GeoAlgorithm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ def canBeExecuted(self, layersCount):
8484
def __str__(self):
8585
s = "ALGORITHM: " + self.name + "\n"
8686
for param in self.parameters:
87-
s+=(str(param) + "\n")
87+
s+=("\t" + str(param) + "\n")
8888
for out in self.outputs:
89-
s+=(str(out) + "\n")
89+
s+=("\t" + str(out) + "\n")
9090
s+=("\n")
9191
return s
9292

9393

9494
def commandLineName(self):
95-
return self.provider.getName().lower() + ":" + self.name.lower().replace(" ", "")
95+
return self.provider.getName().lower() + ":" + self.name.lower().replace(" ", "").replace(",","")
9696

9797

9898
def getOutputFromName(self, name):

‎src/sextante/core/Sextante.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from sextante.gui.SextantePostprocessing import SextantePostprocessing
1616
from sextante.modeler.ProviderIcons import ProviderIcons
1717
from sextante.r.RAlgorithmProvider import RAlgorithmProvider
18+
from sextante.parameters.ParameterSelection import ParameterSelection
1819

1920
class Sextante:
2021

@@ -143,6 +144,23 @@ def alglist(text=None):
143144
s+=(alg.name.ljust(50, "-") + "--->" + alg.commandLineName() + "\n")
144145
print s
145146

147+
148+
@staticmethod
149+
def algoptions(name):
150+
alg = Sextante.getAlgorithm(name)
151+
if alg != None:
152+
s =""
153+
for param in alg.parameters:
154+
if isinstance(param, ParameterSelection):
155+
s+=param.name + "(" + param.description + ")\n"
156+
i=0
157+
for option in param.options:
158+
s+= "\t" + str(i) + " - " + str(option) + "\n"
159+
i+=1
160+
print(s)
161+
else:
162+
print "Algorithm not found"
163+
146164
@staticmethod
147165
def alghelp(name):
148166
alg = Sextante.getAlgorithm(name)

‎src/sextante/ftools/Clip.py

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
from sextante.core.GeoAlgorithm import GeoAlgorithm
2+
import os.path
3+
from PyQt4 import QtGui
4+
from PyQt4.QtCore import *
5+
from PyQt4.QtGui import *
6+
from qgis.core import *
7+
from sextante.parameters.ParameterVector import ParameterVector
8+
from sextante.core.QGisLayers import QGisLayers
9+
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
10+
from sextante.outputs.OutputVector import OutputVector
11+
from sextante.ftools import ftools_utils
12+
from sextante.core.SextanteLog import SextanteLog
13+
from sextante.parameters.ParameterBoolean import ParameterBoolean
14+
15+
class Clip(GeoAlgorithm):
16+
17+
INPUT = "INPUT"
18+
INPUT2 = "INPUT2"
19+
OUTPUT = "OUTPUT"
20+
USE_SELECTED = "USE_SELECTED"
21+
USE_SELECTED2 = "USE_SELECTED2"
22+
23+
def getIcon(self):
24+
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/clip.png")
25+
26+
def processAlgorithm(self, progress):
27+
settings = QSettings()
28+
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
29+
output = self.getOutputValue(Clip.OUTPUT)
30+
useSelection = self.getParameterValue(Clip.USE_SELECTED)
31+
useSelection2 = self.getParameterValue(Clip.USE_SELECTED2)
32+
vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(Clip.INPUT))
33+
vlayerB = QGisLayers.getObjectFromUri(self.getParameterValue(Clip.INPUT2))
34+
GEOS_EXCEPT = True
35+
FEATURE_EXCEPT = True
36+
vproviderA = self.vlayerA.dataProvider()
37+
allAttrsA = vproviderA.attributeIndexes()
38+
vproviderA.select( allAttrsA )
39+
vproviderB = self.vlayerB.dataProvider()
40+
allAttrsB = vproviderB.attributeIndexes()
41+
vproviderB.select( allAttrsB )
42+
# check for crs compatibility
43+
crsA = vproviderA.crs()
44+
crsB = vproviderB.crs()
45+
if not crsA.isValid() or not crsB.isValid():
46+
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Invalid CRS. Results might be unexpected")
47+
else:
48+
if not crsA != crsB:
49+
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Non-matching CRSs. Results might be unexpected")
50+
fields = vproviderA.fields()
51+
writer = QgsVectorFileWriter( output, systemEncoding,fields, vproviderA.geometryType(), vproviderA.crs() )
52+
53+
inFeatA = QgsFeature()
54+
inFeatB = QgsFeature()
55+
outFeat = QgsFeature()
56+
index = ftools_utils.createIndex( vproviderB )
57+
vproviderA.rewind()
58+
nElement = 0
59+
# there is selection in input layer
60+
if useSelection:
61+
nFeat = vlayerA.selectedFeatureCount()
62+
selectionA = self.vlayerA.selectedFeatures()
63+
# we have selection in overlay layer
64+
if useSelection2:
65+
selectionB = self.vlayerB.selectedFeaturesIds()
66+
for inFeatA in selectionA:
67+
nElement += 1
68+
progress.setPercentage(int(nElement/nFeat * 100))
69+
geom = QgsGeometry( inFeatA.geometry() )
70+
int_geom = QgsGeometry( geom )
71+
atMap = inFeatA.attributeMap()
72+
intersects = index.intersects( geom.boundingBox() )
73+
found = False
74+
first = True
75+
for id in intersects:
76+
if id in selectionB:
77+
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
78+
tmpGeom = QgsGeometry( inFeatB.geometry() )
79+
if tmpGeom.intersects( geom ):
80+
found = True
81+
if first:
82+
outFeat.setGeometry( QgsGeometry( tmpGeom ) )
83+
first = False
84+
else:
85+
try:
86+
cur_geom = QgsGeometry( outFeat.geometry() )
87+
new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) )
88+
outFeat.setGeometry( QgsGeometry( new_geom ) )
89+
except:
90+
GEOS_EXCEPT = False
91+
break
92+
if found:
93+
try:
94+
cur_geom = QgsGeometry( outFeat.geometry() )
95+
new_geom = QgsGeometry( geom.intersection( cur_geom ) )
96+
if new_geom.wkbType() == 7:
97+
int_com = QgsGeometry( geom.combine( cur_geom ) )
98+
int_sym = QgsGeometry( geom.symDifference( cur_geom ) )
99+
new_geom = QgsGeometry( int_com.difference( int_sym ) )
100+
try:
101+
outFeat.setGeometry( new_geom )
102+
outFeat.setAttributeMap( atMap )
103+
writer.addFeature( outFeat )
104+
except:
105+
FEAT_EXCEPT = False
106+
continue
107+
except:
108+
GEOS_EXCEPT = False
109+
continue
110+
# we have no selection in overlay layer
111+
else:
112+
for inFeatA in selectionA:
113+
nElement += 1
114+
progress.setPercentage(int(nElement/nFeat * 100))
115+
geom = QgsGeometry( inFeatA.geometry() )
116+
atMap = inFeatA.attributeMap()
117+
intersects = index.intersects( geom.boundingBox() )
118+
found = False
119+
first = True
120+
for id in intersects:
121+
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
122+
tmpGeom = QgsGeometry( inFeatB.geometry() )
123+
if tmpGeom.intersects( geom ):
124+
found = True
125+
if first:
126+
outFeat.setGeometry( QgsGeometry( tmpGeom ) )
127+
first = False
128+
else:
129+
try:
130+
cur_geom = QgsGeometry( outFeat.geometry() )
131+
new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) )
132+
outFeat.setGeometry( QgsGeometry( new_geom ) )
133+
except:
134+
GEOS_EXCEPT = False
135+
break
136+
if found:
137+
try:
138+
cur_geom = QgsGeometry( outFeat.geometry() )
139+
new_geom = QgsGeometry( geom.intersection( cur_geom ) )
140+
if new_geom.wkbType() == 7:
141+
int_com = QgsGeometry( geom.combine( cur_geom ) )
142+
int_sym = QgsGeometry( geom.symDifference( cur_geom ) )
143+
new_geom = QgsGeometry( int_com.difference( int_sym ) )
144+
try:
145+
outFeat.setGeometry( new_geom )
146+
outFeat.setAttributeMap( atMap )
147+
writer.addFeature( outFeat )
148+
except:
149+
FEAT_EXCEPT = False
150+
continue
151+
except:
152+
GEOS_EXCEPT = False
153+
continue
154+
# there is no selection in input layer
155+
else:
156+
nFeat = vproviderA.featureCount()
157+
# we have selection in overlay layer
158+
if useSelection2:
159+
selectionB = self.vlayerB.selectedFeaturesIds()
160+
while vproviderA.nextFeature( inFeatA ):
161+
nElement += 1
162+
progress.setPercentage(int(nElement/nFeat * 100))
163+
geom = QgsGeometry( inFeatA.geometry() )
164+
atMap = inFeatA.attributeMap()
165+
intersects = index.intersects( geom.boundingBox() )
166+
found = False
167+
first = True
168+
for id in intersects:
169+
if id in selectionB:
170+
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
171+
tmpGeom = QgsGeometry( inFeatB.geometry() )
172+
if tmpGeom.intersects( geom ):
173+
found = True
174+
if first:
175+
outFeat.setGeometry( QgsGeometry( tmpGeom ) )
176+
first = False
177+
else:
178+
try:
179+
cur_geom = QgsGeometry( outFeat.geometry() )
180+
new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) )
181+
outFeat.setGeometry( QgsGeometry( new_geom ) )
182+
except:
183+
GEOS_EXCEPT = False
184+
break
185+
if found:
186+
try:
187+
cur_geom = QgsGeometry( outFeat.geometry() )
188+
new_geom = QgsGeometry( geom.intersection( cur_geom ) )
189+
if new_geom.wkbType() == 7:
190+
int_com = QgsGeometry( geom.combine( cur_geom ) )
191+
int_sym = QgsGeometry( geom.symDifference( cur_geom ) )
192+
new_geom = QgsGeometry( int_com.difference( int_sym ) )
193+
try:
194+
outFeat.setGeometry( new_geom )
195+
outFeat.setAttributeMap( atMap )
196+
writer.addFeature( outFeat )
197+
except:
198+
FEAT_EXCEPT = False
199+
continue
200+
except:
201+
GEOS_EXCEPT = False
202+
continue
203+
# we have no selection in overlay layer
204+
else:
205+
while vproviderA.nextFeature( inFeatA ):
206+
nElement += 1
207+
progress.setPercentage(int(nElement/nFeat * 100))
208+
geom = QgsGeometry( inFeatA.geometry() )
209+
atMap = inFeatA.attributeMap()
210+
intersects = index.intersects( geom.boundingBox() )
211+
first = True
212+
found = False
213+
if len( intersects ) > 0:
214+
for id in intersects:
215+
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
216+
tmpGeom = QgsGeometry( inFeatB.geometry() )
217+
if tmpGeom.intersects( geom ):
218+
found = True
219+
if first:
220+
outFeat.setGeometry( QgsGeometry( tmpGeom ) )
221+
first = False
222+
else:
223+
try:
224+
cur_geom = QgsGeometry( outFeat.geometry() )
225+
new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) )
226+
outFeat.setGeometry( QgsGeometry( new_geom ) )
227+
except:
228+
GEOS_EXCEPT = False
229+
break
230+
if found:
231+
try:
232+
cur_geom = QgsGeometry( outFeat.geometry() )
233+
new_geom = QgsGeometry( geom.intersection( cur_geom ) )
234+
if new_geom.wkbType() == 7:
235+
int_com = QgsGeometry( geom.combine( cur_geom ) )
236+
int_sym = QgsGeometry( geom.symDifference( cur_geom ) )
237+
new_geom = QgsGeometry( int_com.difference( int_sym ) )
238+
try:
239+
outFeat.setGeometry( new_geom )
240+
outFeat.setAttributeMap( atMap )
241+
writer.addFeature( outFeat )
242+
except:
243+
FEAT_EXCEPT = False
244+
continue
245+
except:
246+
GEOS_EXCEPT = False
247+
continue
248+
del writer
249+
if not GEOS_EXCEPT:
250+
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing clip")
251+
if not FEATURE_EXCEPT:
252+
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing clip")
253+
254+
def defineCharacteristics(self):
255+
self.name = "Clip"
256+
self.group = "Geoprocessing tools"
257+
self.addParameter(ParameterVector(Clip.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
258+
self.addParameter(ParameterVector(Clip.INPUT2, "Clip layer", ParameterVector.VECTOR_TYPE_ANY))
259+
self.addParameter(ParameterBoolean(Clip.USE_SELECTED, "Use selected features (input)", False))
260+
self.addParameter(ParameterBoolean(Clip.USE_SELECTED2, "Use selected features (clip)", False))
261+
self.addOutput(OutputVector(Clip.OUTPUT, "Clipped"))

‎src/sextante/ftools/Difference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def processAlgorithm(self, progress):
4444
crsA = vproviderA.crs()
4545
crsB = vproviderB.crs()
4646
if not crsA.isValid() or not crsB.isValid():
47-
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Differnece. Invalid CRS. Results might be unexpected")
47+
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Difference. Invalid CRS. Results might be unexpected")
4848
else:
4949
if not crsA != crsB:
5050
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Difference. Non-matching CRSs. Results might be unexpected")

‎src/sextante/ftools/FToolsAlgorithmProvider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from sextante.ftools.Dissolve import Dissolve
2424
from sextante.ftools.Difference import Difference
2525
from sextante.ftools.Intersection import Intersection
26+
from sextante.ftools.Union import Union
27+
from sextante.ftools.Clip import Clip
2628

2729
class FToolsAlgorithmProvider(AlgorithmProvider):
2830

@@ -39,7 +41,7 @@ def _loadAlgorithms(self):
3941
SumLines(), BasicStatistics(), PointsInPolygon(),
4042
NearestNeighbourAnalysis(), MeanCoords(), LinesIntersection(),
4143
ConvexHull(), FixedDistanceBuffer(), VariableDistanceBuffer(),
42-
Dissolve(), Difference(), Intersection()]
44+
Dissolve(), Difference(), Intersection(), Union(), Clip()]
4345
for alg in self.algs:
4446
alg.provider = self
4547

‎src/sextante/ftools/Intersection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ def processAlgorithm(self, progress):
4343
crsA = vproviderA.crs()
4444
crsB = vproviderB.crs()
4545
if not crsA.isValid() or not crsB.isValid():
46-
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Differnece. Invalid CRS. Results might be unexpected")
46+
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Invalid CRS. Results might be unexpected")
4747
else:
4848
if not crsA != crsB:
49-
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Difference. Non-matching CRSs. Results might be unexpected")
49+
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Non-matching CRSs. Results might be unexpected")
5050
fields = ftools_utils.combineVectorFields( self.vlayerA, self.vlayerB )
5151
longNames = ftools_utils.checkFieldNameLength( fields )
5252
if not longNames.isEmpty():

0 commit comments

Comments
 (0)
Please sign in to comment.