#!/usr/bin/env python3 """Render a Leaflet HTML map combining US data centers, submarine cables, and city-level network-dominance points from PostGIS. """ import argparse import json import os from pathlib import Path import psycopg2 DB_NAME = "data_centers" DC_TABLE = "public.master_data_centers" CABLES_TABLE = "public.internet_cables" CITY_TABLE = "public.internet_city_dominance" def connect(): return psycopg2.connect( host=os.environ["PGWEB_HOST"], port=os.environ["PGWEB_PORT"], user=os.environ["PGWEB_USER"], password=os.environ["PGWEB_PASSWORD"], dbname=DB_NAME, ) def load_data_centers(conn): with conn.cursor() as cur: cur.execute( f""" select master_id, source, coalesce(operator, ''), coalesce(name, ''), coalesce(city, ''), coalesce(state, ''), longitude, latitude from {DC_TABLE} where longitude is not null and latitude is not null """ ) return [ { "id": r[0], "source": r[1], "operator": r[2], "name": r[3], "city": r[4], "state": r[5], "lon": float(r[6]), "lat": float(r[7]), } for r in cur.fetchall() ] def load_cables(conn): with conn.cursor() as cur: cur.execute( f""" select feature_id, coalesce(cable_id, ''), coalesce(name, ''), coalesce(color, '#888888'), coalesce(owners, ''), rfs_year, decommission_year, length_km, coalesce(url, ''), ST_AsGeoJSON(geom) from {CABLES_TABLE} where geom is not null """ ) features = [] for r in cur.fetchall(): features.append( { "type": "Feature", "geometry": json.loads(r[9]), "properties": { "feature_id": r[0], "cable_id": r[1], "name": r[2], "color": r[3], "owners": r[4], "rfs_year": r[5], "decommission_year": r[6], "length_km": float(r[7]) if r[7] is not None else None, "url": r[8], }, } ) return {"type": "FeatureCollection", "features": features} def load_cities(conn, us_only=False): where = "where geom is not null" if us_only: where += " and country = 'US'" with conn.cursor() as cur: cur.execute( f""" select id, coalesce(city, ''), coalesce(country, ''), coalesce(country_name, ''), coalesce(region, ''), physical_capacity_tbps, logical_dominance_ips, longitude, latitude from {CITY_TABLE} {where} """ ) return [ { "id": r[0], "city": r[1], "country": r[2], "country_name": r[3], "region": r[4], "tbps": float(r[5]) if r[5] is not None else None, "ips": int(r[6]) if r[6] is not None else None, "lon": float(r[7]), "lat": float(r[8]), } for r in cur.fetchall() ] def render_html(data_centers, cables_geojson, cities, output_path): payload = json.dumps( { "data_centers": data_centers, "cables": cables_geojson, "cities": cities, } ) html = """ US Data Centers + Submarine Cables

Data Centers + Cables

Data centers
Submarine cables
City dominance pts

Layers

Data center source

merged (curated + OSM)
curated only
osm only
other

City dominance

Sized by physical Tbps
""" html = html.replace("__PAYLOAD__", payload) with open(output_path, "w", encoding="utf-8") as f: f.write(html) def parse_args(): p = argparse.ArgumentParser( description="Render a Leaflet map combining data centers, submarine cables, and city dominance." ) p.add_argument("--output", default=str(Path(__file__).parent.parent / "output" / "data_centers_cables_map.html")) p.add_argument( "--us-cities-only", action="store_true", help="Restrict the city-dominance layer to country='US'.", ) return p.parse_args() def main(): args = parse_args() conn = connect() try: dcs = load_data_centers(conn) cables = load_cables(conn) cities = load_cities(conn, us_only=args.us_cities_only) finally: conn.close() render_html(dcs, cables, cities, args.output) print( f"wrote {len(dcs)} data centers, " f"{len(cables['features'])} cables, " f"{len(cities)} city points -> {args.output}" ) if __name__ == "__main__": main()