{"id":1893,"library":"affine","title":"Affine","description":"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.","status":"active","version":"2.4.0","language":"en","source_language":"en","source_url":"https://github.com/rasterio/affine","tags":["affine","transformation","matrix","gis","geometry","raster"],"install":[{"cmd":"pip install affine","lang":"bash","label":"Install stable version"},{"cmd":"pip install --pre affine","lang":"bash","label":"Install pre-release version (e.g., 3.0rcX)"}],"dependencies":[],"imports":[{"symbol":"Affine","correct":"from affine import Affine"}],"quickstart":{"code":"from affine import Affine\n\n# Create an identity affine transform\nidentity = Affine.identity()\nprint(f\"Identity: {identity}\")\n\n# Create a translation transform (move by 10 units in x, 20 in y)\ntranslation = Affine.translation(10, 20)\nprint(f\"Translation: {translation}\")\n\n# Create a scaling transform (scale by 2x)\nscaling = Affine.scale(2.0)\nprint(f\"Scaling: {scaling}\")\n\n# Combine transforms by multiplication (scaling then translation)\ncombined = translation * scaling\nprint(f\"Combined (scale then translate): {combined}\")\n\n# Apply the combined transform to a point (0, 0)\nx, y = 0, 0\nx_prime, y_prime = combined * (x, y)\nprint(f\"Original point ({x}, {y}) transformed to ({x_prime}, {y_prime})\")\n\n# Example of rotation (rotate by 45 degrees)\nrotation = Affine.rotation(45.0)\nprint(f\"Rotation 45 deg: {rotation}\")\n\n# Apply rotation to a point (1, 0)\nx_rot, y_rot = rotation * (1, 0)\nprint(f\"Point (1,0) rotated 45 deg: ({x_rot:.2f}, {y_rot:.2f})\")","lang":"python","description":"Demonstrates creating identity, translation, scaling, and rotation affine objects, combining them, and applying them to a point."},"warnings":[{"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.","message":"In `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.","severity":"breaking","affected_versions":">=3.0a1"},{"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.","message":"The 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.","severity":"breaking","affected_versions":">=3.0a1"},{"fix":"Review and update rotation angle values if your application relies on a specific direction of rotation for positive angles.","message":"The 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.","severity":"breaking","affected_versions":">=3.0a1"},{"fix":"Ensure your environment uses Python 3.9 or a newer compatible version before upgrading to `affine` 3.0.","message":"For `affine` 3.0 and later, Python 3.9 or newer is required. Previous versions (like 2.4.0) supported Python 3.7+.","severity":"breaking","affected_versions":">=3.0a1"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}