Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Matrices describing affine transformation of the plane.

https://travis-ci.org/sgillies/affine.svg?branch=master https://coveralls.io/repos/sgillies/affine/badge.svg

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

Usage

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

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 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 ~ operator.

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

Release files for affine 2.0b1

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 2.0b1
File Size Uploaded
affine-2.0b1.tar.gz 11.1 kB Details

Built distributions (wheels)

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

Total release size: 40.8 kB

Release files / affine-2.0b1.tar.gz

Download URL affine-2.0b1.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7d983f9b0db3116e0f145e0d162d7591a3fc9fe0e85a8c4ee24e6896849aec5a
BLAKE2b-256 checksum
How to use checksums
1fb634f80c52fa4eb7c9178e9b918dc302db0ecb1ddb68c734c2f71559dd089d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / affine-2.0b1-py3-none-any.whl

Download URL affine-2.0b1-py3-none-any.whl
Size 14.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f686b0f7fd3b62e2b8bb1adf5b1f1fc8f3eb55b2b9d0b441bb8208b59d5ea7d3
BLAKE2b-256 checksum
How to use checksums
2c5a9492b7f818337d796860fbd5d871e6548526b3ca4fbde0707986b4042375
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / affine-2.0b1-py2-none-any.whl

Download URL affine-2.0b1-py2-none-any.whl
Size 14.8 kB
Tags Python 2
SHA-256 checksum
How to use checksums
3d40de4033a7347410a8ce999506e19bdb5e4cebaadf1d0c76bd7f47b8ee06f3
BLAKE2b-256 checksum
How to use checksums
0bdae42d7f1542a4b6710f508fd3754dabeb8c8d4820f8612368fa5119534dea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
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