Last active
October 13, 2023 06:09
-
-
Save person142/f2e874ff8d08af4fdefffbe8fe71a0f7 to your computer and use it in GitHub Desktop.
Organic maps tiles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import json | |
import math | |
from pathlib import Path | |
from geopy.geocoders import Nominatim | |
from geopy.extra.rate_limiter import RateLimiter | |
CACHE_DIR = Path(__file__).parent / "cache" | |
NATIONAL_PARKS = [ | |
"Denali National Park", | |
"Gates of the Arctic National Park", | |
"Glacier Bay National Park", | |
"Katmai National Park", | |
"Kenai Fjords National Park", | |
"Kobuk Valley National Park", | |
"Lake Clark National Park", | |
"Wrangell-Saint Elias National Park", | |
] | |
STATE_PARKS = [ | |
"Chugach State Park", | |
"Kachemak Bay State Park", | |
"Shuyak Island State Park", | |
"Denali State Park", | |
] | |
LOCATIONS = ( | |
NATIONAL_PARKS | |
+ STATE_PARKS | |
) | |
def get_geocode_response(location, geocode): | |
cache_key = location.replace(" ", "-") | |
cached = CACHE_DIR / cache_key | |
if cached.exists(): | |
with open(cached) as f: | |
return json.load(f) | |
# Being sloppy here, but I've checked these locations and the | |
# right result is the first one. | |
response = geocode(query=location, exactly_one=True) | |
if response is None: | |
raise Exception(f"No result found for {location}") | |
with open(cached, "w") as f: | |
json.dump(response.raw, f) | |
return response.raw | |
def get_tiles(bbox): | |
bottomLat, topLat, leftLon, rightLon = bbox | |
bottomLat = int(math.floor(bottomLat)) | |
topLat = int(math.ceil(topLat)) | |
leftLon = int(math.floor(leftLon)) | |
rightLon = int(math.ceil(rightLon)) | |
tiles = [ | |
{ "bottomLat": lat, "leftLon": lon } | |
for lat in range(bottomLat, topLat) | |
for lon in range(leftLon, rightLon) | |
] | |
return tiles | |
def main(): | |
CACHE_DIR.mkdir(parents=True, exist_ok=True) | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--out", required=True) | |
args = parser.parse_args() | |
nominatim = Nominatim(user_agent="person142/ak-parks") | |
geocode = RateLimiter(nominatim.geocode, min_delay_seconds=2) | |
tiles = [] | |
for location in LOCATIONS: | |
response = get_geocode_response(location, geocode) | |
bbox = [float(loc) for loc in response["boundingbox"]] | |
tiles.extend(get_tiles(bbox)) | |
tiles = [ | |
dict(tup) for tup in sorted( | |
{tuple(tile.items()) for tile in tiles} | |
) | |
] | |
result = { | |
"key": "US_Alaska", | |
"value": { | |
"profileName": "normal", | |
"tileCoordsSubset": tiles, | |
"tilesAreBanned": False, | |
} | |
} | |
with open(args.out, "w") as f: | |
json.dump(result, f, indent=4) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment