File size: 9,590 Bytes
124ba77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# Copyright (c) Meta Platforms, Inc. and affiliates.

import json
import re
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional

from lxml import etree
import numpy as np

from utils.geo import BoundaryBox, Projection

METERS_PATTERN: re.Pattern = re.compile("^(?P<value>\\d*\\.?\\d*)\\s*m$")
KILOMETERS_PATTERN: re.Pattern = re.compile("^(?P<value>\\d*\\.?\\d*)\\s*km$")
MILES_PATTERN: re.Pattern = re.compile("^(?P<value>\\d*\\.?\\d*)\\s*mi$")


def parse_float(string: str) -> Optional[float]:
    """Parse string representation of a float or integer value."""
    try:
        return float(string)
    except (TypeError, ValueError):
        return None


@dataclass(eq=False)
class OSMElement:
    """
    Something with tags (string to string mapping).
    """

    id_: int
    tags: Dict[str, str]

    def get_float(self, key: str) -> Optional[float]:
        """Parse float from tag value."""
        if key in self.tags:
            return parse_float(self.tags[key])
        return None

    def get_length(self, key: str) -> Optional[float]:
        """Get length in meters."""
        if key not in self.tags:
            return None

        value: str = self.tags[key]

        float_value: float = parse_float(value)
        if float_value is not None:
            return float_value

        for pattern, ratio in [
            (METERS_PATTERN, 1.0),
            (KILOMETERS_PATTERN, 1000.0),
            (MILES_PATTERN, 1609.344),
        ]:
            matcher: re.Match = pattern.match(value)
            if matcher:
                float_value: float = parse_float(matcher.group("value"))
                if float_value is not None:
                    return float_value * ratio

        return None

    def __hash__(self) -> int:
        return self.id_


@dataclass(eq=False)
class OSMNode(OSMElement):
    """
    OpenStreetMap node.

    See https://wiki.openstreetmap.org/wiki/Node
    """

    geo: np.ndarray
    visible: Optional[str] = None
    xy: Optional[np.ndarray] = None

    @classmethod
    def from_dict(cls, structure: Dict[str, Any]) -> "OSMNode":
        """
        Parse node from Overpass-like structure.

        :param structure: input structure
        """
        return cls(
            structure["id"],
            structure.get("tags", {}),
            geo=np.array((structure["lat"], structure["lon"])),
            visible=structure.get("visible"),
        )


@dataclass(eq=False)
class OSMWay(OSMElement):
    """
    OpenStreetMap way.

    See https://wiki.openstreetmap.org/wiki/Way
    """

    nodes: Optional[List[OSMNode]] = field(default_factory=list)
    visible: Optional[str] = None

    @classmethod
    def from_dict(
        cls, structure: Dict[str, Any], nodes: Dict[int, OSMNode]
    ) -> "OSMWay":
        """
        Parse way from Overpass-like structure.

        :param structure: input structure
        :param nodes: node structure
        """
        return cls(
            structure["id"],
            structure.get("tags", {}),
            [nodes[x] for x in structure["nodes"]],
            visible=structure.get("visible"),
        )

    def is_cycle(self) -> bool:
        """Is way a cycle way or an area boundary."""
        return self.nodes[0] == self.nodes[-1]

    def __repr__(self) -> str:
        return f"Way <{self.id_}> {self.nodes}"


@dataclass
class OSMMember:
    """
    Member of OpenStreetMap relation.
    """

    type_: str
    ref: int
    role: str


@dataclass(eq=False)
class OSMRelation(OSMElement):
    """
    OpenStreetMap relation.

    See https://wiki.openstreetmap.org/wiki/Relation
    """

    members: Optional[List[OSMMember]]
    visible: Optional[str] = None

    @classmethod
    def from_dict(cls, structure: Dict[str, Any]) -> "OSMRelation":
        """
        Parse relation from Overpass-like structure.

        :param structure: input structure
        """
        return cls(
            structure["id"],
            structure["tags"],
            [OSMMember(x["type"], x["ref"], x["role"]) for x in structure["members"]],
            visible=structure.get("visible"),
        )


class OSMData:
    """
    The whole OpenStreetMap information about nodes, ways, and relations.
    """

    def __init__(self) -> None:
        self.nodes: Dict[int, OSMNode] = {}
        self.ways: Dict[int, OSMWay] = {}
        self.relations: Dict[int, OSMRelation] = {}
        self.box: BoundaryBox = None

    @classmethod
    def from_dict(cls, structure: Dict[str, Any]):
        data = cls()
        bounds = structure.get("bounds")
        if bounds is not None:
            data.box = BoundaryBox(
                np.array([bounds["minlat"], bounds["minlon"]]),
                np.array([bounds["maxlat"], bounds["maxlon"]]),
            )

        for element in structure["elements"]:
            if element["type"] == "node":
                node = OSMNode.from_dict(element)
                data.add_node(node)
        for element in structure["elements"]:
            if element["type"] == "way":
                way = OSMWay.from_dict(element, data.nodes)
                data.add_way(way)
        for element in structure["elements"]:
            if element["type"] == "relation":
                relation = OSMRelation.from_dict(element)
                data.add_relation(relation)

        return data

    @classmethod
    def from_json(cls, path: Path):
        with path.open(encoding='utf-8') as fid:
            structure = json.load(fid)
        return cls.from_dict(structure)

    @classmethod
    def from_xml(cls, path: Path):
        root = etree.parse(str(path)).getroot()
        structure = {"elements": []}
        from tqdm import tqdm

        for elem in tqdm(root):
            if elem.tag == "bounds":
                structure["bounds"] = {
                    k: float(elem.attrib[k])
                    for k in ("minlon", "minlat", "maxlon", "maxlat")
                }
            elif elem.tag in {"node", "way", "relation"}:
                if elem.tag == "node":
                    item = {
                        "id": int(elem.attrib["id"]),
                        "lat": float(elem.attrib["lat"]),
                        "lon": float(elem.attrib["lon"]),
                        "visible": elem.attrib.get("visible"),
                        "tags": {
                            x.attrib["k"]: x.attrib["v"] for x in elem if x.tag == "tag"
                        },
                    }
                elif elem.tag == "way":
                    item = {
                        "id": int(elem.attrib["id"]),
                        "visible": elem.attrib.get("visible"),
                        "tags": {
                            x.attrib["k"]: x.attrib["v"] for x in elem if x.tag == "tag"
                        },
                        "nodes": [int(x.attrib["ref"]) for x in elem if x.tag == "nd"],
                    }
                elif elem.tag == "relation":
                    item = {
                        "id": int(elem.attrib["id"]),
                        "visible": elem.attrib.get("visible"),
                        "tags": {
                            x.attrib["k"]: x.attrib["v"] for x in elem if x.tag == "tag"
                        },
                        "members": [
                            {
                                "type": x.attrib["type"],
                                "ref": int(x.attrib["ref"]),
                                "role": x.attrib["role"],
                            }
                            for x in elem
                            if x.tag == "member"
                        ],
                    }
                item["type"] = elem.tag
                structure["elements"].append(item)
            elem.clear()
        del root
        return cls.from_dict(structure)

    @classmethod
    def from_file(cls, path: Path):
        ext = path.suffix
        if ext == ".json":
            return cls.from_json(path)
        elif ext in {".osm", ".xml"}:
            return cls.from_xml(path)
        else:
            raise ValueError(f"Unknown extension for {path}")

    def add_node(self, node: OSMNode):
        """Add node and update map parameters."""
        if node.id_ in self.nodes:
            raise ValueError(f"Node with duplicate id {node.id_}.")
        self.nodes[node.id_] = node

    def add_way(self, way: OSMWay):
        """Add way and update map parameters."""
        if way.id_ in self.ways:
            raise ValueError(f"Way with duplicate id {way.id_}.")
        self.ways[way.id_] = way

    def add_relation(self, relation: OSMRelation):
        """Add relation and update map parameters."""
        if relation.id_ in self.relations:
            raise ValueError(f"Relation with duplicate id {relation.id_}.")
        self.relations[relation.id_] = relation

    def add_xy_to_nodes(self, proj: Projection):
        nodes = list(self.nodes.values())
        if len(nodes) == 0:
            return
        geos = np.stack([n.geo for n in nodes], 0)
        if proj.bounds is not None:
            # For some reasons few nodes are sometimes very far off the initial bbox.
            valid = proj.bounds.contains(geos)
            if valid.mean() < 0.9:
                print("Many nodes are out of the projection bounds.")
            xys = np.zeros_like(geos)
            xys[valid] = proj.project(geos[valid])
        else:
            xys = proj.project(geos)
        for xy, node in zip(xys, nodes):
            node.xy = xy