Skip to content

Commit 71a9f81

Browse files
author
cfarmer
committedApr 15, 2010
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
1 parent a44c1bf commit 71a9f81

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed
 

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,14 @@ def accept(self):
103103
outPath = self.outShape.text()
104104
self.compute(inName, inField, joinName, joinField, outPath, keep, useTable, self.progressBar)
105105
self.outShape.clear()
106-
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)
107-
if addToTOC == QMessageBox.Yes:
108-
if not ftools_utils.addShapeToCanvas( unicode( outPath ) ):
109-
QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( outPath ) ))
106+
if res:
107+
addToTOC = QMessageBox.question(self, self.tr("Join Attributes"),
108+
self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?")
109+
.arg( unicode(self.shapefileName) ), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
110+
if addToTOC == QMessageBox.Yes:
111+
if not ftools_utils.addShapeToCanvas( unicode( outPath ) ):
112+
QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" )
113+
.arg( unicode( outPath ) ))
110114
self.progressBar.setValue(0)
111115

112116
def outFile(self):
@@ -171,12 +175,21 @@ def compute(self, inName, inField, joinName, joinField, outName, keep, useTable,
171175
seq = range(0, len(fieldList1) + len(fieldList2))
172176
fieldList1.extend(fieldList2)
173177
fieldList1 = dict(zip(seq, fieldList1))
178+
# check for correct field names
179+
longNames = ftools_utils.checkFieldNameLenght( fieldList1 )
180+
if not longNames.isEmpty():
181+
QMessageBox.warning( self, self.tr( 'Incorrect field names' ),
182+
self.tr( 'No output will be created.\nFollowing field names are longer then 10 characters:\n%1' )
183+
.arg( longNames.join( '\n' ) ) )
184+
return False
174185
sRs = provider1.crs()
175186
progressBar.setValue(13)
176187
check = QFile(self.shapefileName)
177188
if check.exists():
178189
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
179-
return
190+
QMessageBox.warning( self, self.tr( 'Error deleting shapefile' ),
191+
self.tr( "Can't delete existing shapefile\n%1" ).arg( self.shapefileName ) )
192+
return False
180193
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fieldList1, provider1.geometryType(), sRs)
181194
inFeat = QgsFeature()
182195
outFeat = QgsFeature()
@@ -215,6 +228,7 @@ def compute(self, inName, inField, joinName, joinField, outName, keep, useTable,
215228
nElement += 1
216229
progressBar.setValue(nElement)
217230
del writer
231+
return True
218232

219233
def createFieldList(self, table, joinField):
220234
fieldList = {}
@@ -236,14 +250,3 @@ def createFieldList(self, table, joinField):
236250
fieldList[item] = field
237251
item = item + 1
238252
return (fieldList, index2)
239-
240-
def testForUniqueness(self, fieldList1, fieldList2):
241-
changed = True
242-
while changed:
243-
changed = False
244-
for i in fieldList1:
245-
for j in fieldList2:
246-
if j.name() == i.name():
247-
j = ftools_utils.createUniqueFieldName(j)
248-
changed = True
249-
return fieldList2

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ def accept(self):
8888
outName = outName.left(outName.length() - 4)
8989
self.compute(inName, joinName, outPath, summary, sumList, keep, self.progressBar)
9090
self.outShape.clear()
91-
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)
92-
if addToTOC == QMessageBox.Yes:
91+
if res:
92+
addToTOC = QMessageBox.question(self, self.tr("Spatial Join"),
93+
self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?")
94+
.arg(unicode(outPath)), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
95+
if addToTOC == QMessageBox.Yes:
9396
self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
9497
QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)
9598
self.progressBar.setValue(0)
@@ -135,13 +138,23 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
135138
fieldList1.extend(fieldList)
136139
seq = range(0, len(fieldList1))
137140
fieldList1 = dict(zip(seq, fieldList1))
138-
141+
142+
# check for correct field names
143+
longNames = ftools_utils.checkFieldNameLenght( fieldList1 )
144+
if not longNames.isEmpty():
145+
QMessageBox.warning( self, self.tr( 'Incorrect field names' ),
146+
self.tr( 'No output will be created.\nFollowing field names are longer then 10 characters:\n%1' )
147+
.arg( longNames.join( '\n' ) ) )
148+
return False
149+
139150
sRs = provider1.crs()
140151
progressBar.setValue(13)
141152
check = QFile(self.shapefileName)
142153
if check.exists():
143154
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
144-
return
155+
QMessageBox.warning( self, self.tr( 'Error deleting shapefile' ),
156+
self.tr( "Can't delete existing shapefile\n%1" ).arg( self.shapefileName ) )
157+
return False
145158
writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fieldList1, provider1.geometryType(), sRs)
146159
#writer = QgsVectorFileWriter(outName, "UTF-8", fieldList1, provider1.geometryType(), sRs)
147160
inFeat = QgsFeature()
@@ -215,3 +228,4 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
215228
start = start + add
216229
progressBar.setValue(start)
217230
del writer
231+
return True

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# extractPoints( QgsGeometry )
1313
# testForUniqueness( QList *QgsField, QList *QgsField )
1414
# createUniqueFieldName( QgsField.name() )
15+
# checkFieldNameLenght( QgsFieldMap )
1516
# getLayerNames( QGis.vectorType() )
1617
# getFieldNames( QgsVectorLayer )
1718
# getVectorLayerByName( QgsVectorLayer.name() )
@@ -144,20 +145,29 @@ def testForUniqueness( fieldList1, fieldList2 ):
144145
# Create a unique field name based on input field name
145146
def createUniqueFieldName( field ):
146147
check = field.name().right( 2 )
148+
shortName = field.name().left( 8 )
147149
if check.startsWith("_"):
148150
( val, test ) = check.right( 1 ).toInt()
149151
if test:
150152
if val < 2:
151153
val = 2
152154
else:
153155
val = val + 1
154-
field.setName( field.name().left( len( field.name() )-1 ) + unicode( val ) )
156+
field.setName( shortName.left( len( shortName )-1 ) + unicode( val ) )
155157
else:
156-
field.setName( field.name() + "_2" )
158+
field.setName( shortName + "_2" )
157159
else:
158-
field.setName( field.name() + "_2" )
160+
field.setName( shortName + "_2" )
159161
return field
160162

163+
# Return list of field names with more than 10 characters length
164+
def checkFieldNameLenght( fieldList ):
165+
longNames = QStringList()
166+
for num, field in fieldList.iteritems():
167+
if field.name().size() > 10:
168+
longNames << unicode( field.name() )
169+
return longNames
170+
161171
# Return list of names of all layers in QgsMapLayerRegistry
162172
def getLayerNames( vTypes ):
163173
layermap = QgsMapLayerRegistry.instance().mapLayers()

0 commit comments

Comments
 (0)
Please sign in to comment.