Skip to content

Commit f97b043

Browse files
committedMay 29, 2012
Merge pull request #154 from slarosa/master
fix #2733
2 parents 23b40de + 1341a55 commit f97b043

File tree

1 file changed

+113
-46
lines changed

1 file changed

+113
-46
lines changed
 

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

Lines changed: 113 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -552,17 +552,15 @@ def export_geometry_info( self ):
552552
nElement = 0
553553

554554
vprovider = self.vlayer.dataProvider()
555-
555+
allAttrs = vprovider.attributeIndexes()
556556
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
557557
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, vprovider.featureCount() ) )
558558

559559
if self.writeShape:
560-
allAttrs = vprovider.attributeIndexes()
561-
vprovider.select( allAttrs )
562560
( fields, index1, index2 ) = self.checkGeometryFields( self.vlayer )
563561
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
564562
vprovider.geometryType(), vprovider.crs() )
565-
563+
vprovider.select( allAttrs )
566564
while vprovider.nextFeature(inFeat):
567565
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
568566
nElement += 1
@@ -582,10 +580,9 @@ def export_geometry_info( self ):
582580
del writer
583581
return True
584582
else: # update existing file
585-
newFields = []
586-
geomType = self.vlayer.geometryType()
587-
( index1, index2 ) = self.findOrCreateFields()
583+
( index1, index2 ) = self.findOrCreateFields( self.vlayer )
588584

585+
vprovider.select( allAttrs )
589586
while vprovider.nextFeature(inFeat):
590587
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
591588
nElement += 1
@@ -1030,85 +1027,155 @@ def checkGeometryFields( self, vlayer ):
10301027
nameList = []
10311028
fieldList = vprovider.fields()
10321029
geomType = vlayer.geometryType()
1033-
for i in fieldList.keys():
1030+
fieldKeys = fieldList.keys()
1031+
1032+
for i in fieldKeys:
10341033
nameList.append( fieldList[ i ].name().toLower() )
10351034
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" )
10371040
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 )
10401046
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" )
10431052
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 )
10461058
fieldList[ index2 ] = field
10471059
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" )
10491065
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 )
10521071
fieldList[ index1 ] = field
10531072
index2 = index1
10541073
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" )
10561079
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 )
10591085
fieldList[ index1 ] = field
10601086

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" )
10621092
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 )
10651098
fieldList[ index2 ] = field
10661099
return ( fieldList, index1, index2 )
10671100

1068-
def findOrCreateFields( self ):
1069-
vprovider = self.vlayer.dataProvider()
1101+
def findOrCreateFields( self, vlayer ):
1102+
vprovider = vlayer.dataProvider()
10701103
fieldList = vprovider.fields()
1071-
geomType = self.vlayer.geometryType()
1104+
geomType = vlayer.geometryType()
10721105
newFields = []
10731106
nameList = []
1107+
fieldKeys = fieldList.keys()
10741108

1075-
for i in fieldList.keys():
1109+
for i in fieldKeys:
10761110
nameList.append( fieldList[ i ].name().toLower() )
10771111

10781112
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" )
10801118
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 )
10831124
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" )
10861130
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
10891136
newFields.append( field )
10901137
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" )
10921143
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 )
10951149
newFields.append( field )
10961150
index2 = index1
10971151
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" )
10991157
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 )
11021163
newFields.append( field )
11031164

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" )
11051170
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
11081176
newFields.append( field )
1109-
11101177
vprovider.addAttributes( newFields )
1111-
self.vlayer.updateFieldMap()
1178+
vlayer.updateFieldMap()
11121179
return ( index1, index2 )
11131180

11141181
def extractAsLine( self, geom ):

0 commit comments

Comments
 (0)
Please sign in to comment.