開放街圖標誌 OpenStreetMap 開放街圖

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

於 2018年四月22日 由 capiscuasEnglish發表。 上一次更新在 2018年四月24日。

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.

電子郵件圖示 藍天圖示 Facebook 圖示 LinkedIn 圖示 乳齒象圖示 Telegram 圖示 X 圖示

討論

Nakaner2018年04月22日 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.

capiscuas2018年04月22日 17時59分 發表的評論

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

Zverik2018年04月23日 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.

TheSwavu2018年04月23日 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.

capiscuas2018年04月24日 10時13分 發表的評論

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

登入 來留下評論