How to get Affine Transformation


#1

Hi !
For my temporal analysis script (again :grinning:) i’m trying to use rasterstats module to calculate cloud proportion in blocks. To do this I need Affine Transformation of images.

I get cloud mask with a CloudMaskRequest called all_cloud_masks like in s2cloudless example.

for idx, [prob, mask, data] in enumerate(all_cloud_masks):

cloud = zonal_stats(blocks_merge, mask, affine=affine, nodata=-999, add_stats={‘cloud’:cloud_pixel_count})

for the moment I run another WcsRequest to save one image and get affine transformation with rasterio module:

with rasterio.open(pix) as src:

affine = src.transform

But is it possible to get Affine Transformation of masks more simply?


#2

Yes, you can calculate transform from coordinates of image bounding box and shape of the image.

There are only 4 non-constant parameters in transform matrix. Two of them are coordinates of upper left corner of the image, which you can get from the bounding box. The other two are resolution in x and y directions. Those you can calculate by dividing size of bounding box by number of pixels in each of x and y dimensions of your image.