# 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

    def __getitem__(self, a):
        return str(getattr(self, a))


class TileInfo:
    '''description of a tile'''
    def __init__(self, tile, zoom, service, offset=(0,0)):
        self.tile = tile
        (self.x, self.y) = tile
        self.zoom = zoom
        self.service = service
        (self.offsetx, self.offsety) = offset
        self.refresh_time()

    def key(self):
        '''tile cache key'''
        return (self.tile, self.zoom, self.service)

    def refresh_time(self):
        '''reset the request time'''
        self.request_time = time.time()

    def coord(self, offset=(0,0)):
        '''return lat,lon within a tile given (offsetx,offsety)'''
        (tilex, tiley) = self.tile
        (offsetx, offsety) = offset
        world_tiles = 1<<self.zoom
        x = ( tilex + 1.0*offsetx/TILES_WIDTH ) / (world_tiles/2.) - 1
        y = ( tiley + 1.0*offsety/TILES_HEIGHT) / (world_tiles/2.) - 1

# 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


    def __getitem__(self, a):
        return str(getattr(self, a))


class TileInfo:
    '''description of a tile'''
    def __init__(self, tile, zoom, service, offset=(0,0)):
        self.tile = tile
        (self.x, self.y) = tile
        self.zoom = zoom
        self.service = service
        (self.offsetx, self.offsety) = offset
        self.refresh_time()

    def key(self):
        '''tile cache key'''
        return (self.tile, self.zoom, self.service)

    def refresh_time(self):
        '''reset the request time'''
        self.request_time = time.time()

    def coord(self, offset=(0,0)):
        '''return lat,lon within a tile given (offsetx,offsety)'''
        (tilex, tiley) = self.tile
        (offsetx, offsety) = offset
        world_tiles = 1<<self.zoom
        x = ( tilex + 1.0*offsetx/TILES_WIDTH ) / (world_tiles/2.) - 1
        y = ( tiley + 1.0*offsety/TILES_HEIGHT) / (world_tiles/2.) - 1

        lon = mp_util.wrap_180(x * 180.0)