Skip to content

Commit 7f7b5dd

Browse files
committed
more fTools api updates
1 parent 57e753a commit 7f7b5dd

12 files changed

+106
-224
lines changed

python/core/qgsfeature.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class QgsFeature
111111
const QgsAttributes& attributes() const;
112112
//QgsAttributes& attributes();
113113
void setAttributes(const QgsAttributes& attrs);
114-
void setAttribute( int field, const QVariant& attr );
114+
bool setAttribute( int field, const QVariant& attr );
115115
void initAttributes( int fieldCount );
116116

117117
/**Deletes an attribute and its value*/

python/plugins/fTools/tools/doGeometry.py

+59-196
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def extract_nodes( self ):
458458

459459
def polygons_to_lines( self ):
460460
vprovider = self.vlayer.dataProvider()
461-
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vproviders.fields(),
461+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vprovider.fields(),
462462
QGis.WKBLineString, vprovider.crs() )
463463
inFeat = QgsFeature()
464464
outFeat = QgsFeature()
@@ -488,7 +488,7 @@ def polygons_to_lines( self ):
488488

489489
def lines_to_polygons( self ):
490490
vprovider = self.vlayer.dataProvider()
491-
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vproviders.fields(),
491+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vprovider.fields(),
492492
QGis.WKBPolygon, vprovider.crs() )
493493
inFeat = QgsFeature()
494494
outFeat = QgsFeature()
@@ -545,47 +545,45 @@ def export_geometry_info( self ):
545545
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
546546
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, vprovider.featureCount() ) )
547547

548+
( fields, index1, index2 ) = self.checkMeasurementFields( self.vlayer, not self.writeShape )
549+
548550
if self.writeShape:
549-
( fields, index1, index2 ) = self.checkGeometryFields( self.vlayer )
550551
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
551552
vprovider.geometryType(), vprovider.crs() )
552-
fit = vprovider.getFeatures()
553-
while fit.nextFeature(inFeat):
554-
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
555-
nElement += 1
556-
inGeom = inFeat.geometry()
557553

558-
if self.myCalcType == 1:
559-
inGeom.transform( coordTransform )
554+
fit = vprovider.getFeatures()
555+
while fit.nextFeature(inFeat):
556+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
557+
nElement += 1
558+
inGeom = inFeat.geometry()
559+
560+
if self.myCalcType == 1:
561+
inGeom.transform( coordTransform )
560562

561-
( attr1, attr2 ) = self.simpleMeasure( inGeom, self.myCalcType, ellips, crs )
563+
( attr1, attr2 ) = self.simpleMeasure( inGeom, self.myCalcType, ellips, crs )
562564

565+
if self.writeShape:
563566
outFeat.setGeometry( inGeom )
564567
atMap = inFeat.attributes()
568+
maxIndex = index1 if index1>index2 else index2
569+
if maxIndex>len(atMap):
570+
atMap += [ QVariant() ] * ( index2+1 - len(atMap) )
571+
atMap[ index1 ] = attr1
572+
if index1!=index2:
573+
atMap[ index2 ] = attr2
565574
outFeat.setAttributes( atMap )
566-
outFeat.addAttribute( index1, QVariant( attr1 ) )
567-
outFeat.addAttribute( index2, QVariant( attr2 ) )
568575
writer.addFeature( outFeat )
569-
del writer
570-
return True
571-
else: # update existing file
572-
( index1, index2 ) = self.findOrCreateFields( self.vlayer )
573-
574-
fit = vprovider.getFeatures()
575-
while fit.nextFeature(inFeat):
576-
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
577-
nElement += 1
578-
inGeom = inFeat.geometry()
579-
580-
if self.myCalcType == 1:
581-
inGeom.transform( coordTransform )
582-
( attr1, attr2 ) = self.simpleMeasure( inGeom, self.myCalcType, ellips, crs )
583-
576+
else:
584577
changeMap = {}
585-
changeAttributeMap = { index1 : QVariant( attr1 ),
586-
index2 : QVariant( attr2 ) }
587-
changeMap[ inFeat.id() ] = changeAttributeMap
578+
changeMap[ inFeat.id() ] = {}
579+
changeMap[ inFeat.id() ][ index1 ] = QVariant( attr1 )
580+
if index1!=index2:
581+
changeMap[ inFeat.id() ][ index2 ] = QVariant( attr2 )
588582
vprovider.changeAttributeValues( changeMap )
583+
584+
if self.writeShape:
585+
del writer
586+
589587
return True
590588

591589
def polygon_centroids( self ):
@@ -659,7 +657,7 @@ def delaunay_triangulation( self ):
659657
geom = QgsGeometry( inFeat.geometry() )
660658
point = QgsPoint( geom.asPoint() )
661659
polygon.append( point )
662-
if step <= 3: feat.addAttribute( step, QVariant( ids[ index ] ) )
660+
if step <= 3: feat.setAttribute( step, QVariant( ids[ index ] ) )
663661
step += 1
664662
geometry = QgsGeometry().fromPolygon( [ polygon ] )
665663
feat.setGeometry( geometry )
@@ -869,16 +867,16 @@ def feature_extent( self, ):
869867

870868
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
871869

872-
fields = { 0 : QgsField( "MINX", QVariant.Double ),
873-
1 : QgsField( "MINY", QVariant.Double ),
874-
2 : QgsField( "MAXX", QVariant.Double ),
875-
3 : QgsField( "MAXY", QVariant.Double ),
876-
4 : QgsField( "CNTX", QVariant.Double ),
877-
5 : QgsField( "CNTY", QVariant.Double ),
878-
6 : QgsField( "AREA", QVariant.Double ),
879-
7 : QgsField( "PERIM", QVariant.Double ),
880-
8 : QgsField( "HEIGHT", QVariant.Double ),
881-
9 : QgsField( "WIDTH", QVariant.Double ) }
870+
fields = [ QgsField( "MINX", QVariant.Double ),
871+
QgsField( "MINY", QVariant.Double ),
872+
QgsField( "MAXX", QVariant.Double ),
873+
QgsField( "MAXY", QVariant.Double ),
874+
QgsField( "CNTX", QVariant.Double ),
875+
QgsField( "CNTY", QVariant.Double ),
876+
QgsField( "AREA", QVariant.Double ),
877+
QgsField( "PERIM", QVariant.Double ),
878+
QgsField( "HEIGHT", QVariant.Double ),
879+
QgsField( "WIDTH", QVariant.Double ) ]
882880

883881
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
884882
QGis.WKBPolygon, self.vlayer.crs() )
@@ -1000,170 +998,35 @@ def perimMeasure( self, inGeom, measure ):
1000998
value = value + measure.measureLine( k )
1001999
return value
10021000

1003-
def checkForField( self, L, e ):
1004-
e = QString( e ).toLower()
1005-
fieldRange = range( 0, len( L ) )
1006-
for item in fieldRange:
1007-
if L[ item ].toLower() == e:
1008-
return True, item
1009-
return False, len( L )
1001+
def doubleFieldIndex( self, name, desc, fieldList, f ):
1002+
i = 0
1003+
for f in fieldList:
1004+
if name == f.name().toUpper():
1005+
return (i, fieldList )
1006+
i += 1
10101007

1011-
def checkGeometryFields( self, vlayer ):
1012-
vprovider = vlayer.dataProvider()
1013-
nameList = []
1014-
fieldList = vprovider.fields()
1015-
geomType = vlayer.geometryType()
1016-
fieldKeys = fieldList.keys()
1017-
1018-
for i in fieldKeys:
1019-
nameList.append( fieldList[ i ].name().toLower() )
1020-
if geomType == QGis.Polygon:
1021-
if len( fieldKeys ) == max( fieldKeys ): # if equal, then the field geometry is not at the end of the fields list
1022-
( found, index ) = self.checkForField( nameList, "AREA" )
1023-
index1 = index + 1
1024-
else:
1025-
( found, index1 ) = self.checkForField( nameList, "AREA" )
1026-
if not found:
1027-
field = QgsField( "AREA", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon area" ) )
1028-
if len( fieldKeys ) == max( fieldKeys ):
1029-
index1 = len( fieldList ) + 1
1030-
else:
1031-
index1 = len( fieldList )
1032-
fieldList[ index1 ] = field
1033-
if len( fieldKeys ) == max( fieldKeys ):
1034-
( found, index ) = self.checkForField( nameList, "PERIMETER" )
1035-
index2 = index + 1
1036-
else:
1037-
( found, index2 ) = self.checkForField( nameList, "PERIMETER" )
1038-
if not found:
1039-
field = QgsField( "PERIMETER", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon perimeter" ) )
1040-
if len( fieldKeys ) == max( fieldKeys ):
1041-
index2 = len( fieldList ) + 1
1042-
else:
1043-
index2 = len( fieldList )
1044-
fieldList[ index2 ] = field
1045-
elif geomType == QGis.Line:
1046-
if len( fieldKeys ) == max( fieldKeys ):
1047-
( found, index ) = self.checkForField( nameList, "LENGTH" )
1048-
index1 = index + 1
1049-
else:
1050-
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
1051-
if not found:
1052-
field = QgsField( "LENGTH", QVariant.Double, "double precision", 21, 6, self.tr( "Line length" ) )
1053-
if len( fieldKeys ) == max( fieldKeys ):
1054-
index1 = len( fieldList ) + 1
1055-
else:
1056-
index1 = len( fieldList )
1057-
fieldList[ index1 ] = field
1058-
index2 = index1
1059-
else:
1060-
if len( fieldKeys ) == max( fieldKeys ):
1061-
( found, index ) = self.checkForField( nameList, "XCOORD" )
1062-
index1 = index + 1
1063-
else:
1064-
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
1065-
if not found:
1066-
field = QgsField( "XCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point x coordinate" ) )
1067-
if len( fieldKeys ) == max( fieldKeys ):
1068-
index1 = len( fieldList ) + 1
1069-
else:
1070-
index1 = len( fieldList )
1071-
fieldList[ index1 ] = field
1072-
1073-
if len( fieldKeys ) == max( fieldKeys ):
1074-
( found, index ) = self.checkForField( nameList, "YCOORD" )
1075-
index2 = index + 1
1076-
else:
1077-
( found, index2 ) = self.checkForField( nameList, "YCOORD" )
1078-
if not found:
1079-
field = QgsField( "YCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point y coordinate" ) )
1080-
if len( fieldKeys ) == max( fieldKeys ):
1081-
index2 = len( fieldList ) + 2
1082-
else:
1083-
index2 = len( fieldList )
1084-
fieldList[ index2 ] = field
1085-
return ( fieldList, index1, index2 )
1008+
fieldList.append( QgsField( name, QVariant.Double, "double precision", 21, 6, desc ) )
1009+
return ( len(fieldList)-1, fieldList )
10861010

1087-
def findOrCreateFields( self, vlayer ):
1011+
def checkMeasurementFields( self, vlayer, add ):
10881012
vprovider = vlayer.dataProvider()
1089-
fieldList = vprovider.fields()
10901013
geomType = vlayer.geometryType()
1091-
newFields = []
1092-
nameList = []
1093-
fieldKeys = fieldList.keys()
1094-
1095-
for i in fieldKeys:
1096-
nameList.append( fieldList[ i ].name().toLower() )
10971014

10981015
if geomType == QGis.Polygon:
1099-
if len( fieldKeys ) == max( fieldKeys ):
1100-
( found, index ) = self.checkForField( nameList, "AREA" )
1101-
index1 = index + 1
1102-
else:
1103-
( found, index1 ) = self.checkForField( nameList, "AREA" )
1104-
if not found:
1105-
field = QgsField( "AREA", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon area" ) )
1106-
if len( fieldKeys ) == max( fieldKeys ):
1107-
index1 = len( fieldKeys ) + 1
1108-
else:
1109-
index1 = len( fieldKeys )
1110-
newFields.append( field )
1111-
if len( fieldKeys ) == max( fieldKeys ):
1112-
( found, index ) = self.checkForField( nameList, "PERIMETER" )
1113-
index1 = index + 1
1114-
else:
1115-
( found, index2 ) = self.checkForField( nameList, "PERIMETER" )
1116-
if not found:
1117-
field = QgsField( "PERIMETER", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon perimeter" ) )
1118-
if len( fieldKeys ) == max( fieldKeys ):
1119-
index2 = len( fieldKeys ) + 2
1120-
else:
1121-
index2 = len( fieldKeys ) + 1
1122-
newFields.append( field )
1016+
(index1, fieldList) = doubleFieldIndex( "AREA", self.tr( "Polygon area" ), fieldList )
1017+
(index2, fieldList) = doubleFieldIndex( "PERIMETER", self.tr( "Polygon perimeter" ), fieldList )
11231018
elif geomType == QGis.Line:
1124-
if len( fieldKeys ) == max( fieldKeys ):
1125-
( found, index ) = self.checkForField( nameList, "LENGTH" )
1126-
index1 = index + 1
1127-
else:
1128-
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
1129-
if not found:
1130-
field = QgsField( "LENGTH", QVariant.Double, "double precision", 21, 6, self.tr( "Line length" ) )
1131-
if len( fieldKeys ) == max( fieldKeys ):
1132-
index1 = len( fieldKeys ) + 1
1133-
else:
1134-
index1 = len( fieldKeys )
1135-
newFields.append( field )
1019+
(index1, fieldList) = doubleFieldIndex( "LENGTH", self.tr( "Line length" ), fieldList )
11361020
index2 = index1
11371021
else:
1138-
if len( fieldKeys ) == max( fieldKeys ):
1139-
( found, index ) = self.checkForField( nameList, "XCOORD" )
1140-
index1 = index + 1
1141-
else:
1142-
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
1143-
if not found:
1144-
field = QgsField( "XCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point x coordinate" ) )
1145-
if len( fieldKeys ) == max( fieldKeys ):
1146-
index = len( fieldKeys ) + 1
1147-
else:
1148-
index1 = len( fieldKeys )
1149-
newFields.append( field )
1022+
(index1, fieldList) = doubleFieldIndex( "XCOORD", self.tr( "Point x ordinate" ), fieldList )
1023+
(index2, fieldList) = doubleFieldIndex( "YCOORD", self.tr( "Point y ordinate" ), fieldList )
11501024

1151-
if len( fieldKeys ) == max( fieldKeys ):
1152-
( found, index ) = self.checkForField( nameList, "YCOORD" )
1153-
index2 = index + 1
1154-
else:
1155-
( found, index2 ) = self.checkForField( nameList, "YCOORD" )
1156-
if not found:
1157-
field = QgsField( "YCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point y coordinate" ) )
1158-
if len( fieldKeys ) == max( fieldKeys ):
1159-
index2 = len( fieldKeys ) + 2
1160-
else:
1161-
index2 = len( fieldKeys ) + 1
1162-
newFields.append( field )
1163-
# FIXME: addAttributes was deprecated and removed
1164-
vprovider.addAttributes( newFields )
1165-
vlayer.updateFieldMap()
1166-
return ( index1, index2 )
1025+
if add:
1026+
vprovider.addAttributes( newFields )
1027+
vlayer.updateFieldMap()
1028+
1029+
return ( fieldList, index1, index2 )
11671030

11681031
def extractAsLine( self, geom ):
11691032
multi_geom = QgsGeometry()

python/plugins/fTools/tools/doGeoprocessing.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,9 @@ def convex_hull(self, useField ):
507507
outGeom = tmpGeom.convexHull()
508508
outFeat.setGeometry( outGeom )
509509
(area, perim) = self.simpleMeasure( outGeom )
510-
outFeat.addAttribute( 0, QVariant( outID ) )
511-
outFeat.addAttribute( 1, QVariant( area ) )
512-
outFeat.addAttribute( 2, QVariant( perim ) )
510+
outFeat.setAttribute( 0, QVariant( outID ) )
511+
outFeat.setAttribute( 1, QVariant( area ) )
512+
outFeat.setAttribute( 2, QVariant( perim ) )
513513
writer.addFeature( outFeat )
514514
except:
515515
GEOS_EXCEPT = False
@@ -563,9 +563,9 @@ def convex_hull(self, useField ):
563563
outGeom = tmpGeom.convexHull()
564564
outFeat.setGeometry( outGeom )
565565
(area, perim) = self.simpleMeasure( outGeom )
566-
outFeat.addAttribute( 0, QVariant( outID ) )
567-
outFeat.addAttribute( 1, QVariant( area ) )
568-
outFeat.addAttribute( 2, QVariant( perim ) )
566+
outFeat.setAttribute( 0, QVariant( outID ) )
567+
outFeat.setAttribute( 1, QVariant( area ) )
568+
outFeat.setAttribute( 2, QVariant( perim ) )
569569
writer.addFeature( outFeat )
570570
except:
571571
GEOS_EXCEPT = False

python/plugins/fTools/tools/doIntersectLines.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ def compute(self, line1, line2, field1, field2, outPath, progressBar):
165165

166166
for j in tempList:
167167
outFeat.setGeometry(tempGeom.fromPoint(j))
168-
outFeat.addAttribute(0, v1)
169-
outFeat.addAttribute(1, v2)
168+
outFeat.setAttribute(0, v1)
169+
outFeat.setAttribute(1, v2)
170170
writer.addFeature(outFeat)
171171

172172
start = start + add

python/plugins/fTools/tools/doMeanCoords.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ def compute(self, inName, outName, weightField="", times=1, uniqueField=""):
193193
sd += (i-md)*(i-md)
194194
sd = sqrt(sd/item)
195195
outfeat.setGeometry(QgsGeometry.fromPoint(meanPoint).buffer(sd * times, 10))
196-
outfeat.addAttribute(0, QVariant(sd))
197-
outfeat.addAttribute(1, QVariant(j))
196+
outfeat.setAttribute(0, QVariant(sd))
197+
outfeat.setAttribute(1, QVariant(j))
198198
else:
199199
outfeat.setGeometry(QgsGeometry.fromPoint(meanPoint))
200-
outfeat.addAttribute(0, QVariant(cx))
201-
outfeat.addAttribute(1, QVariant(cy))
202-
outfeat.addAttribute(2, QVariant(j))
200+
outfeat.setAttribute(0, QVariant(cx))
201+
outfeat.setAttribute(1, QVariant(cy))
202+
outfeat.setAttribute(2, QVariant(j))
203203
writer.addFeature(outfeat)
204204
if single:
205205
break

python/plugins/fTools/tools/doPointsInPolygon.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def run(self):
219219
interrupted = True
220220
break
221221

222-
outFeat.addAttribute(index, QVariant(count))
222+
outFeat.setAttribute(index, QVariant(count))
223223
writer.addFeature(outFeat)
224224

225225
self.emit( SIGNAL( "updateProgress()" ) )

0 commit comments

Comments
 (0)