@@ -552,17 +552,15 @@ def export_geometry_info( self ):
552
552
nElement = 0
553
553
554
554
vprovider = self .vlayer .dataProvider ()
555
-
555
+ allAttrs = vprovider . attributeIndexes ()
556
556
self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
557
557
self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , vprovider .featureCount () ) )
558
558
559
559
if self .writeShape :
560
- allAttrs = vprovider .attributeIndexes ()
561
- vprovider .select ( allAttrs )
562
560
( fields , index1 , index2 ) = self .checkGeometryFields ( self .vlayer )
563
561
writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
564
562
vprovider .geometryType (), vprovider .crs () )
565
-
563
+ vprovider . select ( allAttrs )
566
564
while vprovider .nextFeature (inFeat ):
567
565
self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
568
566
nElement += 1
@@ -582,10 +580,9 @@ def export_geometry_info( self ):
582
580
del writer
583
581
return True
584
582
else : # update existing file
585
- newFields = []
586
- geomType = self .vlayer .geometryType ()
587
- ( index1 , index2 ) = self .findOrCreateFields ()
583
+ ( index1 , index2 ) = self .findOrCreateFields ( self .vlayer )
588
584
585
+ vprovider .select ( allAttrs )
589
586
while vprovider .nextFeature (inFeat ):
590
587
self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
591
588
nElement += 1
@@ -1030,85 +1027,155 @@ def checkGeometryFields( self, vlayer ):
1030
1027
nameList = []
1031
1028
fieldList = vprovider .fields ()
1032
1029
geomType = vlayer .geometryType ()
1033
- for i in fieldList .keys ():
1030
+ fieldKeys = fieldList .keys ()
1031
+
1032
+ for i in fieldKeys :
1034
1033
nameList .append ( fieldList [ i ].name ().toLower () )
1035
1034
if geomType == QGis .Polygon :
1036
- ( found , index1 ) = self .checkForField ( nameList , "AREA" )
1035
+ if len ( fieldKeys ) == max ( fieldKeys ): # if equal, then the field geometry is not at the end of the fields list
1036
+ ( found , index ) = self .checkForField ( nameList , "AREA" )
1037
+ index1 = index + 1
1038
+ else :
1039
+ ( found , index1 ) = self .checkForField ( nameList , "AREA" )
1037
1040
if not found :
1038
- field = QgsField ( "AREA" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Polygon area" ) )
1039
- index1 = len ( fieldList .keys () )
1041
+ field = QgsField ( "AREA" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Polygon area" ) )
1042
+ if len ( fieldKeys ) == max ( fieldKeys ):
1043
+ index1 = len ( fieldList ) + 1
1044
+ else :
1045
+ index1 = len ( fieldList )
1040
1046
fieldList [ index1 ] = field
1041
-
1042
- ( found , index2 ) = self .checkForField ( nameList , "PERIMETER" )
1047
+ if len ( fieldKeys ) == max ( fieldKeys ):
1048
+ ( found , index ) = self .checkForField ( nameList , "PERIMETER" )
1049
+ index2 = index + 1
1050
+ else :
1051
+ ( found , index2 ) = self .checkForField ( nameList , "PERIMETER" )
1043
1052
if not found :
1044
- field = QgsField ( "PERIMETER" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Polygon perimeter" ) )
1045
- index2 = len ( fieldList .keys () )
1053
+ field = QgsField ( "PERIMETER" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Polygon perimeter" ) )
1054
+ if len ( fieldKeys ) == max ( fieldKeys ):
1055
+ index2 = len ( fieldList ) + 1
1056
+ else :
1057
+ index2 = len ( fieldList )
1046
1058
fieldList [ index2 ] = field
1047
1059
elif geomType == QGis .Line :
1048
- ( found , index1 ) = self .checkForField ( nameList , "LENGTH" )
1060
+ if len ( fieldKeys ) == max ( fieldKeys ):
1061
+ ( found , index ) = self .checkForField ( nameList , "LENGTH" )
1062
+ index1 = index + 1
1063
+ else :
1064
+ ( found , index1 ) = self .checkForField ( nameList , "LENGTH" )
1049
1065
if not found :
1050
- field = QgsField ( "LENGTH" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Line length" ) )
1051
- index1 = len ( fieldList .keys () )
1066
+ field = QgsField ( "LENGTH" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Line length" ) )
1067
+ if len ( fieldKeys ) == max ( fieldKeys ):
1068
+ index1 = len ( fieldList ) + 1
1069
+ else :
1070
+ index1 = len ( fieldList )
1052
1071
fieldList [ index1 ] = field
1053
1072
index2 = index1
1054
1073
else :
1055
- ( found , index1 ) = self .checkForField ( nameList , "XCOORD" )
1074
+ if len ( fieldKeys ) == max ( fieldKeys ):
1075
+ ( found , index ) = self .checkForField ( nameList , "XCOORD" )
1076
+ index1 = index + 1
1077
+ else :
1078
+ ( found , index1 ) = self .checkForField ( nameList , "XCOORD" )
1056
1079
if not found :
1057
- field = QgsField ( "XCOORD" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Point x coordinate" ) )
1058
- index1 = len ( fieldList .keys () )
1080
+ field = QgsField ( "XCOORD" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Point x coordinate" ) )
1081
+ if len ( fieldKeys ) == max ( fieldKeys ):
1082
+ index1 = len ( fieldList ) + 1
1083
+ else :
1084
+ index1 = len ( fieldList )
1059
1085
fieldList [ index1 ] = field
1060
1086
1061
- ( found , index2 ) = self .checkForField ( nameList , "YCOORD" )
1087
+ if len ( fieldKeys ) == max ( fieldKeys ):
1088
+ ( found , index ) = self .checkForField ( nameList , "YCOORD" )
1089
+ index2 = index + 1
1090
+ else :
1091
+ ( found , index2 ) = self .checkForField ( nameList , "YCOORD" )
1062
1092
if not found :
1063
- field = QgsField ( "YCOORD" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Point y coordinate" ) )
1064
- index2 = len ( fieldList .keys () )
1093
+ field = QgsField ( "YCOORD" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Point y coordinate" ) )
1094
+ if len ( fieldKeys ) == max ( fieldKeys ):
1095
+ index2 = len ( fieldList ) + 2
1096
+ else :
1097
+ index2 = len ( fieldList )
1065
1098
fieldList [ index2 ] = field
1066
1099
return ( fieldList , index1 , index2 )
1067
1100
1068
- def findOrCreateFields ( self ):
1069
- vprovider = self . vlayer .dataProvider ()
1101
+ def findOrCreateFields ( self , vlayer ):
1102
+ vprovider = vlayer .dataProvider ()
1070
1103
fieldList = vprovider .fields ()
1071
- geomType = self . vlayer .geometryType ()
1104
+ geomType = vlayer .geometryType ()
1072
1105
newFields = []
1073
1106
nameList = []
1107
+ fieldKeys = fieldList .keys ()
1074
1108
1075
- for i in fieldList . keys () :
1109
+ for i in fieldKeys :
1076
1110
nameList .append ( fieldList [ i ].name ().toLower () )
1077
1111
1078
1112
if geomType == QGis .Polygon :
1079
- ( found , index1 ) = self .checkForField ( nameList , "AREA" )
1113
+ if len ( fieldKeys ) == max ( fieldKeys ):
1114
+ ( found , index ) = self .checkForField ( nameList , "AREA" )
1115
+ index1 = index + 1
1116
+ else :
1117
+ ( found , index1 ) = self .checkForField ( nameList , "AREA" )
1080
1118
if not found :
1081
- field = QgsField ( "AREA" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Polygon area" ) )
1082
- index1 = len ( fieldList .keys () )
1119
+ field = QgsField ( "AREA" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Polygon area" ) )
1120
+ if len ( fieldKeys ) == max ( fieldKeys ):
1121
+ index1 = len ( fieldKeys ) + 1
1122
+ else :
1123
+ index1 = len ( fieldKeys )
1083
1124
newFields .append ( field )
1084
-
1085
- ( found , index2 ) = self .checkForField ( nameList , "PERIMETER" )
1125
+ if len ( fieldKeys ) == max ( fieldKeys ):
1126
+ ( found , index ) = self .checkForField ( nameList , "PERIMETER" )
1127
+ index1 = index + 1
1128
+ else :
1129
+ ( found , index2 ) = self .checkForField ( nameList , "PERIMETER" )
1086
1130
if not found :
1087
- field = QgsField ( "PERIMETER" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Polygon perimeter" ) )
1088
- index2 = len ( fieldList .keys () ) + 1
1131
+ field = QgsField ( "PERIMETER" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Polygon perimeter" ) )
1132
+ if len ( fieldKeys ) == max ( fieldKeys ):
1133
+ index2 = len ( fieldKeys ) + 2
1134
+ else :
1135
+ index2 = len ( fieldKeys ) + 1
1089
1136
newFields .append ( field )
1090
1137
elif geomType == QGis .Line :
1091
- ( found , index1 ) = self .checkForField ( nameList , "LENGTH" )
1138
+ if len ( fieldKeys ) == max ( fieldKeys ):
1139
+ ( found , index ) = self .checkForField ( nameList , "LENGTH" )
1140
+ index1 = index + 1
1141
+ else :
1142
+ ( found , index1 ) = self .checkForField ( nameList , "LENGTH" )
1092
1143
if not found :
1093
- field = QgsField ( "LENGTH" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Line length" ) )
1094
- index1 = len ( fieldList .keys () )
1144
+ field = QgsField ( "LENGTH" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Line length" ) )
1145
+ if len ( fieldKeys ) == max ( fieldKeys ):
1146
+ index1 = len ( fieldKeys ) + 1
1147
+ else :
1148
+ index1 = len ( fieldKeys )
1095
1149
newFields .append ( field )
1096
1150
index2 = index1
1097
1151
else :
1098
- ( found , index1 ) = self .checkForField ( nameList , "XCOORD" )
1152
+ if len ( fieldKeys ) == max ( fieldKeys ):
1153
+ ( found , index ) = self .checkForField ( nameList , "XCOORD" )
1154
+ index1 = index + 1
1155
+ else :
1156
+ ( found , index1 ) = self .checkForField ( nameList , "XCOORD" )
1099
1157
if not found :
1100
- field = QgsField ( "XCOORD" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Point x coordinate" ) )
1101
- index1 = len ( fieldList .keys () )
1158
+ field = QgsField ( "XCOORD" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Point x coordinate" ) )
1159
+ if len ( fieldKeys ) == max ( fieldKeys ):
1160
+ index = len ( fieldKeys ) + 1
1161
+ else :
1162
+ index1 = len ( fieldKeys )
1102
1163
newFields .append ( field )
1103
1164
1104
- ( found , index2 ) = self .checkForField ( nameList , "YCOORD" )
1165
+ if len ( fieldKeys ) == max ( fieldKeys ):
1166
+ ( found , index ) = self .checkForField ( nameList , "YCOORD" )
1167
+ index2 = index + 1
1168
+ else :
1169
+ ( found , index2 ) = self .checkForField ( nameList , "YCOORD" )
1105
1170
if not found :
1106
- field = QgsField ( "YCOORD" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Point y coordinate" ) )
1107
- index2 = len ( fieldList .keys () ) + 1
1171
+ field = QgsField ( "YCOORD" , QVariant .Double , "double precision" , 21 , 6 , self .tr ( "Point y coordinate" ) )
1172
+ if len ( fieldKeys ) == max ( fieldKeys ):
1173
+ index2 = len ( fieldKeys ) + 2
1174
+ else :
1175
+ index2 = len ( fieldKeys ) + 1
1108
1176
newFields .append ( field )
1109
-
1110
1177
vprovider .addAttributes ( newFields )
1111
- self . vlayer .updateFieldMap ()
1178
+ vlayer .updateFieldMap ()
1112
1179
return ( index1 , index2 )
1113
1180
1114
1181
def extractAsLine ( self , geom ):
0 commit comments