Skip to content

Handlers and Handler Functions🔗

osmium.SimpleHandler 🔗

The most generic of OSM data handlers. Derive your data processor from this class and implement callbacks for each object type you are interested in. The following data types are recognised:

node, way, relation, area and changeset

A callback takes exactly one parameter which is the object. Note that all objects that are handed into the handler are only readable and are only valid until the end of the callback is reached. Any data that should be retained must be copied into other data structures.

apply_buffer(buffer: Buffer, format: str, locations: bool = False, idx: str = 'flex_mem', filters: List[HandlerLike] = []) -> None 🔗

Apply the handler to a string buffer. The buffer must be a byte string.

apply_file(filename: Union[str, os.PathLike[str], File], locations: bool = False, idx: str = 'flex_mem', filters: List[HandlerLike] = []) -> None 🔗

Apply the handler to the given file. If locations is true, then a location handler will be applied before, which saves the node positions. In that case, the type of this position index can be further selected in idx. If an area callback is implemented, then the file will be scanned twice and a location handler and a handler for assembling multipolygons and areas from ways will be executed.

enabled_for() -> osm_entity_bits 🔗

Return the list of OSM object types this handler will handle.

osmium.MergeInputReader 🔗

Buffer which collects data from multiple input files, sorts it and optionally deduplicates the data before applying to a handler.

__init__() -> None 🔗

Initialize a new reader.

add_buffer(buffer: Union[ByteString, str], format: str) -> int 🔗

Add input data from a buffer to the reader. The buffer may be any data which follows the Python buffer protocol. The mandatory format parameter describes the format of the data.

The data will be copied into internal buffers, so that the input buffer can be safely discarded after the function has been called.

add_file(file: str) -> int 🔗

Add data from the given input file file to the reader.

apply(*handlers: HandlerLike, idx: str = '', simplify: bool = True) -> None 🔗

Apply collected data to a handler. The data will be sorted first. If simplify is true (default) then duplicates will be eliminated and only the newest version of each object kept. If idx is given a node location cache with the given type will be created and applied when creating the ways. Note that a diff file normally does not contain all node locations to reconstruct changed ways. If the full way geometries are needed, create a persistent node location cache during initial import of the area and reuse it when processing diffs. After the data has been applied the buffer of the MergeInputReader is empty and new data can be added for the next round of application.

apply_to_reader(reader: Reader, writer: Writer, with_history: bool = ...) -> None 🔗

Apply the collected data to data from the given reader and write the result to writer. This function can be used to merge the diff " data together with other OSM data (for example when updating a planet file. If with_history is true, then the collected data will be applied verbatim without removing duplicates. This is important when using OSM history files as input.

osmium.NodeLocationsForWays 🔗

Bases: osmium._osmium.BaseHandler

Handler for retriving and caching locations from ways and adding them to ways.

apply_nodes_to_ways: bool property writable 🔗

When set (the default), the collected locations are propagated to the node list of ways.

__init__(locations: LocationTable) -> None 🔗

Initiate a new handler using the given location table locations to cache the node coordinates.

ignore_errors() -> None 🔗

Disable raising an exception when filling the node list of a way and a coordinate is not available.

Handler functions🔗

osmium.apply(reader: Union[Reader, str, os.PathLike[str], File, FileBuffer], *handlers: HandlerLike) -> None 🔗

Apply a chain of handlers to the given input source. The input source may be a osmium.io.Reader, a file or a file buffer. If one of the handlers is a filter, then processing of the object will be stopped when it does not pass the filter.

osmium.make_simple_handler(node: HandlerFunc[Node] = None, way: HandlerFunc[Way] = None, relation: HandlerFunc[Relation] = None, area: HandlerFunc[Area] = None, changeset: HandlerFunc[Changeset] = None) -> SimpleHandler 🔗

(deprecated) Convenience function that creates a SimpleHandler from a set of callback functions. Each of the parameters takes an optional callable that must expect a single positional parameter with the object being processed.