@@ -458,7 +458,7 @@ def extract_nodes( self ):
458
458
459
459
def polygons_to_lines ( self ):
460
460
vprovider = self .vlayer .dataProvider ()
461
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding , vproviders .fields (),
461
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , vprovider .fields (),
462
462
QGis .WKBLineString , vprovider .crs () )
463
463
inFeat = QgsFeature ()
464
464
outFeat = QgsFeature ()
@@ -488,7 +488,7 @@ def polygons_to_lines( self ):
488
488
489
489
def lines_to_polygons ( self ):
490
490
vprovider = self .vlayer .dataProvider ()
491
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding , vproviders .fields (),
491
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , vprovider .fields (),
492
492
QGis .WKBPolygon , vprovider .crs () )
493
493
inFeat = QgsFeature ()
494
494
outFeat = QgsFeature ()
@@ -545,47 +545,45 @@ def export_geometry_info( self ):
545
545
self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
546
546
self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , vprovider .featureCount () ) )
547
547
548
+ ( fields , index1 , index2 ) = self .checkMeasurementFields ( self .vlayer , not self .writeShape )
549
+
548
550
if self .writeShape :
549
- ( fields , index1 , index2 ) = self .checkGeometryFields ( self .vlayer )
550
551
writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
551
552
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 ()
557
553
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 )
560
562
561
- ( attr1 , attr2 ) = self .simpleMeasure ( inGeom , self .myCalcType , ellips , crs )
563
+ ( attr1 , attr2 ) = self .simpleMeasure ( inGeom , self .myCalcType , ellips , crs )
562
564
565
+ if self .writeShape :
563
566
outFeat .setGeometry ( inGeom )
564
567
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
565
574
outFeat .setAttributes ( atMap )
566
- outFeat .addAttribute ( index1 , QVariant ( attr1 ) )
567
- outFeat .addAttribute ( index2 , QVariant ( attr2 ) )
568
575
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 :
584
577
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 )
588
582
vprovider .changeAttributeValues ( changeMap )
583
+
584
+ if self .writeShape :
585
+ del writer
586
+
589
587
return True
590
588
591
589
def polygon_centroids ( self ):
@@ -659,7 +657,7 @@ def delaunay_triangulation( self ):
659
657
geom = QgsGeometry ( inFeat .geometry () )
660
658
point = QgsPoint ( geom .asPoint () )
661
659
polygon .append ( point )
662
- if step <= 3 : feat .addAttribute ( step , QVariant ( ids [ index ] ) )
660
+ if step <= 3 : feat .setAttribute ( step , QVariant ( ids [ index ] ) )
663
661
step += 1
664
662
geometry = QgsGeometry ().fromPolygon ( [ polygon ] )
665
663
feat .setGeometry ( geometry )
@@ -869,16 +867,16 @@ def feature_extent( self, ):
869
867
870
868
self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
871
869
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 ) ]
882
880
883
881
writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
884
882
QGis .WKBPolygon , self .vlayer .crs () )
@@ -1000,170 +998,35 @@ def perimMeasure( self, inGeom, measure ):
1000
998
value = value + measure .measureLine ( k )
1001
999
return value
1002
1000
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
1010
1007
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 )
1086
1010
1087
- def findOrCreateFields ( self , vlayer ):
1011
+ def checkMeasurementFields ( self , vlayer , add ):
1088
1012
vprovider = vlayer .dataProvider ()
1089
- fieldList = vprovider .fields ()
1090
1013
geomType = vlayer .geometryType ()
1091
- newFields = []
1092
- nameList = []
1093
- fieldKeys = fieldList .keys ()
1094
-
1095
- for i in fieldKeys :
1096
- nameList .append ( fieldList [ i ].name ().toLower () )
1097
1014
1098
1015
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 )
1123
1018
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 )
1136
1020
index2 = index1
1137
1021
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 )
1150
1024
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 )
1167
1030
1168
1031
def extractAsLine ( self , geom ):
1169
1032
multi_geom = QgsGeometry ()
0 commit comments