[GIS] Defining Affine transform with rasterio

affine transformationpythonrasteriosentinel-hub

I have transform vector that I have gotten from bbox with sentinelhub:

transform=bbox.get_transform_vector(10,10)
>>>
(-101.7359960059834, 10.0, 0, 20.8312118894487, 0, -10.0)

The type of this affine vector is tuple:

type(transform)

>>>tuple

I want to define this transform to be affine transform in rasterio, e.g to change it type to be affine.Affine a,so it will look like this:

Affine((-101.7359960059834, 10.0, 0, 20.8312118894487, 0, -10.0)

I haven't found any way to change it, I have tried:

#try1
Affine(transform)

#try2
affine(transform)

but obviously non of them work.

I have also tried:

rasterio.transform.guard_transform(transform)

but then I get this error:

TypeError: GDAL-style transforms have been deprecated. This exception
will be raised for a period of time to highlight potentially confusing
errors, but will eventually be removed.

I still haven't found the way to create affine type from tuple.

My end goal: to get my tuple as type affine in rasterio

Best Answer

If you do this:

>>>import rasterio
>>>dir(rasterio)
['Affine', 'Band', 'DatasetReader', 'Env', 'MemoryFile', 'NullHandler', 'Path', 'RasterioIOError', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__gdal_version__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_base', '_crs', '_env', '_err', '_features', '_io', '_shim', '_transform', '_warp', 'absolute_import', 'band', 'bool_', 'check_dtype', 'compat', 'complex_', 'contextmanager', 'control', 'coords', 'crs', 'default_gtiff_profile', 'drivers', 'dtypes', 'ensure_env_with_credentials', 'enums', 'env', 'errors', 'float32', 'float64', 'gdal_version', 'get_writer_for_driver', 'get_writer_for_path', 'guard_transform', 'int16', 'int32', 'int8', 'io', 'is_blacklisted', 'log', 'logging', 'namedtuple', 'open', 'pad', 'parse_path', 'path', 'profiles', 'rasterio', 'sample', 'sbyte', 'session', 'string_types', 'transform', 'ubyte', 'uint16', 'uint32', 'uint8', 'vrt', 'windows']

it is corroborated that correct method is 'Affine'. So, by using following command you have desired answer without any error.

>>>rasterio.Affine(-101.7359960059834, 10.0, 0, 20.8312118894487, 0, -10.0)
Affine(-101.7359960059834, 10.0, 0.0,
       20.8312118894487, 0.0, -10.0)

You can get same result as follows:

>>>transform = (-101.7359960059834, 10.0, 0, 20.8312118894487, 0, -10.0)
>>>rasterio.Affine(transform[0],transform[1],transform[2],transform[3],transform[4],transform[5] )
Affine(-101.7359960059834, 10.0, 0.0,
       20.8312118894487, 0.0, -10.0)

With a help to rasterio.Affine;

>>>help(rasterio.Affine)
Help on class Affine in module affine:

class Affine(Affine)
 |  Affine(a, b, c, d, e, f)
 |  
 |  Two dimensional affine transform for 2D linear mapping.
 |  
 |  Parameters
 |  ----------
 |  a, b, c, d, e, f : float
.
.
.

So, when you do:

>>>rasterio.Affine(transform)
Traceback (most recent call last):
  File "/usr/lib/python3.7/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: __new__() missing 5 required positional arguments: 'b', 'c', 'd', 'e', and 'f'

you got an error because you require 6 positional arguments as float (not a tuple).

Related Question