Skip to content

Commit

Permalink
Convert lat/lng in EPSG:4326
Browse files Browse the repository at this point in the history
  • Loading branch information
chunlaw committed Nov 29, 2023
1 parent 66f59b9 commit 723997f
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions waypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,41 @@
import os
import zipfile
import io
from pyproj import Transformer

epsgTransformer = Transformer.from_crs('epsg:2326', 'epsg:4326')

r = requests.get("https://static.csdi.gov.hk/csdi-webpage/download/common/51bbe0d88d421c1e94572e503ad0428fabe11e3300c40e221146550044e54de5")
z = zipfile.ZipFile(io.BytesIO(r.content))
with z.open("FB_ROUTE_LINE.json") as f:
data = json.loads(re.sub(r"([0-9]+\.[0-9]{6})[0-9]+", r"\1", f.read().decode("utf-8")).replace("\n", ""))
data = json.loads(f.read().decode("utf-8"))

for i, feature in enumerate(data["features"]):
if feature["geometry"]["type"] == "MultiLineString":
for j, coordinates in enumerate(feature["geometry"]["coordinates"]):
for k, coordinate in enumerate(coordinates):
lat, lng = epsgTransformer.transform(coordinate[1], coordinate[0])
data["features"][i]["geometry"]["coordinates"][j][k] = [lng, lat]
else:
for j, coordinate in enumerate(feature["geometry"]["coordinates"]):
lat, lng = epsgTransformer.transform(coordinate[1], coordinate[0])
data["features"][i]["geometry"]["coordinates"][j] = [lng, lat]

os.makedirs("waypoints", exist_ok=True)

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") as f:
f.write(json.dumps({
"features": [feature],
"type": "FeatureCollection"
}, ensure_ascii=False))
f.write(
re.sub(
r"([0-9]+\.[0-9]{5})[0-9]+",
r"\1",
json.dumps({
"features": [feature],
"type": "FeatureCollection"
},
ensure_ascii=False,
separators=(",", ":")
)
)
)

0 comments on commit 723997f

Please sign in to comment.