[email protected] wrote:
> Hi,
>
> Is it possible for sqlalchemy to utilise the methods of a SQL Server
> geography instance?
>
> I have been adapting the postgis example from
> http://svn.sqlalchemy.org/sqlalchemy/trunk/examples/postgis/postgis.py
> which I believe formed the foundation for the GeoAlchemy project. I would
> like to get this example working for the SQL Server geography type.
>
> PostGIS, and the other spatial databases supported by GeoAlchemy, use
> database functions for their spatial operations, such as:
>
> ST_AsText(geometry1)
> ST_Intersects(  geometry1  , geometry2  )
>
> So, using the example, the GISElement class can define:
>     @property
>     def wkt(self):
>         return func.ST_AsText(literal(self, Geometry))
>
> and the GISComparator class can do this:
>     def intersects(self, other):
>         return func.ST_Intersects(self.__clause_element__(),
> _to_postgis(other))
>
> My problem arises because SQL Server has the spatial operators as methods
> of the geography type:
> geography1.STAsText ( )
> geography1.STIntersects ( geography2 )
>
> Is it possible to use these geography methods within the GISComparator and
> GISElement classes?
>
> Regards,
> Daniel Pollock
>
> Note: My current workaround is to create two database functions,
> my_STAsText(geography1) and my_STIntersects(geography1, geography2) and
> call these as per the postgis example.

the most direct way is to use the compiler extension. Here is a recipe
which will achieve that:

from sqlalchemy.sql.expression import FunctionElement, column
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.functions import GenericFunction

def compiles_as_bound(cls):
    @compiles(cls)
    def compile_function(element, compiler, **kw):
        return "%s.%s(%s)" % (
            element.clauses.clauses[0],
            element.name,
            ", ".join([compiler.process(e) for e in
element.clauses.clauses[1:]])
        )
    return cls

@compiles_as_bound
class Foo(FunctionElement):
    name = 'foo'

c1, c2, c3 = column('one'), column('two'), column('three')

print Foo(c1, c2, c3)






>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" 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/sqlalchemy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" 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/sqlalchemy?hl=en.

Reply via email to