Iterate over .osm file and call a provided callback function for each element
Project description
This code loads .osm file and allows to call function on all OSM objects in dataset.
Installation
pip install osm-iterator
It is distributed as an osm_iterator
PyPI package.
Usage example
from osm_iterator import osm_iterator
import requests
import os.path
def download_from_overpass(query, output_filepath):
print(query)
url = "http://overpass-api.de/api/interpreter"
r = requests.get(url, params={'data': query})
result = r.text
with open(output_filepath, 'w') as file:
file.write(str(result))
def show_places(element):
place_tag = element.get_tag_value("place")
name_tag = element.get_tag_value("name")
osm_object_url = element.get_link()
if place_tag != None:
print(name_tag, "(", place_tag, ") is ", osm_object_url)
filepath = "places_in_Kraków.osm"
query = """
[out:xml][timeout:2500];
area[name='Kraków']->.searchArea;
(
node["place"](area.searchArea);
way["place"](area.searchArea);
relation["place"](area.searchArea);
);
out center;
"""
if os.path.isfile(filepath) == False:
download_from_overpass(query, filepath)
osm = osm_iterator.Data(filepath)
osm.iterate_over_data(show_places)
Running tests
nosetests3
or python3 -m unittest
or python3 tests.py
History
Design explanation: this code has deeply suboptimal handling of pretty much everything. For start, all data is loaded into memory and then duplicated in-memory dataset is created.
As result, attempt to process any large datasets will cause issues due to excessive memory consumption.
This situation is consequence of following facts
- This code was written during my first attempt to process OSM data using Python
- API allows (at least in theory) to painlessly switch to real iterator that is not loading all data into memory at once
- So far this was good enough for my purposes so I had no motivation to spend time on improving something that is not a bottleneck
Though, if someone has good ideas for improvements (especially in form of a working code) - comments and pull requests are welcomed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for osm_iterator-1.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7031d5457a525a4aecd22f27b2e7bf878f3900ae723bc71560a77f509f6b5adc |
|
MD5 | 1d4aa11d6805acb515aa9c70963fc62f |
|
BLAKE2b-256 | 47af19b14759889c7a953755210188b3d977d6699deeed7ad4c8f915e6d156c3 |