wasdee / geojsonchemy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GeoJSONchemy [Under Development] 🌍πŸ§ͺ

GeoJSONchemy is a Python library that provides support for GeoJSON spatial data types in SQLAlchemy and SQLModel. It allows you to easily work with GeoJSON data in your database models. It currently supports ONLY PostgreSQL-PostGIS.

Cover Image Gen via MS Copilot Pro, a globe with map creator tools

Installation πŸ“¦

You can install GeoJSONchemy using pip:

pip install geojsonchemy

Usage πŸš€

SQLAlchemy In SQLAlchemy, you can use the GeomJSON and Geomdantic classes from GeoJSONchemy as types for your model fields. Both are subclasses from geoalchemy2.types.Geometry Here's an example:

from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Mapped, mapped_column
from geojsonchemy import GeomJSON, Geomdantic
from geojson_pydantic.geometries import Geometry, Point, MultiPlolygon

Base = declarative_base()

class FooModel(Base):
    __tablename__ = 'foo'

    id: Mapped[int] = Column(Integer, primary_key=True)
    geom: Mapped[dict] = mapped_column(GeomJSON(geometry_type="POINT", srid=4326), nullable=False, index=True)
    geom2: Mapped[Geometry] = mapped_column(Geomdantic(geometry_type="GEOMETRY", srid=4326), nullable=False, index=True)

SQLModel

In SQLModel, you can use the GeomJSON and Geomdantic classes in a similar way:

from sqlmodel import Field, SQLModel

class GeomTable(SQLModel, table=True):
    __tablename__ = "bar"

    id: int = Field(primary_key=True)
    name: str
    geom: dict = Field(
        sa_type=GeomJSON(geometry_type="GEOMETRY", srid=4326),
        nullable=False,
    )
    geom2: Point = Field(
        sa_type=Geomdantic(geometry_type="POINT", srid=4326),
        nullable=False,
    )
    geom3: Multiploygon = Field(
        sa_type=Geomdantic(geometry_type="MULTIPLOYGON", srid=4326, spatial_index=False),
        nullable=False,
    )

Note πŸ“

Please note that GeoJSONchemy currently only supports PostgreSQL. If you try to use it with a different database, it will raise a NotImplementedError.

Contributing 🀝

Contributions are welcome! Please feel free to submit a pull request.

TODO

  • Add support for MutableDict

License πŸ“„

GeoJSONchemy is licensed under the MIT license. See the LICENSE file for more details.

About

License:ISC License


Languages

Language:Python 100.0%