mirror of
https://github.com/heyarne/earth-observation-for-journalism.git
synced 2026-05-06 19:13:40 +02:00
Add option to return a rasterized cloud mask
This commit is contained in:
parent
17b250815b
commit
8af42f6091
1 changed files with 26 additions and 3 deletions
|
|
@ -2,10 +2,12 @@ import urllib.parse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import fiona
|
import fiona
|
||||||
|
import folium
|
||||||
import geopandas as gpd
|
import geopandas as gpd
|
||||||
from matplotlib import pyplot as plt
|
from matplotlib import pyplot as plt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import rasterio as r
|
import rasterio as r
|
||||||
|
from rasterio.features import geometry_mask
|
||||||
from rasterio.warp import calculate_default_transform, reproject, Resampling
|
from rasterio.warp import calculate_default_transform, reproject, Resampling
|
||||||
|
|
||||||
from shapely.geometry import shape
|
from shapely.geometry import shape
|
||||||
|
|
@ -109,11 +111,17 @@ def scihub_bgr_paths(product_path, resolution=None):
|
||||||
return scihub_band_paths(product_path, ['B02', 'B03', 'B04'], resolution)
|
return scihub_band_paths(product_path, ['B02', 'B03', 'B04'], resolution)
|
||||||
|
|
||||||
|
|
||||||
def scihub_cloud_mask(product_path):
|
def scihub_cloud_mask(product_path, **kwargs):
|
||||||
'''
|
'''
|
||||||
Given a `product_path` pointing to a product downlaoded from the Copernicus
|
Given a `product_path` pointing to a product downlaoded from the Copernicus
|
||||||
Open Access Hub, returns a shapely geometry representing the included cloud
|
Open Access Hub, returns a shapely geometry representing the included cloud
|
||||||
mask.
|
mask.
|
||||||
|
|
||||||
|
If an additional parameter, `rasterize=True` is given, the returned cloud
|
||||||
|
mask will be a rasterized numpy ndarray instead of a vector geometry. Two
|
||||||
|
additional parameters, `target_path` and `target_transform` are needed to
|
||||||
|
determine the size of this array. In this array, pixels with clouds are
|
||||||
|
`False` and pixels without clouds are `True`.
|
||||||
'''
|
'''
|
||||||
with TemporaryDirectory() as tmp_dir:
|
with TemporaryDirectory() as tmp_dir:
|
||||||
# we need the temporary directory to work around a problem with reading
|
# we need the temporary directory to work around a problem with reading
|
||||||
|
|
@ -137,10 +145,25 @@ def scihub_cloud_mask(product_path):
|
||||||
warnings.simplefilter("ignore")
|
warnings.simplefilter("ignore")
|
||||||
# this returns a warning because the iterator has to be
|
# this returns a warning because the iterator has to be
|
||||||
# rewound; while this is a performance issue, we can ignore it
|
# rewound; while this is a performance issue, we can ignore it
|
||||||
return unary_union([shape(f['geometry']) for f in features])
|
mask = unary_union([shape(f['geometry']) for f in features])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# empty cloud mask
|
# empty cloud mask
|
||||||
return Polygon([])
|
mask = Polygon([])
|
||||||
|
|
||||||
|
if kwargs.get('rasterize'):
|
||||||
|
# return raster version of the vector geometry we found above
|
||||||
|
target_shape = kwargs.get('target_shape')
|
||||||
|
target_transform = kwargs.get('target_transform')
|
||||||
|
if not target_transform or not target_shape:
|
||||||
|
error_msg = 'target_transform and target_shape need to be set ' + \
|
||||||
|
'to construct a rasterized cloud mask.'
|
||||||
|
raise ValueError(error_msg)
|
||||||
|
|
||||||
|
return geometry_mask(mask,
|
||||||
|
out_shape=target_shape,
|
||||||
|
transform=target_transform)
|
||||||
|
else:
|
||||||
|
return mask
|
||||||
|
|
||||||
|
|
||||||
def scihub_normalize_range(v):
|
def scihub_normalize_range(v):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue