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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
004f613bca10b149ca6cd1d47bc98f268a7d4ae6259915b442367d57755db5ed
|
|
| MD5 |
c26ce021975e9a688c5591466d400f35
|
|
| BLAKE2b-256 |
6978b88ded8397273b6b584a2f8c1f7825919e1c51b92239f9b66d27140709b0
|
Provenance
The following attestation bundles were made for litestar_geoalchemy-0.2.1.tar.gz:
Publisher:
publish.yml on cemrehancavdar/litestar-geoalchemy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litestar_geoalchemy-0.2.1.tar.gz -
Subject digest:
004f613bca10b149ca6cd1d47bc98f268a7d4ae6259915b442367d57755db5ed - Sigstore transparency entry: 855303590
- Sigstore integration time:
-
Permalink:
cemrehancavdar/litestar-geoalchemy@2222bccd3908429aeb0f32b42cd152c07e03c992 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/cemrehancavdar
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2222bccd3908429aeb0f32b42cd152c07e03c992 -
Trigger Event:
release
-
Statement type:
File details
Details for the file litestar_geoalchemy-0.2.1-py3-none-any.whl.
File metadata
- Download URL: litestar_geoalchemy-0.2.1-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6f9df56672c2cd994153b966aef94275b0bd759423109e9bc1e4c006b689c9e
|
|
| MD5 |
adbb3a1bf8f9d045409a35f626140307
|
|
| BLAKE2b-256 |
d9a372e1b79d5806a7767a9960f59138ce741c5b3dc74687fe7d2e77d21c847b
|
Provenance
The following attestation bundles were made for litestar_geoalchemy-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on cemrehancavdar/litestar-geoalchemy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litestar_geoalchemy-0.2.1-py3-none-any.whl -
Subject digest:
c6f9df56672c2cd994153b966aef94275b0bd759423109e9bc1e4c006b689c9e - Sigstore transparency entry: 855303597
- Sigstore integration time:
-
Permalink:
cemrehancavdar/litestar-geoalchemy@2222bccd3908429aeb0f32b42cd152c07e03c992 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/cemrehancavdar
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2222bccd3908429aeb0f32b42cd152c07e03c992 -
Trigger Event:
release
-
Statement type: