asf_tools
v0.8.0 API Reference¶
Tools developed by ASF for working with SAR data
composite
¶
Create a local-resolution-weighted composite from Sentinel-1 RTC products.
Create a local-resolution-weighted composite from a set of Sentinel-1 RTC products (D. Small, 2012). The local resolution, defined as the inverse of the local contributing (scattering) area, is used to weight each RTC products' contributions to the composite image on a pixel-by-pixel basis. The composite image is created as a Cloud Optimized GeoTIFF (COG). Additionally, a COG specifying the number of rasters contributing to each composite pixel is created.
References
David Small, 2012: https://doi.org/10.1109/IGARSS.2012.6350465
get_area_raster(raster)
¶
Determine the path of the area raster for a given backscatter raster based on naming conventions for HyP3 RTC products
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster |
str
|
path of the backscatter raster, e.g. S1A_IW_20181102T155531_DVP_RTC30_G_gpuned_5685_VV.tif |
required |
Returns:
Name | Type | Description |
---|---|---|
area_raster |
str
|
path of the area raster, e.g. S1A_IW_20181102T155531_DVP_RTC30_G_gpuned_5685_area.tif |
Source code in asf_tools/composite.py
60 61 62 63 64 65 66 67 68 69 70 |
|
get_full_extent(raster_info)
¶
Determine the corner coordinates and geotransform for the full extent of a set of rasters
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster_info |
dict
|
A dictionary of gdal.Info results for the set of rasters |
required |
Returns:
Name | Type | Description |
---|---|---|
upper_left |
The upper left corner of the extent as a tuple |
|
upper_right |
The lower right corner of the extent as a tuple |
|
geotransform |
The geotransform of the extent as a list |
Source code in asf_tools/composite.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
get_target_epsg_code(codes)
¶
Determine the target UTM EPSG projection for the output composite
Parameters:
Name | Type | Description | Default |
---|---|---|---|
codes |
List[int]
|
List of UTM EPSG codes |
required |
Returns:
Name | Type | Description |
---|---|---|
target |
int
|
UTM EPSG code |
Source code in asf_tools/composite.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
make_composite(out_name, rasters, resolution=None)
¶
Creates a local-resolution-weighted composite from Sentinel-1 RTC products
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_name |
str
|
The base name of the output GeoTIFFs |
required |
rasters |
List[str]
|
A list of file paths of the images to composite |
required |
resolution |
float
|
The pixel size for the output GeoTIFFs |
None
|
Returns:
Name | Type | Description |
---|---|---|
out_raster |
Path to the created composite backscatter GeoTIFF |
|
out_counts_raster |
Path to the created GeoTIFF with counts of scenes contributing to each pixel |
Source code in asf_tools/composite.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
reproject_to_target(raster_info, target_epsg_code, target_resolution, directory)
¶
Reprojects a set of raster images to a common projection and resolution
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster_info |
dict
|
A dictionary of gdal.Info results for the set of rasters |
required |
target_epsg_code |
int
|
The integer EPSG code for the target projection |
required |
target_resolution |
float
|
The target resolution |
required |
directory |
str
|
The directory in which to create the reprojected files |
required |
Returns:
Name | Type | Description |
---|---|---|
target_raster_info |
dict
|
An updated dictionary of gdal.Info results for the reprojected files |
Source code in asf_tools/composite.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
dem
¶
Prepare a Copernicus GLO-30 DEM virtual raster (VRT) covering a given geometry
prepare_dem_vrt(vrt, geometry)
¶
Create a DEM mosaic VRT covering a given geometry
The DEM mosaic is assembled from the Copernicus GLO-30 DEM tiles that intersect the geometry.
Note: asf_tools
does not currently support geometries that cross the antimeridian.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vrt |
Union[str, Path]
|
Path for the output VRT file |
required |
geometry |
Union[Geometry, BaseGeometry]
|
Geometry in EPSG:4326 (lon/lat) projection for which to prepare a DEM mosaic |
required |
Source code in asf_tools/dem.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
hydrosar
¶
flood_map
¶
Generate flood depth map from surface water extent map.
Create a flood depth map from a surface water extent map and a HAND image. The HAND image must be pixel-aligned (same extent and size) to the water extent map, and the surface water extent map should be a byte GeoTIFF indicating water (true), not water (false). Flood depth maps are estimated using either a numerical, normalized median absolute deviation, logarithmic or iterative approach.
logstat(data, func=np.nanstd)
¶
Calculate a function in logarithmic scale and return in linear scale. INF values inside the data array are set to nan.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
ndarray
|
array of data |
required |
func |
Callable
|
statistical function to calculate in logarithmic scale |
nanstd
|
Returns: statistic: statistic of data in linear scale
Source code in asf_tools/hydrosar/flood_map.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
make_flood_map(out_raster, vv_raster, water_raster, hand_raster, estimator='iterative', water_level_sigma=3.0, known_water_threshold=None, iterative_bounds=(0, 15), iterative_min_size=0, minimization_metric='ts')
¶
Create a flood depth map from a surface water extent map.
WARNING: This functionality is still under active development and the products created using this function are likely to change in the future.
Create a flood depth map from a single surface water extent map and a HAND image. The HAND image must be pixel-aligned to the surface water extent map. The surface water extent map should be a byte GeoTIFF indicating water (true) and not water (false)
Known perennial Global Surface-water data are produced under the Copernicus Programme (Pekel et al., 2016), and are included with surface-water detection maps when generating the flood depth product.
Flood depth maps are estimated using one of the approaches: Iterative: (Default) Basin hopping optimization method matches flooded areas to flood depth estimates given by the HAND layer. This is the most accurate method but also the most time-intensive. Normalized Median Absolute Deviation (nmad): Uses a median operator to estimate the variation to increase robustness in the presence of outliers. Logstat: Calculates the mean and standard deviation of HAND heights in the logarithmic domain to improve robustness for very non-Gaussian data distributions. Numpy: Calculates statistics on a linear scale. Least robust to outliers and non-Gaussian distributions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_raster |
Union[str, Path]
|
Flood depth GeoTIFF to create |
required |
vv_raster |
Union[str, Path]
|
Sentinel-1 RTC GeoTIFF, in power scale, with VV polarization |
required |
water_raster |
Union[str, Path]
|
Surface water extent GeoTIFF |
required |
hand_raster |
Union[str, Path]
|
Height Above Nearest Drainage (HAND) GeoTIFF aligned to the surface water extent raster |
required |
estimator |
str
|
Estimation approach for determining flood depth |
'iterative'
|
water_level_sigma |
float
|
Max water height used in logstat, nmad, and numpy estimations |
3.0
|
known_water_threshold |
Optional[float]
|
Threshold for extracting the known water area in percent.
If |
None
|
iterative_bounds |
Tuple[int, int]
|
Minimum and maximum bound on the flood depths calculated by the basin-hopping algorithm used in the iterative estimator |
(0, 15)
|
iterative_min_size |
int
|
Minimum size of a connected waterbody in pixels for calculating flood depths with the iterative estimator. Waterbodies smaller than this wll be skipped. |
0
|
minimization_metric |
str
|
Evaluation method to minimize when using the iterative estimator. Options include a Fowlkes-Mallows index (fmi) or a threat score (ts). |
'ts'
|
References
Jean-Francios Pekel, Andrew Cottam, Noel Gorelik, Alan S. Belward. 2016. https://doi:10.1038/nature20584
Source code in asf_tools/hydrosar/flood_map.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
hand
¶
calculate_hand_for_basins(out_raster, geometries, dem_file, acc_thresh=100)
¶
Calculate the Height Above Nearest Drainage (HAND) for watershed boundaries (hydrobasins).
For watershed boundaries, see: https://www.hydrosheds.org/page/hydrobasins
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_raster |
Union[str, Path]
|
HAND GeoTIFF to create |
required |
geometries |
GeometryCollection
|
watershed boundary (hydrobasin) polygons to calculate HAND over |
required |
dem_file |
Union[str, Path]
|
DEM raster covering (containing) |
required |
acc_thresh |
Optional[int]
|
Accumulation threshold for determining the drainage mask.
If |
100
|
Source code in asf_tools/hydrosar/hand/calculate.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
make_copernicus_hand(out_raster, vector_file, acc_thresh=100)
¶
Copernicus GLO-30 Height Above Nearest Drainage (HAND)
Make a Height Above Nearest Drainage (HAND) GeoTIFF from the Copernicus GLO-30 DEM covering the watershed boundaries (hydrobasins) defined in a vector file.
For watershed boundaries, see: https://www.hydrosheds.org/page/hydrobasins
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_raster |
Union[str, Path]
|
HAND GeoTIFF to create |
required |
vector_file |
Union[str, Path]
|
Vector file of watershed boundary (hydrobasin) polygons to calculate HAND over |
required |
acc_thresh |
Optional[int]
|
Accumulation threshold for determining the drainage mask.
If |
100
|
Source code in asf_tools/hydrosar/hand/calculate.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
prepare_hand_vrt(vrt, geometry)
¶
Prepare a HAND mosaic VRT covering a given geometry
Prepare a Height Above Nearest Drainage (HAND) virtual raster (VRT) covering a given geometry. The Height Above Nearest Drainage (HAND) mosaic is assembled from the HAND tiles that intersect the geometry, using a HAND derived from the Copernicus GLO-30 DEM.
Note: asf_tools
does not currently support geometries that cross the antimeridian.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vrt |
Union[str, Path]
|
Path for the output VRT file |
required |
geometry |
Union[Geometry, BaseGeometry]
|
Geometry in EPSG:4326 (lon/lat) projection for which to prepare a HAND mosaic |
required |
Source code in asf_tools/hydrosar/hand/prepare.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
calculate
¶
Calculate Height Above Nearest Drainage (HAND) from the Copernicus GLO-30 DEM
calculate_hand(dem_array, dem_affine, dem_crs, basin_mask, acc_thresh=100)
¶
Calculate the Height Above Nearest Drainage (HAND)
Calculate the Height Above Nearest Drainage (HAND) using pySHEDS library. Because HAND is tied to watershed boundaries (hydrobasins), clipped/cut basins will produce weird edge effects, and incomplete basins should be masked out. For watershed boundaries, see: https://www.hydrosheds.org/page/hydrobasins
This involves:
* Filling pits (single-cells lower than their surrounding neighbors)
in the Digital Elevation Model (DEM)
* Filling depressions (regions of cells lower than their surrounding neighbors)
in the Digital Elevation Model (DEM)
* Resolving un-drainable flats
* Determining the flow direction using the ESRI D8 routing scheme
* Determining flow accumulation (number of upstream cells)
* Creating a drainage mask using the accumulation threshold acc_thresh
* Calculating HAND
In the HAND calculation, NaNs inside the basin filled using fill_hand
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dem_array |
DEM to calculate HAND for |
required | |
dem_crs |
CRS
|
DEM Coordinate Reference System (CRS) |
required |
dem_affine |
Affine
|
DEM Affine geotransform |
required |
basin_mask |
Array of booleans indicating wither an element should be masked out (Ă la Numpy Masked Arrays: https://numpy.org/doc/stable/reference/maskedarray.generic.html#what-is-a-masked-array) |
required | |
acc_thresh |
Optional[int]
|
Accumulation threshold for determining the drainage mask.
If |
100
|
Source code in asf_tools/hydrosar/hand/calculate.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
calculate_hand_for_basins(out_raster, geometries, dem_file, acc_thresh=100)
¶
Calculate the Height Above Nearest Drainage (HAND) for watershed boundaries (hydrobasins).
For watershed boundaries, see: https://www.hydrosheds.org/page/hydrobasins
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_raster |
Union[str, Path]
|
HAND GeoTIFF to create |
required |
geometries |
GeometryCollection
|
watershed boundary (hydrobasin) polygons to calculate HAND over |
required |
dem_file |
Union[str, Path]
|
DEM raster covering (containing) |
required |
acc_thresh |
Optional[int]
|
Accumulation threshold for determining the drainage mask.
If |
100
|
Source code in asf_tools/hydrosar/hand/calculate.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
fill_hand(hand, dem)
¶
Replace NaNs in a HAND array with values interpolated from their neighbor's HOND
Replace NaNs in a HAND array with values interpolated from their neighbor's HOND (height of nearest drainage) using a 2D Gaussian kernel. Here, HOND is defined as the DEM value less the HAND value. For the kernel, see: https://docs.astropy.org/en/stable/convolution/#using-astropy-s-convolution-to-replace-bad-data
Source code in asf_tools/hydrosar/hand/calculate.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
fill_nan(array)
¶
Replace NaNs with values interpolated from their neighbors
Replace NaNs with values interpolated from their neighbors using a 2D Gaussian kernel, see: https://docs.astropy.org/en/stable/convolution/#using-astropy-s-convolution-to-replace-bad-data
Source code in asf_tools/hydrosar/hand/calculate.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
make_copernicus_hand(out_raster, vector_file, acc_thresh=100)
¶
Copernicus GLO-30 Height Above Nearest Drainage (HAND)
Make a Height Above Nearest Drainage (HAND) GeoTIFF from the Copernicus GLO-30 DEM covering the watershed boundaries (hydrobasins) defined in a vector file.
For watershed boundaries, see: https://www.hydrosheds.org/page/hydrobasins
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_raster |
Union[str, Path]
|
HAND GeoTIFF to create |
required |
vector_file |
Union[str, Path]
|
Vector file of watershed boundary (hydrobasin) polygons to calculate HAND over |
required |
acc_thresh |
Optional[int]
|
Accumulation threshold for determining the drainage mask.
If |
100
|
Source code in asf_tools/hydrosar/hand/calculate.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
prepare
¶
Prepare a Height Above Nearest Drainage (HAND) virtual raster (VRT) covering a given geometry
prepare_hand_for_raster(hand_raster, source_raster, resampling_method='lanczos')
¶
Create a HAND raster pixel-aligned to a source raster
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hand_raster |
Union[str, Path]
|
Path for the output HAND raster |
required |
source_raster |
Union[str, Path]
|
Path for the source raster |
required |
resampling_method |
str
|
Name of the resampling method to use. For available methods, see: https://gdal.org/programs/gdalwarp.html#cmdoption-gdalwarp-r |
'lanczos'
|
Source code in asf_tools/hydrosar/hand/prepare.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
prepare_hand_vrt(vrt, geometry)
¶
Prepare a HAND mosaic VRT covering a given geometry
Prepare a Height Above Nearest Drainage (HAND) virtual raster (VRT) covering a given geometry. The Height Above Nearest Drainage (HAND) mosaic is assembled from the HAND tiles that intersect the geometry, using a HAND derived from the Copernicus GLO-30 DEM.
Note: asf_tools
does not currently support geometries that cross the antimeridian.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vrt |
Union[str, Path]
|
Path for the output VRT file |
required |
geometry |
Union[Geometry, BaseGeometry]
|
Geometry in EPSG:4326 (lon/lat) projection for which to prepare a HAND mosaic |
required |
Source code in asf_tools/hydrosar/hand/prepare.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
threshold
¶
expectation_maximization_threshold(tile, number_of_classes=3)
¶
Water threshold Calculation using a multi-mode Expectation Maximization Approach
Thresholding works best when backscatter tiles are provided on a decibel scale to get Gaussian distribution that is scaled to a range of 0-255, and performed on a small tile that is likely to have a transition between water and not water.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tile |
ndarray
|
array of backscatter values for a tile from an RTC raster |
required |
number_of_classes |
int
|
classify the tile into this many classes. Typically, three classes capture: (1) urban and bright slopes, (2) average brightness farmland, and (3) water, as is often seen in the US Midwest. |
3
|
Returns:
Name | Type | Description |
---|---|---|
threshold |
float
|
threshold value that can be used to create a water extent map |
Source code in asf_tools/hydrosar/threshold.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
water_map
¶
Generate surface water maps from Sentinel-1 RTC products
Create a surface water extent map from a dual-pol Sentinel-1 RTC product and a HAND image. The HAND image must be pixel-aligned (same extent and size) to the RTC images. The water extent maps are created using an adaptive Expectation Maximization thresholding approach and refined using Fuzzy Logic.
format_raster_data(raster, padding_mask=None, nodata=np.iinfo(np.uint8).max)
¶
Ensure raster data is uint8 and set the area outside the valid data to nodata
Source code in asf_tools/hydrosar/water_map.py
136 137 138 139 140 141 142 143 144 145 146 |
|
make_water_map(out_raster, vv_raster, vh_raster, hand_raster=None, tile_shape=(100, 100), max_vv_threshold=-15.5, max_vh_threshold=-23.0, hand_threshold=15.0, hand_fraction=0.8, membership_threshold=0.45)
¶
Creates a surface water extent map from a Sentinel-1 RTC product
Create a surface water extent map from a dual-pol Sentinel-1 RTC product and a HAND image. The HAND image must be pixel-aligned (same extent and size) to the RTC images. The water extent maps are created using an adaptive Expectation Maximization thresholding approach and refined with Fuzzy Logic.
The input images are broken into a set of corresponding tiles with a shape of
tile_shape
, and a set of tiles are selected from the VH RTC
image that contain water boundaries to determine an appropriate water threshold.
Candidate tiles must meet these criteria:
* hand_fraction
of pixels within a tile must have HAND pixel values lower
than hand_threshold
* The median backscatter value for the tile must be lower than an average tiles'
backscatter values
* The tile must have a high variance -- high variance is considered initially to
be a variance in the 95th percentile of the tile variances, but progressively
relaxed to the 5th percentile if there not at least 5 candidate tiles.
The 5 VH tiles with the highest variance are selected for thresholding and a
water threshold value is determined using an Expectation Maximization approach.
If there were not enough candidate tiles or the threshold is too high,
max_vh_threshold
and/or max_vv_threshold
will be used instead.
From the initial threshold-based water extent maps, Fuzzy Logic is used to remove spurious false detections and improve the water extent map quality. The fuzzy logic uses these indicators for the presence of water: * radar cross section in a pixel relative to the determined detection threshold * the height above nearest drainage (HAND) * the surface slope, which is derived from the HAND data * the size of the detected water feature
For each indicator, a Z-shaped activation function is used to determine pixel membership.
The membership maps are combined to form the final water extent map. Pixels classified
as water pixels will:
* have non-zero membership in all of the indicators, and
* have an average membership above the membership_threshold
value.
Finally, the VV and VH water masks will be combined to include all water pixels
from both masks, and the combined water map will be written to out_raster
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_raster |
Union[str, Path]
|
Water map GeoTIFF to create |
required |
vv_raster |
Union[str, Path]
|
Sentinel-1 RTC GeoTIFF, in power scale, with VV polarization |
required |
vh_raster |
Union[str, Path]
|
Sentinel-1 RTC GeoTIFF, in power scale, with VH polarization |
required |
hand_raster |
Optional[Union[str, Path]]
|
Height Above Nearest Drainage (HAND) GeoTIFF aligned to the RTC rasters |
None
|
tile_shape |
Tuple[int, int]
|
shape (height, width) in pixels to tile the image to |
(100, 100)
|
max_vv_threshold |
float
|
Maximum threshold value to use for |
-15.5
|
max_vh_threshold |
float
|
Maximum threshold value to use for |
-23.0
|
hand_threshold |
float
|
The maximum height above nearest drainage in meters to consider a pixel valid |
15.0
|
hand_fraction |
float
|
The minimum fraction of valid HAND pixels required in a tile for thresholding |
0.8
|
membership_threshold |
float
|
The average membership to the fuzzy indicators required for a water pixel |
0.45
|
Source code in asf_tools/hydrosar/water_map.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
|
raster
¶
convert_scale(array, in_scale, out_scale)
¶
Convert calibrated raster scale between db, amplitude and power
Source code in asf_tools/raster.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
read_as_array(raster, band=1)
¶
Reads data from a raster image into memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster |
str
|
The file path to a raster image |
required |
band |
int
|
The raster band to read |
1
|
Returns:
Name | Type | Description |
---|---|---|
data |
array
|
The raster pixel data as a numpy array |
Source code in asf_tools/raster.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
read_as_masked_array(raster, band=1)
¶
Reads data from a raster image into memory, masking invalid and NoData values
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster |
Union[str, Path]
|
The file path to a raster image |
required |
band |
int
|
The raster band to read |
1
|
Returns:
Name | Type | Description |
---|---|---|
data |
MaskedArray
|
The raster pixel data as a numpy MaskedArray |
Source code in asf_tools/raster.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
write_cog(file_name, data, transform, epsg_code, dtype=gdal.GDT_Float32, nodata_value=None)
¶
Creates a Cloud Optimized GeoTIFF
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
Union[str, Path]
|
The output file name |
required |
data |
ndarray
|
The raster data |
required |
transform |
List[float]
|
The geotransform for the output GeoTIFF |
required |
epsg_code |
int
|
The integer EPSG code for the output GeoTIFF projection |
required |
dtype |
The pixel data type for the output GeoTIFF |
GDT_Float32
|
|
nodata_value |
The NODATA value for the output Geotiff |
None
|
Returns:
Name | Type | Description |
---|---|---|
file_name |
The output file name |
Source code in asf_tools/raster.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
tile
¶
tile_array(array, tile_shape=(200, 200), pad_value=None)
¶
Tile a 2D numpy array
Turn a 2D numpy array like
array = [[0, 0, 1, 1], ... [0, 0, 1, 1], ... [2, 2, 3, 3], ... [2, 2, 3, 3]] array.shape (4, 4)
into a tiled array like
tiles = tiled_array(array, 2, 2) print(tiles) [[[0, 0], [0, 0]], [[1, 1], [1, 1]], [[2, 2], [2, 2]], [[3, 3], [3, 3]]] tiles.shape (4, 2, 2)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
Union[ndarray, MaskedArray]
|
2D array to tile |
required |
tile_shape |
Tuple[int, int]
|
the shape of each tile |
(200, 200)
|
pad_value |
float
|
right-bottom pad |
None
|
Returns:
Type | Description |
---|---|
Union[ndarray, MaskedArray]
|
the tiled array |
Source code in asf_tools/tile.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
untile_array(tiled_array, array_shape)
¶
Untile a tiled array into a 2D numpy array
This is the reverse of tile_array
and will turn a tiled array like:
>>> tiled_array = [[[0,0],
... [0,0]],
... [[1,1],
... [1,1]],
... [[2,2],
... [2,2]],
... [[3,3],
... [3,3]]]
>>> tiled_array.shape
(4, 2, 2)
into a 2D array like
array = untile_array(tiled_array) print(array) [[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]] array.shape (4, 4)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tiled_array |
Union[ndarray, MaskedArray]
|
a tiled array |
required |
array_shape |
Tuple[int, int]
|
shape to untile the array to. If array_shape's size is smaller
than the tiled array, |
required |
Returns:
Type | Description |
---|---|
Union[ndarray, MaskedArray]
|
the untiled array |
Source code in asf_tools/tile.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
util
¶
GDALConfigManager
¶
Context manager for setting GDAL config options temporarily
Source code in asf_tools/util.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
__init__(**options)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**options |
GDAL Config |
{}
|
Source code in asf_tools/util.py
10 11 12 13 14 15 16 |
|
epsg_to_wkt(epsg_code)
¶
Get the WKT representation of a projection from its EPSG code
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epsg_code |
int
|
The integer EPSG code |
required |
Returns:
Name | Type | Description |
---|---|---|
wkt |
str
|
The WKT representation of the projection |
Source code in asf_tools/util.py
58 59 60 61 62 63 64 65 66 67 68 69 |
|
get_coordinates(info)
¶
Get the corner coordinates from a GDAL Info dictionary
Parameters:
Name | Type | Description | Default |
---|---|---|---|
info |
dict
|
The dictionary returned by a gdal.Info call |
required |
Returns:
Type | Description |
---|---|
(west, south, east, north)
|
the corner coordinates values |
Source code in asf_tools/util.py
44 45 46 47 48 49 50 51 52 53 54 55 |
|
get_epsg_code(info)
¶
Get the EPSG code from a GDAL Info dictionary
Parameters:
Name | Type | Description | Default |
---|---|---|---|
info |
dict
|
The dictionary returned by a gdal.Info call |
required |
Returns:
Name | Type | Description |
---|---|---|
epsg_code |
int
|
The integer EPSG code |
Source code in asf_tools/util.py
30 31 32 33 34 35 36 37 38 39 40 41 |
|
watermasking
¶
fill_missing_tiles
¶
generate_osm_tiles
¶
extract_water(water_file, lat, lon, tile_width_deg, tile_height_deg, interior_tile_dir)
¶
Rasterize a water tile from the processed global PBF file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
water_file |
The path to the processed global PBF file. |
required | |
lat |
The minimum latitude of the tile. |
required | |
lon |
The minimum longitude of the tile. |
required | |
tile_width_deg |
The desired width of the tile in degrees. |
required | |
tile_height_deg |
The desired height of the tile in degrees. |
required |
Source code in asf_tools/watermasking/generate_osm_tiles.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
merge_interior_and_ocean(internal_tile_dir, ocean_tile_dir, finished_tile_dir, translate_to_cog=False)
¶
Merge the interior water tiles and ocean water tiles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interior_tile_dir |
The path to the directory containing the interior water tiles. |
required | |
ocean_tile_dir |
The path to the directory containing the ocean water tiles. |
required | |
merged_tile_dir |
The path to the directory containing the merged water tiles. |
required |
Source code in asf_tools/watermasking/generate_osm_tiles.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
process_ocean_tiles(ocean_polygons_path, lat, lon, tile_width_deg, tile_height_deg, output_dir)
¶
Process and crop OSM ocean polygons into a tif tile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ocean_polygons_path |
The path to the global ocean polygons file from OSM. |
required | |
lat |
The minimum latitude of the tile. |
required | |
lon |
The minimum longitude of the tile. |
required | |
tile_width_deg |
The width of the tile in degrees. |
required | |
tile_height_deg |
The height of the tile in degrees. |
required |
Source code in asf_tools/watermasking/generate_osm_tiles.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
process_pbf(planet_file, output_file)
¶
Process the global PBF file so that it only contains water features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
planet_file |
str
|
The path to the OSM Planet PBF file. |
required |
output_file |
str
|
The desired path of the processed PBF file. |
required |
Source code in asf_tools/watermasking/generate_osm_tiles.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
generate_worldcover_tiles
¶
build_dataset(worldcover_tile_dir, lat_range, lon_range, tile_width, tile_height)
¶
Main function for generating a dataset with worldcover tiles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
worldcover_tile_dir |
The directory containing the unprocessed worldcover tiles. |
required | |
lat_range |
The range of latitudes the dataset should cover. |
required | |
lon_range |
The range of longitudes the dataset should cover. |
required | |
out_degrees |
The width of the outputed dataset tiles in degrees. |
required |
Source code in asf_tools/watermasking/generate_worldcover_tiles.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
create_missing_tiles(tile_dir, lat_range, lon_range)
¶
Check for, and build, tiles that may be missing from WorldCover, such as over the ocean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lat_range |
The range of latitudes to check. |
required | |
lon_range |
The range of longitudes to check. |
required |
Returns: current_existing_tiles: The list of tiles that exist after the function has completed.
Source code in asf_tools/watermasking/generate_worldcover_tiles.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
crop_tile(tile, lat, lon, tile_width, tile_height)
¶
Crop the merged tiles
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tile |
The filename of the desired tile to crop. |
required |
Source code in asf_tools/watermasking/generate_worldcover_tiles.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
get_tiles(osm_tile_coord, wc_tile_width, tile_width)
¶
Get a list of the worldcover tile locations necessary to fully cover an OSM tile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
osm_tile_coord |
tuple
|
The lower left corner coordinate (lat, lon) of the desired OSM tile. |
required |
wc_tile_width |
int
|
The width/height of the Worldcover tiles in degrees. |
required |
tile_width |
int
|
The width/height of the OSM tiles in degrees. |
required |
Returns:
Name | Type | Description |
---|---|---|
tiles |
A list of the lower left corner coordinates of the Worldcover tiles that overlap the OSM tile. |
Source code in asf_tools/watermasking/generate_worldcover_tiles.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
lat_lon_to_filenames(worldcover_tile_dir, osm_tile_coord, wc_tile_width, tile_width)
¶
Get a list of the Worldcover tile filenames that are necessary to overlap an OSM tile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
osm_tile |
The lower left corner (lat, lon) of the desired OSM tile. |
required | |
wc_tile_width |
int
|
The width of the Worldcover tiles in degrees. |
required |
tile_width |
int
|
The width of the OSM tiles in degrees. |
required |
Returns:
Name | Type | Description |
---|---|---|
filenames |
The list of Worldcover filenames. |
Source code in asf_tools/watermasking/generate_worldcover_tiles.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
tile_preprocessing(tile_dir, min_lat, max_lat, min_lon, max_lon)
¶
The worldcover tiles have lots of unnecessary classes, these need to be removed first. Note: make a back-up copy of this directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tile_dir |
The directory containing all of the worldcover tiles. |
required |
Source code in asf_tools/watermasking/generate_worldcover_tiles.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
utils
¶
lat_lon_to_tile_string(lat, lon, is_worldcover=False, postfix='.tif')
¶
Get the name of the tile with lower left corner (lat, lon).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lat |
The minimum latitude of the tile. |
required | |
lon |
The minimum longitude of the tile. |
required | |
is_worldcover |
bool
|
Wheter the tile is Worldcover or OSM. |
False
|
postfix |
str
|
A postfix to append to the tile name to make it a filename. |
'.tif'
|
Returns:
Type | Description |
---|---|
The name of the tile. |
Source code in asf_tools/watermasking/utils.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
merge_tiles(tiles, out_filename, out_format, compress=False)
¶
Merge tiles via buildvrt and translate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tiles |
The list of tiles to be merged. |
required | |
out_format |
The format of the output image. |
required | |
out_filename |
The name of the output COG. |
required |
Source code in asf_tools/watermasking/utils.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
remove_temp_files(temp_files)
¶
Remove each file in a list of files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
temp_files |
list
|
The list of temporary files to remove. |
required |
Source code in asf_tools/watermasking/utils.py
57 58 59 60 61 62 63 64 65 66 67 |
|
setup_directories(dirs)
¶
Setup the directories necessary for running the script.
Source code in asf_tools/watermasking/utils.py
70 71 72 73 74 75 76 77 |
|