Skip to main content

Hi all,

I have an attribute table in the following format:

localitycountmonthbright123janbright456febbright789marmyrtleford987janmyrtleford654febmyrtleford321mar

I would like to change it to the following format:

localityjanfebmarbright123456789myrtleford987654321

I have a convoluted process using an attribute filter and a bunch of statistics calculators but it's not very scaleable. I was wondering if anyone can point me in the right direction for an improved solution!

Cheers,

Barrett

Hi @barrett_h, I think a general approach is to rename 'count' to the value of 'month' in the same record, and then aggregate the records grouped by 'locality'. An implementation example is:

Instead of renaming with the BulkAttributeRenamer, you can also use the AttributeCreator to create a new attribute whose name is equal to the value of 'month' and assign the value of 'count' to that. This may be easier to understand.

Here is another approach. Create a JSON document containing pairs of key (month) and value (count), and flatten it to extract the key/value pairs as attributes.

There is more than one way, but I usually use the BulkAttributeRenamer in a similar case.

Hope this helps.


Hi @barrett_h, I think a general approach is to rename 'count' to the value of 'month' in the same record, and then aggregate the records grouped by 'locality'. An implementation example is:

0684Q00000ArKSAQA3.png

Instead of renaming with the BulkAttributeRenamer, you can also use the AttributeCreator to create a new attribute whose name is equal to the value of 'month' and assign the value of 'count' to that. This may be easier to understand.

0684Q00000ArKPGQA3.png

Here is another approach. Create a JSON document containing pairs of key (month) and value (count), and flatten it to extract the key/value pairs as attributes.

0684Q00000ArKdqQAF.png

There is more than one way, but I usually use the BulkAttributeRenamer in a similar case.

Hope this helps.

If you were familiar with Python scripting, the PythonCaller could also be an alternative of course.

 

# PythonCaller Script Example
class FeatureProcessor(object):
    def __init__(self):
        # {key=locaity : value=feature}
        self.kv = {}
        
    def input(self, feature):
        loc = feature.getAttribute('locality')
        mon = feature.getAttribute('month')
        cnt = feature.getAttribute('count')
        self.kv.setdefault(loc, feature).setAttribute(mon, cnt)
        
    def close(self):
        for feature in self.kv.values():
            self.pyoutput(feature)
0684Q00000ArMK4QAN.png

 


Hi @barrett_h, I think a general approach is to rename 'count' to the value of 'month' in the same record, and then aggregate the records grouped by 'locality'. An implementation example is:

Instead of renaming with the BulkAttributeRenamer, you can also use the AttributeCreator to create a new attribute whose name is equal to the value of 'month' and assign the value of 'count' to that. This may be easier to understand.

Here is another approach. Create a JSON document containing pairs of key (month) and value (count), and flatten it to extract the key/value pairs as attributes.

There is more than one way, but I usually use the BulkAttributeRenamer in a similar case.

Hope this helps.

Those are great suggestions @takashi! Thank you very much, I ended up going for the JSON idea as it provides so much flexibility.

 


Reply