forked from socrse/rse-groups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroups.py
104 lines (84 loc) · 3.32 KB
/
groups.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
97
98
99
100
101
102
103
104
# SPDX-FileCopyrightText: © 2022 Matt Williams <[email protected]>
# SPDX-License-Identifier: MIT
__version__ = '0.1.0'
from difflib import get_close_matches
import json
from pathlib import Path
import xml.etree.ElementTree as ET
from typing import List
import tomli
def generate_geojson():
data_path = Path("groups.toml")
geojson_path = Path("groups.json")
with open(data_path, "rb") as f:
all_groups = tomli.load(f)
geo_groups = []
valid_keys = {"name", "head", "phone", "email", "postcode", "website", "twitter", "lat", "lon"}
for group_id, group in all_groups.items():
if "name" not in group:
raise ValueError(f"The RSE group '{group_id}' is missing the `name` key.")
if not group.keys() <= valid_keys:
invalid_keys = group.keys() - valid_keys
suggestions = ((key, list_to_english(get_close_matches(key, valid_keys))) for key in invalid_keys)
suggestion_texts = [f"Found '{k}', did you mean {s}" if s else f"Found '{k}'" for k, s in suggestions]
suggestion_text = '\n '.join(suggestion_texts)
raise ValueError(f"The RSE group '{group_id}' has invalid keys:\n {suggestion_text}")
geo_groups.append({
"type": "Feature",
"properties": {k:v for k, v in group.items() if k not in ["lat", "lon"]},
"geometry": {
"type": "Point",
"coordinates": [group["lon"], group["lat"]]
}
})
with geojson_path.open("w") as out:
json.dump(geo_groups, out, indent=" ")
def list_to_english(words: List[str]) -> str:
"""
Examples:
>>> list_to_english(["a", "b", "c"])
"'a', 'b' or 'c'"
>>> list_to_english(["a"])
"'a'"
"""
if not words:
return ""
*head, tail = words
tail = f"'{tail}'"
if head:
head = ", ".join(f"'{w}'" for w in head)
return f"{head} or {tail}"
else:
return f"{tail}"
def convert_kml_to_toml():
"""
This is the code that was used to converty from the old KML file to the TOML file
"""
kml_path = Path("doc.kml")
tree = ET.parse(kml_path)
ns = {"kml": "http://www.opengis.net/kml/2.2"}
output_path = Path("groups.toml")
extra_mapping = {
"Head of RSE": "head",
"Contact number": "phone",
"Contact email": "email",
"Location": "postcode",
"Website": "website",
"Twitter Handle": "twitter",
}
with open(output_path, "w") as out:
for place in tree.findall(".//{http://www.opengis.net/kml/2.2}Placemark", ns):
name = place.find("kml:name", ns).text
group_id = name.lower().replace(" ", "-").replace(",", "-")
out.write(f"[{group_id}]\n")
extra = place.find("kml:ExtendedData", ns)
new_extra = {"name": name}
for data in extra.findall("kml:Data", ns):
key = data.attrib["name"]
value = data.find("kml:value", ns).text
if value:
new_extra[extra_mapping[key]] = value.strip()
new_extra = {k: new_extra[k] for k in ["name", "head", "website", "email", "phone", "twitter", "postcode"] if k in new_extra}
for k, v in new_extra.items():
out.write(f'{k} = "{v}"\n')
out.write("\n")