├── LICENSE ├── osm-blame.py └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /osm-blame.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import xml.etree.ElementTree as ET 4 | import urllib.request 5 | from tabulate import tabulate 6 | from optparse import OptionParser 7 | 8 | parser = OptionParser() 9 | 10 | parser.add_option("-a", "--attribs", action="store", dest="attributes", default="user,version") 11 | parser.add_option("-d", "--hide-deleted", action="store_false", dest="show_deleted", default=True) 12 | parser.add_option("-c", "--changeset-tags", action="store", dest="changeset_tags", default=None) 13 | 14 | (options, args) = parser.parse_args() 15 | 16 | show_attrib = options.attributes.split(',') 17 | show_changeset_tag = [] if options.changeset_tags is None else options.changeset_tags.split(',') 18 | changeset_cache = {} 19 | 20 | def get_changeset_tag(changeset, find_tag): 21 | if (not changeset_cache.get(changeset)): 22 | crequest = "https://www.openstreetmap.org/api/0.6/changeset/{}".format(changeset) 23 | cresponse = urllib.request.urlopen(crequest) 24 | changeset_cache[changeset] = cresponse.read() 25 | 26 | changeset_root = ET.fromstring(changeset_cache[changeset]) 27 | 28 | for cversion in changeset_root: 29 | for tag in cversion.findall('tag'): 30 | if tag.get('k') == find_tag: 31 | return tag.get('v') 32 | return None 33 | 34 | item = args[0] 35 | 36 | request = "https://www.openstreetmap.org/api/0.6/{}/history".format(item) 37 | response = urllib.request.urlopen(request) 38 | history_xml = response.read() 39 | 40 | history_root = ET.fromstring(history_xml) 41 | 42 | blame_tag = {} 43 | 44 | 45 | for version in sorted(history_root, key=lambda x: int(x.get('version'))): 46 | 47 | attrib = dict(version.attrib) 48 | 49 | for tag in version.findall('tag'): 50 | tag_key = tag.get('k') 51 | tag_val = tag.get('v') 52 | if ( 53 | tag_key not in blame_tag or 54 | tag_val != blame_tag[tag_key]['value'] 55 | ): 56 | blame_tag[tag_key] = {'value': tag_val, 'attrib': attrib} 57 | 58 | for key in blame_tag.keys(): 59 | if ( 60 | version.find("./tag[@k='{}']".format(key)) is None and 61 | blame_tag[tag_key]['value'] is not None 62 | ): 63 | blame_tag[key] = {'value': None, 'attrib': attrib} 64 | 65 | final = [] 66 | for key, tag in blame_tag.items(): 67 | if options.show_deleted: 68 | line = ['+' if tag['value'] else '-'] 69 | else: 70 | if tag['value'] is None: 71 | continue 72 | line = [] 73 | 74 | line += [key, tag['value']] + [tag['attrib'][attrib_name] for attrib_name in show_attrib] + \ 75 | [get_changeset_tag(tag['attrib']['changeset'], c_tag_name) for c_tag_name in show_changeset_tag] 76 | 77 | final.append(line) 78 | 79 | if options.show_deleted: 80 | headers = [''] 81 | else: 82 | headers = [] 83 | 84 | headers += ['key', 'value'] + show_attrib + show_changeset_tag 85 | 86 | print(tabulate(final, headers=headers)) 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # osm-blame 2 | 3 | _Who touched this element?_ 4 | 5 | osm-blame is a minimalist tool inspired by git blame, whicch only objective is to show informations about last modifications of an OpenStreetMap element, tag by tag. By default it shows also tags that once existed and were deleted. 6 | 7 | ## Basic usage 8 | 9 | The script takes as argument the reference of the element under the form `/`. 10 | 11 | The output is a table showing a list of the tags and info about last time it was altered. 12 | 13 | ``` 14 | $ ./osm-blame.py node/1987567153 15 | key value user version 16 | -- -------------------- ----------------------------------------- ---------------- --------- 17 | - addr:city Rom1 11 18 | - addr:country Rom1 11 19 | + addr:housenumber 50 Kalaallit Nunaat 1 20 | - addr:postcode Rom1 11 21 | - addr:street Rom1 11 22 | + amenity restaurant Kalaallit Nunaat 1 23 | + cuisine french Kalaallit Nunaat 1 24 | + name Brasserie Excelsior Kalaallit Nunaat 1 25 | + opening_hours Mo-Su 00:00-00:30,08:00-24:00 Xapitoun 3 26 | + payment:credit_cards yes Kalaallit Nunaat 1 27 | - phone Rom1 11 28 | + source local_knowledge Kalaallit Nunaat 1 29 | - website Rom1 11 30 | + wikipedia fr:Brasserie Excelsior py_berrard 4 31 | - fax Rom1 11 32 | + wikidata Q2923998 nyuriks 8 33 | + loc_name L’Excel’ gendy54 9 34 | + contact:fax +33 3 83351848 Rom1 11 35 | + contact:phone +33 3 83352457 Rom1 11 36 | + contact:website https://www.brasserie-excelsior-nancy.fr/ Rom1 11 37 | ``` 38 | 39 | About the symbols before each line: `+` means that the tag is still applied, `-` means that it was deleted and the following info is about the time it has been deleted. 40 | 41 | ## Options 42 | 43 | * `-a LIST`, `--attribs LIST`: `LIST` is a comma-separated list of attributes to show (not including keys and values). Default is `user,version`. Possible attributes: `user`, `version`, `uid`, `changeset`, `timestamp`. Other attributes are possibles but vary from the element type. More informations about attributes on OpenStreetMap API documentation. 44 | * `-c LIST`, `--changeset-tags LIST`: `LIST` is a comma-separated list of changeset tags to show. Default is none, as it is slower due to more API calls. Common changeset tags: `created_by`, `comment`, `source`, `imagery_used`. Other changeset tags are possible but more rare, see the [OSM changeset documentation](https://wiki.openstreetmap.org/wiki/Changeset#Tags_on_changesets) 45 | * `-d`, `--hide-deleted`: Do not show deleted tags. 46 | 47 | ## Contribute 48 | 49 | As you can notice, this tool is no more than a dirty script for now. I am no developper and any help is of course always welcome, especially for cleaning the code and making it more readable. Don't hesitate to take a look at the issue tracker. 50 | --------------------------------------------------------------------------------