Example #1
0
    def loadOsm(
            self,
            osm_file: Union[str, bytes, int, IO[bytes]],
            osm_file_format: Literal['xml', 'gz', 'bz2',
                                     'pbf'] = "xml") -> None:
        """Parses provided osm file and saves routing data."""
        encounteredNodes: Dict[int, Position] = {}
        usedWays: Dict[int, List[int]] = {}

        for feature in iter_from_osm(osm_file, osm_file_format, set()):
            if feature["type"] == "node":
                # N O D E
                # Save position, if it's used by some way, it'll be saved to the Datastore
                encounteredNodes[
                    feature["id"]] = feature["lat"], feature["lon"]

            elif feature["type"] == "way":
                # W A Y
                # Pass it to self.store_way; if it was stored:
                # store un-saved node positions
                # and cache way["nd"] for relation parsing
                stored = self.storeWay(feature, encounteredNodes)
                if stored:
                    usedWays[feature["id"]] = feature["nd"]

                    for nd in filter(lambda i: i not in self.rnodes,
                                     feature["nd"]):
                        self.rnodes[nd] = encounteredNodes[nd]

            elif feature["type"] == "relation":
                # R E L A T I O N
                # Check if it's a turn restriction and process it
                # Ignore non-restrictions
                if feature["tag"].get("type") not in {
                        "restriction", "restriction:" + self.transport
                }:
                    continue

                # Store this restriction
                try:
                    self.storeRestriction(feature, usedWays)
                except (OsmReferenceError, OsmInvalidRestriction):
                    if self.ignoreDataErrs:
                        continue
                    else:
                        raise

            else:
                raise OsmStructureError(
                    f"Unexpected feature type: {feature['type']!r} "
                    f"on feature with id {feature['id']!r}")
Example #2
0
    def parseOsmFile(self, file, filetype):
        """Return nodes, ways and realations of given file
           Only highway=* and railway=* ways are returned, and
           only type=restriction (and type=restriction:<transport type>) are returned"""
        nodes = self.storage_class()
        ways = self.storage_class()
        relations = self.storage_class()

        for elem in osmiter.iter_from_osm(file, filetype):

            if elem["type"] == "node":
                nodes[elem["id"]] = elem

            # Store only potentially routable ways
            elif elem["type"] == "way" and (elem["tag"].get("highway")
                                            or elem["tag"].get("railway")):
                ways[elem["id"]] = elem

            # Store only potential turn restrictions
            elif elem["type"] == "relation" and elem["tag"].get(
                    "type", "").startswith("restriction"):
                relations[elem["id"]] = elem

        return nodes, ways, relations
Example #3
0
import osmiter
import json
import datetime

shop_count = 0
ways = {}


def myconverter(o):
    if isinstance(o, datetime.datetime):
        return o.__str__()


for feature in osmiter.iter_from_osm("andorra-latest.osm.pbf"):
    if feature["type"] == "way":
        print(feature["id"], json.dumps(feature, default=myconverter))
        ways.update({feature.get("id"): feature})

    if feature["type"] == "node" and "shop" in feature["tag"]:
        shop_count += 1

print(f"this osm file containes {shop_count} shop nodes")

print(ways.get("945815975"))
with open('data-ways.json', 'w') as outfile:
    json.dump(ways, outfile, default=myconverter)
Example #4
0
from geojson import Point, Feature, FeatureCollection, dumps
import osmiter

way_id = 154870255
osm_file = "./bj_11.osm"

features = osmiter.iter_from_osm(osm_file)
lst_node = []
for feature in features:
    if feature["type"] == "way" and feature["id"] == way_id:
        lst_node = feature["nd"]
        break

if len(lst_node):
    features = osmiter.iter_from_osm(osm_file)
    with open('./out.geojson', 'w') as w:
        lst_feature = []
        for feature in features:
            if feature["type"] == "node" and feature["id"] in lst_node:
                lst_feature.append(
                    Feature(geometry=Point(
                        tuple([feature['lon'], feature['lat']])),
                            properties={
                                'id': feature['id'],
                                'name': '西单北大街'
                            }))
        feature_collection = FeatureCollection(lst_feature)
        w.write('{}'.format(dumps(feature_collection, indent=4)))
Example #5
0
def test_pbf_str_source():
    source = "tests/example.osm.pbf"
    actually_verify(osmiter.iter_from_osm(source), is_pbf=True)
Example #6
0
def test_bz2_buffer_source():
    with open("tests/example.osm.bz2", mode="rb") as source:
        actually_verify(osmiter.iter_from_osm(source, file_format="bz2"))
Example #7
0
def test_gzip_bytes_source():
    source = os.fsencode("tests/example.osm.gz")
    actually_verify(osmiter.iter_from_osm(source))
Example #8
0
def test_xml_str_source():
    source = "tests/example.osm"
    actually_verify(osmiter.iter_from_osm(source))