Skip to main content

Litestar plugin for GeoAlchemy2 - seamless GeoJSON serialization with PostGIS

Project description

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

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

litestar_geoalchemy-0.3.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

litestar_geoalchemy-0.3.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file litestar_geoalchemy-0.3.0.tar.gz.

File metadata

  • Download URL: litestar_geoalchemy-0.3.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for litestar_geoalchemy-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b5510e7a62913a4947e9d12f02e9e4a4ef45b3c2a26221923c2c7000b54bc7d2
MD5 3692712f0345d84b34cb953a4465c8ac
BLAKE2b-256 91e98fb9d63463c81db6b7548b0b5783b044729f7847f4d2c6a6c4e3d63167a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_geoalchemy-0.3.0.tar.gz:

Publisher: publish.yml on cemrehancavdar/litestar-geoalchemy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file litestar_geoalchemy-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for litestar_geoalchemy-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7564aebf24217bae3a4755dcd7077d6303cfad5005772e5530c26585de0afe1
MD5 a1bf52efc80254a1337af19f3ac42e8f
BLAKE2b-256 06621a8d6bf741345cbb5a518431f2db62bb606ed20b64f6afff6aa5ca262736

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_geoalchemy-0.3.0-py3-none-any.whl:

Publisher: publish.yml on cemrehancavdar/litestar-geoalchemy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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