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-1.3.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distributions

affine-1.3.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

affine-1.3.0-py2-none-any.whl (14.5 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for affine-1.3.0.tar.gz
Algorithm Hash digest
SHA256 abbead5a77fcb47a1b5d7ae444d715bf71a2475bdb19c674b55dccf231d7c1f1
MD5 a8e4dec94d91731952092fd0adbf0611
BLAKE2b-256 928804925bb196a1b49b3f6d5cc2eb15b204e05b8a360baf93364ae18792944c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for affine-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 563d8f75a03d6f32dc5842a452f575b47a0a903511eb5bc12697cc2f608f7b2c
MD5 e2910bc4249d0fef104025b3404989b2
BLAKE2b-256 6512b05d06b59de60680b8d7f21050f01e275193d26b1fde4429b7a450ca45e6

See more details on using hashes here.

File details

Details for the file affine-1.3.0-py2-none-any.whl.

File metadata

File hashes

Hashes for affine-1.3.0-py2-none-any.whl
Algorithm Hash digest
SHA256 faef30537d7a9d5586be098f61f14c1b7742dbc0e2488ba0483f220122fd87c5
MD5 e7b95280029ed6f8e405e76020396738
BLAKE2b-256 eb1ed70e9677450766e2daf0153e469305e86b3b26e7e1446e407e78ee83569a

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