2

company name

I have a column titled CompanyNam and I need to extract the ownership information. For example, if CEZ is the first company then, I assign to it number 1, then Sokolovska is the next number, etc. If CEZ appears again in some other row, it will get number 1. It is important to note that if I have NULL in the column I assign a different number to every NULL row entry. I need the numbers corresponding to the CompanyNam output. I have the following code:

EmptyArray = []
d = {}
newlist = []
for gFeat in GeneratorLayer.getFeatures():
    Owner = gFeat.attributes()[gProvider.fieldNameIndex('CompanyNam')].toString()
    A = ([str(i) for i in Owner]) #convert from PyQt4.QtCore.QString to normal string
    B = ''.join(A)
    EmptyArray.append(B)
    for m, n in enumerate(EmptyArray):
        if n not in d:
            d[n] = [m+1]
        newlist.append({n: d[n]})
        if n == '':
            d[n] = [m+1]
        newlist.append({n: d[n]}) #Every NULL gets a new number
    for names in newlist:
        for o, p in names.iteritems():
            if o == '':
                a2 = str('{},NULL'.format(p))
            elif o != '':
                a2 = str('{},{}'.format(p,o)

I then use a2 in further steps. The code runs fine for a column with 60-100 rows but for larger columns, the computational times are really high. Can you suggest any way in which I can re-write this code, keeping the logic? The output looks like this:

[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[1],CEZ
[9],Sokolovska
[10],International
[11],ENERGOTRANS,
[12],Alpiq
[13],Mittal Steel
[14],United
[1],CEZ
[1],CEZ
[17],Dalkia.....

It would be even nicer to have the numbering [1 ], [2], [3] and not [1 ], [9], [10], but I haven't figure out how to do that.

  • do you need to output a dictionary, or just a list? Do you just want to make a print out? – mr.adam Jul 14 '15 at 14:08
  • I would like to output a list, but for every row, I need to keep track of the ownership info by storing it in a2 because I use a2 to do further calculations for every row in my GeneratorLayer. – ELD Jul 14 '15 at 14:15
3

I am not qgis python savvy, but logically I can follow your script. The main problem is that while you iterate features, you are also iterating the other list and the dictionary, so the elapsed time is growing exponentially. You have created a loop inside a loop, and you're seeing the cost of doing so.

This is how I would go about it, though it's still a little cumbersome (I gather from your code that NULL in the attribute table == '' in the script):

counter = 1
ref_dict = {}
newlist = []
for gFeat in GeneratorLayer.getFeatures():
    Owner = gFeat.attributes()[gProvider.fieldNameIndex('CompanyNam')].toString()
    A = ([str(i) for i in Owner]) #convert from PyQt4.QtCore.QString to normal string
    name = ''.join(A)

    if name == '':
        name = 'NULL'
        newlist.append((counter, name))
        a2 = "[{0}], {1}".format(counter, name)
        counter += 1
    elif not name in ref_dict.keys():
        ref_dict[name] = counter
        newlist.append((counter, name))
        a2 = "[{0}], {1}".format(counter, name)
        counter += 1
    else:
        newlist.append((ref_dict[name], name))
        a2 = "[{0}], {1}".format(ref_dict[name], name)

This should produce a list of tuples that looks like this:

[(1,CEZ),(1,CEZ),...,(2,Sokolovska),(3,International),(4,ENERGOTRANS), etc.]

It will also leave you with the a2 variable which you can use for another operation on the same feature. The ref_dict is just used for the companies whose name is not NULL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.