[QGIS Commit] r13313 - trunk/qgis/python/plugins/fTools/tools

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Apr 14 20:27:12 EDT 2010


Author: cfarmer
Date: 2010-04-14 20:27:11 -0400 (Wed, 14 Apr 2010)
New Revision: 13313

Modified:
   trunk/qgis/python/plugins/fTools/tools/doJoinAttributes.py
   trunk/qgis/python/plugins/fTools/tools/doSpatialJoin.py
   trunk/qgis/python/plugins/fTools/tools/ftools_utils.py
Log:
Ensures that field names for output shapefiles remain less than 10 chars. Patch contributed by Alexander Bruy

Modified: trunk/qgis/python/plugins/fTools/tools/doJoinAttributes.py
===================================================================
--- trunk/qgis/python/plugins/fTools/tools/doJoinAttributes.py	2010-04-15 00:24:02 UTC (rev 13312)
+++ trunk/qgis/python/plugins/fTools/tools/doJoinAttributes.py	2010-04-15 00:27:11 UTC (rev 13313)
@@ -103,10 +103,14 @@
       outPath = self.outShape.text()
       self.compute(inName, inField, joinName, joinField, outPath, keep, useTable, self.progressBar)
       self.outShape.clear()
-      addToTOC = QMessageBox.question(self, self.tr("Join Attributes"), self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?").arg( unicode(self.shapefileName) ), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
-      if addToTOC == QMessageBox.Yes:
-        if not ftools_utils.addShapeToCanvas( unicode( outPath ) ):
-          QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( outPath ) ))
+      if res:
+        addToTOC = QMessageBox.question(self, self.tr("Join Attributes"),
+            self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?")
+            .arg( unicode(self.shapefileName) ), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
+        if addToTOC == QMessageBox.Yes:
+          if not ftools_utils.addShapeToCanvas( unicode( outPath ) ):
+            QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" )
+            .arg( unicode( outPath ) ))
     self.progressBar.setValue(0)
 
   def outFile(self):
@@ -171,12 +175,21 @@
     seq = range(0, len(fieldList1) + len(fieldList2))
     fieldList1.extend(fieldList2)
     fieldList1 = dict(zip(seq, fieldList1))
+    # check for correct field names
+    longNames = ftools_utils.checkFieldNameLenght( fieldList1 )
+    if not longNames.isEmpty():
+      QMessageBox.warning( self, self.tr( 'Incorrect field names' ),
+                  self.tr( 'No output will be created.\nFollowing field names are longer then 10 characters:\n%1' )
+                  .arg( longNames.join( '\n' ) ) )
+      return False
     sRs = provider1.crs()
     progressBar.setValue(13)
     check = QFile(self.shapefileName)
     if check.exists():
       if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
-        return
+        QMessageBox.warning( self, self.tr( 'Error deleting shapefile' ),
+                    self.tr( "Can't delete existing shapefile\n%1" ).arg( self.shapefileName ) )
+        return False
     writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fieldList1, provider1.geometryType(), sRs)
     inFeat = QgsFeature()
     outFeat = QgsFeature()
@@ -215,6 +228,7 @@
       nElement += 1
       progressBar.setValue(nElement)
     del writer
+    return True
 
   def createFieldList(self, table, joinField):
     fieldList = {}
@@ -236,14 +250,3 @@
       fieldList[item] = field
       item = item + 1
     return (fieldList, index2)
-
-  def testForUniqueness(self, fieldList1, fieldList2):
-    changed = True
-    while changed:
-      changed = False
-      for i in fieldList1:
-        for j in fieldList2:
-          if j.name() == i.name():
-            j = ftools_utils.createUniqueFieldName(j)
-            changed = True
-    return fieldList2

Modified: trunk/qgis/python/plugins/fTools/tools/doSpatialJoin.py
===================================================================
--- trunk/qgis/python/plugins/fTools/tools/doSpatialJoin.py	2010-04-15 00:24:02 UTC (rev 13312)
+++ trunk/qgis/python/plugins/fTools/tools/doSpatialJoin.py	2010-04-15 00:27:11 UTC (rev 13313)
@@ -88,8 +88,11 @@
                 outName = outName.left(outName.length() - 4)
             self.compute(inName, joinName, outPath, summary, sumList, keep, self.progressBar)
             self.outShape.clear()
-            addToTOC = QMessageBox.question(self, self.tr("Spatial Join"), self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?").arg(unicode(outPath)), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
-            if addToTOC == QMessageBox.Yes:
+            if res:
+              addToTOC = QMessageBox.question(self, self.tr("Spatial Join"),
+                      self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?")
+                      .arg(unicode(outPath)), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
+              if addToTOC == QMessageBox.Yes:
                 self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
                 QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)
         self.progressBar.setValue(0)
@@ -135,13 +138,23 @@
             fieldList1.extend(fieldList)
             seq = range(0, len(fieldList1))
             fieldList1 = dict(zip(seq, fieldList1))
-
+        
+        # check for correct field names
+        longNames = ftools_utils.checkFieldNameLenght( fieldList1 )
+        if not longNames.isEmpty():
+            QMessageBox.warning( self, self.tr( 'Incorrect field names' ),
+                        self.tr( 'No output will be created.\nFollowing field names are longer then 10 characters:\n%1' )
+                        .arg( longNames.join( '\n' ) ) )
+            return False
+        
         sRs = provider1.crs()
         progressBar.setValue(13)
         check = QFile(self.shapefileName)
         if check.exists():
             if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
-                return
+                QMessageBox.warning( self, self.tr( 'Error deleting shapefile' ),
+                            self.tr( "Can't delete existing shapefile\n%1" ).arg( self.shapefileName ) )
+                return False
         writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fieldList1, provider1.geometryType(), sRs)
         #writer = QgsVectorFileWriter(outName, "UTF-8", fieldList1, provider1.geometryType(), sRs)
         inFeat = QgsFeature()
@@ -215,3 +228,4 @@
             start = start + add
             progressBar.setValue(start)
         del writer
+        return True

Modified: trunk/qgis/python/plugins/fTools/tools/ftools_utils.py
===================================================================
--- trunk/qgis/python/plugins/fTools/tools/ftools_utils.py	2010-04-15 00:24:02 UTC (rev 13312)
+++ trunk/qgis/python/plugins/fTools/tools/ftools_utils.py	2010-04-15 00:27:11 UTC (rev 13313)
@@ -12,6 +12,7 @@
 # extractPoints( QgsGeometry )
 # testForUniqueness( QList *QgsField, QList *QgsField )
 # createUniqueFieldName( QgsField.name() )
+# checkFieldNameLenght( QgsFieldMap )
 # getLayerNames( QGis.vectorType() )
 # getFieldNames( QgsVectorLayer )
 # getVectorLayerByName( QgsVectorLayer.name() )
@@ -144,6 +145,7 @@
 # Create a unique field name based on input field name
 def createUniqueFieldName( field ):
     check = field.name().right( 2 )
+    shortName = field.name().left( 8 )
     if check.startsWith("_"):
         ( val, test ) = check.right( 1 ).toInt()
         if test:
@@ -151,13 +153,21 @@
                 val = 2
             else:
                 val = val + 1
-            field.setName( field.name().left( len( field.name() )-1 ) + unicode( val ) )
+            field.setName( shortName.left( len( shortName )-1 ) + unicode( val ) )
         else:
-            field.setName( field.name() + "_2" )
+            field.setName( shortName + "_2" )
     else:
-        field.setName( field.name() + "_2" )
+        field.setName( shortName + "_2" )
     return field
 
+# Return list of field names with more than 10 characters length
+def checkFieldNameLenght( fieldList ):
+    longNames = QStringList()
+    for num, field in fieldList.iteritems():
+        if field.name().size() > 10:
+            longNames << unicode( field.name() )
+    return longNames
+
 # Return list of names of all layers in QgsMapLayerRegistry
 def getLayerNames( vTypes ):
     layermap = QgsMapLayerRegistry.instance().mapLayers()



More information about the QGIS-commit mailing list