def main(ctx, ndvi_threshold, ndwi_threshold, pre_event, post_event): dump(ctx) os.environ["PREFIX"] = "/opt/anaconda/envs/env_burned_area" os.environ["PROJ_LIB"] = os.path.join(os.environ["PREFIX"], "share/proj") os.environ["GDAL_DATA"] = os.path.join(os.environ["PREFIX"], "share/gdal") burned_area_item = burned( pre_item=get_item(os.path.join(pre_event, "catalog.json")), post_item=get_item(os.path.join(post_event, "catalog.json")), ndvi_threshold=ndvi_threshold, ndwi_threshold=ndwi_threshold, ) logging.info("Output catalog") catalog = Catalog(id="catalog", description="Results") catalog.clear_items() catalog.clear_children() catalog.add_items([burned_area_item]) catalog.describe() catalog.normalize_and_save(root_href="./", catalog_type=CatalogType.SELF_CONTAINED)
def test_clear_children_sets_parent_and_root_to_None(self): catalog = Catalog(id='test', description='test') subcat1 = Catalog(id='subcat', description='test') subcat2 = Catalog(id='subcat2', description='test2') catalog.add_children([subcat1, subcat2]) self.assertIsNotNone(subcat1.get_parent()) self.assertIsNotNone(subcat2.get_parent()) self.assertIsNotNone(subcat1.get_root()) self.assertIsNotNone(subcat2.get_root()) children = list(catalog.get_children()) self.assertEqual(len(children), 2) catalog.clear_children() self.assertIsNone(subcat1.get_parent()) self.assertIsNone(subcat2.get_parent()) self.assertIsNone(subcat1.get_root()) self.assertIsNone(subcat2.get_root())
def test_clear_children_removes_from_cache(self): catalog = Catalog(id='test', description='test') subcat = Catalog(id='subcat', description='test') catalog.add_child(subcat) children = list(catalog.get_children()) self.assertEqual(len(children), 1) self.assertEqual(children[0].description, 'test') catalog.clear_children() subcat = Catalog(id='subcat', description='test2') catalog.add_child(subcat) children = list(catalog.get_children()) self.assertEqual(len(children), 1) self.assertEqual(children[0].description, 'test2') catalog.remove_child('subcat') subcat = Catalog(id='subcat', description='test3') catalog.add_child(subcat) children = list(catalog.get_children()) self.assertEqual(len(children), 1) self.assertEqual(children[0].description, 'test3')
def main(ndvi_threshold, ndwi_threshold, pre_event, post_event): os.environ['PREFIX']='/opt/anaconda/envs/env_burned_area' os.environ['PROJ_LIB'] = os.path.join(os.environ['PREFIX'], 'share/proj') os.environ['GDAL_DATA'] = os.path.join(os.environ['PREFIX'], 'share/gdal') s2_item_pre = S2_stac_item(pre_event['value']) s2_item_post = S2_stac_item(post_event['value']) s2_items = dict() s2_items['pre-event'] = S2_stac_item(pre_event['value']) s2_items['post-event'] = S2_stac_item(post_event['value']) dates = [] bboxes = [] for index, item in enumerate([s2_item_pre.item, s2_item_post.item]): dates.append(item.datetime) bboxes.append(shape(item.geometry).bounds) logging.info('Stacking bands for input {}'.format(item.id)) vrt_bands = [] for band in ['B04', 'B08', 'B11', 'SCL']: vrt_bands.append('/vsicurl/{}'.format(item.assets[band].get_absolute_href())) vrt = '{}.vrt'.format('pre_event' if index == 0 else 'post_event') tif = '{}.tif'.format('pre_event' if index == 0 else 'post_event') logging.info('Build vrt for {}'.format(item.id)) ds = gdal.BuildVRT(vrt, vrt_bands, srcNodata=0, xRes=10, yRes=10, separate=True) ds.FlushCache() logging.info('Translate {}'.format(item.id)) gdal.Translate(tif, vrt, outputType=gdal.GDT_UInt16) os.remove(vrt) ds = gdal.Open('pre_event.tif') pre_b04 = ds.GetRasterBand(1).ReadAsArray() pre_b08 = ds.GetRasterBand(2).ReadAsArray() pre_b11 = ds.GetRasterBand(3).ReadAsArray() pre_scl = ds.GetRasterBand(4).ReadAsArray() ds = None os.remove('pre_event.tif') ds = gdal.Open('post_event.tif') post_b04 = ds.GetRasterBand(1).ReadAsArray() post_b08 = ds.GetRasterBand(2).ReadAsArray() post_b11 = ds.GetRasterBand(3).ReadAsArray() post_scl = ds.GetRasterBand(4).ReadAsArray() width = ds.RasterXSize height = ds.RasterYSize input_geotransform = ds.GetGeoTransform() input_georef = ds.GetProjectionRef() ds = None os.remove('post_event.tif') gain = 10000 pre_ndwi2 = (pre_b08 / gain - pre_b11 / gain) / (pre_b08 / gain + pre_b11 / gain) post_ndwi2 = (post_b08 / gain - post_b11 / gain) / (post_b08 / gain + post_b11 / gain) pre_b11 = None post_b11 = None pre_ndvi = (pre_b08 / gain - pre_b04 / gain) / (pre_b08 / gain + pre_b04 / gain) post_ndvi = (post_b08 / gain - post_b04 / gain) / (post_b08 / gain + post_b04 / gain) pre_b04 = None post_b04 = None pre_b08 = None post_b08 = None conditions = (((post_ndwi2 - pre_ndwi2) > float(ndwi_threshold['value'])) & ((post_ndvi - pre_ndvi) > float(ndvi_threshold['value'])) & (pre_scl == 4) | (post_scl == 4)) burned = np.zeros((height, width), dtype=np.uint8) burned[conditions] = 1 pre_ndwi2 = None post_ndwi2 = None pre_ndvi = None post_ndvi = None burned[np.where((pre_scl == 0) | (post_scl == 0) | (pre_scl == 1) | (post_scl == 1) | (pre_scl == 5) | (post_scl == 5) | (pre_scl == 6) | (post_scl == 6) | (pre_scl == 7) | (post_scl == 7) | (pre_scl == 8) | (post_scl == 8) | (pre_scl == 9) | (post_scl == 9))] = 2 logging.info('Write output product') output_name = 'S2_BURNED_AREA_{}'.format('_'.join([d.strftime("%Y%m%d") for d in dates])) write_tif(burned, '{}.tif'.format(output_name), width, height, input_geotransform, input_georef) logging.info('Output catalog') catalog = Catalog(id='catalog', description='Results') catalog.clear_items() catalog.clear_children() result_titles = dict() result_titles[output_name] = {'title': 'Burned area analysis from Sentinel-2', 'media_type': MediaType.COG} items = [] for key, value in result_titles.items(): result_item = Item(id=key, geometry=s2_items['pre-event'].item.geometry, bbox=s2_items['pre-event'].item.bbox, datetime=s2_items['pre-event'].item.datetime, properties={}) result_item.add_asset(key='data', asset=Asset(href='./{}.tif'.format(key), media_type=value['media_type'], title=value['title'])) items.append(result_item) #collection.add_items(items) catalog.add_items(items) catalog.describe() catalog.normalize_and_save(root_href='./', catalog_type=CatalogType.SELF_CONTAINED) shutil.move('{}.tif'.format(output_name), os.path.join('./', output_name, '{}.tif'.format(output_name)))