Package: osm-bot-abstraction-layer
OSM bot abstraction layer is a python package making easier to automate OpenStreetMap edits without causing problems.This projects contains code shared between OpenStreetMap bots, to avoid writing the same functionality more than once.For example, automated edit may change many objects across large area. In that case it is preferable to split it into multiple edits to avoid country-sized bounding boxes. This logic is available as one of tools included in this project.Installationpip install osm-bot-abstraction-layer --userTestingpython3 -m unittestContributingFirst contact with any project is crucial, so please report any and all issues in this readme. If you needed tool like this but decided to not use it for some fixable reason - please open an issue!Reports about how documentation can be improved, bug reports, pull requests are welcomed!Pull requests are welcomed from smallest typo to big new features - though in case of huge changes it is always a good idea to start from opening an issue.PurposeThis tool is for people who are at onceprogrammersexperienced OSM editorsfollowing OSM rulesAnyone who runs automated edits is responsible for all problems that appear, including ones caused by bugs in external libraries like this one.I eliminated all bugs that I noticed, after all I am using this code myself, but some are still lurking. Issue reports and pull requests are welcomed!Reminder about OSM rulesNote that automated must not be done without consultation or agreement of a community.See theImport/GuidelinesandAutomated Edits/Code of Conductfor more information.Note that automated edits violating rules mentioned above are routinely undone. Undiscussed automatic edits may be reverted by anybody, without any consultation.And yes, it means that some automated edits that would save time and make perfect sense were rejected and should not be made. It is still preferable over unrestricted automated edits.ConfigurationCreatesecret.jsonfile with content like this:{"bot_account":{"username":"Your OSM username for a bot account","password":"?6ofGZm=qr*skR?C,a,1E#k9g8:kE7"},"human_account":{"username":"Your OSM username for a human operated account","password":"t?\\q~,?m;2l?Dd$cKc`?n9PeSDBjj/"}}Usage exampleBot editFollowing is example based on a real automated edit, followingguidelines for the automatic edits.Relevant community was asked. In this case it affected Polish mappers, so thread appearedin Polish section of forum.openstreetmap.org. Note that different communities may use different forums or mailing lists as their communication channels.In additionPage documenting the automated edit was created at OSM wiki.OSM community accepted the edit.Following code was created usingrun_simple_retagging_taskcomponent. Running this script will result in:downloading OSM data using Overpass Turbo as specified inobjects_to_consider_queryparameteriterate over all and objects, ignoring ones where function passed as parameteris_element_editable_checker_functionreturns falsefor all otheredit_element_functionis appliedchanges are automatically split in multiple changesets (if necessary) to avoid too large bounding boxes or too many objects in one editchangeset_comment,discussion_url,osm_wiki_documentation_pageparameter values are used to apply correct changeset tagsRunning this code resulted in two edits:#64628901and#64628951fromosm_bot_abstraction_layer.generic_bot_retaggingimportrun_simple_retagging_taskdefedit_element(tags):iftags.get('amenity')!=("atm"):returntagsiftags.get('name')==("bankomat"):tags.pop('name',None)iftags.get('name')==("Bankomat"):tags.pop('name',None)returntagsdefmain():run_simple_retagging_task(max_count_of_elements_in_one_changeset=500,objects_to_consider_query="""[out:xml][timeout:25000];area[name="Polska"]->.a;(nwr[amenity='atm'][name='Bankomat'](area.a);nwr[amenity='atm'][name='bankomat'](area.a););out body;>;out skel qt;""",cache_folder_filepath='/media/mateusz/5bfa9dfc-ed86-4d19-ac36-78df1060707c/OSM-cache',is_in_manual_mode=False,changeset_comment='usuwanie nazw opisowych z bankomatów (name="bankomat" i name="Bankomat")',discussion_url='https://forum.openstreetmap.org/viewtopic.php?id=66038',osm_wiki_documentation_page='https://wiki.openstreetmap.org/wiki/Mechanical_Edits/Mateusz_Konieczny_-_bot_account/fix_descriptive_name_on_ATMs_in_Poland',edit_element_function=edit_element,)main()Downloading dataUsing Overpassfromosm_bot_abstraction_layer.overpass_downloaderimportdownload_overpass_queryvienna_download="""[timeout:250];(nwr(48.10,16.26,48.26,16.51););out body;>;out skel qt;"""download_overpass_query(vienna_download,'/tmp/vienna.osm')Using OSM Editing APICreatesecret.jsonfile with{"bot_account":{"username":"OSM user name","password":"password in plaintext"}}importosm_bot_abstraction_layer.osm_bot_abstraction_layerasosm_bot_abstraction_layerimportjsonid=1type="node"returned=osm_bot_abstraction_layer.get_data(id,type)json.dumps(returned,default=str,indent=3)it will give output like{"id":1,"visible":true,"version":33,"changeset":124176968,"timestamp":"2022-07-28 09:47:39","user":"owene","uid":29598,"lat":42.7957187,"lon":13.5690032,"tag":{"communication:microwave":"yes","communication:radio":"fm","description":"Radio Subasio","frequency":"105.5 MHz","man_made":"mast","name":"Monte Piselli - San Giacomo","tower:construction":"lattice","tower:type":"communication"}}Handling note spamfromosm_bot_abstraction_layerimportosm_bot_abstraction_layerimportosmapiforidinrange(3158344,3158710):print(id)data=osm_bot_abstraction_layer.get_data(id,"note")text=data['comments'][0]['text']print("----")print(text)print("----")iftextin["tf"]:try:osm_bot_abstraction_layer.close_note(id,"Bot goes brrrrrrr. Spam cleanup.",'bot_account')exceptosmapi.ApiErrorase:# nicer exception requested in https://github.com/metaodi/osmapi/issues/135if"was closed at"instr(e):passelse:raiseShow internal administrative division of IranSeedocs at OSM Wikihow internal administrative structure is taggedimportosm_bot_abstraction_layer.world_dataasworld_dataprint(world_data.list_of_area_division_data('IR',4,["name","wikidata"],'/tmp/boundary_data.osm'))Returns list of dicts with names (fromnametag, so should contain local ones) and wikidata codes.Further documentationDocumentation is currently mostly missing - please, open an issue if it would be useful for you (pull requests are also welcomed).History and etymologyI created this library as part of writingbot editing OpenStreetMap.Parts of the project built uponosmapiand provide an additional abstraction layer. This part was initial and was source of the project name.Project locationThis project resides athttps://github.com/matkoniecz/osm_bot_abstraction_layer