# -------------------------------------------------------------------
# ORTHOGRAPHIC
# Your personal aerial satellite. Always on. At any altitude.*
# Developed by MarStrMind
# License: Open Software License 3.0
# Up to date version always on marstr.online
# -------------------------------------------------------------------
# functions.py
# A number of utility functions that don't belong in any class
# -------------------------------------------------------------------

import os
import json
from defines import *


# Find out if we have a tiledata.json file at all
def doesTileDataExist(lat, lng):
    e = False
    if os.path.isfile(mstr_datafolder + "Tiles/" + str(lat) + "_" + str(lng) + "/data.db"):
        e = True
    return e



# Find adjacent tiles to check against existing chosen imagery
#
# The provided tile is the center, and it will provide a list of
# all adjacent tiles, like so:
#
#  #
# #X#
#  #
#
# If we are in a corner, those will be omitted.
def findAdjacentTilesTo(v, h):
    adj = []
    adj.append( (v+1, h) ) # Top
    adj.append( (v, h+1) ) # Right
    adj.append( (v-1, h) ) # Bottom
    adj.append( (v, h-1) ) # Left

    return adj


# Find the tiles we need in order to construct zoom level 16 photos,
# based on the tile provided. This is usually called at the very end
# of the ortho generation process.
def findZL16tiles(v, h):
    # Contains the tiles, in forward order (order of processing)
    tiles = []

    for y in range(0, 4):
        tr = []
        for x in range(0, 4):
            tl = (v+y, h+x)
            tr.append(tl)
        tiles.append(tr)
    
    return tiles


# Find the tiles to keep around an airport, using the defined tile
# radius amount in defines.py
def findAirportTiles(av, ah):
    # The tiles
    tiles=[]

    # Starting points
    sty = av - int(mstr_airport_radius/2)
    stx = ah - int(mstr_airport_radius/2)

    for y in range(mstr_airport_radius):
        for x in range(mstr_airport_radius):
            a = ( sty+y, stx+x )
            tiles.append(a)

    # Return the tiles
    return tiles


# Testing
def in_circle(center_x, center_y, radius, x, y):
    square_dist = (center_x - x) ** 2 + (center_y - y) ** 2
    return square_dist <= radius ** 2


# Find meters per pixel. Requires width of tile in meters
# Needed for proper approximation of shadow length per building level
def meters_per_pixel(lngwidth):
    mpx = lngwidth / mstr_photores
    return mpx


# Construct an X-Plane compatible folder name for latitude and longitude
def xplane_latlng_folder(numbers):
    fstr = ""
    if numbers[0] >= 0: fstr = "+"
    if numbers[0] <  0: fstr = "-"
    if abs(numbers[0]) < 10: fstr = fstr + "0" + str(numbers[0])
    if abs(numbers[0]) >= 10 and numbers[0] <= 90: fstr = fstr + str(numbers[0])

    if numbers[1] >= 0: fstr = fstr + "+"
    if numbers[1] <  0: fstr = fstr + "-"
    if abs(numbers[1]) < 10: fstr = fstr + "00" + str(numbers[1])
    if abs(numbers[1]) >= 10 and numbers[0] <= 99: fstr = fstr + "0" + str(numbers[1])
    if abs(numbers[1]) >= 100 : fstr = fstr + str(numbers[1])

    return fstr