TransWikia.com

Affine Tranformation Matrix; shapely asks 6 coefficients, but rasterio delivers 9; how to translate

Geographic Information Systems Asked on April 11, 2021

Some lines have been extracted via scikit-image, now tho goal is to vectorize them within the origin coordinate reference system

#some geotif opened with rasterio
src = rasterio.open(raster_path)
transform = src.tranform
print("transform",transform)
>> | 0.10, 0.00, 350482.83|
| 0.00,-0.10, 5617200.95|
| 0.00, 0.00, 1.00|
print("type(transform)",type(transform))
>> <class 'affine.Affine'>
#some line extract from the geotiff with scikit-image
line = ((849, 2363), (1626, 1711)) 
#extract start- and end-pointof line
p0, p1 = line
# construct points in shapely
A = Point(p0[0],p0[1])
B = Point(p1[0],p1[1])
AB = LineString([A, B])
print("AB",AB)
>> LINESTRING (849 2363, 1626 1711)
AB = affine_transform(AB, transform)
>>ValueError: 'matrix' expects either 6 or 12 coefficients

How does one get a 6 coefficient Matrix or a 12 coefficient Matrix from from a geotiff with rasterio?

or

How does one translate an 9 coefficient affine transformation to a 6 coefficient one?

One Answer

A 9 coefficient transform matrix has each 3 coefficients, each corresponding to the 3 spacial dimensions. Shapely expects a coefficient matrix for data with 2 spatial dimensions. Hence a selection has to be performed.

beware these are right coefficients, albeit in the wrong order:

[*transform[0:3],*transform[3:6]]
>>[0.10000036968576852, 0.0, 350482.8259, 0.0, -0.1000006060605726, 5617200.9533]

rather extract them with the Affine Object native function .column_vectors and unpack to tuples to a list to obtain the correct order:

coef_6_transform = [element for tupl in transform.column_vectors for element in tupl]
>> [0.10000036968576852, 0.0, 0.0, -0.1000006060605726, 350482.8259, 5617200.9533]
AB = affine_transform(AB, coef_6_transform))

This worked fine and yielded the lines on the right position

Correct answer by Alexander Vocaet on April 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP