How to use the osmium.SimpleHandler function in osmium

To help you get started, we’ve selected a few osmium examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Oslandia / osm-data-classification / src / node-history-stats.py View on Github external
"""


import sys
import os.path as osp
from collections import defaultdict
from datetime import datetime, timezone
#if sys.version_info[0] == 3:
#    from datetime import timezone
import pandas as pd
import osmium as osm

DEFAULT_START = pd.Timestamp("2000-01-01T00:00:00Z")

########################################
class NodeTimelineHandler(osm.SimpleHandler):
    def __init__(self):
        osm.SimpleHandler.__init__(self)
        self.ids = set()
        # list of lists (node_id, lon, lat, version, visible, ts, uid, changesetid, ntags, tagkeys)
        self.nodetimeline = []
        
    def node(self,n):
        self.ids.add(n.id)
        nodeloc = n.location
        if nodeloc.valid():
            self.nodetimeline.append([n.id,
                                      nodeloc.lon,
                                      nodeloc.lat,
                                      n.version,
                                      not(n.deleted),
                                      pd.Timestamp(n.timestamp),
github Oslandia / osm-data-classification / src / relation-history-stats.py View on Github external
"""Extract some stats for OSM ways, from a history OSM data file
The file has to be runned through the following format:                            
python   
"""

import sys
import os.path as osp
from collections import defaultdict
from datetime import datetime, timezone
import pandas as pd
import osmium as osm

DEFAULT_START = pd.Timestamp("2000-01-01T00:00:00Z")

class RelationTimelineHandler(osm.SimpleHandler):
    def __init__(self):
        osm.SimpleHandler.__init__(self)
        self.ids = set()
        # list of lists (way_id, version, visible, ts, uid, changesetid, nb_members, mids, nb_tags, tagkeys)
        self.relationtimeline = []
        
    def relation(self,r):
        self.ids.add(r.id)
        self.relationtimeline.append([r.id,
                                  r.version,
                                  r.visible,
                                  pd.Timestamp(r.timestamp),
                                  r.uid,
                                  r.changeset,
                                  len(r.members),
                                  [(m.ref,m.role,m.type) for m in r.members],
github krithin / gullies / getlocations.py View on Github external
def __init__(self, node_count: int) -> None:
		osmium.SimpleHandler.__init__(self)
		self.node_count = node_count
		self.location_list: List[common.Location] = []
		self.node_index = 0  # indexes nodes while iterating over them
github mapbox / robosat / robosat / osm / building.py View on Github external
import sys

import osmium
import geojson
import shapely.geometry

from robosat.osm.core import FeatureStorage, is_polygon


class BuildingHandler(osmium.SimpleHandler):
    """Extracts building polygon features (visible in satellite imagery) from the map.
    """

    # building=* to discard because these features are not vislible in satellite imagery
    building_filter = set(
        ["construction", "houseboat", "static_caravan", "stadium", "conservatory", "digester", "greenhouse", "ruins"]
    )

    # location=* to discard because these features are not vislible in satellite imagery
    location_filter = set(["underground", "underwater"])

    def __init__(self, out, batch):
        super().__init__()
        self.storage = FeatureStorage(out, batch)

    def way(self, w):
github hotosm / osm-export-tool-python / osm_export_tool / tabular.py View on Github external
layer = self.layers[(layer_name,geom_type)]
        feature = ogr.Feature(layer.defn)
        feature.SetGeometry(geom)
        if layer.osm_id:
            feature.SetField('osm_id',osm_id)
        for column_name in layer.columns:
            if column_name in tags:
                feature.SetField(column_name,tags[column_name])
        layer.ogr_layer.CreateFeature(feature)

    def finalize(self):
        for k, layer in self.layers.items():
            layer.ds.CommitTransaction()
        self.layers = None

class Handler(o.SimpleHandler):
    def __init__(self,outputs,mapping,clipping_geom=None):
        super(Handler, self).__init__()
        self.outputs = outputs
        self.mapping = mapping
        self.clipping_geom = clipping_geom
        if clipping_geom:
            self.prepared_clipping_geom = prep(clipping_geom)

    def node(self,n):
        if len(n.tags) == 0:
            return
        geom = None
        for theme in self.mapping.themes:
            if theme.matches(GeomType.POINT,n.tags):
                if not geom:
                    wkb = fab.create_point(n)
github krithin / gullies / getlocations.py View on Github external
#! /usr/bin/env python

from typing import List
import sys
import random
import osmium

import common

class RandomNodeSelector(osmium.SimpleHandler):
	"""Picks 'node_count' uniformly randomly selected OSM nodes."""
	def __init__(self, node_count: int) -> None:
		osmium.SimpleHandler.__init__(self)
		self.node_count = node_count
		self.location_list: List[common.Location] = []
		self.node_index = 0  # indexes nodes while iterating over them

	def _location(self, node: osmium.osm.Node) -> common.Location:
		return common.Location(latitude = node.location.lat,
		                       longitude = node.location.lon)

	def node(self, n):
		self.node_index += 1
		if self.node_index <= self.node_count:
			# We're still looking at the initial few nodes
			self.location_list.append(self._location(n))
github mapbox / robosat / robosat / osm / parking.py View on Github external
import sys

import osmium
import geojson
import shapely.geometry

from robosat.osm.core import FeatureStorage, is_polygon


class ParkingHandler(osmium.SimpleHandler):
    """Extracts parking lot polygon features (visible in satellite imagery) from the map.
    """

    # parking=* to discard because these features are not vislible in satellite imagery
    parking_filter = set(["underground", "sheds", "carports", "garage_boxes"])

    def __init__(self, out, batch):
        super().__init__()
        self.storage = FeatureStorage(out, batch)

    def way(self, w):
        if not is_polygon(w):
            return

        if "amenity" not in w.tags or w.tags["amenity"] != "parking":
            return

osmium

Python bindings for libosmium, the data processing library for OSM data

BSD-2-Clause
Latest version published 1 month ago

Package Health Score

75 / 100
Full package analysis