Skip to main content

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

Likely pip3 install osm-iterator if pip points to Python2 pip.

It is distributed as an osm_iterator PyPI package.

PyPI version

Usage example

Download data and show it

This usage example includes downloading data using requests library, that you may need to install (also available via pip).

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)

Load data only

from osm_iterator import osm_iterator

global osm_object_store
osm_object_store = []

def record_objects(element):
    global osm_object_store
    print(element.element.tag, element.element.attrib['id'])
    osm_object_store.append({"type": element.get_type(), "id": element.get_id()})

filepath = "output.osm"
osm = osm_iterator.Data(filepath)
osm.iterate_over_data(record_objects)
for entry in osm_object_store:
    print(entry)

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

osm_iterator-1.5.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

osm_iterator-1.5.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file osm_iterator-1.5.0.tar.gz.

File metadata

  • Download URL: osm_iterator-1.5.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.28.1 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.10

File hashes

Hashes for osm_iterator-1.5.0.tar.gz
Algorithm Hash digest
SHA256 b67b031e34ec0b1bd9039898a9168261b0001fe33d9001bc8ecd2183edcfb585
MD5 38bfa479223796ea554cc14b13925aaa
BLAKE2b-256 d0a4c396ac82831ce6fce2f00a2f621bea4c656aec9e88622f05f7a69988be5d

See more details on using hashes here.

File details

Details for the file osm_iterator-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: osm_iterator-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.28.1 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.10

File hashes

Hashes for osm_iterator-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d55759dca78dad0d08733d3d8ce507988cfc5351eba8d087eb3db45cd5ef27f4
MD5 7b34d9dbc863e3a48dcc6452d4538475
BLAKE2b-256 79a14bb4d0b82961462c150874ab66fe350191b7866fa536b34a99e86bcf5fc4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page