コード例 #1
0
    def setUpClass(cls):
        bbox = BBox(bbox=(47.94, -5.23, 48.17, -5.03), crs=CRS.WGS84)
        cls.stat_expect = {
            'min': 0.0008,
            'max': 0.6511,
            'mean': 0.2307,
            'median': 0.2397
        }
        cls.request_save = WmsRequest(data_folder=cls.OUTPUT_FOLDER,
                                      image_format=MimeType.TIFF_d32f,
                                      layer='ALL_BANDS',
                                      maxcc=1.0,
                                      width=512,
                                      bbox=bbox,
                                      time=('2017-10-01', '2017-10-02'),
                                      instance_id=cls.INSTANCE_ID)
        cls.data_save = cls.request_save.get_data(save_data=True,
                                                  redownload=True)

        cls.request_get = WmsRequest(image_format=MimeType.TIFF_d32f,
                                     layer='ALL_BANDS',
                                     maxcc=1.0,
                                     width=512,
                                     bbox=bbox,
                                     time=('2017-10-01', '2017-10-02'),
                                     instance_id=cls.INSTANCE_ID)
        cls.data_get = cls.request_get.get_data()
コード例 #2
0
    def __init__(self, project_name, bbox, time_interval, instance_id, full_size=(1920, 1080), preview_size=(455, 256),
                 cloud_mask_res=('60m', '60m'), use_atmcor=True, layer='TRUE_COLOR',
                 time_difference=datetime.timedelta(seconds=-1)):

        self.project_name = project_name
        self.preview_request = WmsRequest(data_folder=project_name + '/previews', layer=layer, bbox=bbox,
                                          time=time_interval, width=preview_size[0], height=preview_size[1],
                                          maxcc=1.0, image_format=MimeType.PNG, instance_id=instance_id,
                                          custom_url_params={CustomUrlParam.TRANSPARENT: True},
                                          time_difference=time_difference)

        self.fullres_request = WcsRequest(data_folder=project_name + '/fullres', layer=layer, bbox=bbox,
                                          time=time_interval, resx='10m', resy='10m',
                                          maxcc=1.0, image_format=MimeType.PNG, instance_id=instance_id,
                                          custom_url_params={CustomUrlParam.TRANSPARENT: True,
                                              CustomUrlParam.ATMFILTER: 'ATMCOR'} if use_atmcor else {CustomUrlParam.TRANSPARENT: True},
                                          time_difference=time_difference)

        wcs_request = WcsRequest(layer=layer, bbox=bbox, time=time_interval,
                                 resx=cloud_mask_res[0], resy=cloud_mask_res[1], maxcc=1.0,
                                 image_format=MimeType.TIFF_d32f, instance_id=instance_id,
                                 time_difference=time_difference, custom_url_params={CustomUrlParam.EVALSCRIPT:
                                                                                     MODEL_EVALSCRIPT})

        self.cloud_mask_request = CloudMaskRequest(wcs_request)

        self.transparency_data = None
        self.preview_transparency_data = None
        self.invalid_coverage = None

        self.dates = self.preview_request.get_dates()
        if not self.dates:
            raise ValueError('Input parameters are not valid. No Sentinel 2 image is found.')

        if self.dates != self.fullres_request.get_dates():
            raise ValueError('Lists of previews and full resolution images do not match.')

        if self.dates != self.cloud_mask_request.get_dates():
            raise ValueError('List of previews and cloud masks do not match.')

        self.mask = np.zeros((len(self.dates),), dtype=np.uint8)
        self.cloud_masks = None
        self.cloud_coverage = None

        self.full_res_data = None
        self.previews = None
        self.full_size = full_size
        self.timelapse = None

        LOGGER.info('Found %d images of %s between %s and %s.', len(self.dates), project_name,
                    time_interval[0], time_interval[1])

        LOGGER.info('\nI suggest you start by downloading previews first to see,\n'
                    'if BBOX is OK, images are usefull, etc...\n'
                    'Execute get_previews() method on your object.\n')
コード例 #3
0
 def setUpClass(cls):
     bbox = BBox(bbox=(47.94, -5.23, 48.17, -5.03), crs=CRS.WGS84)
     cls.request = WmsRequest(data_folder=cls.FALSE_FOLDER,
                              image_format=MimeType.PNG,
                              layer='TRUE_COLOR',
                              maxcc=1.0,
                              width=512,
                              height=512,
                              time='latest',
                              bbox=bbox,
                              instance_id=cls.INSTANCE_ID)
     cls.data = cls.request.get_data(save_data=False, redownload=True)
コード例 #4
0
ファイル: test_ogc.py プロジェクト: cdmawow/sentinelhub-py
    def setUpClass(cls):
        bbox = BBox(bbox=(47.94, -5.23, 48.17, -5.03), crs=CRS.WGS84)

        cls.stat_expect_atmfilter = {'min': 12, 'max': 255, 'mean': 190.0, 'median': 199.0}
        cls.stat_expect_preview = {'min': 28, 'max': 253, 'mean': 171.8, 'median': 171.0}
        cls.stat_expect_evalscript_url = {'min': 17, 'max': 255, 'mean': 162.4, 'median': 159.0}
        cls.stat_expect_evalscript = {'min': 0, 'max': 235, 'mean': 46.22, 'median': 54.0}

        cls.request_atmfilter = WmsRequest(data_folder='TestOutputs', image_format=MimeType.PNG, layer='TRUE_COLOR',
                                           maxcc=1.0, width=512, height=512, bbox=bbox, instance_id=INSTANCE_ID,
                                           time=('2017-10-01', '2017-10-02'),
                                           custom_url_params={CustomUrlParam.ATMFILTER: 'ATMCOR',
                                                              CustomUrlParam.QUALITY: 100,
                                                              CustomUrlParam.DOWNSAMPLING: 'BICUBIC',
                                                              CustomUrlParam.UPSAMPLING: 'BICUBIC'})

        cls.request_preview = WmsRequest(data_folder='TestOutputs', image_format=MimeType.PNG, layer='TRUE_COLOR',
                                         maxcc=1.0, width=512, height=512, bbox=bbox, time=('2017-10-01', '2017-10-02'),
                                         instance_id=INSTANCE_ID, custom_url_params={CustomUrlParam.PREVIEW: 2})

        cls.request_evalscript_url = WmsRequest(data_folder='TestOutputs', image_format=MimeType.PNG,
                                                layer='TRUE_COLOR', maxcc=1.0, width=512, height=512, bbox=bbox,
                                                time=('2017-10-01', '2017-10-02'), instance_id=INSTANCE_ID,
                                                custom_url_params={CustomUrlParam.EVALSCRIPTURL:
                                                                   'https://raw.githubusercontent.com/sentinel-hub/'
                                                                   'customScripts/master/sentinel-2/false_color_'
                                                                   'infrared/script.js'})
        cls.request_evalscript = WmsRequest(data_folder='TestOutputs', image_format=MimeType.PNG, layer='TRUE_COLOR',
                                            maxcc=1.0, height=512, bbox=bbox,
                                            time=('2017-10-01', '2017-10-02'), instance_id=INSTANCE_ID,
                                            custom_url_params={CustomUrlParam.EVALSCRIPT: 'return [B10,B8A,B03]'})

        cls.data_atmfilter = cls.request_atmfilter.get_data(redownload=True)
        cls.data_preview = cls.request_preview.get_data(redownload=True)
        cls.data_evalscript_url = cls.request_evalscript_url.get_data(redownload=True)
        cls.data_evalscript = cls.request_evalscript.get_data(redownload=True)
コード例 #5
0
class SentinelHubTimelapse(object):
    """
    Class for creating timelapses with Sentinel-2 images using Sentinel Hub's Python library.
    """

    def __init__(self, project_name, bbox, time_interval, instance_id, full_size=(1920, 1080), preview_size=(455, 256),
                 cloud_mask_res=('60m', '60m'), use_atmcor=True, layer='TRUE_COLOR',
                 time_difference=datetime.timedelta(seconds=-1)):

        self.project_name = project_name
        self.preview_request = WmsRequest(data_folder=project_name + '/previews', layer=layer, bbox=bbox,
                                          time=time_interval, width=preview_size[0], height=preview_size[1],
                                          maxcc=1.0, image_format=MimeType.PNG, instance_id=instance_id,
                                          custom_url_params={CustomUrlParam.TRANSPARENT: True},
                                          time_difference=time_difference)

        self.fullres_request = WcsRequest(data_folder=project_name + '/fullres', layer=layer, bbox=bbox,
                                          time=time_interval, resx='10m', resy='10m',
                                          maxcc=1.0, image_format=MimeType.PNG, instance_id=instance_id,
                                          custom_url_params={CustomUrlParam.TRANSPARENT: True,
                                              CustomUrlParam.ATMFILTER: 'ATMCOR'} if use_atmcor else {CustomUrlParam.TRANSPARENT: True},
                                          time_difference=time_difference)

        wcs_request = WcsRequest(layer=layer, bbox=bbox, time=time_interval,
                                 resx=cloud_mask_res[0], resy=cloud_mask_res[1], maxcc=1.0,
                                 image_format=MimeType.TIFF_d32f, instance_id=instance_id,
                                 time_difference=time_difference, custom_url_params={CustomUrlParam.EVALSCRIPT:
                                                                                     MODEL_EVALSCRIPT})

        self.cloud_mask_request = CloudMaskRequest(wcs_request)

        self.transparency_data = None
        self.preview_transparency_data = None
        self.invalid_coverage = None

        self.dates = self.preview_request.get_dates()
        if not self.dates:
            raise ValueError('Input parameters are not valid. No Sentinel 2 image is found.')

        if self.dates != self.fullres_request.get_dates():
            raise ValueError('Lists of previews and full resolution images do not match.')

        if self.dates != self.cloud_mask_request.get_dates():
            raise ValueError('List of previews and cloud masks do not match.')

        self.mask = np.zeros((len(self.dates),), dtype=np.uint8)
        self.cloud_masks = None
        self.cloud_coverage = None

        self.full_res_data = None
        self.previews = None
        self.full_size = full_size
        self.timelapse = None

        LOGGER.info('Found %d images of %s between %s and %s.', len(self.dates), project_name,
                    time_interval[0], time_interval[1])

        LOGGER.info('\nI suggest you start by downloading previews first to see,\n'
                    'if BBOX is OK, images are usefull, etc...\n'
                    'Execute get_previews() method on your object.\n')

    def get_previews(self, redownload=False):
        """
        Downloads and returns an numpy array of previews if previews were not already downloaded and saved to disk.
        Set `redownload` to True if to force downloading the previews again.
        """

        self.previews = np.asarray(self.preview_request.get_data(save_data=True, redownload=redownload))
        self.preview_transparency_data = self.previews[:,:,:,-1]

        LOGGER.info('%d previews have been downloaded and stored to numpy array of shape %s.', self.previews.shape[0],
                    self.previews.shape)

    def save_fullres_images(self, redownload=False):
        """
        Downloads and saves fullres images used to produce the timelapse. Note that images for all available dates
        within the specified time interval are downloaded, although they will be for example masked due to too high
        cloud coverage.
        """
        
        data4d = np.asarray(self.fullres_request.get_data(save_data=True, redownload=redownload))
        self.full_res_data = data4d[:,:,:,:-1]
        self.transparency_data = data4d[:,:,:,-1]

    def plot_preview(self, within_range=None, filename=None):
        """
        Plots all previews if within_range is None, or only previews in a given range.
        """
        within_range = CommonUtil.get_within_range(within_range, self.previews.shape[0])
        self._plot_image(self.previews[within_range[0]: within_range[1]] / 255., factor=1, filename=filename)

    def plot_cloud_masks(self, within_range=None, filename=None):
        """
        Plots all cloud masks if within_range is None, or only masks in a given range.
        """
        within_range = CommonUtil.get_within_range(within_range, self.cloud_masks.shape[0])
        self._plot_image(self.cloud_masks[within_range[0]: within_range[1]],
                         factor=1, cmap=plt.cm.binary, filename=filename)

    def _plot_image(self, data, factor=2.5, cmap=None, filename=None):
        rows = data.shape[0] // 5 + (1 if data.shape[0] % 5 else 0)
        aspect_ratio = (1.0 * data.shape[1]) / data.shape[2]
        fig, axs = plt.subplots(nrows=rows, ncols=5, figsize=(15, 3 * rows * aspect_ratio))
        for index, ax in enumerate(axs.flatten()):
            if index < data.shape[0] and index < len(self.dates):
                caption = str(index) + ': ' + self.dates[index].strftime('%Y-%m-%d')
                if self.cloud_coverage is not None:
                    caption = caption + '(' + "{0:2.0f}".format(self.cloud_coverage[index] * 100.0) + '%)'

                ax.set_axis_off()
                ax.imshow(data[index] * factor if data[index].shape[-1] == 3 or data[index].shape[-1] == 4 else
                          data[index] * factor, cmap=cmap, vmin=0.0, vmax=1.0)
                ax.text(0, -2, caption, fontsize=12, color='r' if self.mask[index] else 'g')
            else:
                ax.set_axis_off()

        if filename:
            plt.savefig(self.project_name + '/' + filename, bbox_inches='tight')

    def _load_cloud_masks(self):
        """
        Loads masks from disk, if they already exist.
        """
        cloud_masks_filename = self.project_name + '/cloudmasks/cloudmasks.npy'

        if not os.path.isfile(cloud_masks_filename):
            return False

        with open(cloud_masks_filename, 'rb') as fp:
            self.cloud_masks = np.load(fp)
        return True

    def _save_cloud_masks(self):
        """
        Saves masks to disk.
        """
        cloud_masks_filename = self.project_name + '/cloudmasks/cloudmasks.npy'

        if not os.path.exists(self.project_name + '/cloudmasks'):
            os.makedirs(self.project_name + '/cloudmasks')

        with open(cloud_masks_filename, 'wb') as fp:
            np.save(fp, self.cloud_masks)

    def _run_cloud_detection(self, rerun, threshold):
        """
        Determines cloud masks for each acquisition.
        """
        loaded = self._load_cloud_masks()
        if loaded and not rerun:
            LOGGER.info('Nothing to do. Masks are loaded.')
        else:
            LOGGER.info('Downloading cloud data and running cloud detection. This may take a while.')
            self.cloud_masks = self.cloud_mask_request.get_cloud_masks(threshold=threshold)
            self._save_cloud_masks()

    def mask_cloudy_images(self, rerun=False, max_cloud_coverage=0.1, threshold=None):
        """
        Marks images whose cloud coverage exceeds ``max_cloud_coverage``. Those
        won't be used in timelapse.

        :param rerun: Whether to rerun cloud detector
        :type rerun: bool
        :param max_cloud_coverage: Limit on the cloud coverage of images forming timelapse, 0 <= maxcc <= 1.
        :type max_cloud_coverage: float
        :param threshold:  A float from [0,1] specifying cloud threshold
        :type threshold: float or None
        """
        self._run_cloud_detection(rerun, threshold)

        self.cloud_coverage = np.asarray([self._get_coverage(mask) for mask in self.cloud_masks])

        for index in range(0, len(self.mask)):
            if self.cloud_coverage[index] > max_cloud_coverage:
                self.mask[index] = 1



    def mask_invalid_images(self, max_invalid_coverage=0.1):
        """
        Marks images whose invalid area coverage exceeds ``max_invalid_coverage``. Those
        won't be used in timelapse.

        :param max_invalid_coverage: Limit on the invalid area coverage of images forming timelapse, 0 <= maxic <= 1.
        :type max_invalid_coverage: float
        """

        # low-res and hi-res images/cloud masks may differ, just to be safe
        coverage_fullres = np.asarray([1.0-self._get_coverage(mask) for mask in self.transparency_data])
        coverage_preview = np.asarray([1.0-self._get_coverage(mask) for mask in self.preview_transparency_data])

        self.invalid_coverage = np.array([max(x,y) for x,y in zip(coverage_fullres, coverage_preview)])
        
        for index in range(0, len(self.mask)):
            if self.invalid_coverage[index] > max_invalid_coverage:
                self.mask[index] = 1

    def mask_images(self, idx):
        """
        Mannualy mask images with given indexes.
        """
        for index in idx:
            self.mask[index] = 1

    def unmask_images(self, idx):
        """
        Mannualy unmask images with given indexes.
        """
        for index in idx:
            self.mask[index] = 0

    def create_date_stamps(self):
        """
        Create date stamps to be included to gif.
        """
        filtered = list(compress(self.dates, list(np.logical_not(self.mask))))

        if not os.path.exists(self.project_name + '/datestamps'):
            os.makedirs(self.project_name + '/datestamps')

        for date in filtered:
            TimestampUtil.create_date_stamp(date, filtered[0], filtered[-1],
                                            self.project_name + '/datestamps/' + date.strftime(
                                                "%Y-%m-%dT%H-%M-%S") + '.png')

    def create_timelapse(self, scale_factor=0.3):
        """
        Adds date stamps to full res images and stores them in timelapse subdirectory.
        """
        filtered = list(compress(self.dates, list(np.logical_not(self.mask))))

        if not os.path.exists(self.project_name + '/timelapse'):
            os.makedirs(self.project_name + '/timelapse')

        self.timelapse = [TimestampUtil.add_date_stamp(self._get_filename('fullres', date.strftime("%Y-%m-%dT%H-%M-%S")),
                                         self.project_name + '/timelapse/' + date.strftime(
                                             "%Y-%m-%dT%H-%M-%S") + '.png',
                                         self._get_filename('datestamps', date.strftime("%Y-%m-%dT%H-%M-%S")),
                                         scale_factor=scale_factor) for date in filtered]

    @staticmethod
    def _get_coverage(mask):
        coverage_pixels = np.count_nonzero(mask)
        return 1.0 * coverage_pixels / mask.size

    @staticmethod
    def _iso_to_datetime(date):
        """ Convert ISO 8601 time format to datetime format

        This function converts a date in ISO format, e.g. 2017-09-14 to a datetime instance, e.g.
        datetime.datetime(2017,9,14,0,0)

        :param date: date in ISO 8601 format
        :type date: str
        :return: datetime instance
        :rtype: datetime
        """
        chunks = list(map(int, date.split('T')[0].split('-')))
        return datetime(chunks[0], chunks[1], chunks[2])

    @staticmethod
    def _datetime_to_iso(date, only_date=True):
        """ Convert datetime format to ISO 8601 time format

        This function converts a date in datetime instance, e.g. datetime.datetime(2017,9,14,0,0) to ISO format,
        e.g. 2017-09-14

        :param date: datetime instance to convert
        :type date: datetime
        :param only_date: whether to return date only or also time information. Default is `True`
        :type only_date: bool
        :return: date in ISO 8601 format
        :rtype: str
        """
        if only_date:
            return date.isoformat().split('T')[0]
        return date.isoformat()

    @staticmethod
    def _diff_month(start_dt, end_dt):
        return (end_dt.year - start_dt.year) * 12 + end_dt.month - start_dt.month + 1

    @staticmethod
    def _get_month_list(start_dt, end_dt):
        month_names = {1: 'J', 2: 'F', 3: 'M', 4: 'A', 5: 'M', 6: 'J', 7: 'J', 8: 'A', 9: 'S', 10: 'O', 11: 'N',
                       12: 'D'}

        total_months = SentinelHubTimelapse._diff_month(start_dt, end_dt)
        all_months = list(rrule(MONTHLY, count=total_months, dtstart=start_dt))
        return [month_names[date.month] for date in all_months]

    def _get_filename(self, subdir, date):
        for filename in glob.glob(self.project_name + '/' + subdir + '/*'):
            if date in filename:
                return filename

        return None

    def _get_timelapse_images(self):
        if self.timelapse is None:
            data = np.array(self.fullres_request.get_data())[:,:,:,:-1]
            return [data[idx] for idx, _ in enumerate(data) if self.mask[idx] == 0]
        return self.timelapse

    def make_video(self, filename='timelapse.avi', fps=2, is_color=True, n_repeat=1):
        """
        Creates and saves an AVI video from timelapse into ``timelapse.avi``
        :param fps: frames per second
        :type param: int
        :param is_color:
        :type is_color: bool
        """

        images = np.array([image[:,:,[2,1,0]] for image in self._get_timelapse_images()])

        if None in self.full_size:
            self.full_size = (int(images.shape[2]),int(images.shape[1]))

        fourcc = cv2.VideoWriter_fourcc(*"mp4v")
        video = cv2.VideoWriter(os.path.join(self.project_name, filename), fourcc, float(fps), self.full_size,
                                is_color)
                
        for _ in range(n_repeat):
            for image in images:
                video.write(image)
        video.write(images[-1])

        video.release()
        cv2.destroyAllWindows()

    def make_gif(self, filename='timelapse.gif', fps=3):
        """
        Creates and saves a GIF animation from timelapse into ``timelapse.gif``
        :param fps: frames per second
        :type fps: int
        """

        frames = []
        for image in self._get_timelapse_images():
            frames.append(Image.fromarray(image))

        frames[0].save(os.path.join(self.project_name, filename), save_all=True, append_images=frames[1:], fps=fps, loop=True, optimize=False)
コード例 #6
0
ファイル: test_ogc.py プロジェクト: hectare/sentinelhub-py
    def setUpClass(cls):
        wgs84_bbox = BBox(bbox=(-5.23, 48.0, -5.03, 48.17), crs=CRS.WGS84)
        wgs84_bbox_2 = BBox(bbox=(21.3, 64.0, 22.0, 64.5), crs=CRS.WGS84)
        wgs84_bbox_3 = BBox(bbox=(-72.0, -70.4, -71.8, -70.2), crs=CRS.WGS84)
        pop_web_bbox = BBox(bbox=(1292344.0, 5195920.0, 1310615.0, 5214191.0),
                            crs=CRS.POP_WEB)
        geometry_wkt = 'POLYGON((1292344.0 5205055.5, 1301479.5 5195920.0, 1310615.0 5205055.5, ' \
                       '1301479.5 5214191.0, 1292344.0 5205055.5))'
        img_width = 100
        img_height = 100
        resx = '53m'
        resy = '78m'
        expected_date = datetime.datetime.strptime('2017-10-07T11:20:58',
                                                   '%Y-%m-%dT%H:%M:%S')

        cls.test_cases = [
            cls.OgcTestCase('generalWmsTest',
                            OgcRequest(
                                data_folder=cls.OUTPUT_FOLDER,
                                image_format=MimeType.TIFF_d32f,
                                bbox=wgs84_bbox,
                                layer='BANDS-S2-L1C',
                                maxcc=0.5,
                                size_x=img_width,
                                size_y=img_height,
                                time=('2017-01-01', '2018-01-01'),
                                instance_id=cls.INSTANCE_ID,
                                service_type=ServiceType.WMS,
                                time_difference=datetime.timedelta(days=10)),
                            result_len=14,
                            img_min=0.0,
                            img_max=1.5964,
                            img_mean=0.1810,
                            img_median=0.1140,
                            tile_num=29,
                            save_data=True,
                            data_filter=[0, -2, 0]),
            cls.OgcTestCase('generalWcsTest',
                            OgcRequest(
                                data_folder=cls.OUTPUT_FOLDER,
                                image_format=MimeType.TIFF_d32f,
                                bbox=wgs84_bbox,
                                layer='BANDS-S2-L1C',
                                maxcc=0.6,
                                size_x=resx,
                                size_y=resy,
                                time=('2017-10-01', '2018-01-01'),
                                instance_id=cls.INSTANCE_ID,
                                service_type=ServiceType.WCS,
                                time_difference=datetime.timedelta(days=5)),
                            result_len=5,
                            img_min=0.0002,
                            img_max=0.5266,
                            img_mean=0.1038,
                            img_median=0.0948,
                            tile_num=9,
                            date_check=expected_date,
                            save_data=True,
                            data_filter=[0, -1]),
            # CustomUrlParam tests:
            cls.OgcTestCase('customUrlAtmcorQualitySampling',
                            WmsRequest(data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.PNG,
                                       layer='TRUE-COLOR-S2-L1C',
                                       width=img_width,
                                       bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02'),
                                       custom_url_params={
                                           CustomUrlParam.SHOWLOGO: True,
                                           CustomUrlParam.ATMFILTER: 'ATMCOR',
                                           CustomUrlParam.QUALITY: 100,
                                           CustomUrlParam.DOWNSAMPLING:
                                           'BICUBIC',
                                           CustomUrlParam.UPSAMPLING: 'BICUBIC'
                                       }),
                            result_len=1,
                            img_min=11,
                            img_max=255,
                            img_mean=193.796,
                            img_median=206,
                            tile_num=2,
                            data_filter=[0, -1]),
            cls.OgcTestCase('customUrlPreview',
                            WmsRequest(data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.PNG,
                                       layer='TRUE-COLOR-S2-L1C',
                                       height=img_height,
                                       bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02'),
                                       custom_url_params={
                                           CustomUrlParam.SHOWLOGO: True,
                                           CustomUrlParam.PREVIEW: 2
                                       }),
                            result_len=1,
                            img_min=27,
                            img_max=253,
                            img_mean=176.732,
                            img_median=177,
                            tile_num=2),
            cls.OgcTestCase(
                'customUrlEvalscripturl',
                WcsRequest(
                    data_folder=cls.OUTPUT_FOLDER,
                    image_format=MimeType.PNG,
                    layer='TRUE-COLOR-S2-L1C',
                    resx=resx,
                    resy=resy,
                    bbox=pop_web_bbox,
                    instance_id=cls.INSTANCE_ID,
                    time=('2017-10-01', '2017-10-02'),
                    custom_url_params={
                        CustomUrlParam.SHOWLOGO:
                        True,
                        CustomUrlParam.EVALSCRIPTURL:
                        'https://raw.githubusercontent.com/sentinel-hub/'
                        'customScripts/master/sentinel-2/false_color_infrared/'
                        'script.js'
                    }),
                result_len=1,
                img_min=41,
                img_max=255,
                img_mean=230.568,
                img_median=255,
                tile_num=3),
            cls.OgcTestCase('customUrlEvalscript,Transparent',
                            WcsRequest(data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.PNG,
                                       layer='TRUE-COLOR-S2-L1C',
                                       resx=resx,
                                       resy=resy,
                                       bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02'),
                                       custom_url_params={
                                           CustomUrlParam.SHOWLOGO:
                                           True,
                                           CustomUrlParam.TRANSPARENT:
                                           True,
                                           CustomUrlParam.EVALSCRIPT:
                                           'return [B10,B8A, B03 ]'
                                       }),
                            result_len=1,
                            img_min=0,
                            img_max=255,
                            img_mean=100.1543,
                            img_median=68.0,
                            tile_num=2),
            cls.OgcTestCase('FalseLogo,BgColor,Geometry',
                            WmsRequest(data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.PNG,
                                       layer='TRUE-COLOR-S2-L1C',
                                       width=img_width,
                                       height=img_height,
                                       bbox=pop_web_bbox,
                                       instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02'),
                                       custom_url_params={
                                           CustomUrlParam.SHOWLOGO: False,
                                           CustomUrlParam.BGCOLOR: "F4F86A",
                                           CustomUrlParam.GEOMETRY:
                                           geometry_wkt
                                       }),
                            result_len=1,
                            img_min=63,
                            img_max=255,
                            img_mean=213.3590,
                            img_median=242.0,
                            tile_num=3),

            # DataSource tests:
            cls.OgcTestCase('S2 L1C Test',
                            WmsRequest(data_source=DataSource.SENTINEL2_L1C,
                                       data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d8,
                                       layer='BANDS-S2-L1C',
                                       width=img_width,
                                       height=img_height,
                                       bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02')),
                            result_len=1,
                            img_min=38,
                            img_max=152,
                            img_mean=84.6465,
                            img_median=84.0,
                            tile_num=2),
            cls.OgcTestCase('S2 L2A Test',
                            WmsRequest(data_source=DataSource.SENTINEL2_L2A,
                                       data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF,
                                       layer='BANDS-S2-L2A',
                                       width=img_width,
                                       height=img_height,
                                       bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02')),
                            result_len=1,
                            img_min=0.0,
                            img_max=65535,
                            img_mean=22743.5164,
                            img_median=21390.0,
                            tile_num=2),
            cls.OgcTestCase('L8 Test',
                            WmsRequest(
                                data_source=DataSource.LANDSAT8,
                                data_folder=cls.OUTPUT_FOLDER,
                                image_format=MimeType.TIFF_d32f,
                                layer='BANDS-L8',
                                width=img_width,
                                height=img_height,
                                bbox=wgs84_bbox,
                                instance_id=cls.INSTANCE_ID,
                                time=('2017-10-05', '2017-10-10'),
                                time_difference=datetime.timedelta(hours=1)),
                            result_len=1,
                            img_min=0.0011,
                            img_max=285.72415,
                            img_mean=52.06075,
                            img_median=0.5192,
                            tile_num=2),
            cls.OgcTestCase('DEM Test',
                            WmsRequest(data_source=DataSource.DEM,
                                       data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f,
                                       layer='DEM',
                                       width=img_width,
                                       height=img_height,
                                       bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID),
                            result_len=1,
                            img_min=-108.0,
                            img_max=-18.0,
                            img_mean=-72.1819,
                            img_median=-72.0),
            cls.OgcTestCase('MODIS Test',
                            WmsRequest(data_source=DataSource.MODIS,
                                       data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f,
                                       layer='BANDS-MODIS',
                                       width=img_width,
                                       height=img_height,
                                       bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID,
                                       time='2017-10-01'),
                            result_len=1,
                            img_min=0.0,
                            img_max=3.2767,
                            img_mean=0.136408,
                            img_median=0.00240,
                            tile_num=1),
            cls.OgcTestCase('S1 IW Test',
                            WmsRequest(
                                data_source=DataSource.SENTINEL1_IW,
                                data_folder=cls.OUTPUT_FOLDER,
                                image_format=MimeType.TIFF_d32f,
                                layer='BANDS-S1-IW',
                                width=img_width,
                                height=img_height,
                                bbox=wgs84_bbox,
                                instance_id=cls.INSTANCE_ID,
                                time=('2017-10-01', '2017-10-02'),
                                time_difference=datetime.timedelta(hours=1)),
                            result_len=1,
                            img_min=0.0,
                            img_max=1.0,
                            img_mean=0.104584,
                            img_median=0.06160,
                            tile_num=2),
            cls.OgcTestCase('S1 EW Test',
                            WmsRequest(
                                data_source=DataSource.SENTINEL1_EW,
                                data_folder=cls.OUTPUT_FOLDER,
                                image_format=MimeType.TIFF_d32f,
                                layer='BANDS-S1-EW',
                                width=img_width,
                                height=img_height,
                                bbox=wgs84_bbox_2,
                                instance_id=cls.INSTANCE_ID,
                                time=('2018-2-7', '2018-2-8'),
                                time_difference=datetime.timedelta(hours=1)),
                            result_len=2,
                            img_min=0.0003,
                            img_max=1.0,
                            img_mean=0.53118,
                            img_median=1.0,
                            tile_num=3),
            cls.OgcTestCase(
                'S1 EW SH Test',
                WmsRequest(data_source=DataSource.SENTINEL1_EW_SH,
                           data_folder=cls.OUTPUT_FOLDER,
                           image_format=MimeType.TIFF_d16,
                           layer='BANDS-S1-EW-SH',
                           width=img_width,
                           height=img_height,
                           bbox=wgs84_bbox_3,
                           custom_url_params={CustomUrlParam.SHOWLOGO: True},
                           instance_id=cls.INSTANCE_ID,
                           time=('2018-2-6', '2018-2-8'),
                           time_difference=datetime.timedelta(hours=1)),
                result_len=1,
                img_min=465,
                img_max=59287,
                img_mean=5323.0523,
                img_median=943.0,
                tile_num=1)
        ]
        """
        cls.test_cases.extend([
            cls.OgcTestCase('EOCloud S1 IW Test',
                            WmsRequest(data_source=DataSource.SENTINEL1_IW, data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f, layer='BANDS_S1_IW',
                                       width=img_width, height=img_height, bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID, time=('2017-10-01', '2017-10-02'),
                                       time_difference=datetime.timedelta(hours=1)),
                            result_len=1, img_min=0.0, img_max=0.49706, img_mean=0.04082, img_median=0.00607,
                            tile_num=2),
        ])
        """

        for test_case in cls.test_cases:
            test_case.collect_data()
コード例 #7
0
    def __init__(
            self,
            project_name,
            bbox=None,
            time_interval=None,
            new=True,
            clean=False,
            instance_id='',
            full_res=('10m', '10m'),
            preview_res=('60m', '60m'),
            cloud_mask_res=('60m', '60m'),
            full_size=(1920, 1080),
            preview_size=(600, None),
            use_atmcor=False,
            layer='TRUE-COLOR-S2-L1C',
            custom_script='return [B01,B02,B04,B05,B08,B8A,B09,B10,B11,B12]',
            time_difference=datetime.timedelta(hours=2),
            pix_based=False):

        self.project_name = project_name
        self.preview_folder = os.path.join(project_name, 'data', 'previews')
        self.data_folder = os.path.join(project_name, 'data', 'full_res')
        self.mask_folder = os.path.join(project_name, 'data', 'custom')
        self.cloud_masks = None
        self.cloud_coverage = None
        self.full_res_data = None
        self.previews = None
        self.full_res = full_res
        self.timelapse = None

        if clean:
            self.clean_all()

        if not new:
            return

        if pix_based:
            self.preview_request = WcsRequest(
                data_folder=self.preview_folder,
                layer=layer,
                bbox=bbox,
                time=time_interval,
                resx=preview_res[0],
                resy=preview_res[1],
                maxcc=1.0,
                image_format=MimeType.PNG,
                instance_id=instance_id,
                custom_url_params={CustomUrlParam.TRANSPARENT: True},
                time_difference=time_difference)

            self.fullres_request = WcsRequest(
                data_folder=self.data_folder,
                layer=layer,
                bbox=bbox,
                time=time_interval,
                resx=full_res[0],
                resy=full_res[1],
                maxcc=1.0,
                image_format=MimeType.PNG,
                instance_id=instance_id,
                custom_url_params={
                    CustomUrlParam.TRANSPARENT: True,
                    CustomUrlParam.ATMFILTER: 'ATMCOR'
                } if use_atmcor else {CustomUrlParam.TRANSPARENT: True},
                time_difference=time_difference)
            self.custom_request = WcsRequest(data_folder=self.mask_folder,
                                             layer=layer,
                                             bbox=bbox,
                                             time=time_interval,
                                             resx=cloud_mask_res[0],
                                             resy=cloud_mask_res[1],
                                             maxcc=1.0,
                                             image_format=MimeType.TIFF_d32f,
                                             instance_id=instance_id,
                                             time_difference=time_difference,
                                             custom_url_params={
                                                 CustomUrlParam.EVALSCRIPT:
                                                 custom_script,
                                                 CustomUrlParam.ATMFILTER:
                                                 'NONE'
                                             })
        else:
            self.preview_request = WmsRequest(
                data_folder=self.preview_folder,
                layer=layer,
                bbox=bbox,
                time=time_interval,
                width=preview_size[0],
                height=preview_size[1],
                maxcc=1.0,
                image_format=MimeType.PNG,
                instance_id=instance_id,
                custom_url_params={CustomUrlParam.TRANSPARENT: True},
                time_difference=time_difference)

            self.fullres_request = WmsRequest(
                data_folder=self.data_folder,
                layer=layer,
                bbox=bbox,
                time=time_interval,
                width=full_size[0],
                height=full_size[1],
                maxcc=1.0,
                image_format=MimeType.PNG,
                instance_id=instance_id,
                custom_url_params={
                    CustomUrlParam.TRANSPARENT: True,
                    CustomUrlParam.ATMFILTER: 'ATMCOR'
                } if use_atmcor else {CustomUrlParam.TRANSPARENT: True},
                time_difference=time_difference)

            self.custom_request = WmsRequest(data_folder=self.mask_folder,
                                             layer=layer,
                                             bbox=bbox,
                                             time=time_interval,
                                             width=preview_size[0],
                                             height=preview_size[1],
                                             maxcc=1.0,
                                             image_format=MimeType.TIFF_d32f,
                                             instance_id=instance_id,
                                             time_difference=time_difference,
                                             custom_url_params={
                                                 CustomUrlParam.EVALSCRIPT:
                                                 custom_script,
                                                 CustomUrlParam.ATMFILTER:
                                                 'NONE'
                                             })

        self.cloud_mask_request = None  # CloudMaskRequest(wcs_request)

        self.transparency_data = None
        self.preview_transparency_data = None
        self.invalid_coverage = None

        self.dates = self.preview_request.get_dates()

        if not self.dates:
            raise ValueError(
                'Input parameters are not valid. No Sentinel 2 image is found.'
            )

        if self.dates != self.fullres_request.get_dates():
            raise ValueError(
                'Lists of previews and full resolution images do not match.')

        # if self.dates != self.cloud_mask_request.get_dates():
        #     raise ValueError('List of previews and cloud masks do not match.')
        self.dates = np.array(self.dates)
        self.mask = np.zeros((len(self.dates), ), dtype=np.uint8)

        LOGGER.info('Found %d images of %s between %s and %s.',
                    len(self.dates), project_name, time_interval[0],
                    time_interval[1])

        LOGGER.info(
            '\nI suggest you start by downloading previews first to see,\n'
            'if BBOX is OK, images are usefull, etc...\n'
            'Execute get_previews() method on your object.\n')
コード例 #8
0
    def setUpClass(cls):
        wgs84_bbox = BBox(bbox=(48.0, -5.23, 48.17, -5.03), crs=CRS.WGS84)
        wgs84_bbox_2 = BBox(bbox=(64.0, 21.3, 64.5, 22.0), crs=CRS.WGS84)
        wgs84_bbox_3 = BBox(bbox=(-70.4, -72.0, -70.2, -71.8), crs=CRS.WGS84)
        pop_web_bbox = BBox(bbox=(1292344.0, 5195920.0, 1310615.0, 5214191.0), crs=CRS.POP_WEB)
        img_width = 100
        img_height = 100
        resx='53m'
        resy='78m'
        expected_date = datetime.datetime.strptime('2017-10-07T11:20:58', '%Y-%m-%dT%H:%M:%S')

        cls.test_cases = [
            cls.OgcTestCase('generalWmsTest',
                            OgcRequest(data_folder=cls.OUTPUT_FOLDER, image_format=MimeType.TIFF_d32f, bbox=wgs84_bbox,
                                       layer='ALL_BANDS', maxcc=0.5, size_x=img_width, size_y=img_height,
                                       time=('2017-10-01', '2018-01-01'), instance_id=cls.INSTANCE_ID,
                                       service_type=ServiceType.WMS, time_difference=datetime.timedelta(days=10)),
                            result_len=3, img_min=0.0, img_max=0.4544, img_mean=0.1038, img_median=0.0945,
                            date_check=expected_date, save_data=True),
            cls.OgcTestCase('generalWcsTest',
                            OgcRequest(data_folder=cls.OUTPUT_FOLDER, image_format=MimeType.TIFF_d32f, bbox=wgs84_bbox,
                                       layer='ALL_BANDS', maxcc=0.6, size_x=resx, size_y=resy,
                                       time=('2017-10-01', '2018-01-01'), instance_id=cls.INSTANCE_ID,
                                       service_type=ServiceType.WCS, time_difference=datetime.timedelta(days=5)),
                            result_len=5, img_min=0.0002, img_max=0.5266, img_mean=0.1038, img_median=0.0948,
                            date_check=expected_date, save_data=True),

            # CustomUrlParam tests:
            cls.OgcTestCase('customUrlAtmcorQualitySampling',
                            WmsRequest(data_folder=cls.OUTPUT_FOLDER, image_format=MimeType.PNG, layer='TRUE_COLOR',
                                       width=img_width, bbox=wgs84_bbox, instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02'),
                                       custom_url_params={CustomUrlParam.ATMFILTER: 'ATMCOR',
                                                          CustomUrlParam.QUALITY: 100,
                                                          CustomUrlParam.DOWNSAMPLING: 'BICUBIC',
                                                          CustomUrlParam.UPSAMPLING: 'BICUBIC'}),
                            result_len=1, img_min=11, img_max=255, img_mean=193.796, img_median=206),
            cls.OgcTestCase('customUrlPreview',
                            WmsRequest(data_folder=cls.OUTPUT_FOLDER, image_format=MimeType.PNG, layer='TRUE_COLOR',
                                       height=img_height, bbox=wgs84_bbox, instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02'),
                                       custom_url_params={CustomUrlParam.PREVIEW: 2}),
                            result_len=1, img_min=27, img_max=253, img_mean=176.732, img_median=177),
            cls.OgcTestCase('customUrlEvalscripturl',
                            WcsRequest(data_folder=cls.OUTPUT_FOLDER, image_format=MimeType.PNG, layer='TRUE_COLOR',
                                       resx=resx, resy=resy, bbox=pop_web_bbox, instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02'),
                                       custom_url_params={CustomUrlParam.EVALSCRIPTURL:
                                                          'https://raw.githubusercontent.com/sentinel-hub/'
                                                          'customScripts/master/sentinel-2/false_color_infrared/'
                                                          'script.js'}),
                            result_len=1, img_min=41, img_max=255, img_mean=230.568, img_median=255),
            cls.OgcTestCase('customUrlEvalscript',
                            WcsRequest(data_folder=cls.OUTPUT_FOLDER, image_format=MimeType.PNG, layer='TRUE_COLOR',
                                       resx=resx, resy=resy, bbox=wgs84_bbox, instance_id=cls.INSTANCE_ID,
                                       time=('2017-10-01', '2017-10-02'),
                                       custom_url_params={CustomUrlParam.EVALSCRIPT: 'return [B10,B8A, B03 ]'}),
                            result_len=1, img_min=0, img_max=235, img_mean=48.539, img_median=55),
            cls.OgcTestCase('customUrlLogoTransparent',
                            WmsRequest(data_folder=cls.OUTPUT_FOLDER, image_format=MimeType.PNG, layer='TRUE_COLOR',
                                       width=img_width, height=img_height, bbox=pop_web_bbox,
                                       instance_id=cls.INSTANCE_ID, time=('2017-10-01', '2017-10-02'),
                                       custom_url_params={CustomUrlParam.SHOWLOGO: True,
                                                          CustomUrlParam.TRANSPARENT: True}),
                            result_len=1, img_min=47, img_max=255, img_mean=229.3749, img_median=242.0),

            # DataSource tests:
            cls.OgcTestCase('S2 L1C Test',
                            WmsRequest(data_source=DataSource.SENTINEL2_L1C, data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f, layer='BANDS-S2-L1C',
                                       width=img_width, height=img_height, bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID, time=('2017-10-01', '2017-10-02')),
                            result_len=1, img_min=0.00089, img_max=0.6284, img_mean=0.2373, img_median=0.2477),
            cls.OgcTestCase('S2 L2A Test',
                            WmsRequest(data_source=DataSource.SENTINEL2_L2A, data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f, layer='BANDS-S2-L2A',
                                       width=img_width, height=img_height, bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID, time=('2017-10-01', '2017-10-02')),
                            result_len=1, img_min=0.0, img_max=1.6720, img_mean=0.34747, img_median=0.32640),
            cls.OgcTestCase('L8 Test',
                            WmsRequest(data_source=DataSource.LANDSAT8, data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f, layer='BANDS-L8',
                                       width=img_width, height=img_height, bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID, time=('2017-10-05', '2017-10-10'),
                                       time_difference=datetime.timedelta(hours=1)),
                            result_len=1, img_min=0.0011, img_max=285.72415, img_mean=52.06075, img_median=0.5192),
            cls.OgcTestCase('DEM Test',
                            WmsRequest(data_source=DataSource.DEM, data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f, layer='DEM',
                                       width=img_width, height=img_height, bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID),
                            result_len=1, img_min=-108.0, img_max=-18.0, img_mean=-72.1819, img_median=-72.0),
            cls.OgcTestCase('MODIS Test',
                            WmsRequest(data_source=DataSource.MODIS, data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f, layer='BANDS-MODIS',
                                       width=img_width, height=img_height, bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID, time='2017-10-01'),
                            result_len=1, img_min=0.0, img_max=3.2767, img_mean=0.136408, img_median=0.00240),
            cls.OgcTestCase('S1 IW Test',
                            WmsRequest(data_source=DataSource.SENTINEL1_IW, data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f, layer='BANDS-S1-IW',
                                       width=img_width, height=img_height, bbox=wgs84_bbox,
                                       instance_id=cls.INSTANCE_ID, time=('2017-10-01', '2017-10-02'),
                                       time_difference=datetime.timedelta(hours=1)),
                            result_len=1, img_min=0.0, img_max=1.0, img_mean=0.104584, img_median=0.06160),
            cls.OgcTestCase('S1 EW Test',
                            WmsRequest(data_source=DataSource.SENTINEL1_EW, data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f, layer='BANDS-S1-EW',
                                       width=img_width, height=img_height, bbox=wgs84_bbox_2,
                                       instance_id=cls.INSTANCE_ID, time=('2018-2-7', '2018-2-8'),
                                       time_difference=datetime.timedelta(hours=1)),
                            result_len=2, img_min=0.0003, img_max=1.0, img_mean=0.53118, img_median=1.0),
            cls.OgcTestCase('S1 EW SH Test',
                            WmsRequest(data_source=DataSource.SENTINEL1_EW_SH,
                                       data_folder=cls.OUTPUT_FOLDER,
                                       image_format=MimeType.TIFF_d32f, layer='BANDS-S1-EW-SH',
                                       width=img_width, height=img_height, bbox=wgs84_bbox_3,
                                       instance_id=cls.INSTANCE_ID, time=('2018-2-6', '2018-2-8'),
                                       time_difference=datetime.timedelta(hours=1)),
                            result_len=1, img_min=0.006987, img_max=0.83078, img_mean=0.06599, img_median=0.0140)
        ]

        for test_case in cls.test_cases:
            test_case.collect_data()