# Path: gbdxtools/deprecate.py
# def deprecation(message):
#     warnings.warn(message, GBDXDeprecation)
# 
# Path: gbdxtools/images/browse_image.py
# class BrowseImage(object):
#     """
#       Image class for fetching and spatially indexing an image's browse image (low res previews)
# 
#       Args:
#           catalog_id (str): the catalog id for the browse image
#           bbox (list): the bounding box in WGS84 coords for spatial indexing
#       Returns:
#           BrowseImage (object)
#     """    
#     def __init__(self, catalog_id, bbox=None):
#         self._interface = Auth() 
#         self.catalog_id = catalog_id
#         self._get_image()
#         self.metadata = self._get_metadata()
#         self.shape = self.image.shape
#         self.geom = self._get_geometry()
#         self.xmin, self.ymin, self.xmax, self.ymax = self.geom.bounds
#         self.cell_width = (self.xmax - self.xmin)/self.shape[1]
#         self.cell_height = (self.ymax - self.ymin)/self.shape[0]
#         self.bbox = bbox
# 
#         if self.bbox is not None:
#             # find which cells intersect the bbox
#             bbox_xmin, bbox_ymin, bbox_xmax, bbox_ymax = self.bbox
#             window_xmin = int(np.floor((bbox_xmin - self.xmin)/self.cell_width))
#             window_xmax = int(np.ceil((bbox_xmax - self.xmin)/self.cell_width))
#             window_ymax = self.shape[0]-int(np.floor((bbox_ymin - self.ymin)/self.cell_height))
#             window_ymin = self.shape[0]-int(np.ceil((bbox_ymax - self.ymin)/self.cell_height))
#             self.window = ((window_ymin, window_ymax), (window_xmin, window_xmax))
#         else:
#             self.window = None
# 
#             
#     def _get_image(self):
#         url = 'https://api.discover.digitalglobe.com/show?id={}'.format(self.catalog_id)
#         self.image = imread(url)
#             
#     def rgb(self):
#         return self.read()
#     
#     def _get_metadata(self):
#         url = 'https://geobigdata.io/catalog/v2/record/{}'.format(self.catalog_id)
#         response = self._interface.gbdx_connection.get(url)
#         response.raise_for_status()
#         return response.json()
#         
#     def _get_geometry(self):
#         if 'footprintWkt' in self.metadata['properties'].keys():
#             geom = loads(self.metadata['properties']['footprintWkt'])
#         elif 'geometry' in self.metadata.keys():
#             geom = shape(self.metadata['geometry'])
#         else:
#             raise ValueError("Invalid metadata returned by metadata service. Cannot create BrowseImage")
#         return geom
#         
#     def read(self):
#         if self.window is None:
#             return self.image
#         else:
#             (miny, maxy), (minx, maxx) = self.window
#             return self.image[miny:maxy,minx:maxx,:]
#         
#     def plot(self, w=10, h=10, title='', fontsize=24):
#         assert has_matplotlib, 'Matplotlib is required to plot images'
#         plt.figure(figsize=(w, h))
#         sp = plt.subplot(1, 1, 1)
#         plt.axis('off')
#         sp.set_title(title, fontsize=fontsize)
#         plt.imshow(self.rgb())
#         plt.show()


import os
import math
import mercantile
import numpy as np
    from rio_hist.match import histogram_match as rio_match
from shapely.geometry import box, shape, mapping, asShape
from gbdxtools.deprecate import deprecation
from gbdxtools.images.browse_image import BrowseImage
    from matplotlib import pyplot as plt
            raise KeyError('Unknown histogram parameter, use "equalize", "match", "minmax", or "ignore"')

    def histogram_equalize(self, use_bands, **kwargs):
        ''' Equalize and the histogram and normalize value range
            Equalization is on all three bands, not per-band'''
        data = self._read(self[use_bands,...], **kwargs)
        data = np.rollaxis(data.astype(np.float32), 0, 3)
        flattened = data.flatten()
        if 0 in data:
            masked = np.ma.masked_values(data, 0).compressed()
            image_histogram, bin_edges = np.histogram(masked, 256)
        else:
            image_histogram, bin_edges = np.histogram(flattened, 256)
        bins = (bin_edges[:-1] + bin_edges[1:]) / 2.0
        cdf = image_histogram.cumsum() 
        cdf = cdf / float(cdf[-1])
        image_equalized = np.interp(flattened, bins, cdf).reshape(data.shape)
        if 'stretch' in kwargs or 'gamma' in kwargs:
            return self._histogram_stretch(image_equalized, **kwargs)
        else:
            return image_equalized

    def histogram_match(self, use_bands, blm_source='browse', **kwargs):
        ''' Match the histogram to Browse imagery '''
        assert has_rio, "To match image histograms please install rio_hist"
        data = self._read(self[use_bands,...], **kwargs)
        data = np.rollaxis(data.astype(np.float32), 0, 3)
        if 0 in data:
            data = np.ma.masked_values(data, 0)
        bounds = self._reproject(box(*self.bounds), from_proj=self.proj, to_proj="EPSG:4326").bounds

# Path: gbdxtools/deprecate.py
# def deprecation(message):
#     warnings.warn(message, GBDXDeprecation)
# 
# Path: gbdxtools/images/browse_image.py
# class BrowseImage(object):
#     """
#       Image class for fetching and spatially indexing an image's browse image (low res previews)
# 
#       Args:
#           catalog_id (str): the catalog id for the browse image
#           bbox (list): the bounding box in WGS84 coords for spatial indexing
#       Returns:
#           BrowseImage (object)
#     """    
#     def __init__(self, catalog_id, bbox=None):
#         self._interface = Auth() 
#         self.catalog_id = catalog_id
#         self._get_image()
#         self.metadata = self._get_metadata()
#         self.shape = self.image.shape
#         self.geom = self._get_geometry()
#         self.xmin, self.ymin, self.xmax, self.ymax = self.geom.bounds
#         self.cell_width = (self.xmax - self.xmin)/self.shape[1]
#         self.cell_height = (self.ymax - self.ymin)/self.shape[0]
#         self.bbox = bbox
# 
#         if self.bbox is not None:
#             # find which cells intersect the bbox
#             bbox_xmin, bbox_ymin, bbox_xmax, bbox_ymax = self.bbox
#             window_xmin = int(np.floor((bbox_xmin - self.xmin)/self.cell_width))
#             window_xmax = int(np.ceil((bbox_xmax - self.xmin)/self.cell_width))
#             window_ymax = self.shape[0]-int(np.floor((bbox_ymin - self.ymin)/self.cell_height))
#             window_ymin = self.shape[0]-int(np.ceil((bbox_ymax - self.ymin)/self.cell_height))
#             self.window = ((window_ymin, window_ymax), (window_xmin, window_xmax))
#         else:
#             self.window = None
# 
#             
#     def _get_image(self):
#         url = 'https://api.discover.digitalglobe.com/show?id={}'.format(self.catalog_id)
#         self.image = imread(url)
#             
#     def rgb(self):
#         return self.read()
#     
#     def _get_metadata(self):
#         url = 'https://geobigdata.io/catalog/v2/record/{}'.format(self.catalog_id)
#         response = self._interface.gbdx_connection.get(url)
#         response.raise_for_status()
#         return response.json()
#         
#     def _get_geometry(self):
#         if 'footprintWkt' in self.metadata['properties'].keys():
#             geom = loads(self.metadata['properties']['footprintWkt'])
#         elif 'geometry' in self.metadata.keys():
#             geom = shape(self.metadata['geometry'])
#         else:
#             raise ValueError("Invalid metadata returned by metadata service. Cannot create BrowseImage")
#         return geom
#         
#     def read(self):
#         if self.window is None:
#             return self.image
#         else:
#             (miny, maxy), (minx, maxx) = self.window
#             return self.image[miny:maxy,minx:maxx,:]
#         
#     def plot(self, w=10, h=10, title='', fontsize=24):
#         assert has_matplotlib, 'Matplotlib is required to plot images'
#         plt.figure(figsize=(w, h))
#         sp = plt.subplot(1, 1, 1)
#         plt.axis('off')
#         sp.set_title(title, fontsize=fontsize)
#         plt.imshow(self.rgb())
#         plt.show()


# Path: gbdxtools/images/mixins/geo.py
import os
import math
import mercantile
import numpy as np
    from rio_hist.match import histogram_match as rio_match
from shapely.geometry import box, shape, mapping, asShape
from gbdxtools.deprecate import deprecation
from gbdxtools.images.browse_image import BrowseImage
    from matplotlib import pyplot as plt

            raise KeyError('Unknown histogram parameter, use "equalize", "match", "minmax", or "ignore"')

    def histogram_equalize(self, use_bands, **kwargs):
        ''' Equalize and the histogram and normalize value range
            Equalization is on all three bands, not per-band'''
        data = self._read(self[use_bands,...], **kwargs)
        data = np.rollaxis(data.astype(np.float32), 0, 3)
        flattened = data.flatten()
        if 0 in data:
            masked = np.ma.masked_values(data, 0).compressed()
            image_histogram, bin_edges = np.histogram(masked, 256)
        else:
            image_histogram, bin_edges = np.histogram(flattened, 256)
        bins = (bin_edges[:-1] + bin_edges[1:]) / 2.0
        cdf = image_histogram.cumsum() 
        cdf = cdf / float(cdf[-1])
        image_equalized = np.interp(flattened, bins, cdf).reshape(data.shape)
        if 'stretch' in kwargs or 'gamma' in kwargs:
            return self._histogram_stretch(image_equalized, **kwargs)
        else:
            return image_equalized

    def histogram_match(self, use_bands, blm_source='browse', **kwargs):
        ''' Match the histogram to Browse imagery '''
        assert has_rio, "To match image histograms please install rio_hist"
        data = self._read(self[use_bands,...], **kwargs)
        data = np.rollaxis(data.astype(np.float32), 0, 3)
        if 0 in data:
            data = np.ma.masked_values(data, 0)
        bounds = self._reproject(box(*self.bounds), from_proj=self.proj, to_proj="EPSG:4326").bounds

        ref = BrowseImage(self.cat_id, bbox=bounds).read()