Skip to main content

Matrices describing affine transformation of the plane

Project description

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)

Project details


Download files

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

Source Distribution

affine-2.0b2.tar.gz (11.3 kB view details)

Uploaded Source

Built Distributions

affine-2.0b2-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

affine-2.0b2-py2-none-any.whl (15.1 kB view details)

Uploaded Python 2

File details

Details for the file affine-2.0b2.tar.gz.

File metadata

  • Download URL: affine-2.0b2.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for affine-2.0b2.tar.gz
Algorithm Hash digest
SHA256 4e8bb402442eb7dcca5645217ab4c998720cab8d828ed70282ced3696242ca6b
MD5 13867e8e71151b09a837573f3793f8ca
BLAKE2b-256 defb5a14e7be60cdf2b323e5e2e2a45954f22742413ca674d196113c42d0cd61

See more details on using hashes here.

File details

Details for the file affine-2.0b2-py3-none-any.whl.

File metadata

File hashes

Hashes for affine-2.0b2-py3-none-any.whl
Algorithm Hash digest
SHA256 6446d85a0a14d6bc20ffb39c6695acc5128b36c7662e3d4a08e37b84746b5efb
MD5 53be0d3271659a71ceb1ec0e7c2ff5d7
BLAKE2b-256 569e6df6aa38ce5e147b6c52887cf777269792cd32eea2d2e31d6d592efba60e

See more details on using hashes here.

File details

Details for the file affine-2.0b2-py2-none-any.whl.

File metadata

File hashes

Hashes for affine-2.0b2-py2-none-any.whl
Algorithm Hash digest
SHA256 8e03438b385b68c6a222ce4da6d00608b2e42cf35ee79e32d28cd4cc5c3932a2
MD5 3a5f51b2a2094fa404bba9086b8c787d
BLAKE2b-256 49e792d8904baf2dc2defadc794fcc66ffafbdf633c404b05d32096594a107c9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page