Пример #1
0
 def _run_file(self, fn):
     try:
         rd = o.io.Reader(fn)
         o.apply(rd, o.SimpleHandler())
         rd.close()
     finally:
         os.remove(fn)
Пример #2
0
 def _run_file(self, fn):
     try:
         rd = o.io.Reader(fn)
         o.apply(rd, o.SimpleHandler())
         rd.close()
     finally:
         os.remove(fn)
Пример #3
0
 def test_func(self):
     fn = create_osm_file(self.data)
     try:
         rd = osmium.io.Reader(fn)
         osmium.apply(rd, self.Handler())
         rd.close()
     finally:
         os.remove(fn)
Пример #4
0
 def test_func(self):
     fn = create_osm_file(self.data)
     try:
         rd = osmium.io.Reader(fn)
         osmium.apply(rd, self.Handler())
         rd.close()
     finally:
         os.remove(fn)
Пример #5
0
def test_broken_timestamp(test_data):
    fn = test_data('n1 tx')
    try:
        rd = o.io.Reader(fn)
        with pytest.raises(RuntimeError):
            o.apply(rd, o.SimpleHandler())
    finally:
        rd.close()
Пример #6
0
 def test_broken_timestamp(self):
     fn = create_osm_file([osmobj('N', id=1, timestamp='x')])
     try:
         rd = o.io.Reader(fn)
         with assert_raises(ValueError):
             o.apply(rd, o.SimpleHandler())
             rd.close()
     finally:
         os.remove(fn)
Пример #7
0
 def test_broken_timestamp(self):
     fn = create_osm_file([osmobj('N', id=1, timestamp='x')])
     try:
         rd = o.io.Reader(fn)
         with assert_raises(ValueError):
             o.apply(rd, o.SimpleHandler())
             rd.close()
     finally:
         os.remove(fn)
Пример #8
0
  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
Пример #9
0
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')
Пример #10
0
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()
Пример #11
0
import osmium as o
import sys

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.NODE)

idxfile = open(sys.argv[2], 'a+b')

idx = o.index.DenseLocationMapFile(idxfile.fileno())
lh = o.NodeLocationsForWays(idx)

o.apply(reader, lh)

reader.close()
Пример #12
0
import osmium as o
import sys

class WayHandler(o.SimpleHandler):

    def __init__(self, idx):
        o.SimpleHandler.__init__(self)
        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)

idxfile = open(sys.argv[2], 'a+b')

idx = o.index.DenseLocationMapFile(idxfile.fileno())
lh = o.NodeLocationsForWays(idx)

o.apply(reader, lh, WayHandler(idx))

reader.close()
Пример #13
0
def _run_file(fn):
    rd = o.io.Reader(fn)
    try:
        o.apply(rd, o.SimpleHandler())
    finally:
        rd.close()
Пример #14
0
    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()
Пример #15
0
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
Пример #16
0
        self.writer.add_relation(relation)


if (__name__ == "__main__"):

    input_file = 'data/greater-london-input.osm.pbf'
    output_file = 'data/greater-london-output.osm.pbf'

    writer = osmium.SimpleWriter(output_file)
    idx = osmium.index.create_map(
        'sparse_file_array,data/node-cache.nodecache')
    handler = Handler(idx, writer)

    locations = osmium.NodeLocationsForWays(idx)
    locations.ignore_errors()

    nodes = osmium.io.Reader(input_file, osmium.osm.osm_entity_bits.NODE)
    osmium.apply(nodes, locations, handler)
    nodes.close()

    ways = osmium.io.Reader(input_file, osmium.osm.osm_entity_bits.WAY)
    osmium.apply(ways, handler)
    ways.close()

    relations = osmium.io.Reader(input_file,
                                 osmium.osm.osm_entity_bits.RELATION)
    osmium.apply(relations, handler)
    relations.close()

    writer.close()