Skip to content

Commit

Permalink
Ensures that field names for output shapefiles remain less than 10 ch…
Browse files Browse the repository at this point in the history
…ars. Patch contributed by Alexander Bruy

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13313 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer committed Apr 15, 2010
1 parent a020030 commit f193203
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
35 changes: 19 additions & 16 deletions python/plugins/fTools/tools/doJoinAttributes.py
Expand Up @@ -103,10 +103,14 @@ def accept(self):
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):
Expand Down Expand Up @@ -171,12 +175,21 @@ def compute(self, inName, inField, joinName, joinField, outName, keep, useTable,
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()
Expand Down Expand Up @@ -215,6 +228,7 @@ def compute(self, inName, inField, joinName, joinField, outName, keep, useTable,
nElement += 1
progressBar.setValue(nElement)
del writer
return True

def createFieldList(self, table, joinField):
fieldList = {}
Expand All @@ -236,14 +250,3 @@ def createFieldList(self, table, joinField):
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
22 changes: 18 additions & 4 deletions python/plugins/fTools/tools/doSpatialJoin.py
Expand Up @@ -88,8 +88,11 @@ def accept(self):
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)
Expand Down Expand Up @@ -135,13 +138,23 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
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()
Expand Down Expand Up @@ -215,3 +228,4 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
start = start + add
progressBar.setValue(start)
del writer
return True
16 changes: 13 additions & 3 deletions python/plugins/fTools/tools/ftools_utils.py
Expand Up @@ -12,6 +12,7 @@
# extractPoints( QgsGeometry )
# testForUniqueness( QList *QgsField, QList *QgsField )
# createUniqueFieldName( QgsField.name() )
# checkFieldNameLenght( QgsFieldMap )
# getLayerNames( QGis.vectorType() )
# getFieldNames( QgsVectorLayer )
# getVectorLayerByName( QgsVectorLayer.name() )
Expand Down Expand Up @@ -144,20 +145,29 @@ 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:
if val < 2:
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()
Expand Down

0 comments on commit f193203

Please sign in to comment.