-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathosm_mmap.py
More file actions
28 lines (21 loc) · 808 Bytes
/
osm_mmap.py
File metadata and controls
28 lines (21 loc) · 808 Bytes
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
from mmap import ACCESS_READ, mmap
from pathlib import Path
from typing import Iterator, Self
class OpenStreetMap:
def __init__(self, path: Path) -> None:
self.file = path.open(mode="rb")
self.stream = mmap(self.file.fileno(), 0, access=ACCESS_READ)
def __enter__(self) -> Self:
return self
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
self.stream.close()
self.file.close()
def __iter__(self) -> Iterator[bytes]:
end = 0
while (begin := self.stream.find(b"<way", end)) != -1:
end = self.stream.find(b"</way>", begin)
yield self.stream[begin : end + len(b"</way>")]
if __name__ == "__main__":
with OpenStreetMap(Path("map.osm")) as osm:
for way_tag in osm:
print(way_tag)