Skip to main content

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.

Metadata

Release files for osm-iterator 1.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for osm-iterator 1.6.0
File Size Uploaded Provenance
osm_iterator-1.6.0.tar.gz 4.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for osm-iterator 1.6.0
File Interpreter ABI Platform
osm_iterator-1.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / osm_iterator-1.6.0.tar.gz

Download URL osm_iterator-1.6.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9545e98b6820850faee761c15939a02e805c32408488db1c78798cb195016dfa
BLAKE2b-256 checksum
How to use checksums
d20a297e09459a69b9151e74a4023fa8d0e567fae7c7b931139fc5561a0027e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / osm_iterator-1.6.0-py3-none-any.whl

Download URL osm_iterator-1.6.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
438f36a239dde54fa13ebc7e5e64aa7e3ac0fc61db4c6ed4ae134c0bc4102c9a
BLAKE2b-256 checksum
How to use checksums
2f03d23940717e68ca3cec3e9746c092de6e66108ccf041c025afe104c0a45ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Security for osm-iterator 1.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page