Last active
December 8, 2015 09:19
-
-
Save atlefren/5f9d917f031b59310981 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import json | |
| import sqlalchemy as sa | |
| from sqlalchemy.types import UserDefinedType | |
| from sqlalchemy.ext.declarative import declarative_base | |
| Base = declarative_base() | |
| class Geometry(UserDefinedType): | |
| def get_col_spec(self): | |
| return "GEOMETRY" | |
| def bind_expression(self, bindvalue): | |
| return sa.func.ST_GeomFromGeoJSON(bindvalue, type_=self) | |
| def column_expression(self, col): | |
| return sa.func.ST_AsGeoJSON(col, type_=self) | |
| def result_processor(self, dialect, coltype): | |
| def process(value): | |
| if value is not None: | |
| return json.loads(value) | |
| return None | |
| return process | |
| def bind_processor(self, dialect): | |
| def process(value): | |
| if value is not None: | |
| return json.dumps(value) | |
| return None | |
| return process | |
| class TableWithGeom(Base): | |
| __tablename__ = 'with_geom' | |
| id = sa.Column(sa.Integer, primary_key=True) | |
| geom = sa.Column('the_geom', Geometry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment