Skip to content

Commit

Permalink
[processing] refactored some helpre functions (contributed by Nathan …
Browse files Browse the repository at this point in the history
…Woodrow)
  • Loading branch information
alexbruy committed Dec 29, 2013
1 parent 6b4641d commit 77fb9a5
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions python/plugins/processing/tools/vector.py
Expand Up @@ -135,34 +135,27 @@ def spatialindex(layer):


def createUniqueFieldName(fieldName, fieldList):
shortName = fieldName[:10]
def nextname(name):
num = 1
while True:
returnname ='{name}_{num}'.format(name=name[:8], num=num)
yield returnname
num += 1

if len(fieldList) == 0:
return shortName
def found(name):
return any(f.name() == name for f in fieldList)

fieldNames = [f.name() for f in fieldList]
shortName = fieldName[:10]

if shortName not in fieldNames:
if not fieldList:
return shortName

shortName = fieldName[:8] + '_1'
changed = True
while changed:
changed = False
for n in fieldList:
if n == shortName:

# Create unique field name
num = int(shortName[-1:])
if num < 9:
shortName = shortName[:8] + '_' + str(num + 1)
else:
shortName = shortName[:7] + '_' + str(num + 1)

changed = True

return shortName
if not found(shortName):
return shortName

for newname in nextname(shortName):
if not found(newname):
return newname

def findOrCreateField(layer, fieldList, fieldName, fieldLen=24, fieldPrec=15):
idx = layer.fieldNameIndex(fieldName)
Expand Down

0 comments on commit 77fb9a5

Please sign in to comment.