Ensures that field names for output shapefiles remain less than 10 chars. Patch contributed by Alexander Bruy

git-svn-id: http://svn.osgeo.org/qgis/trunk@13313 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
cfarmer 2010-04-15 00:27:11 +00:00
parent a44c1bfefa
commit 71a9f815a5
3 changed files with 50 additions and 23 deletions

View File

@ -103,10 +103,14 @@ class Dialog(QDialog, Ui_Dialog):
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 @@ class Dialog(QDialog, Ui_Dialog):
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 @@ class Dialog(QDialog, Ui_Dialog):
nElement += 1
progressBar.setValue(nElement)
del writer
return True
def createFieldList(self, table, joinField):
fieldList = {}
@ -236,14 +250,3 @@ class Dialog(QDialog, Ui_Dialog):
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

View File

@ -88,8 +88,11 @@ class Dialog(QDialog, Ui_Dialog):
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 @@ class Dialog(QDialog, Ui_Dialog):
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 @@ class Dialog(QDialog, Ui_Dialog):
start = start + add
progressBar.setValue(start)
del writer
return True

View File

@ -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 @@ def testForUniqueness( fieldList1, fieldList2 ):
# 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 @@ def createUniqueFieldName( field ):
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()