#!/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 import psycopg2 DB_NAME = "data_centers" DC_TABLE = "public.us_dc_sample_geocoded" 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 id, coalesce(provider, ''), coalesce(facility_name, ''), coalesce(city, ''), coalesce(state_code, ''), longitude, latitude, coalesce(geocode_source, '') from {DC_TABLE} where longitude is not null and latitude is not null """ ) return [ { "id": r[0], "provider": r[1], "facility_name": r[2], "city": r[3], "state_code": r[4], "lon": float(r[5]), "lat": float(r[6]), "geocode_source": 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 = """