craigerl / digipi

DigiPi code and config files
42 stars 11 forks source link

Suggestion. Add maidenhead to latitude longitude too the initial config screen. #7

Open mbridak opened 6 months ago

mbridak commented 6 months ago

Hello Sir, I believe it would be a nicer on-boarding experience if the initial configuration screen had an option to enter the users grid square and have the latitude and longitude calculated for them.

Something like:

def gridtolatlon(maiden):
    """
    Converts a maidenhead gridsquare to a latitude longitude pair.
    """
    try:
        maiden = str(maiden).strip().upper()

        chars_in_grid_square = len(maiden)
        if not 8 >= chars_in_grid_square >= 2 and chars_in_grid_square % 2 == 0:
            return 0, 0

        lon = (ord(maiden[0]) - 65) * 20 - 180
        lat = (ord(maiden[1]) - 65) * 10 - 90

        if chars_in_grid_square >= 4:
            lon += (ord(maiden[2]) - 48) * 2
            lat += ord(maiden[3]) - 48

        if chars_in_grid_square >= 6:
            lon += (ord(maiden[4]) - 65) / 12 + 1 / 24
            lat += (ord(maiden[5]) - 65) / 24 + 1 / 48

        if chars_in_grid_square >= 8:
            lon += (ord(maiden[6])) * 5.0 / 600
            lat += (ord(maiden[7])) * 2.5 / 600

        return round(lat, 4), round(lon, 4)
    except IndexError:
        return 0, 0

Since most people know their gridsquare, this maybe useful.

Cheers! Mike K6GTE

craigerl commented 6 months ago

yah, I agree, or calculate gridsquare from lat/lon, or both.

craigerl commented 5 months ago

Is anyone here smart enough to write java script that would auto-populate the gridsquare field as the user enters the lat/lon? i'm not the javascript guy

mbridak commented 5 months ago

Something like:


function latlonToMaiden(lat, lon)
{
// This is assuming lat and lon have been parsed with parseFloat() from user input
lon += 180;
lat +=  90;

var longitude = Math.floor(lon/20);
var latitude = Math.floor(lat/10);

var maiden = String.fromCharCode(65 + longitude) + String.fromCharCode(65 + latitude);

var square_lon = Math.floor((lon - 20*longitude)/2);
var square_lat = Math.floor(lat - 10*latitude);
maiden += square_lon.toString() + square_lat.toString();

var subsquare_longitude = Math.floor(12*(lon - 20*longitude - 2*square_lon))
var subsquare_latitude = Math.floor(24*(lat - 10*latitude - square_lat))
maiden += String.fromCharCode(97 + subsquare_longitude) + String.fromCharCode(97 + subsquare_latitude)
return maiden;      
}

I'm just assuming this works. Found in one of my old projects.