Skip to main content

Matrices describing 2D affine transformation of the plane.

https://github.com/rasterio/affine/actions/workflows/ci.yml/badge.svg?branch=main https://codecov.io/gh/rasterio/affine/branch/main/graph/badge.svg Documentation Status

The Affine package is derived from Casey Duncan’s Planar package. Please see the copyright statement in src/affine.py.

Usage

The 3x3 augmented affine transformation matrix for transformations in two dimensions is illustrated below.

| x' |   | a  b  c | | x |
| y' | = | d  e  f | | y |
| 1  |   | 0  0  1 | | 1 |

Matrices can be created by passing the values a, b, c, d, e, f to the affine.Affine constructor or by using its identity(), translation(), scale(), shear(), and rotation() class methods.

>>> from affine import Affine
>>> Affine.identity()
Affine(1.0, 0.0, 0.0,
       0.0, 1.0, 0.0)
>>> Affine.translation(1.0, 5.0)
Affine(1.0, 0.0, 1.0,
       0.0, 1.0, 5.0)
>>> Affine.scale(2.0)
Affine(2.0, 0.0, 0.0,
       0.0, 2.0, 0.0)
>>> Affine.shear(45.0, 45.0)  # decimal degrees
Affine(1.0, 0.9999999999999999, 0.0,
       0.9999999999999999, 1.0, 0.0)
>>> Affine.rotation(45.0)     # decimal degrees
Affine(0.7071067811865476, -0.7071067811865475, 0.0,
       0.7071067811865475, 0.7071067811865476, 0.0)

These matrices can be applied to (x, y) tuples using the @ matrix multiplier operator to obtain transformed coordinates (x', y').

>>> Affine.translation(1.0, 5.0) @ (1.0, 1.0)
(2.0, 6.0)
>>> Affine.rotation(45.0) @ (1.0, 1.0)
(1.1102230246251565e-16, 1.414213562373095)

They may also be multiplied together to combine transformations.

>>> Affine.translation(1.0, 5.0) @ Affine.rotation(45.0)
Affine(0.7071067811865476, -0.7071067811865475, 1.0,
       0.7071067811865475, 0.7071067811865476, 5.0)

Usage with GIS data packages

Georeferenced raster datasets use affine transformations to map from image coordinates to world coordinates. The affine.Affine.from_gdal() class method helps convert GDAL GeoTransform, sequences of 6 numbers in which the first and fourth are the x and y offsets and the second and sixth are the x and y pixel sizes.

Using a GDAL dataset transformation matrix, the world coordinates (x, y) corresponding to the top left corner of the pixel 100 rows down from the origin can be easily computed.

>>> geotransform = (-237481.5, 425.0, 0.0, 237536.4, 0.0, -425.0)
>>> fwd = Affine.from_gdal(*geotransform)
>>> col, row = 0, 100
>>> fwd @ (col, row)
(-237481.5, 195036.4)

The reverse transformation is obtained using the ~ inverse operator.

>>> rev = ~fwd
>>> rev @ fwd @ (col, row)
(0.0, 99.99999999999999)

Release files for affine 3.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for affine 3.0.1
File Size Uploaded
affine-3.0.1.tar.gz 21.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for affine 3.0.1
File Interpreter ABI Platform
affine-3.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 31.9 kB

Release files / affine-3.0.1.tar.gz

Download URL affine-3.0.1.tar.gz
Size 21.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e1b3c38c5d4d3ef5024a182a6d1bf1e0c51ab221825781c741aeb4d0c079a7e2
BLAKE2b-256 checksum
How to use checksums
63e94a4480601992a529c5d0f406605f70ca59aeaef4a6f5ba8905cfde217d0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0rc1

Release files / affine-3.0.1-py3-none-any.whl

Download URL affine-3.0.1-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cda3b303325e7bf2bf34817e68753a0d1c4cacbdd451fe67c4878dc2ecbaa540
BLAKE2b-256 checksum
How to use checksums
3d87e62f55c956b583380e7d2a71705dfd431ee32dd1689d50491ba0c610fc11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0rc1
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page