# Path: MAVProxy/modules/lib/mp_util.py
# def wrap_360(angle):
# def wrap_180(angle):
# def gps_distance(lat1, lon1, lat2, lon2):
# def gps_bearing(lat1, lon1, lat2, lon2):
# def wrap_valid_longitude(lon):
# def constrain(v, minv, maxv):
# def constrain_latlon(latlon):
# def gps_newpos(lat, lon, bearing, distance):
# def gps_offset(lat, lon, east, north):
# def mkdir_p(dir):
# def polygon_load(filename):
# def polygon_bounds(points):
# def bounds_overlap(bound1, bound2):
#     def __init__(self, object, debug=False):
# def degrees_to_dms(degrees):
#     def __init__(self, zone, easting, northing, hemisphere='S'):
#     def __str__(self):
#     def latlon(self):
# def latlon_to_grid(latlon):
# def latlon_round(latlon, spacing=1000):
# def wxToPIL(wimg):
# def PILTowx(pimg):
# def dot_mavproxy(name=None):
# def download_url(url):
# def download_files(files):
# def child_close_fds():
# def child_fd_list_add(fd):
# def child_fd_list_remove(fd):
# def quaternion_to_axis_angle(q):
# def null_term(str):
# def decode_devid(devid, pname):
# def decode_flight_sw_version(flight_sw_version):
# class object_container:
# class UTMGrid:


import collections
import errno
import hashlib
import sys
import math
import threading
import os
import string
import time
import cv2
import numpy as np
                    import tempfile
            import ordereddict
        import pkg_resources
            import pkgutil
from math import log, tan, radians, degrees, sin, cos, exp, pi, asin, atan
    from urllib2 import Request as url_request
    from urllib2 import urlopen as url_open
    from urllib2 import URLError as url_error
    from urllib.request import Request as url_request
    from urllib.request import urlopen as url_open
    from urllib.error import URLError as url_error
from MAVProxy.modules.lib import mp_util
    from optparse import OptionParser
        '''start the downloader'''
        if self._download_thread:
            return
        t = threading.Thread(target=self.downloader)
        t.daemon = True
        self._download_thread = t
        t.start()

    def load_tile_lowres(self, tile):
        '''load a lower resolution tile from cache to fill in a
        map while waiting for a higher resolution tile'''
        if tile.zoom == self.min_zoom:
            return None

        # find the equivalent lower res tile
        (lat,lon) = tile.coord()

        width2 = TILES_WIDTH
        height2 = TILES_HEIGHT

        for zoom2 in range(tile.zoom-1, self.min_zoom-1, -1):
            width2 //= 2
            height2 //= 2

            if width2 == 0 or height2 == 0:
                break

            tile_info = self.coord_to_tile(lat, lon, zoom2)

            # see if its in the tile cache

# Path: MAVProxy/modules/lib/mp_util.py
# def wrap_360(angle):
# def wrap_180(angle):
# def gps_distance(lat1, lon1, lat2, lon2):
# def gps_bearing(lat1, lon1, lat2, lon2):
# def wrap_valid_longitude(lon):
# def constrain(v, minv, maxv):
# def constrain_latlon(latlon):
# def gps_newpos(lat, lon, bearing, distance):
# def gps_offset(lat, lon, east, north):
# def mkdir_p(dir):
# def polygon_load(filename):
# def polygon_bounds(points):
# def bounds_overlap(bound1, bound2):
#     def __init__(self, object, debug=False):
# def degrees_to_dms(degrees):
#     def __init__(self, zone, easting, northing, hemisphere='S'):
#     def __str__(self):
#     def latlon(self):
# def latlon_to_grid(latlon):
# def latlon_round(latlon, spacing=1000):
# def wxToPIL(wimg):
# def PILTowx(pimg):
# def dot_mavproxy(name=None):
# def download_url(url):
# def download_files(files):
# def child_close_fds():
# def child_fd_list_add(fd):
# def child_fd_list_remove(fd):
# def quaternion_to_axis_angle(q):
# def null_term(str):
# def decode_devid(devid, pname):
# def decode_flight_sw_version(flight_sw_version):
# class object_container:
# class UTMGrid:


# Path: MAVProxy/modules/mavproxy_map/mp_tile.py
import collections
import errno
import hashlib
import sys
import math
import threading
import os
import string
import time
import cv2
import numpy as np
                    import tempfile
            import ordereddict
        import pkg_resources
            import pkgutil
from math import log, tan, radians, degrees, sin, cos, exp, pi, asin, atan
    from urllib2 import Request as url_request
    from urllib2 import urlopen as url_open
    from urllib2 import URLError as url_error
    from urllib.request import Request as url_request
    from urllib.request import urlopen as url_open
    from urllib.error import URLError as url_error
from MAVProxy.modules.lib import mp_util
    from optparse import OptionParser

        '''start the downloader'''
        if self._download_thread:
            return
        t = threading.Thread(target=self.downloader)
        t.daemon = True
        self._download_thread = t
        t.start()

    def load_tile_lowres(self, tile):
        '''load a lower resolution tile from cache to fill in a
        map while waiting for a higher resolution tile'''
        if tile.zoom == self.min_zoom:
            return None

        # find the equivalent lower res tile
        (lat,lon) = tile.coord()

        width2 = TILES_WIDTH
        height2 = TILES_HEIGHT

        for zoom2 in range(tile.zoom-1, self.min_zoom-1, -1):
            width2 //= 2
            height2 //= 2

            if width2 == 0 or height2 == 0:
                break

            tile_info = self.coord_to_tile(lat, lon, zoom2)

            # see if its in the tile cache

            key = tile_info.key()