Hello all,

I've being trying to get Elixir to work along with GeoAlchemy.
I saw that I an not the first one to consider this idea
http://groups.google.vu/group/geoalchemy/browse_thread/thread/c85a161f08004292
and even though Sanjiv Singh (the author of GeoAlchemy) was sceptic
about it (see link) I managed to get both extensions
working with small modification to Elixir (patch and working example attached).

However I am still unable to use PostGIS comparator ('wkt' and other properties)
even though the ColumnProperty is added to mapper.

With following redefinition of GeometryColumn the 'wkt' attribute
still references geoalchemy.functions.functions.wkt

from functools import partial
from geoalchemy import GeometryColumn as _GeometryColumn
GeometryColumn = partial(_GeometryColumn, comparator=PGComparator)

Do you have a clue what goes wrong here?

Any suggestions and comments are appreciated.

Thanks,

-- 
Łukasz Cieśnik

-- 
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en.

=== modified file 'elixir/fields.py'
--- elixir/fields.py	2009-10-01 14:12:10 +0000
+++ elixir/fields.py	2010-06-27 11:49:30 +0000
@@ -106,7 +106,7 @@
         has_field('name', String(50))
 '''
 from sqlalchemy import Column
-from sqlalchemy.orm import deferred, synonym
+from sqlalchemy.orm import deferred, synonym, ColumnProperty
 from sqlalchemy.ext.associationproxy import association_proxy
 
 from elixir.statements import ClassMutator
@@ -132,6 +132,7 @@
             kwargs['nullable'] = not kwargs.pop('required')
         self.type = type
         self.primary_key = kwargs.get('primary_key', False)
+        self.col_factory = kwargs.pop('col_factory', Column)
 
         self.column = None
         self.property = None
@@ -155,8 +156,16 @@
             self.create_col()
 
     def create_col(self):
-        self.column = Column(self.colname, self.type,
-                             *self.args, **self.kwargs)
+        result = self.col_factory(self.colname, self.type,
+                                  *self.args, **self.kwargs)
+        if isinstance(result, Column):
+            self.column = result
+        elif isinstance(result, ColumnProperty):
+            assert len(result.columns) == 1
+            self.column = result.columns[0]
+            self.property = result
+        else:
+            raise RuntimeError("unknown column type: %r" % result)
         self.add_table_column(self.column)
 
     def create_properties(self):

"""Elixir + GeoAlchemy example."""

from elixir import *
from geoalchemy import *
from sqlalchemy import orm

Session = orm.scoped_session(orm.sessionmaker())
__session__ = Session

class POI(Entity):
    using_options(tablename='pois')
    has_field('address', Unicode(255))
    has_field('location', Point(2), col_factory=GeometryColumn)

def setup_db():
    GeometryDDL(POI.table)
    create_all()
    if POI.query.count() == 0:
        Session.add_all([
                POI(location='POINT(52.229676 21.012229)', address=u'Warsaw, Poland'),
                POI(location='POINT(50.064650 19.944980)', address=u'Krakow, Poland'),
                POI(location='POINT(52.406374 16.925168)', address=u'Poznan, Poland'),
                POI(location='POINT(51.107885 17.038538)', address=u'Wroclaw, Poland'),
                POI(location='POINT(54.352025 18.646638)', address=u'Gdansk, Poland'),
                ])
        Session.commit()

def main():
    metadata.bind = 'postgresql:///lukasz'
    setup_all()
    setup_db()
    for poi in POI.query.order_by(
            POI.location.distance('POINT(51.919438 19.145136)')):
        print poi.address

if __name__ == '__main__':
    main()

Reply via email to