Skip to content

Instantly share code, notes, and snippets.

@wipfli
Last active April 7, 2025 06:59
Show Gist options
  • Save wipfli/3f6546e107b7cac214054e9c6b8d92ee to your computer and use it in GitHub Desktop.
Save wipfli/3f6546e107b7cac214054e9c6b8d92ee to your computer and use it in GitHub Desktop.
country-zooms-tilezen.md

Converting country zooms from tilezen to protomaps resources/country-zooms.csv

Clone https://github.com/tilezen/vector-datasource/, go to spreadsheets/min_zooms

download place=country from overpass-turbo to the the wikidata ids of the countries and other properties, store in countries.geojson

save the following to country.py

# country.py
import yaml

# Read the YAML content from a file
with open("country.yaml", "r") as file:
    yaml_content = file.read()

# Parse the YAML content
country_names = {}
current_country_name = None

# First pass to extract comments containing country names
for line in yaml_content.splitlines():
    line = line.strip()
    if line.startswith('#'):
        # Store the country name from comment
        current_country_name = line[1:].strip()
    elif ':' in line and not line.startswith(' '):
        # This is a country code line
        country_code = line.split(':')[0].strip()
        if current_country_name:
            country_names[country_code] = current_country_name
            current_country_name = None

# Parse the actual YAML data
data = yaml.safe_load(yaml_content)

# Create the list of dictionaries with the required format
tilezen_countries = []
for country_code, country_data in data.items():
    country_dict = {
        "code": country_code,
        "min_zoom": round(country_data.get("min_zoom")) - 1,
        "max_zoom": round(country_data.get("max_zoom")) - 1,
        "name": country_names.get(country_code, "Unknown")
    }
    tilezen_countries.append(country_dict)

# # Print the list as JSON
# for country in countries_list:
#     print(f'{round(country["min_zoom"]) - 1}, {round(country["max_zoom"]) - 1}, {country["code"]}, {country["name"]}')

import json
with open('countries.geojson') as f:
    osm_countries = json.load(f)["features"]

def get_osm_by_name(tilezen_country, osm_countries):
    result = []
    for osm_country in osm_countries:
        key = "name:en"
        if key in osm_country["properties"] and tilezen_country["name"] == osm_country["properties"][key]:
            result.append(osm_country)
    return result

def get_osm_by_code(tilezen_country, osm_countries):
    result = []
    for osm_country in osm_countries:
        key = "ISO3166-1:alpha2"
        if key in osm_country["properties"] and tilezen_country["code"] == osm_country["properties"][key]:
            result.append(osm_country)
    return result

def get_osm_by_code_alt(tilezen_country, osm_countries):
    result = []
    for osm_country in osm_countries:
        key = "country_code_iso3166_1_alpha_2"
        if key in osm_country["properties"] and tilezen_country["code"] == osm_country["properties"][key]:
            result.append(osm_country)
    return result

for tilezen_country in tilezen_countries:
    
    matches = [
        get_osm_by_name(tilezen_country, osm_countries),
        get_osm_by_code(tilezen_country, osm_countries),
        get_osm_by_code_alt(tilezen_country, osm_countries)
    ]

    wikidatas = []
    for match in matches:
        if len(match) > 0 and "wikidata" in match[0]["properties"]:
            wikidatas.append(match[0]["properties"]["wikidata"])
        else:
            wikidatas.append("")
    

    wikidatas = set(wikidatas)
    if "" in wikidatas:
        wikidatas.remove("")
    
    wikidatas = list(wikidatas)

    wikidata = wikidatas[0] if len(wikidatas) == 1 else None

    if wikidata:
        print(f'{wikidata},{tilezen_country["min_zoom"]},{tilezen_country["max_zoom"]}')

run python3 country.py

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment