1

I've got something working for QGIS 2.8 using Python 2.7 on Windows 7, but I am missing an important part of the puzzle. I'm trying to develop an algorithm to number homes. I've now come to a point where some neighbors have a duplicate house number. For this reason, I've come with a python script that successfully increments the numbers by two (to keep numbers odd or even), depending on the number of duplicates (n-1 for n duplicates, so that exactly one gets to keep the original number). I'm dealing with a metric numbering system, where the house number corresponds with a distance of the home along the road it belongs to.

Starting with this dictionary, where the keys are feature IDs and the values are the concatenated homeNumber_roadNumber:

Deca_dict = {
    "1": "2_506",
    "2": "2_506",
    "3": "2_506",
    "4": "2_600",
    "5": "1_650",
    "6": "2_600"
}

I counted the number of occurrences and kept track of the original number i'm interested in incrementing:

decaAdd_occurrences = {k: (int(k.split('_')[0]), v) for k,v in
                                Counter(Deca_dict.values()).items()}

With the help of a Stack Overflow answer, I adapted my needs and got here:

I adapted the answer's code like so:

changed_deca_dict = {}
alpha_deca_dict = {}
for key, value in Deca_dict.items():
    num, cnt = decaAdd_occurrences[value]
    if cnt > 1:
        route = value.split('_')[1]
        next_num = num + 1
        next_add = str(next_num) + '_' + str(route)
        if not next_add in Deca_dict.values():
            Deca_dict[key] = '{}_{}'.format(next_num, route)
            decaAdd_occurrences[value] = next_num, cnt-1
            changed_deca_dict[key] = '{}_{}_{}'.format(num, next_num, route)
        else:
            alpha_deca_dict[key] = value

This gives me the resulting dictionaries:

Deca_dict -> {
    "1": "3_506",
    "2": "2_506",
    "3": "4_506",
    "4": "3_600",
    "5": "1_650",
    "6": "2_600"
}

changed_deca_dict = {'1':'2_3_506', '3':'2_4_506','4':'2_3_600'}

I have another dictionary with the same keys and their distances along an axis (the order in which I'm trying to number these values)

distance_dict = {'3': 1.0,'1': 2.2,'2': 3.1,'4': 2.0,'5': 1.5,'6': 3.0}

This is the result I would like, re-numbering the duplicate values taking into account the distances of each feature on it's line. Since features 1, 2, and 3 are on the same road, I would re-number the duplicates taking into account their distances along the road. Same for features 4 and 6.

Deca_dict -> {
    "1": "3_506",
    "2": "4_506",
    "3": "2_506",
    "4": "2_600",
    "5": "1_650",
    "6": "3_600"
}

I saw this other thread, which shows how to sort features by a particular field. I'm not sure how to work it in. I'm trying to start by grouping the keys by similar values. I don't want the whole value compared, so this is what I have so far:

for deca_key, deca_value in changed_deca_dict:
    old_num = value.split('_')[0]
    new_num = value.split('_')[1]
    route = value.split('_')[2]
    old_add = str(old_num) + str(route)
    for dist_key, dist_value in distance_dict:
        if dist_key == deca_key:
            check the distance and compare with other Deca_dict.items(), including the one that didn't change
            re-order new_num according to distance along route

Your Answer

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

Browse other questions tagged or ask your own question.