-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwaypoints.py
96 lines (85 loc) · 3.3 KB
/
waypoints.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
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
import datetime
from zoneinfo import ZoneInfo
import requests
import json
import re
import os
import zipfile
import io
import glob
import shutil
import geopandas
from tempfile import TemporaryDirectory
import logging
logging.basicConfig(level=logging.INFO)
def store_version(key: str, version: str):
logging.info(f"{key} version: {version}")
# "0" is prepended in filename so that this file appears first in Github directory listing
try:
with open('waypoints/0versions.json', 'r') as f:
version_dict = json.load(f)
except BaseException:
version_dict = {}
version_dict[key] = version
version_dict = dict(sorted(version_dict.items()))
with open('waypoints/0versions.json', 'w', encoding='UTF-8') as f:
json.dump(version_dict, f, indent=4)
os.makedirs("waypoints", exist_ok=True)
for csdi_dataset in [
# 巴士路線
# https://portal.csdi.gov.hk/geoportal/?lang=zh-hk&datasetId=td_rcd_1638844988873_41214
{"name": "bus", "id": "td_rcd_1638844988873_41214"},
# 專線小巴路線
# https://portal.csdi.gov.hk/geoportal/?lang=zh-hk&datasetId=td_rcd_1697082463580_57453
{"name": "gmb", "id": "td_rcd_1697082463580_57453"}
]:
logging.info("csdi_dataset=" + json.dumps(csdi_dataset))
logging.info("Fetching metadata")
r = requests.get(
"https://portal.csdi.gov.hk/geoportal/rest/metadata/item/" +
csdi_dataset["id"])
src_id = json.loads(r.content)['_source']['fileid'].replace('-', '')
logging.info("Fetching FGDB")
r = requests.get(
"https://static.csdi.gov.hk/csdi-webpage/download/" + src_id + "/fgdb")
z = zipfile.ZipFile(io.BytesIO(r.content))
version = min([f.date_time for f in z.infolist()])
version = datetime.datetime(
*version, tzinfo=ZoneInfo("Asia/Hong_Kong"))
store_version(csdi_dataset["name"], version.isoformat())
gdb_name = next(s[0:s.index('/')]
for s in z.namelist() if s != "__MACOSX")
with TemporaryDirectory() as tmpdir:
logging.info("Extracting data")
z.extractall(tmpdir)
gdb_path = os.path.join(tmpdir, gdb_name)
logging.info("Reading data (1)")
gdf = geopandas.read_file(gdb_path, encoding='utf-8')
logging.info("Transforming data")
gdf.to_crs(epsg=4326, inplace=True)
logging.info("Reading data (2)")
data = gdf.to_geo_dict(drop_id=True)
logging.info("Storing data")
for feature in data["features"]:
properties = feature["properties"]
with open("waypoints/" + str(properties["ROUTE_ID"]) + "-" + ("O" if properties["ROUTE_SEQ"] == 1 else "I") + ".json", "w", encoding='utf-8') as f:
f.write(
re.sub(
r"([0-9]+\.[0-9]{5})[0-9]+",
r"\1",
json.dumps({
"features": [feature],
"type": "FeatureCollection"
},
ensure_ascii=False,
separators=(",", ":")
)
)
)
logging.info("Copying static data")
for file in glob.glob(r'./mtr/*.json'):
shutil.copy(file, "waypoints")
for file in glob.glob(r'./lrt/*.json'):
shutil.copy(file, "waypoints")
for file in glob.glob(r'./ferry/*.json'):
shutil.copy(file, "waypoints")