Registry / data / affine

affine

JSON →
library3.0.0pypypi✓ verified 24d ago

Affine is a Python library providing matrices that describe two-dimensional affine transformations of the plane. It is commonly used in geospatial contexts, such as with raster data, to transform between image and world coordinates. The current stable version is 2.4.0, with a major rewrite for version 3.0 currently in release candidate stages.

pip install affine
INSTALL
IMPORT
SIG · AFFINE
A
affine
datapythonv3.0.0
Install
1.6s avg
Import
96ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.101s · 18.4MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.091s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Affine
from affine import Affine

Demonstrates creating identity, translation, scaling, and rotation affine objects, combining them, and applying them to a point.

from affine import Affine # Create an identity affine transform identity = Affine.identity() print(f"Identity: {identity}") # Create a translation transform (move by 10 units in x, 20 in y) translation = Affine.translation(10, 20) print(f"Translation: {translation}") # Create a scaling transform (scale by 2x) scaling = Affine.scale(2.0) print(f"Scaling: {scaling}") # Combine transforms by multiplication (scaling then translation) combined = translation * scaling print(f"Combined (scale then translate): {combined}") # Apply the combined transform to a point (0, 0) x, y = 0, 0 x_prime, y_prime = combined * (x, y) print(f"Original point ({x}, {y}) transformed to ({x_prime}, {y_prime})") # Example of rotation (rotate by 45 degrees) rotation = Affine.rotation(45.0) print(f"Rotation 45 deg: {rotation}") # Apply rotation to a point (1, 0) x_rot, y_rot = rotation * (1, 0) print(f"Point (1,0) rotated 45 deg: ({x_rot:.2f}, {y_rot:.2f})")
Debug
Known issues
breakingIn `affine` 3.0 (alpha/beta/rc releases), the `Affine` class is no longer a named tuple. This means direct JSON serialization without a custom adapter will break, and any code relying on `namedtuple` specific behaviors will need adjustment.
fix
Update code to treat `Affine` as a regular class instance. For JSON serialization, implement a custom adapter (e.g., by converting to a list or dictionary) or serialize its `to_coeffs()` output.
affects: >=3.0a1
breakingThe precision for properties like `is_conformal` has changed from a global module attribute to an `Affine` class or instance attribute in version 3.0. Additionally, the `is_degenerate` property is now exact, removing previous precision considerations.
fix
Access precision settings via `Affine` class or instance attributes. Adjust code that relied on a global `precision` value or the non-exact `is_degenerate` behavior.
affects: >=3.0a1
breakingThe sense of rotation has been reversed in `affine` 3.0. A positive angle now rotates a point counter-clockwise about the pivot point, which might be a change from previous versions' behavior.
fix
Review and update rotation angle values if your application relies on a specific direction of rotation for positive angles.
affects: >=3.0a1
breakingFor `affine` 3.0 and later, Python 3.9 or newer is required. Previous versions (like 2.4.0) supported Python 3.7+.
fix
Ensure your environment uses Python 3.9 or a newer compatible version before upgrading to `affine` 3.0.
affects: >=3.0a1
gotchaWhen working with geospatial raster data, it's crucial to remember that the `affine` library (like GDAL and Rasterio) typically uses an origin (0,0) at the upper-left corner of the image, with Y increasing downwards. This differs from a standard Cartesian coordinate system where Y typically increases upwards.
fix
Always consider the coordinate system conventions (e.g., 'pixel corner' vs 'pixel center', Y-axis direction) when applying or interpreting affine transforms for geospatial data. Tools like `Affine.from_gdal()` can help convert standard GDAL geotransforms.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'affine'
The 'affine' library is not installed in the Python environment.
fix
Install the 'affine' library using pip: 'pip install affine'.
ImportError: cannot import name 'Affine' from 'affine'
The 'Affine' class is not directly accessible from the 'affine' module.
fix
Import the 'Affine' class correctly: 'from affine import Affine'.
TypeError: 'Affine' object does not support item assignment
Attempting to modify an immutable 'Affine' object directly.
fix
Create a new 'Affine' object with the desired modifications instead of modifying the existing one.
AttributeError: 'DatasetReader' object has no attribute 'affine'.
When working with `rasterio` datasets, the affine transformation matrix is accessed via the `.transform` attribute, not the deprecated `.affine` attribute.
fix
Use `raster.transform` instead of `raster.affine`. For example, if `raster = rasterio.open(...)`, use `affine_transform = raster.transform`.
ModuleNotFoundError: No module named 'affine'.
The `affine` Python package is not installed in your current Python environment.
fix
Install the package using pip: `pip install affine`.
Upgrade
Version history
3.0.0latest on PyPI · released Aug 8, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
70 hits · last 30 days
node
60
Amazon
1
OpenAI (training)
1
Resources
affine — pip install affine · libregistry