def run(osm_file, geometry_handler, index_type = 'sparse_mem_array'): idx = o.index.create_map(index_type) lh = o.NodeLocationsForWays(idx) # lh.ignore_errors() handler = OSMHandler(idx, geometry_handler) relations_pass_reader = o.io.Reader(osm_file, o.osm.osm_entity_bits.RELATION) o.apply(relations_pass_reader, lh, handler) relations_pass_reader.close() print('**** REL PASS COMPLETE ****') handler.receive_rels = False # Hack for not being able to specify entity bits properly full_pass_reader = o.io.Reader(osm_file, o.osm.osm_entity_bits.ALL) o.apply(full_pass_reader, lh, handler) full_pass_reader.close() handler.geom_handler.run_complete() return handler
def main(): idx = osmium.index.create_map( 'sparse_file_array,data/node-cache.nodecache') locations = osmium.NodeLocationsForWays(idx) locations.ignore_errors() osm_file = 'data/greater-london-latest.osm.pbf' node_reader = osmium.io.Reader(osm_file, osmium.osm.osm_entity_bits.NODE) osmium.apply(node_reader, locations) node_reader.close() handler = RouteHandler(idx) way_reader = osmium.io.Reader(osm_file, osmium.osm.osm_entity_bits.WAY) osmium.apply(way_reader, locations, handler) way_reader.close() dump(nodes, 'data/nodes.json') dump(ways, 'data/ways.json')
import osmium as o import sys class WayHandler(o.SimpleHandler): def __init__(self, idx): super(WayHandler, self).__init__() self.idx = idx def way(self, w): for n in w.nodes: n.lat, n.lon # throws an exception if the coordinates are missing loc = idx.get(n.ref) print("%d %s" % (w.id, len(w.nodes))) if len(sys.argv) != 3: print("Usage: python create_nodecache.py <osm file> <node cache>") exit() reader = o.io.Reader(sys.argv[1], o.osm.osm_entity_bits.WAY) idx = o.index.create_map("sparse_file_array," + sys.argv[2]) lh = o.NodeLocationsForWays(idx) lh.ignore_errors() o.apply(reader, lh, WayHandler(idx)) reader.close()
def way(self, way): if ('name' in way.tags): print(way.tags['name']) else: print('No name') coordinates = [] for node in way.nodes: loc = idx.get(node.ref) coordinates.append((loc.lat, loc.lon)) line = self.shape_tools.create_line(coordinates) line_buffer = self.shape_tools.create_line_buffer(coordinates) self.shape_plotter.plot_mappings([line, line_buffer]) if (__name__ == "__main__"): idx = osmium.index.create_map( 'sparse_file_array,data/node-cache.nodecache') locations = osmium.NodeLocationsForWays(idx) locations.ignore_errors() nodes = osmium.io.Reader('data/greater-london-latest.osm.pbf', osmium.osm.osm_entity_bits.NODE) osmium.apply(nodes, locations) nodes.close() ways = osmium.io.Reader('data/greater-london-latest.osm.pbf', osmium.osm.osm_entity_bits.WAY) osmium.apply(ways, locations, WayHandler(idx)) ways.close()
def mapping(file: str, ultra: bool, ratio: bool) -> list: """ Reads an OSM file using Osmium and return a list of coordinates """ class Reference(): """ A reference coordinate """ def __init__(self, lat, lon, x, y): self.lon = lon self.lat = lat self.x = x self.y = y self.pos = mercator(lat, lon) def getCoords(self): return (self.lat, self.lon) f = Path(file) if not f.is_file(): raise Exception("OSM file not found.") with progressbar.ProgressBar(max_value=progressbar.UnknownLength) as bar: bar.update(1) rd = osmium.io.Reader(str(f)) bx = rd.header().box() bounds = ((bx.top_right.lat, bx.top_right.lon), (bx.bottom_left.lat, bx.bottom_left.lon)) bar.update(1) # keeping mapping ratio if ratio: b = mercator(bounds[1][0], bounds[0][1]) # bottom right t = mercator(bounds[0][0], bounds[1][1]) # top left w = np.absolute(b[0] - t[0]) # mapping width h = np.absolute(b[1] - t[1]) # mapping height ir = h / w # inversed ratio config["height"] = int(np.floor(config["width"] * ir)) bottom = Reference(bounds[1][0], bounds[0][1], config["width"], config["height"]) top = Reference(bounds[0][0], bounds[1][1], 0, 0) bar.update(1) idx = osmium.index.create_map("sparse_file_array," + str(f) + ".nodecache") lh = osmium.NodeLocationsForWays(idx) lh.ignore_errors() bar.update(1) w = ways.WaysHandler(idx, ultra) osmium.apply(rd, lh, w) bar.update(1) for way in w.ways: for part in way.parts: sPos = part.startPos() ePos = part.endPos() a = to_screen(top, bottom, sPos[0], sPos[1]) b = to_screen(top, bottom, ePos[0], ePos[1]) part.applyCoords(a, b) bar.update(1) return w.ways