Skip to content

Commit 2a2d944

Browse files
committedFeb 2, 2013
more api updates in ftools
1 parent 63cc49b commit 2a2d944

File tree

7 files changed

+70
-66
lines changed

7 files changed

+70
-66
lines changed
 

‎python/plugins/fTools/tools/doGeoprocessing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ def union( self ):
11571157
# in geometries of different types
11581158
# produced by the intersection
11591159
# fix #3549
1160-
gList = ftools_utils.getGeomType( geom.wkbType() )
1160+
gList = ftools_utils.getGeomType( geom.wkbType() )
11611161
if int_geom.wkbType() in gList:
11621162
try:
11631163
outFeat.setGeometry( int_geom )

‎python/plugins/fTools/tools/doRandPoints.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,12 @@ def loopThruPolygons(self, inLayer, numRand, design):
239239
sGeom = QgsGeometry()
240240
sPoints = []
241241
if design == self.tr("field"):
242-
for (i, attr) in sProvider.fields().iteritems():
242+
i = 0
243+
for attr in sProvider.fields():
243244
if (unicode(numRand) == attr.name()):
244245
index = i #get input field index
245246
break
247+
i += 1
246248
count = 10.00
247249
add = 60.00 / sProvider.featureCount()
248250
sFit = sProvider.getFeatures()

‎python/plugins/fTools/tools/ftools_utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def createUniqueFieldName( field ):
183183
# Return list of field names with more than 10 characters length
184184
def checkFieldNameLength( fieldList ):
185185
longNames = QStringList()
186-
for num, field in fieldList.iteritems():
186+
for field in fieldList:
187187
if field.name().size() > 10:
188188
longNames << unicode( field.name() )
189189
return longNames
@@ -209,7 +209,7 @@ def getLayerNames( vTypes ):
209209
def getFieldNames( vlayer ):
210210
fieldmap = getFieldList( vlayer )
211211
fieldlist = []
212-
for name, field in fieldmap.iteritems():
212+
for field in fieldmap:
213213
if not field.name() in fieldlist:
214214
fieldlist.append( unicode( field.name() ) )
215215
return sorted( fieldlist, cmp=locale.strcoll )
@@ -299,14 +299,12 @@ def openDialog( parent, filtering="Shapefiles (*.shp *.SHP)", dialogMode="Single
299299
dirName = settings.value( "/UI/lastShapefileDir" ).toString()
300300
encode = settings.value( "/UI/encoding" ).toString()
301301
fileDialog = QgsEncodingFileDialog( parent, "Save output shapefile", dirName, QString(filtering), encode )
302-
#fileDialog.setFileMode( QFileDialog.AnyFile )
303302
fileDialog.setFileMode( QFileDialog.ExistingFiles )
304303
fileDialog.setAcceptMode( QFileDialog.AcceptOpen )
305304
if not fileDialog.exec_() == QDialog.Accepted:
306305
return None, None
307306
files = fileDialog.selectedFiles()
308307
settings.setValue("/UI/lastShapefileDir", QVariant( QFileInfo( unicode( files.first() ) ).absolutePath() ) )
309-
#return ( unicode( files.first() ), unicode( fileDialog.encoding() ) )
310308
if dialogMode == "SingleFile":
311309
return ( unicode( files.first() ), unicode( fileDialog.encoding() ) )
312310
else:

‎python/plugins/mapserver_export/ms_export.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def writeMapLayers(self):
553553
# TODO: add epsg to all METADATA tags??
554554
except:
555555
print "ERROR while trying to write ows_srs METADATA"
556-
pass
556+
pass
557557
layer_def += " 'wms_format' '" + format + "'\n"
558558
layer_def += " 'wms_style' '" + ','.join(wmsStyles) + "'\n"
559559
layer_def += " END\n"
@@ -687,9 +687,11 @@ def getPrimaryKey(self, layerId, tableName):
687687
intTypes = [QVariant.Int, QVariant.LongLong, QVariant.UInt, QVariant.ULongLong]
688688

689689
integerFields = []
690-
for id, field in fields.iteritems():
690+
id = 0
691+
for field in fields:
691692
if field.type() in intTypes:
692693
integerFields.append(id)
694+
id += 1
693695

694696
# fid end
695697
fidIntegerFields = []

‎python/plugins/sextante/algs/ftools/Intersection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def processAlgorithm(self, progress):
9595
int_geom = QgsGeometry( int_com.difference( int_sym ) )
9696
try:
9797
outFeat.setGeometry( int_geom )
98-
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
98+
outFeat.setAttributeMap( atMapA.extend( atMapB ) )
9999
writer.addFeature( outFeat )
100100
except:
101101
FEATURE_EXCEPT = False

‎python/plugins/sextante/algs/ftools/Union.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def processAlgorithm(self, progress):
133133
int_geom = QgsGeometry( i )
134134
try:
135135
outFeat.setGeometry( int_geom )
136-
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
136+
outFeat.setAttributes( atMapA.extend( atMapB ) )
137137
writer.addFeature( outFeat )
138138
except Exception, err:
139139
FEATURE_EXCEPT = False

‎python/plugins/sextante/algs/ftools/ftools_utils.py

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* *
1717
***************************************************************************
1818
"""
19+
1920
from sextante.core.QGisLayers import QGisLayers
2021

2122
__author__ = 'Carson Farmer, Victor Olaya'
@@ -27,7 +28,6 @@
2728
# Utility functions
2829
# -------------------------------------------------
2930
#
30-
# combineVectorAttributes( QgsAttributeMap, QgsAttributeMap )
3131
# convertFieldNameType( QgsField.name() )
3232
# combineVectorFields( QgsVectorLayer, QgsVectorLayer )
3333
# checkCRSCompatibility( QgsCoordinateReferenceSystem, QgsCoordinateReferenceSystem )
@@ -42,12 +42,12 @@
4242
# getFieldNames( QgsVectorLayer )
4343
# getVectorLayerByName( QgsVectorLayer.name() )
4444
# getFieldList( QgsVectorLayer )
45-
# createIndex( QgsVectorDataProvider )
45+
# createIndex( QgsFeatureList )
4646
# addShapeToCanvas( QString *file path )
4747
# getUniqueValues( QgsVectorDataProvider, int *field id )
4848
# saveDialog( QWidget *parent )
4949
# getFieldType( QgsVectorLayer, QgsField.name() )
50-
# getUniqueValuesCount( QgsVectorLayer, int fieldIndex, bool useSelection ):
50+
# getUniqueValuesCount( QgsVectorLayer, int fieldIndex )
5151
#
5252
# -------------------------------------------------
5353

@@ -56,14 +56,7 @@
5656
from qgis.core import *
5757
from qgis.gui import *
5858

59-
# From two input attribute maps, create single attribute map
60-
def combineVectorAttributes( atMapA, atMapB ):
61-
attribA = atMapA.values()
62-
lengthA = len(attribA)
63-
attribB = atMapB.values()
64-
lengthB = len(attribB)
65-
attribA.extend( attribB )
66-
return dict( zip( range( 0, lengthB + lengthA ), attribA ) )
59+
import locale
6760

6861
# For use with memory provider/layer, converts full field type to simple string
6962
def convertFieldNameType( inName ):
@@ -76,12 +69,10 @@ def convertFieldNameType( inName ):
7669

7770
# From two input field maps, create single field map
7871
def combineVectorFields( layerA, layerB ):
79-
fieldsA = layerA.dataProvider().fields().values()
80-
fieldsB = layerB.dataProvider().fields().values()
72+
fieldsA = layerA.dataProvider().fields()
73+
fieldsB = layerB.dataProvider().fields()
8174
fieldsB = testForUniqueness( fieldsA, fieldsB )
82-
seq = range( 0, len( fieldsA ) + len( fieldsB ) )
8375
fieldsA.extend( fieldsB )
84-
fieldsA = dict( zip ( seq, fieldsA ) )
8576
return fieldsA
8677

8778
# Check if two input CRSs are identical
@@ -188,7 +179,7 @@ def createUniqueFieldName( field ):
188179
# Return list of field names with more than 10 characters length
189180
def checkFieldNameLength( fieldList ):
190181
longNames = QStringList()
191-
for num, field in fieldList.iteritems():
182+
for field in fieldList:
192183
if field.name().size() > 10:
193184
longNames << unicode( field.name() )
194185
return longNames
@@ -208,16 +199,16 @@ def getLayerNames( vTypes ):
208199
elif layer.type() == QgsMapLayer.RasterLayer:
209200
if "Raster" in vTypes:
210201
layerlist.append( unicode( layer.name() ) )
211-
return layerlist
202+
return sorted( layerlist, cmp=locale.strcoll )
212203

213204
# Return list of names of all fields from input QgsVectorLayer
214205
def getFieldNames( vlayer ):
215206
fieldmap = getFieldList( vlayer )
216207
fieldlist = []
217-
for name, field in fieldmap.iteritems():
208+
for field in fieldmap:
218209
if not field.name() in fieldlist:
219210
fieldlist.append( unicode( field.name() ) )
220-
return fieldlist
211+
return sorted( fieldlist, cmp=locale.strcoll )
221212

222213
# Return QgsVectorLayer from a layer name ( as string )
223214
def getVectorLayerByName( myName ):
@@ -229,6 +220,16 @@ def getVectorLayerByName( myName ):
229220
else:
230221
return None
231222

223+
# Return QgsRasterLayer from a layer name ( as string )
224+
def getRasterLayerByName( myName ):
225+
layermap = QgsMapLayerRegistry.instance().mapLayers()
226+
for name, layer in layermap.iteritems():
227+
if layer.type() == QgsMapLayer.RasterLayer and layer.name() == myName:
228+
if layer.isValid():
229+
return layer
230+
else:
231+
return None
232+
232233
# Return QgsMapLayer from a layer name ( as string )
233234
def getMapLayerByName( myName ):
234235
layermap = QgsMapLayerRegistry.instance().mapLayers()
@@ -241,19 +242,13 @@ def getMapLayerByName( myName ):
241242

242243
# Return the field list of a vector layer
243244
def getFieldList( vlayer ):
244-
vprovider = vlayer.dataProvider()
245-
feat = QgsFeature()
246-
allAttrs = vprovider.attributeIndexes()
247-
vprovider.select( allAttrs )
248-
myFields = vprovider.fields()
249-
return myFields
245+
return vlayer.dataProvider().fields()
250246

251247
# Convinience function to create a spatial index for input QgsVectorDataProvider
252248
def createIndex( features ):
253-
feat = QgsFeature()
254249
index = QgsSpatialIndex()
255-
for feature in features:
256-
index.insertFeature( feature )
250+
for feat in features:
251+
index.insertFeature( feat )
257252
return index
258253

259254
# Convinience function to add a vector layer to canvas based on input shapefile path ( as string )
@@ -273,11 +268,10 @@ def addShapeToCanvas( shapefile_path ):
273268

274269
# Return all unique values in field based on field index
275270
def getUniqueValues( provider, index ):
276-
values = provider.uniqueValues( index )
277-
return values
271+
return provider.uniqueValues( index )
278272

279273
# Generate a save file dialog with a dropdown box for choosing encoding style
280-
def saveDialog( parent, filtering="Shapefiles (*.shp)"):
274+
def saveDialog( parent, filtering="Shapefiles (*.shp *.SHP)"):
281275
settings = QSettings()
282276
dirName = settings.value( "/UI/lastShapefileDir" ).toString()
283277
encode = settings.value( "/UI/encoding" ).toString()
@@ -293,7 +287,7 @@ def saveDialog( parent, filtering="Shapefiles (*.shp)"):
293287
return ( unicode( files.first() ), unicode( fileDialog.encoding() ) )
294288

295289
# Generate a save file dialog with a dropdown box for choosing encoding style
296-
def openDialog( parent, filtering="Shapefiles (*.shp)"):
290+
def openDialog( parent, filtering="Shapefiles (*.shp *.SHP)" ):
297291
settings = QSettings()
298292
dirName = settings.value( "/UI/lastShapefileDir" ).toString()
299293
encode = settings.value( "/UI/encoding" ).toString()
@@ -318,21 +312,17 @@ def dirDialog( parent ):
318312
if not fileDialog.exec_() == QDialog.Accepted:
319313
return None, None
320314
folders = fileDialog.selectedFiles()
321-
settings.setValue("/UI/lastShapefileDir", QVariant( QFileInfo( unicode( folders.first() ) ) ) )
315+
settings.setValue("/UI/lastShapefileDir", QVariant( QFileInfo( unicode( folders.first() ) ).absolutePath() ) )
322316
return ( unicode( folders.first() ), unicode( fileDialog.encoding() ) )
323317

324318
# Return field type from it's name
325319
def getFieldType(vlayer, fieldName):
326-
fields = vlayer.dataProvider().fields()
327-
for name, field in fields.iteritems():
320+
for field in vlayer.dataProvider().fields():
328321
if field.name() == fieldName:
329322
return field.typeName()
330323

331324
# return the number of unique values in field
332-
def getUniqueValuesCount( layer, fieldIndex):
333-
vprovider = layer.dataProvider()
334-
allAttrs = vprovider.attributeIndexes()
335-
vprovider.select( allAttrs )
325+
def getUniqueValuesCount( layer, fieldIndex ):
336326
count = 0
337327
values = []
338328
features = QGisLayers.features(layer)
@@ -342,21 +332,33 @@ def getUniqueValuesCount( layer, fieldIndex):
342332
count += 1
343333
return count
344334

335+
def getGeomType(gT):
336+
if gT == 3 or gT == 6:
337+
gTypeListPoly = [ QGis.WKBPolygon, QGis.WKBMultiPolygon ]
338+
return gTypeListPoly
339+
elif gT == 2 or gT == 5:
340+
gTypeListLine = [ QGis.WKBLineString, QGis.WKBMultiLineString ]
341+
return gTypeListLine
342+
elif gT == 1 or gT == 4:
343+
gTypeListPoint = [ QGis.WKBPoint, QGis.WKBMultiPoint ]
344+
return gTypeListPoint
345+
345346
def getShapesByGeometryType( baseDir, inShapes, geomType ):
346-
outShapes = QStringList()
347-
for fileName in inShapes:
348-
layerPath = QFileInfo( baseDir + "/" + fileName ).absoluteFilePath()
349-
vLayer = QgsVectorLayer( layerPath, QFileInfo( layerPath ).baseName(), "ogr" )
350-
if not vLayer.isValid():
351-
continue
352-
layerGeometry = vLayer.geometryType()
353-
if layerGeometry == QGis.Polygon and geomType == 0:
354-
outShapes << fileName
355-
elif layerGeometry == QGis.Line and geomType == 1:
356-
outShapes << fileName
357-
elif layerGeometry == QGis.Point and geomType == 2:
358-
outShapes << fileName
359-
360-
if outShapes.count() == 0:
361-
return None
362-
return outShapes
347+
outShapes = QStringList()
348+
for fileName in inShapes:
349+
layerPath = QFileInfo( baseDir + "/" + fileName ).absoluteFilePath()
350+
vLayer = QgsVectorLayer( layerPath, QFileInfo( layerPath ).baseName(), "ogr" )
351+
if not vLayer.isValid():
352+
continue
353+
layerGeometry = vLayer.geometryType()
354+
if layerGeometry == QGis.Polygon and geomType == 0:
355+
outShapes << fileName
356+
elif layerGeometry == QGis.Line and geomType == 1:
357+
outShapes << fileName
358+
elif layerGeometry == QGis.Point and geomType == 2:
359+
outShapes << fileName
360+
361+
if outShapes.count() == 0:
362+
return None
363+
364+
return outShapes

0 commit comments

Comments
 (0)
Please sign in to comment.