Skip to content

Instantly share code, notes, and snippets.

@hcarvalhoalves
Created May 6, 2014 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hcarvalhoalves/2d4529d1502db87131a6 to your computer and use it in GitHub Desktop.
Save hcarvalhoalves/2d4529d1502db87131a6 to your computer and use it in GitHub Desktop.
geoalchemy Geometry wrapper for simple "lat,lon" fields
from sqlalchemy import types
from sqlalchemy.sql.expression import func, text
from geoalchemy2 import functions as geofunc
from geoalchemy2.elements import WKTElement
class Coordinate(types.TypeDecorator):
impl = Geometry
def process_bind_param(self, value, dialect):
return 'POINT({} {})'.format(*value.split(',', 1))
def bind_expression(self, bindvalue):
return func.ST_GeomFromText(bindvalue, self.srid, type_=self)
def column_expression(self, bindvalue):
return geofunc.ST_Y(bindvalue, type_=types.String) + text("','") + geofunc.ST_X(bindvalue, type_=types.String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment