SPB Git

Repository Public BSD-3-Clause

world-labels.py 5.9 KB 125 lines python

1#!/usr/bin/env python32# =============================================================================3#                  SP BOUCHER | SOFTWARE & RESEARCH4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project          : ORIX Maps10# File             : Infrastructure/tile-builder/world-labels.py11# Description      : Builds the world label set bundled with the application12#                    from Natural Earth (public domain): countries, first-level13#                    regions and populated places, with English and French14#                    names.15# Module           : Infrastructure16# Repository       : https://git.spboucher.ai/orix-maps17# Created          : 2026-11-2518# Last Modified    : 2026-11-2519#20# =============================================================================21# AUTHOR & PROFESSIONAL AFFILIATION22# =============================================================================23#24# Author           : Simon-Pierre Boucher25# Position         : Professor of Finance26# Institution      : Université du Québec en Outaouais (UQO)27# Department       : Département des sciences administratives28# Organization     : SP Boucher29#30# Website          : https://spboucher.ai31# Git              : https://git.spboucher.ai32#33# =============================================================================34# OFFICIAL CONTACT DIRECTORY35# =============================================================================36#37# General          : info@spboucher.ai38# Professional     : contact@spboucher.ai39# Direct           : simon@spboucher.ai40# Research         : research@spboucher.ai41# Development      : dev@spboucher.ai42# Support          : support@spboucher.ai43# Security/Admin   : admin@spboucher.ai44# Notifications    : notifications@spboucher.ai45#46# Preferred Contact: support@spboucher.ai47#48# =============================================================================49# COPYRIGHT & INTELLECTUAL PROPERTY50# =============================================================================51#52# Copyright (c) 202653# Simon-Pierre Boucher54#55# All rights reserved where applicable.56#57# This file is governed by the applicable project license,58# copyright notices, institutional agreements and relevant law.59#60# Unless expressly authorized by the applicable license or61# rights holder, copying, redistribution, sublicensing,62# publication and commercial exploitation are not permitted.63#64# Existing third-party rights and notices remain unaffected.65#66# =============================================================================67# CONFIDENTIALITY & SECURITY68# =============================================================================69#70# Classification   : Public71# Distribution     : Public repository; use is governed by the project license72# Security Contact : admin@spboucher.ai73#74# Access to this source does not independently grant permission75# to redistribute or disclose confidential materials.76#77# Do not embed credentials, private keys, access tokens,78# certificates or confidential personal information.79#80# =============================================================================81# LICENSE & MAINTENANCE82# =============================================================================83#84# License          : Proprietary, all rights reserved (see license file)85# License File     : LICENSE86# Maintainer       : Simon-Pierre Boucher87# Technical Contact: dev@spboucher.ai88#89# =============================================================================90# END OF SP BOUCHER SOURCE HEADER91# =============================================================================9293"""Builds the world label set bundled with the application from Natural Earth (public domain):94countries, first-level regions and populated places, with English and French names.95usage: world-labels.py <natural_earth_vector.gpkg> <out.geojson>96Output: GeoJSON FeatureCollection of points with properties kind (country|region|locality), name, name:en, name:fr,97min_zoom, rank (smaller = more important), capital (localities only)."""98import json, sqlite3, sys99100db = sqlite3.connect(sys.argv[1])101feats = []102103def add(lon, lat, kind, name, en, fr, min_zoom, rank, extra=None):104    if lon is None or lat is None or not name: return105    p = {"kind": kind, "name": name, "min_zoom": round(float(min_zoom), 1), "rank": int(rank)}106    if en and en != name: p["name:en"] = en107    if fr and fr != name: p["name:fr"] = fr108    if extra: p.update(extra)109    feats.append({"type": "Feature", "geometry": {"type": "Point", "coordinates": [round(lon, 4), round(lat, 4)]}, "properties": p})110111for r in db.execute("select LABEL_X, LABEL_Y, NAME, NAME_EN, NAME_FR, MIN_LABEL, LABELRANK from ne_10m_admin_0_countries"):112    add(r[0], r[1], "country", r[2], r[3], r[4], r[5] if r[5] is not None else 3, r[6] or 9)113# Regions: only those Natural Earth itself labels by zoom 6.5 (provinces and states of large countries).114for r in db.execute("select longitude, latitude, name, name_en, name_fr, min_label, labelrank from ne_10m_admin_1_states_provinces where min_label is not null and min_label <= 6.5 and name is not null"):115    add(r[0], r[1], "region", r[2], r[3], r[4], r[5], r[6] or 9)116for r in db.execute("select LONGITUDE, LATITUDE, NAME, NAME_EN, NAME_FR, MIN_ZOOM, SCALERANK, ADM0CAP, POP_MAX from ne_10m_populated_places"):117    add(r[0], r[1], "locality", r[2], r[3], r[4], r[5] if r[5] is not None else 8, r[6] if r[6] is not None else 10,118        {"capital": bool(r[7]), "population": int(r[8] or 0)})119120feats.sort(key=lambda f: (f["properties"]["kind"], f["properties"]["rank"], f["properties"]["name"]))121json.dump({"type": "FeatureCollection", "features": feats}, open(sys.argv[2], "w"), ensure_ascii=False, separators=(",", ":"))122kinds = {}123for f in feats: kinds[f["properties"]["kind"]] = kinds.get(f["properties"]["kind"], 0) + 1124print(kinds)125
Files main
Menu

Theme