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.2.1.tar.gz (6.9 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.2.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: litestar_geoalchemy-0.2.1.tar.gz
  • Upload date:
  • Size: 6.9 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.2.1.tar.gz
Algorithm Hash digest
SHA256 004f613bca10b149ca6cd1d47bc98f268a7d4ae6259915b442367d57755db5ed
MD5 c26ce021975e9a688c5591466d400f35
BLAKE2b-256 6978b88ded8397273b6b584a2f8c1f7825919e1c51b92239f9b66d27140709b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_geoalchemy-0.2.1.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.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for litestar_geoalchemy-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c6f9df56672c2cd994153b966aef94275b0bd759423109e9bc1e4c006b689c9e
MD5 adbb3a1bf8f9d045409a35f626140307
BLAKE2b-256 d9a372e1b79d5806a7767a9960f59138ce741c5b3dc74687fe7d2e77d21c847b

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_geoalchemy-0.2.1-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