-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosm_countries.py
38 lines (30 loc) · 1.03 KB
/
osm_countries.py
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
import json
from collections.abc import Sequence
from typing import cast
from sentry_sdk import trace
from shapely import MultiPolygon, Point, Polygon
from shapely.geometry import shape
from zstandard import ZstdDecompressor
from config import COUNTRY_GEOJSON_URL
from models.osm_country import OSMCountry
from utils import HTTP
_zstd_decompress = ZstdDecompressor().decompress
@trace
async def get_osm_countries() -> Sequence[OSMCountry]:
r = await HTTP.get(COUNTRY_GEOJSON_URL)
r.raise_for_status()
buffer = _zstd_decompress(r.content)
data: dict = json.loads(buffer)
result = []
for feature in data['features']:
props = feature['properties']
geometry = feature['geometry']
result.append(
OSMCountry(
tags=props['tags'],
geometry=cast(Polygon | MultiPolygon, shape(geometry)),
representative_point=cast(Point, shape(props['representative_point'])),
timestamp=props['timestamp'],
)
)
return result