Skip to main content

Litestar GeoAlchemy2 Plugin

A Litestar plugin for seamless GeoJSON serialization with GeoAlchemy2/PostGIS.

Features

  • Automatic serialization: WKBElement to GeoJSON on response
  • Automatic deserialization: GeoJSON to WKBElement on request
  • Works with SQLAlchemyDTO: Return models directly, no manual mapping
  • All geometry types: Point, LineString, Polygon, Multi*, GeometryCollection
  • Feature support: Accepts raw geometry, Feature, or FeatureCollection input
  • Type validation: Point field rejects Polygon input with clear errors
  • Configurable SRID: Default 4326 (WGS84), customizable per-app
  • OpenAPI schemas: Full GeoJSON spec in Swagger UI

Installation

pip install litestar-geoalchemy advanced-alchemy
# or
uv add litestar-geoalchemy advanced-alchemy

Quick Start

from advanced_alchemy.extensions.litestar import SQLAlchemyDTO, SQLAlchemyDTOConfig
from geoalchemy2 import Geometry
from litestar import Litestar, get, post
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

from litestar_geoalchemy import GeoAlchemyPlugin, Point


# Model
class Base(DeclarativeBase):
    pass


class City(Base):
    __tablename__ = "cities"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column()
    location: Mapped[Point] = mapped_column(Geometry("POINT"))  # type: ignore[assignment]


# DTOs
class CityReadDTO(SQLAlchemyDTO[City]):
    config = SQLAlchemyDTOConfig()


class CityWriteDTO(SQLAlchemyDTO[City]):
    config = SQLAlchemyDTOConfig(exclude={"id"})


# Routes - return models directly!
@get("/cities/{city_id:int}", return_dto=CityReadDTO)
async def get_city(city_id: int, session: AsyncSession) -> City:
    return await session.get(City, city_id)


@post("/cities", dto=CityWriteDTO, return_dto=CityReadDTO)
async def create_city(data: City, session: AsyncSession) -> City:
    session.add(data)
    await session.commit()
    return data


# App
app = Litestar(
    route_handlers=[get_city, create_city],
    plugins=[GeoAlchemyPlugin()],
)

Response

{
    "id": 1,
    "name": "Paris",
    "location": {
        "type": "Point",
        "coordinates": [2.35, 48.86]
    }
}

Input Formats

The plugin accepts three GeoJSON input formats:

# 1. Raw geometry
{"type": "Point", "coordinates": [2.35, 48.86]}

# 2. Feature (geometry extracted automatically)
{
    "type": "Feature",
    "geometry": {"type": "Point", "coordinates": [2.35, 48.86]},
    "properties": {"name": "Paris"}
}

# 3. FeatureCollection (single feature)
{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [2.35, 48.86]},
        "properties": {}
    }]
}

Geometry Types

from litestar_geoalchemy import (
    Point,
    LineString,
    Polygon,
    MultiPoint,
    MultiLineString,
    MultiPolygon,
    GeometryCollection,
    AnyGeometry,  # Accepts any geometry type
)

Configuration

Custom SRID

Default is 4326 (WGS84). For Web Mercator or other projections:

app = Litestar(
    plugins=[GeoAlchemyPlugin(srid=3857)],
)

Error Handling

The plugin provides clear, specific error messages:

from litestar_geoalchemy import GeoJSONError, GeoJSONTypeError, GeoJSONValidationError
Error Message
Type mismatch Expected Point geometry, got Polygon
Missing coordinates Point missing required 'coordinates' field
Missing type GeoJSON missing required 'type' field
Unknown type Unknown GeoJSON type: InvalidType

Requirements

  • Python 3.11+
  • Litestar 2.x
  • GeoAlchemy2
  • Shapely
  • PostGIS database (for production)

Demo

Run the demo app with PostGIS testcontainer (PEP 723 compatible):

git clone https://github.com/cemrehancavdar/litestar-geoalchemy
cd litestar-geoalchemy
uv run demo/app.py

Open http://localhost:8001/schema/swagger

License

MIT

Metadata

Release files for litestar-geoalchemy 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for litestar-geoalchemy 0.3.0
File Size Uploaded Provenance
litestar_geoalchemy-0.3.0.tar.gz 9.5 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for litestar-geoalchemy 0.3.0
File Interpreter ABI Platform
litestar_geoalchemy-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.8 kB

Release files / litestar_geoalchemy-0.3.0.tar.gz

Download URL litestar_geoalchemy-0.3.0.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b5510e7a62913a4947e9d12f02e9e4a4ef45b3c2a26221923c2c7000b54bc7d2
BLAKE2b-256 checksum
How to use checksums
91e98fb9d63463c81db6b7548b0b5783b044729f7847f4d2c6a6c4e3d63167a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 12, 2026.

Transparency log

Release files / litestar_geoalchemy-0.3.0-py3-none-any.whl

Download URL litestar_geoalchemy-0.3.0-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c7564aebf24217bae3a4755dcd7077d6303cfad5005772e5530c26585de0afe1
BLAKE2b-256 checksum
How to use checksums
06621a8d6bf741345cbb5a518431f2db62bb606ed20b64f6afff6aa5ca262736
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 12, 2026.

Transparency log

Security for litestar-geoalchemy 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for litestar_geoalchemy-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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