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 * operator (or the @ matrix multiplier operator for future releases) 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)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

affine-3.0.0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

affine-3.0.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file affine-3.0.0.tar.gz.

File metadata

  • Download URL: affine-3.0.0.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for affine-3.0.0.tar.gz
Algorithm Hash digest
SHA256 573514d5c37e98401a0ec34139c2b725d9f9ae4d074662f4b62a47d6a2ba9f06
MD5 640334c3033a6f331fe5540e2b11a997
BLAKE2b-256 6d482642498353f0f2f7179fe5ee5337497551f74f25b0ac09299fea5fe3f3ac

See more details on using hashes here.

File details

Details for the file affine-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: affine-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for affine-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d1b15ed1877a4649a623468a97af6b2182889dc748d7f96d59d504e5c83c00bf
MD5 b99cc1dbaf784ced62bf1fec6618a3fa
BLAKE2b-256 b8b28f8bfbee441896b5bd275fba49fbcd276ffbb640ecd2f7fc9160294e7295

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 files

2.4.0

2 files

2.3.1

2 files

2.3.0

2 files

2.2.2

2 files

2.2.1

2 files

2.2.0

2 files

2.1.0

2 files

2.0.0.post1

3 files

2.0.0

1.3.0

3 files

1.2.0

3 files

1.1.0

1 file

1.0.1

1 file

1.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page