Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #154 from slarosa/master
fix #2733
  • Loading branch information
alexbruy committed May 29, 2012
2 parents 23b40de + 1341a55 commit f97b043
Showing 1 changed file with 113 additions and 46 deletions.
159 changes: 113 additions & 46 deletions python/plugins/fTools/tools/doGeometry.py
Expand Up @@ -552,17 +552,15 @@ def export_geometry_info( self ):
nElement = 0

vprovider = self.vlayer.dataProvider()

allAttrs = vprovider.attributeIndexes()
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, vprovider.featureCount() ) )

if self.writeShape:
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
( fields, index1, index2 ) = self.checkGeometryFields( self.vlayer )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
vprovider.geometryType(), vprovider.crs() )

vprovider.select( allAttrs )
while vprovider.nextFeature(inFeat):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
nElement += 1
Expand All @@ -582,10 +580,9 @@ def export_geometry_info( self ):
del writer
return True
else: # update existing file
newFields = []
geomType = self.vlayer.geometryType()
( index1, index2 ) = self.findOrCreateFields()
( index1, index2 ) = self.findOrCreateFields( self.vlayer )

vprovider.select( allAttrs )
while vprovider.nextFeature(inFeat):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
nElement += 1
Expand Down Expand Up @@ -1030,85 +1027,155 @@ def checkGeometryFields( self, vlayer ):
nameList = []
fieldList = vprovider.fields()
geomType = vlayer.geometryType()
for i in fieldList.keys():
fieldKeys = fieldList.keys()

for i in fieldKeys:
nameList.append( fieldList[ i ].name().toLower() )
if geomType == QGis.Polygon:
( found, index1 ) = self.checkForField( nameList, "AREA" )
if len( fieldKeys ) == max( fieldKeys ): # if equal, then the field geometry is not at the end of the fields list
( found, index ) = self.checkForField( nameList, "AREA" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "AREA" )
if not found:
field = QgsField( "AREA", QVariant.Double, "double", 21, 6, self.tr( "Polygon area" ) )
index1 = len( fieldList.keys() )
field = QgsField( "AREA", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon area" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldList ) + 1
else:
index1 = len( fieldList )
fieldList[ index1 ] = field

( found, index2 ) = self.checkForField( nameList, "PERIMETER" )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "PERIMETER" )
index2 = index + 1
else:
( found, index2 ) = self.checkForField( nameList, "PERIMETER" )
if not found:
field = QgsField( "PERIMETER", QVariant.Double, "double", 21, 6, self.tr( "Polygon perimeter" ) )
index2 = len( fieldList.keys() )
field = QgsField( "PERIMETER", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon perimeter" ) )
if len( fieldKeys ) == max( fieldKeys ):
index2 = len( fieldList ) + 1
else:
index2 = len( fieldList )
fieldList[ index2 ] = field
elif geomType == QGis.Line:
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "LENGTH" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
if not found:
field = QgsField( "LENGTH", QVariant.Double, "double", 21, 6, self.tr( "Line length" ) )
index1 = len( fieldList.keys() )
field = QgsField( "LENGTH", QVariant.Double, "double precision", 21, 6, self.tr( "Line length" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldList ) + 1
else:
index1 = len( fieldList )
fieldList[ index1 ] = field
index2 = index1
else:
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "XCOORD" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
if not found:
field = QgsField( "XCOORD", QVariant.Double, "double", 21, 6, self.tr( "Point x coordinate" ) )
index1 = len( fieldList.keys() )
field = QgsField( "XCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point x coordinate" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldList ) + 1
else:
index1 = len( fieldList )
fieldList[ index1 ] = field

( found, index2 ) = self.checkForField( nameList, "YCOORD" )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "YCOORD" )
index2 = index + 1
else:
( found, index2 ) = self.checkForField( nameList, "YCOORD" )
if not found:
field = QgsField( "YCOORD", QVariant.Double, "double", 21, 6, self.tr( "Point y coordinate" ) )
index2 = len( fieldList.keys() )
field = QgsField( "YCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point y coordinate" ) )
if len( fieldKeys ) == max( fieldKeys ):
index2 = len( fieldList ) + 2
else:
index2 = len( fieldList )
fieldList[ index2 ] = field
return ( fieldList, index1, index2 )

def findOrCreateFields( self ):
vprovider = self.vlayer.dataProvider()
def findOrCreateFields( self, vlayer ):
vprovider = vlayer.dataProvider()
fieldList = vprovider.fields()
geomType = self.vlayer.geometryType()
geomType = vlayer.geometryType()
newFields = []
nameList = []
fieldKeys = fieldList.keys()

for i in fieldList.keys():
for i in fieldKeys:
nameList.append( fieldList[ i ].name().toLower() )

if geomType == QGis.Polygon:
( found, index1 ) = self.checkForField( nameList, "AREA" )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "AREA" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "AREA" )
if not found:
field = QgsField( "AREA", QVariant.Double, "double", 21, 6, self.tr( "Polygon area" ) )
index1 = len( fieldList.keys() )
field = QgsField( "AREA", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon area" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldKeys ) + 1
else:
index1 = len( fieldKeys )
newFields.append( field )

( found, index2 ) = self.checkForField( nameList, "PERIMETER" )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "PERIMETER" )
index1 = index + 1
else:
( found, index2 ) = self.checkForField( nameList, "PERIMETER" )
if not found:
field = QgsField( "PERIMETER", QVariant.Double, "double", 21, 6, self.tr( "Polygon perimeter" ) )
index2 = len( fieldList.keys() ) + 1
field = QgsField( "PERIMETER", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon perimeter" ) )
if len( fieldKeys ) == max( fieldKeys ):
index2 = len( fieldKeys ) + 2
else:
index2 = len( fieldKeys ) + 1
newFields.append( field )
elif geomType == QGis.Line:
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "LENGTH" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
if not found:
field = QgsField( "LENGTH", QVariant.Double, "double", 21, 6, self.tr( "Line length" ) )
index1 = len( fieldList.keys() )
field = QgsField( "LENGTH", QVariant.Double, "double precision", 21, 6, self.tr( "Line length" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldKeys ) + 1
else:
index1 = len( fieldKeys )
newFields.append( field )
index2 = index1
else:
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "XCOORD" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
if not found:
field = QgsField( "XCOORD", QVariant.Double, "double", 21, 6, self.tr( "Point x coordinate" ) )
index1 = len( fieldList.keys() )
field = QgsField( "XCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point x coordinate" ) )
if len( fieldKeys ) == max( fieldKeys ):
index = len( fieldKeys ) + 1
else:
index1 = len( fieldKeys )
newFields.append( field )

( found, index2 ) = self.checkForField( nameList, "YCOORD" )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "YCOORD" )
index2 = index + 1
else:
( found, index2 ) = self.checkForField( nameList, "YCOORD" )
if not found:
field = QgsField( "YCOORD", QVariant.Double, "double", 21, 6, self.tr( "Point y coordinate" ) )
index2 = len( fieldList.keys() ) + 1
field = QgsField( "YCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point y coordinate" ) )
if len( fieldKeys ) == max( fieldKeys ):
index2 = len( fieldKeys ) + 2
else:
index2 = len( fieldKeys ) + 1
newFields.append( field )

vprovider.addAttributes( newFields )
self.vlayer.updateFieldMap()
vlayer.updateFieldMap()
return ( index1, index2 )

def extractAsLine( self, geom ):
Expand Down

0 comments on commit f97b043

Please sign in to comment.