# 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
        px is pixel coord to the right from top,left
        py is pixel coord down from top left
        '''

        pixel_width_equator = (ground_width / float(width)) / cos(radians(lat))
        latr = radians(lat)
        lat2r = radians(lat2)

        C = mp_util.radius_of_earth / pixel_width_equator
        y = C * (log(abs(1.0/cos(latr) + tan(latr))) - log(abs(1.0/cos(lat2r) + tan(lat2r))))
        y = int(y+0.5)

        dx = mp_util.gps_distance(lat2, lon, lat2, lon2)
        if mp_util.gps_bearing(lat2, lon, lat2, lon2) > 180:
            dx = -dx
        x = int(0.5 + dx / (pixel_width_equator * cos(radians(lat2))))
        return (x,y)

    def area_to_tile_list(self, lat, lon, width, height, ground_width, zoom=None):
        '''return a list of TileInfoScaled objects needed for
        an area of land, with ground_width in meters, and
        width/height in pixels.

        lat/lon is the top left corner. If unspecified, the
        zoom is automatically chosen to avoid having to grow
        the tiles
        '''

        pixel_width = ground_width / float(width)
        ground_height = ground_width * (height/(float(width)))

# 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

        px is pixel coord to the right from top,left
        py is pixel coord down from top left
        '''

        pixel_width_equator = (ground_width / float(width)) / cos(radians(lat))
        latr = radians(lat)
        lat2r = radians(lat2)

        C = mp_util.radius_of_earth / pixel_width_equator
        y = C * (log(abs(1.0/cos(latr) + tan(latr))) - log(abs(1.0/cos(lat2r) + tan(lat2r))))
        y = int(y+0.5)

        dx = mp_util.gps_distance(lat2, lon, lat2, lon2)
        if mp_util.gps_bearing(lat2, lon, lat2, lon2) > 180:
            dx = -dx
        x = int(0.5 + dx / (pixel_width_equator * cos(radians(lat2))))
        return (x,y)

    def area_to_tile_list(self, lat, lon, width, height, ground_width, zoom=None):
        '''return a list of TileInfoScaled objects needed for
        an area of land, with ground_width in meters, and
        width/height in pixels.

        lat/lon is the top left corner. If unspecified, the
        zoom is automatically chosen to avoid having to grow
        the tiles
        '''

        pixel_width = ground_width / float(width)
        ground_height = ground_width * (height/(float(width)))

        top_right = mp_util.gps_newpos(lat, lon, 90, ground_width)