Files
WebAisMap/tools/build_mmsi_mid_json.py
2026-05-04 08:06:34 +03:00

175 lines
7.5 KiB
Python

"""Build static/js/mmsi_mid_iso2.json from Wikipedia MID table (embedded). Run: python tools/build_mmsi_mid_json.py"""
import json
import re
# Fragment of https://en.wikipedia.org/wiki/Maritime_identification_digits (action=raw), table rows only
WIKI_TABLE = r"""
| [Russian Federation](https://en.wikipedia.org/wiki/Russia) | 273 |
| [Latvia](https://en.wikipedia.org/wiki/Latvia)(Republic of) | 275 |
| [Lithuania](https://en.wikipedia.org/wiki/Lithuania)(Republic of) | 277 |
| [Estonia](https://en.wikipedia.org/wiki/Estonia)(Republic of) | 276 |
| [Ukraine](https://en.wikipedia.org/wiki/Ukraine) | 272 |
| [Poland](https://en.wikipedia.org/wiki/Poland)(Republic of) | 261 |
| [Germany](https://en.wikipedia.org/wiki/Germany)(Federal Republic of) | 211; 218 |
| [France](https://en.wikipedia.org/wiki/France) | 226; 227; 228 |
| [Italy](https://en.wikipedia.org/wiki/Italy) | 247 |
| [Norway](https://en.wikipedia.org/wiki/Norway) | 257; 258; 259 |
| [Sweden](https://en.wikipedia.org/wiki/Sweden) | 265; 266 |
| [Finland](https://en.wikipedia.org/wiki/Finland) | 230 |
| [Denmark](https://en.wikipedia.org/wiki/Denmark) | 219; 220 |
| [Netherlands](https://en.wikipedia.org/wiki/Netherlands)(Kingdom of the) | 244; 245; 246 |
| [Belgium](https://en.wikipedia.org/wiki/Belgium) | 205 |
| [Spain](https://en.wikipedia.org/wiki/Spain) | 224; 225 |
| [Greece](https://en.wikipedia.org/wiki/Greece) | 237; 239; 240; 241 |
| [Turkey](https://en.wikipedia.org/wiki/Turkey) | 271 |
| [Romania](https://en.wikipedia.org/wiki/Romania) | 264 |
| [Bulgaria](https://en.wikipedia.org/wiki/Bulgaria)(Republic of) | 207 |
| [Croatia](https://en.wikipedia.org/wiki/Croatia)(Republic of) | 238 |
| [United Kingdom of Great Britain and Northern Ireland](https://en.wikipedia.org/wiki/United_Kingdom) | 232; 233; 234; 235 |
| [Ireland](https://en.wikipedia.org/wiki/Republic_of_Ireland) | 250 |
| [Iceland](https://en.wikipedia.org/wiki/Iceland) | 251 |
| [Malta](https://en.wikipedia.org/wiki/Malta) | 215; 229; 248; 249; 256 |
| [Cyprus](https://en.wikipedia.org/wiki/Cyprus)(Republic of) | 209; 210; 212 |
| [United States of America](https://en.wikipedia.org/wiki/United_States) | 338; 366; 367; 368; 369 |
| [Canada](https://en.wikipedia.org/wiki/Canada) | 316 |
| [China](https://en.wikipedia.org/wiki/China)(People's Republic of) | 412; 413; 414 |
| [Japan](https://en.wikipedia.org/wiki/Japan) | 431; 432 |
| [Korea](https://en.wikipedia.org/wiki/South_Korea)(Republic of) | 440; 441 |
| [Singapore](https://en.wikipedia.org/wiki/Singapore)(Republic of) | 563; 564; 565; 566 |
| [India](https://en.wikipedia.org/wiki/India)(Republic of) | 419 |
| [Australia](https://en.wikipedia.org/wiki/Australia) | 503 |
| [Brazil](https://en.wikipedia.org/wiki/Brazil)(Federative Republic of) | 710 |
| [Argentina](https://en.wikipedia.org/wiki/Argentina) | 701 |
| [Panama](https://en.wikipedia.org/wiki/Panama)(Republic of) | 351; 352; 353; 354; 355; 356; 357; 370; 371; 372; 373; 374 |
| [Liberia](https://en.wikipedia.org/wiki/Liberia)(Republic of) | 636; 637 |
| [Marshall Islands](https://en.wikipedia.org/wiki/Marshall_Islands)(Republic of the) | 538 |
| [Bahamas](https://en.wikipedia.org/wiki/The_Bahamas)(Commonwealth of the) | 308; 309; 311 |
| [Hong Kong](https://en.wikipedia.org/wiki/Hong_Kong)(Special Administrative Region of China) | 477 |
| [Taiwan](https://en.wikipedia.org/wiki/Taiwan)(Republic of China) | 416 |
| [Vietnam](https://en.wikipedia.org/wiki/Vietnam)(Socialist Republic of) | 574 |
| [Indonesia](https://en.wikipedia.org/wiki/Indonesia)(Republic of) | 525 |
| [Malaysia](https://en.wikipedia.org/wiki/Malaysia) | 533 |
| [Philippines](https://en.wikipedia.org/wiki/Philippines)(Republic of the) | 548 |
| [Thailand](https://en.wikipedia.org/wiki/Thailand) | 567 |
| [Saudi Arabia](https://en.wikipedia.org/wiki/Saudi_Arabia)(Kingdom of) | 403 |
| [United Arab Emirates](https://en.wikipedia.org/wiki/United_Arab_Emirates) | 470; 471 |
| [Israel](https://en.wikipedia.org/wiki/Israel)(State of) | 428 |
| [Egypt](https://en.wikipedia.org/wiki/Egypt)(Arab Republic of) | 622 |
| [South Africa](https://en.wikipedia.org/wiki/South_Africa)(Republic of) | 601 |
| [New Zealand](https://en.wikipedia.org/wiki/New_Zealand) | 512 |
| [Mexico](https://en.wikipedia.org/wiki/Mexico) | 345 |
| [Chile](https://en.wikipedia.org/wiki/Chile) | 725 |
| [Portugal](https://en.wikipedia.org/wiki/Portugal) | 263 |
| [Switzerland](https://en.wikipedia.org/wiki/Switzerland)(Confederation of) | 269 |
| [Austria](https://en.wikipedia.org/wiki/Austria) | 203 |
| [Czech Republic](https://en.wikipedia.org/wiki/Czech_Republic) | 270 |
| [Hungary](https://en.wikipedia.org/wiki/Hungary)(Republic of) | 243 |
| [Serbia](https://en.wikipedia.org/wiki/Serbia) | 279 |
| [Slovenia](https://en.wikipedia.org/wiki/Slovenia)(Republic of) | 278 |
| [Slovakia](https://en.wikipedia.org/wiki/Slovakia) | 267 |
| [Montenegro](https://en.wikipedia.org/wiki/Montenegro)(Republic of) | 262 |
| [Albania](https://en.wikipedia.org/wiki/Albania)(Republic of) | 201 |
| [Georgia](https://en.wikipedia.org/wiki/Georgia_(country)) | 213 |
| [Kazakhstan](https://en.wikipedia.org/wiki/Kazakhstan)(Republic of) | 436 |
| [Iran](https://en.wikipedia.org/wiki/Iran)(Islamic Republic of) | 422 |
"""
NAME_TO_ISO2 = {
"Russian Federation": "RU",
"Latvia": "LV",
"Lithuania": "LT",
"Estonia": "EE",
"Ukraine": "UA",
"Poland": "PL",
"Germany": "DE",
"France": "FR",
"Italy": "IT",
"Norway": "NO",
"Sweden": "SE",
"Finland": "FI",
"Denmark": "DK",
"Netherlands": "NL",
"Belgium": "BE",
"Spain": "ES",
"Greece": "GR",
"Turkey": "TR",
"Romania": "RO",
"Bulgaria": "BG",
"Croatia": "HR",
"United Kingdom of Great Britain and Northern Ireland": "GB",
"Ireland": "IE",
"Iceland": "IS",
"Malta": "MT",
"Cyprus": "CY",
"United States of America": "US",
"Canada": "CA",
"China": "CN",
"Japan": "JP",
"Korea": "KR",
"Singapore": "SG",
"India": "IN",
"Australia": "AU",
"Brazil": "BR",
"Argentina": "AR",
"Panama": "PA",
"Liberia": "LR",
"Marshall Islands": "MH",
"Bahamas": "BS",
"Hong Kong": "HK",
"Taiwan": "TW",
"Vietnam": "VN",
"Indonesia": "ID",
"Malaysia": "MY",
"Philippines": "PH",
"Thailand": "TH",
"Saudi Arabia": "SA",
"United Arab Emirates": "AE",
"Israel": "IL",
"Egypt": "EG",
"South Africa": "ZA",
"New Zealand": "NZ",
"Mexico": "MX",
"Chile": "CL",
"Portugal": "PT",
"Switzerland": "CH",
"Austria": "AT",
"Czech Republic": "CZ",
"Hungary": "HU",
"Serbia": "RS",
"Slovenia": "SI",
"Slovakia": "SK",
"Montenegro": "ME",
"Albania": "AL",
"Georgia": "GE",
"Kazakhstan": "KZ",
"Iran": "IR",
}
def main():
pat = re.compile(r"\| \[([^\]]+)\]\([^\)]+\)[^\|]*\| ([0-9][^|\n]*) \|")
mid_to_iso = {}
for m in pat.finditer(WIKI_TABLE):
name = m.group(1).strip()
codes_cell = m.group(2)
codes = [
x.strip()
for x in codes_cell.replace(";", " ").split()
if x.strip().isdigit() and len(x.strip()) == 3
]
iso = NAME_TO_ISO2.get(name)
if not iso:
raise SystemExit(f"Missing ISO2 for country: {name!r}")
for mid in codes:
mid_to_iso[mid] = iso
from pathlib import Path
root = Path(__file__).resolve().parent.parent
out = root / "static" / "js" / "mmsi_mid_iso2.json"
with open(out, "w", encoding="utf-8") as f:
json.dump(mid_to_iso, f, separators=(",", ":"))
print("Wrote", out, len(mid_to_iso), "MIDs")
if __name__ == "__main__":
main()