Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i cant get a map to generate #12

Closed
SnipeHype200 opened this issue Nov 14, 2023 · 8 comments
Closed

i cant get a map to generate #12

SnipeHype200 opened this issue Nov 14, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@SnipeHype200
Copy link

when i tryed this project ive gotten everything to work but generating a map i changed the cordanates in the skyportal_config.py but im stuck with the default map

@sco1
Copy link
Owner

sco1 commented Nov 14, 2023

Sorry, it looks like I forgot to change it to a configuration option (I keep it off while developing since I restart so often).

Can you try changing line 362 in skyportal/displaylib.py:

self.set_base_map(grid_bounds=self.grid_bounds, use_default=True)

Either to use_default=False, or get rid of the argument altogether, and see if that solves the issue? I will try and fix this evening.

@sco1 sco1 added the bug Something isn't working label Nov 14, 2023
@sco1
Copy link
Owner

sco1 commented Nov 14, 2023

It looks like even if you fix this issue you're going to run into a memory error (similar to #11) trying to download the new map tile that I wasn't noticing because I've been skipping that step. I've extracted the logic below into a little script that you can use do generate the URL to a map tile of your choice that you can use to overwrite default_map.bmp on your device.

import math
from collections import OrderedDict


secrets = {
    "geoapify_key": "YOUR_GEOAPIFY_API_KEY",
    "aio_username": "YOUR_AIO_USERNAME",
    "aio_key": "YOUR_AIO_KEY",
}

MAP_CENTER_LAT = 42.41
MAP_CENTER_LON = -71.17
GRID_WIDTH_MI = 15
MAP_STYLE = "klokantech-basic"

GEOAPIFY_API_URL_BASE = "https://maps.geoapify.com/v1/staticmap"
AIO_URL_BASE = f"https://io.adafruit.com/api/v2/{secrets['aio_username']}/integrations/image-formatter"



def build_url(base, params):
    param_str = "&".join(f"{k}={v}" for k, v in params.items())
    return f"{base}?{param_str}"


def urlencode(url):
    encoded_chars = []
    for c in url:
        if any((c.isalpha(), c.isdigit(), (c in ("-", ".")))):
            encoded_chars.append(c)
        else:
            encoded_chars.append(f"%{ord(c):02X}")

    return "".join(encoded_chars)

def build_bounding_box(
    map_center_lat = MAP_CENTER_LAT, map_center_lon = MAP_CENTER_LON, grid_width_mi = GRID_WIDTH_MI,
):
    earth_radius_km = 6378.1

    center_lat_rad = math.radians(map_center_lat)
    center_lon_rad = math.radians(map_center_lon)
    grid_size_km = grid_width_mi * 1.6

    # Calculate distance deltas
    ang_dist = grid_size_km / earth_radius_km
    d_lat = ang_dist
    d_lon = math.asin(math.sin(ang_dist) / math.cos(center_lat_rad))

    # Scale rectangle height from the specified width
    aspect_ratio = 320 / 240
    d_lon *= aspect_ratio

    # Calculate latitude bounds
    min_center_lat_rad = center_lat_rad - d_lat
    max_center_lat_rad = center_lat_rad + d_lat

    # Calculate longitude bounds
    min_center_lon_rad = center_lon_rad - d_lon
    max_center_lon_rad = center_lon_rad + d_lon

    # Convert from radians to degrees
    lat_min = math.degrees(min_center_lat_rad)
    lat_max = math.degrees(max_center_lat_rad)
    lon_min = math.degrees(min_center_lon_rad)
    lon_max = math.degrees(max_center_lon_rad)

    return lat_min, lat_max, lon_min, lon_max


def get_base_map_url(grid_bounds):
    lat_min, lat_max, lon_min, lon_max = grid_bounds
    map_params = OrderedDict(
        [
            ("apiKey", secrets["geoapify_key"]),
            ("style", MAP_STYLE),
            ("format", "png"),
            ("center", f"lonlat:{MAP_CENTER_LON},{MAP_CENTER_LAT}"),
            ("area", f"rect:{lon_min},{lat_min},{lon_max},{lat_max}"),
            ("width", 320 * 2),
            ("height", 240 * 2),
        ]
    )
    map_query_url = build_url(GEOAPIFY_API_URL_BASE, map_params)

    adaIO_params = OrderedDict(
        [
            ("x-aio-key", secrets["aio_key"]),
            ("width", 320),
            ("height", 240),
            ("output", "BMP16"),
            ("url", urlencode(map_query_url)),
        ]
    )
    adaIO_query_url = build_url(AIO_URL_BASE, adaIO_params)
    return adaIO_query_url


print(get_base_map_url(build_bounding_box()))

@SnipeHype200
Copy link
Author

where is this code supposed to go?

@SnipeHype200
Copy link
Author

is it just a python program to run?

@SnipeHype200
Copy link
Author

one time

@sco1
Copy link
Owner

sco1 commented Nov 14, 2023

Yes.

@SnipeHype200
Copy link
Author

worked perfectly thanks!

@sco1
Copy link
Owner

sco1 commented Nov 16, 2023

OpenSky is still down so I can't fully test this but I think the map tile download is back to working now & has its own configuration option.

@sco1 sco1 closed this as completed Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants