Exemple #1
0
    def map_dataset(self, collection, dataset_items, **kwargs) -> OrderedDict:
        from osgeo import gdal
        from datetime import datetime
        import stac2odc.environment as environment

        if kwargs['verbose']:
            logger.info("item2dataset is running!")

        crs_proj4 = collection['properties']['bdc:crs']
        if kwargs['is_pre_collection']:
            crs_proj4 = utils.fix_precollection_crs(crs_proj4)

        odc_items = []
        for f in dataset_items:
            try:
                _startdate, _enddate = utils.stacdate_to_odcdate(f['id'])
            except:
                _tmp = f['properties']['datetime']
                _startdate, _enddate = _tmp, _tmp

            _featureid = utils.generate_id(f)
            if kwargs['verbose']:
                logger.info(f"New item found: {_featureid}")

            feature = OrderedDict()
            feature['id'] = _featureid
            feature['creation_dt'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%fZ")
            feature['product_type'] = self.generate_product_type(collection)
            feature['platform'] = {'code': kwargs['plataform_code']}
            feature['instrument'] = {'name': kwargs['instrument_type']}
            feature['format'] = {'name': kwargs['format_name']}
            feature['lineage'] = {'source_datasets': {}}

            feature['extent'] = OrderedDict()
            feature['extent']['coord'] = OrderedDict()
            feature['extent']['coord'] = utils.geometry_coordinates(f)
            feature['extent']['from_dt'] = _startdate
            feature['extent']['center_dt'] = _startdate  # ToDo: Verify this
            feature['extent']['to_dt'] = _enddate

            # verify if necessary download data
            if kwargs['download']:
                logger.info(f"Downloading item: {_featureid}")
                environment.download_stac_tree(f, **kwargs)
                kwargs['basepath'] = kwargs['download_out']

            # Extract image bbox
            first_band = next(iter(collection['properties']['bdc:bands']))
            first_band_path = utils.href_to_path(
                f['assets'][first_band]['href'], kwargs['basepath'])

            # build grid_spatial
            lrx, lry, ulx, uly = utils.raster_bounds(first_band_path)
            feature['grid_spatial'] = OrderedDict()
            feature['grid_spatial']['projection'] = OrderedDict()
            feature['grid_spatial']['projection']['geo_ref_points'] = OrderedDict()
            feature['grid_spatial']['projection']['geo_ref_points']['ul'] = {
                'x': ulx, 'y': uly}
            feature['grid_spatial']['projection']['geo_ref_points']['ur'] = {
                'x': lrx, 'y': uly}
            feature['grid_spatial']['projection']['geo_ref_points']['lr'] = {
                'x': lrx, 'y': lry}
            feature['grid_spatial']['projection']['geo_ref_points']['ll'] = {
                'x': ulx, 'y': lry}

            feature['grid_spatial']['projection']['spatial_reference'] = utils.to_wkt(crs_proj4)
            feature['image'] = OrderedDict()
            feature['image']['bands'] = OrderedDict()
            band_counter = 1
            for band in collection['properties']['bdc:bands'].keys():
                if band not in kwargs['ignore']:
                    if band in f['assets']:
                        feature['image']['bands'][band] = OrderedDict()
                        feature['image']['bands'][band]['path'] = utils.href_to_path(
                            f['assets'][band]['href'], kwargs['basepath'])
                        feature['image']['bands'][band]['layer'] = 1
                        band_counter += 1
                    else:
                        logger.info("Band '{}' was not found in asset '{}'".format(
                            band, f['id']))
            odc_items.append(feature)
        return odc_items
Exemple #2
0
    def map_dataset(self, collection, dataset_items, **kwargs) -> OrderedDict:
        from datetime import datetime
        import stac2odc.environment as environment

        if kwargs['verbose']:
            logger.info("item2dataset is running!")

        crs_proj4 = collection['cube:dimensions']['x']['reference_system']
        if kwargs['is_pre_collection']:
            crs_proj4 = utils.fix_precollection_crs(crs_proj4)

        odc_items = []
        for f in dataset_items:
            try:
                _startdate, _enddate = utils.stacdate_to_odcdate(f['id'])
            except:
                _tmp = f['properties']['datetime']
                _startdate, _enddate = _tmp, _tmp

            _featureid = utils.generate_id(f)
            if kwargs['verbose']:
                logger.info(f"New item found: {_featureid}")

            feature = OrderedDict()
            feature['id'] = _featureid
            feature['creation_dt'] = datetime.now().strftime(
                "%Y-%m-%d %H:%M:%S.%fZ")
            feature['product_type'] = self.generate_product_type(collection)
            feature['platform'] = {'code': kwargs['plataform_code']}
            feature['instrument'] = {'name': kwargs['instrument_type']}
            feature['format'] = {'name': kwargs['format_name']}
            feature['lineage'] = {'source_datasets': {}}

            feature['extent'] = OrderedDict()
            feature['extent']['coord'] = OrderedDict()
            # Using extent.coord from file metadata because BDC-STAC is updating this data
            # feature['extent']['coord'] = utils.geometry_coordinates(f)
            feature['extent']['from_dt'] = _startdate
            feature['extent']['center_dt'] = _startdate  # ToDo: Verify this
            feature['extent']['to_dt'] = _enddate

            # verify if necessary download data
            if kwargs['download']:
                logger.info(f"Downloading item: {_featureid}")
                environment.download_stac_tree(f, **kwargs)
                kwargs['basepath'] = kwargs['download_out']

            # Extract image bbox
            first_band = next(iter(
                collection['properties']['eo:bands']))['name']
            first_band_path = utils.href_to_path(
                f['assets'][first_band]['href'], kwargs['basepath'])

            # build grid_spatial
            lrx, lry, ulx, uly = utils.raster_bounds(first_band_path)
            feature['grid_spatial'] = OrderedDict()
            feature['grid_spatial']['projection'] = OrderedDict()
            feature['grid_spatial']['projection'][
                'geo_ref_points'] = OrderedDict()
            feature['grid_spatial']['projection']['geo_ref_points']['ul'] = {
                'x': ulx,
                'y': uly
            }
            feature['grid_spatial']['projection']['geo_ref_points']['ur'] = {
                'x': lrx,
                'y': uly
            }
            feature['grid_spatial']['projection']['geo_ref_points']['lr'] = {
                'x': lrx,
                'y': lry
            }
            feature['grid_spatial']['projection']['geo_ref_points']['ll'] = {
                'x': ulx,
                'y': lry
            }

            feature['grid_spatial']['projection'][
                'spatial_reference'] = utils.to_wkt(crs_proj4)

            # Temporary convertion. Remove after BDC-STAC coordinates update
            # Converting grid to EPSG:4326
            in_spatial_ref = osr.SpatialReference()
            in_spatial_ref.ImportFromProj4(crs_proj4)
            out_spatial_ref = osr.SpatialReference()
            out_spatial_ref.ImportFromEPSG(4326)

            feature['extent']['coord'] = utils.convert_coords_xy(
                feature['grid_spatial']['projection']['geo_ref_points'],
                in_spatial_ref, out_spatial_ref)

            feature['image'] = OrderedDict()
            feature['image']['bands'] = OrderedDict()
            band_counter = 1
            bands = [b['name'] for b in collection['properties']['eo:bands']]
            for band in bands:
                if band not in kwargs['ignore']:
                    if band in f['assets']:
                        feature['image']['bands'][band] = OrderedDict()
                        feature['image']['bands'][band][
                            'path'] = utils.href_to_path(
                                f['assets'][band]['href'], kwargs['basepath'])
                        feature['image']['bands'][band]['layer'] = 1
                        band_counter += 1
                    else:
                        logger.info(
                            "Band '{}' was not found in asset '{}'".format(
                                band, f['id']))
            odc_items.append(feature)
        return odc_items