OpenStreetMap logo OpenStreetMap

Simple script to bulk add non redundant tags on boundary=administrative ways

Posted by capiscuas on 22 April 2018 in English. Last updated on 24 April 2018.

Sometimes we find in some cities/countries some boundary ways (the ones that define the political borders of provinces/towns/countries) that are missing some tag(not appearing already in the relations, because it’s better to avoid redundant data.

Example of boundary way without any tag:

For the renders and 3rd party apps that is not a big problem because that way is indide the the corresponding relations with different admin_level, it can be shared by one or many relations, but what happens if we need to add a tag to those ways based on the data from their relations?

The script is the following, based on Dmitri Lebedev’s osm2python library (not python3 compatible yet, help migrating).

# -*- coding: utf-8 -*-
# Use python2, since osm2python is not ported to python3

from osm2python.tree import load, dump
from collections import defaultdict

origin_osm_filename = 'boundaries.osm'
destiny_osm_filename = 'boundaries_updated.osm'

# loading the .osm file
osmtree = load(open(origin_osm_filename))

ways_min_admin_levels = defaultdict(int)
for i, r in osmtree.relations.items():
    if 'admin_level' in r.tags:
        admin_level = int(r.tags['admin_level'])
    else:
        print(r, 'has no admin_level')

    for way in r.members:
        if ways_min_admin_levels[way.ref] == 0 or ways_min_admin_levels[way.ref] > admin_level:
            ways_min_admin_levels[way.ref] = admin_level

for i, w in osmtree.ways.items():
    w.tags['admin_level'] = str(ways_min_admin_levels[i])

# Saving to .osm
dump(open(destiny_osm_filename, 'w'), osmtree)

The result example looks like this: As you can see the way now has admin_level=5 which is the minimum level of all the boundary relations that way is inside of.

After some comments below, I realized that adding redundant data in OSM is not a good practice so better to use the script for other cases.

I hope it is helpful for you and you can hack it to do more things than needed.

Email icon Bluesky Icon Facebook Icon LinkedIn Icon Mastodon Icon Telegram Icon X Icon

Discussion

Comment from Nakaner on 22 April 2018 at 17:20

Automated edits like the one you propose are governed by the Automated Edits Code of Conduct. You have to discuss such edits on the communication channels mentioned by the guideline.

Ways of administrative boundaries don’t need to be tagged if the are only boundary lines and don’t represent any other feature. Editors with proper styling will show these lines as boundary lines even if they don’t have any tags. In addition, experience tells us that other mappers will break boundary relations even if they are rendered as such.

Comment from capiscuas on 22 April 2018 at 17:59

Yeah, as I said it is not a mandatory thing to do because editors and renders can understand from the relation.

Comment from Zverik on 23 April 2018 at 13:53

This is a bad idea and simply creates a lot of duplicate data in OSM, which will definitely fall out of sync in a couple years.

Nice code though.

Comment from TheSwavu on 23 April 2018 at 23:02

This was extensively discussed on the tagging mail list:

https://www.mail-archive.com/tagging@openstreetmap.org/msg35194.html

and the view was strongly against duplicating tags on the member ways of relations.

Comment from capiscuas on 24 April 2018 at 10:13

Thanks for the comments, I realized it was a bad practice in OSM and changed the title of it.

Log in to leave a comment