Lightweight SQLAlchemy custom type for PostgreSQL POINT columns with tuple binding and an earth-distance comparator.
Project description
sqlalchemy-postgres-point
Lightweight, pure-Python SQLAlchemy custom type for PostgreSQL POINT columns.
Why
PostgreSQL has a native POINT type (stored internally as a pair of float8 values). SQLAlchemy does not ship a dedicated high-level type wrapper for simple geometric primitives. This package provides a very small PointType you can use immediately without pulling in a full spatial stack (e.g. PostGIS + GeoAlchemy2) when all you need is storing and retrieving (longitude, latitude) pairs.
Features
- Simple
(lng, lat)tuple binding and result conversion. - Safe NULL handling.
- Literal rendering for DDL / SQL emission.
- Custom comparator exposing the PostgreSQL earth-distance
<@>operator (returns aFloat). cache_ok = Truefor SQLAlchemy 2.x compilation caching.
Installation
Using uv (recommended):
uv add sqlalchemy-postgres-point
Or with pip:
pip install sqlalchemy-postgres-point
Usage
from sqlalchemy import Column, Integer
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy_postgres_point import PointType
class Base(DeclarativeBase):
pass
class Place(Base):
__tablename__ = "places"
id = Column(Integer, primary_key=True)
# Store as (longitude, latitude)
location = Column(PointType)
# Example query using the custom comparator
from sqlalchemy import select
origin = (0.0, 0.0)
stmt = select(Place.id, Place.location.earth_distance(origin).label("dist"))
The comparator translates Place.location.earth_distance(origin) into SQL using the <@> operator (requires PostgreSQL with the cube / earthdistance extension for meaningful results; without extensions the operator may not exist—adapt as needed for your environment). This library only emits the operator; it does not manage PostgreSQL extensions.
Returned Python Values
Values are loaded as a 2-tuple of floats (lng, lat) or None when NULL.
Testing
Run the test suite with:
uv run pytest -q
Development
After cloning:
uv sync # installs runtime + dev deps
uv run pytest -q
Project Structure
sqlalchemy_postgres_point/point.py– Implementation ofPointType.tests/test_point.py– Unit tests for processors and comparator.
Limitations / Notes
- No automatic validation of longitude/latitude ranges; add your own if required.
- Uses simple textual representation
(lng,lat)accepted by PostgreSQLPOINTinput parser. - If you need advanced spatial indexing / SRID support, look at GeoAlchemy2/PostGIS instead.
License
MIT (see your project's LICENSE file if added later). Contributions welcome.
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 sqlalchemy_postgres_point-0.1.0.tar.gz.
File metadata
- Download URL: sqlalchemy_postgres_point-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
121edd1d0faa6ec81ab2b404f6c62d680d021201a3b5c7cf7d928f3e3c87853d
|
|
| MD5 |
8407a1fefbbe354df118fc70bf97aa93
|
|
| BLAKE2b-256 |
30c278b35b5d1057b364c7ba8c8d625f486aaae76add3ef2be6dcceb47ecf573
|
File details
Details for the file sqlalchemy_postgres_point-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_postgres_point-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d0fbb81d782af52623404160e9ff19238234b6cdf5795f2424c2f101fbf7096
|
|
| MD5 |
910bcad5e02c638374ce7610a0ad8820
|
|
| BLAKE2b-256 |
ae45de0ad905959694bad9b977d0e026915c12561ab2a2d9faa15986792863ee
|