Discussion:
Help on Custom Type with TypeEngine (Oracle SDO_GEOM)
Michael Bayer
2009-09-07 14:55:18 UTC
Permalink
SQL expressions can't be called from within the bind processor of
types. The bind processor only affects the values sent as parameters
to the DBAPI execute() and executemany() functions. There is a
feature request that is as yet unimplemented to provide this feature,
however.

An approach towards embedding geometry functions has been addressed in
a SQLAlchemy example which you can find in the source distribution in
examples/postgis/ . This example builds upon the ORM to provide the
embedding so might not work for your needs.

A full blown package for GIS integration is GeoAlchemy: http://geoalchemy.org/
, which supports three different spatial databases. Oracle's
backend could be added to this package as well if you were in it for
the long haul and could contribute to them. I am not entirely
certain but I think this package may be building off of the examples
in the Postgis example.

Here's yet another approach which uses the compiler extension. This
is at the SQL level and may be enough to suit your needs, and also
adds bind parameter support to your function. It requires the usage
of a custom SQL element for the "value" side of an expression (it is
essentially a more explicit version of how the aforementioned feature
improvement would be implemented):

from sqlalchemy import *
from sqlalchemy.sql import ClauseElement, table, column
from sqlalchemy.ext.compiler import compiles

m = MetaData()

t = table('mytable',
column('some_geom')
)

class GeomValue(ClauseElement):
def __init__(self, wkt, srid):
self.wkt = literal(wkt)
self.srid = srid

@compiles(GeomValue)
def compile_geom(element, compiler, **kw):
return "SDO_GEOMETRY(%s,%s)" % (compiler.process(element.wkt),
element.srid)


print t.insert().values(some_geom=GeomValue("foo", "bar"))

print t.select().where(t.c.some_geom==GeomValue("foo", "bar"))
Hi list,
While trying to build a type for spatial geometry in Oracle Spatial
( SDO_GEOMETRY) , i try to use the build-in function SDO_GEOMETRY from
oracle in the bind_processor for converting WKT (text representation
for spatial geometry) to SDO_GEOM.
The problem is that sqlalchemy place quotes around the SDO_GEOMETRY
expected MDSYS.SDO_GEOMETRY got CHAR
I there a way to call functions in the bind_processor ???
"""convert value from a geometry object to database"""
return None
------> return "SDO_GEOMETRY('%s',%s)" % (value.wkt, self.srid)
return convert
"""convert value from database to a geometry object"""
return None
return asShape(sdo.Geometry(value))
return convert
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---
Nicolas
2009-09-07 11:39:36 UTC
Permalink
Hi list,

While trying to build a type for spatial geometry in Oracle Spatial
( SDO_GEOMETRY) , i try to use the build-in function SDO_GEOMETRY from
oracle in the bind_processor for converting WKT (text representation
for spatial geometry) to SDO_GEOM.

The problem is that sqlalchemy place quotes around the SDO_GEOMETRY
function call and then i have the following error :

DatabaseError: (DatabaseError) ORA-00932: inconsistent datatypes:
expected MDSYS.SDO_GEOMETRY got CHAR

I there a way to call functions in the bind_processor ???

Sample code :

def bind_processor(self, dialect):
"""convert value from a geometry object to database"""
def convert(value):
if value is None:
return None
else:
------> return "SDO_GEOMETRY('%s',%s)" % (value.wkt, self.srid)

return convert

def result_processor(self, dialect):
"""convert value from database to a geometry object"""
def convert(value):
if value is None:
return None
else:
return asShape(sdo.Geometry(value))

return convert

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---
Sanjiv Singh
2009-09-07 15:23:32 UTC
Permalink
Post by Michael Bayer
SQL expressions can't be called from within the bind processor of
types.  The bind processor only affects the values sent as parameters
to the DBAPI execute() and executemany() functions.    There is a
feature request that is as yet unimplemented to provide this feature,
however.
An approach towards embedding geometry functions has been addressed in
a SQLAlchemy example which you can find in the source distribution in
examples/postgis/ .    This example builds upon the ORM to provide the
embedding so might not work for your needs.
A full blown package for GIS integration is GeoAlchemy:  http://geoalchemy.org/
  , which supports three different spatial databases.   Oracle's
backend could be added to this package as well if you were in it for
the long haul and could contribute to them.   I am not entirely
certain but I think this package may be building off of the examples
in the Postgis example.
Absolutely. It has been acknowledged here[1] :)

The code could be improved a lot as has been discussed here earlier[2]
but I have not yet had the time for that.
I would be happy to see some participation. So please feel free to
clone the repo[3] and send pull requests.

regards
Sanjiv

[1] http://geoalchemy.org/intro.html#author
[2] http://groups.google.com/group/sqlalchemy/browse_thread/thread/b84682489891563e#
[3] http://bitbucket.org/sanjiv/geoalchemy/
Post by Michael Bayer
Here's yet another approach which uses the compiler extension.  This
is at the SQL level and may be enough to suit your needs, and also
adds bind parameter support to your function.  It requires the usage
of a custom SQL element for the "value" side of an expression (it is
essentially a more explicit version of how the aforementioned feature
from sqlalchemy import *
from sqlalchemy.sql import ClauseElement, table, column
from sqlalchemy.ext.compiler import compiles
m = MetaData()
t = table('mytable',
    column('some_geom')
)
        self.wkt = literal(wkt)
        self.srid = srid
@compiles(GeomValue)
    return "SDO_GEOMETRY(%s,%s)" % (compiler.process(element.wkt),
element.srid)
print t.insert().values(some_geom=GeomValue("foo", "bar"))
print t.select().where(t.c.some_geom==GeomValue("foo", "bar"))
Hi list,
While trying to build a type for spatial geometry in Oracle Spatial
( SDO_GEOMETRY) , i try to use the build-in function SDO_GEOMETRY from
oracle in the bind_processor for converting WKT (text representation
for spatial geometry) to SDO_GEOM.
The problem is that sqlalchemy place quotes around the SDO_GEOMETRY
expected MDSYS.SDO_GEOMETRY got CHAR
I there a  way to call functions in the bind_processor ???
       """convert value from a geometry object to database"""
               return None
------>        return "SDO_GEOMETRY('%s',%s)" % (value.wkt, self.srid)
       return convert
       """convert value from database to a geometry object"""
               return None
               return asShape(sdo.Geometry(value))
       return convert
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---
Nicolas Dufrane
2009-09-08 07:32:40 UTC
Permalink
thanks for this complete answer !

I will investigate a little more in existing projects like geoalchemy,
mapfish to find a nice way to implement oracle spatial support.

regards
Nicolas
Post by Michael Bayer
Post by Michael Bayer
SQL expressions can't be called from within the bind processor of
types. The bind processor only affects the values sent as parameters
to the DBAPI execute() and executemany() functions. There is a
feature request that is as yet unimplemented to provide this feature,
however.
An approach towards embedding geometry functions has been addressed in
a SQLAlchemy example which you can find in the source distribution in
examples/postgis/ . This example builds upon the ORM to provide the
embedding so might not work for your needs.
http://geoalchemy.org/
Post by Michael Bayer
, which supports three different spatial databases. Oracle's
backend could be added to this package as well if you were in it for
the long haul and could contribute to them. I am not entirely
certain but I think this package may be building off of the examples
in the Postgis example.
Absolutely. It has been acknowledged here[1] :)
The code could be improved a lot as has been discussed here earlier[2]
but I have not yet had the time for that.
I would be happy to see some participation. So please feel free to
clone the repo[3] and send pull requests.
regards
Sanjiv
[1] http://geoalchemy.org/intro.html#author
[2]
http://groups.google.com/group/sqlalchemy/browse_thread/thread/b84682489891563e#
[3] http://bitbucket.org/sanjiv/geoalchemy/
Post by Michael Bayer
Here's yet another approach which uses the compiler extension. This
is at the SQL level and may be enough to suit your needs, and also
adds bind parameter support to your function. It requires the usage
of a custom SQL element for the "value" side of an expression (it is
essentially a more explicit version of how the aforementioned feature
from sqlalchemy import *
from sqlalchemy.sql import ClauseElement, table, column
from sqlalchemy.ext.compiler import compiles
m = MetaData()
t = table('mytable',
column('some_geom')
)
self.wkt = literal(wkt)
self.srid = srid
@compiles(GeomValue)
return "SDO_GEOMETRY(%s,%s)" % (compiler.process(element.wkt),
element.srid)
print t.insert().values(some_geom=GeomValue("foo", "bar"))
print t.select().where(t.c.some_geom==GeomValue("foo", "bar"))
Hi list,
While trying to build a type for spatial geometry in Oracle Spatial
( SDO_GEOMETRY) , i try to use the build-in function SDO_GEOMETRY from
oracle in the bind_processor for converting WKT (text representation
for spatial geometry) to SDO_GEOM.
The problem is that sqlalchemy place quotes around the SDO_GEOMETRY
expected MDSYS.SDO_GEOMETRY got CHAR
I there a way to call functions in the bind_processor ???
"""convert value from a geometry object to database"""
return None
------> return "SDO_GEOMETRY('%s',%s)" % (value.wkt, self.srid)
return convert
"""convert value from database to a geometry object"""
return None
return asShape(sdo.Geometry(value))
return convert
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---
Jarrod Chesney
2009-09-08 23:19:23 UTC
Permalink
Sounds like you are working with Intergraph GIS, Is that correct?
Post by Nicolas Dufrane
thanks for this complete answer !
I will investigate a little more in existing projects like geoalchemy,
mapfish to find a nice way to implement oracle spatial support.
regards
Nicolas
Post by Michael Bayer
Post by Michael Bayer
SQL expressions can't be called from within the bind processor of
types.  The bind processor only affects the values sent as parameters
to the DBAPI execute() and executemany() functions.    There is a
feature request that is as yet unimplemented to provide this feature,
however.
An approach towards embedding geometry functions has been addressed in
a SQLAlchemy example which you can find in the source distribution in
examples/postgis/ .    This example builds upon the ORM to provide the
embedding so might not work for your needs.
http://geoalchemy.org/
Post by Michael Bayer
  , which supports three different spatial databases.   Oracle's
backend could be added to this package as well if you were in it for
the long haul and could contribute to them.   I am not entirely
certain but I think this package may be building off of the examples
in the Postgis example.
Absolutely. It has been acknowledged here[1] :)
The code could be improved a lot as has been discussed here earlier[2]
but I have not yet had the time for that.
I would be happy to see some participation. So please feel free to
clone the repo[3] and send pull requests.
regards
Sanjiv
[1]http://geoalchemy.org/intro.html#author
[2]
http://groups.google.com/group/sqlalchemy/browse_thread/thread/b84682...
[3]http://bitbucket.org/sanjiv/geoalchemy/
Post by Michael Bayer
Here's yet another approach which uses the compiler extension.  This
is at the SQL level and may be enough to suit your needs, and also
adds bind parameter support to your function.  It requires the usage
of a custom SQL element for the "value" side of an expression (it is
essentially a more explicit version of how the aforementioned feature
from sqlalchemy import *
from sqlalchemy.sql import ClauseElement, table, column
from sqlalchemy.ext.compiler import compiles
m = MetaData()
t = table('mytable',
    column('some_geom')
)
        self.wkt = literal(wkt)
        self.srid = srid
@compiles(GeomValue)
    return "SDO_GEOMETRY(%s,%s)" % (compiler.process(element.wkt),
element.srid)
print t.insert().values(some_geom=GeomValue("foo", "bar"))
print t.select().where(t.c.some_geom==GeomValue("foo", "bar"))
Hi list,
While trying to build a type for spatial geometry in Oracle Spatial
( SDO_GEOMETRY) , i try to use the build-in function SDO_GEOMETRY from
oracle in the bind_processor for converting WKT (text representation
for spatial geometry) to SDO_GEOM.
The problem is that sqlalchemy place quotes around the SDO_GEOMETRY
expected MDSYS.SDO_GEOMETRY got CHAR
I there a  way to call functions in the bind_processor ???
       """convert value from a geometry object to database"""
               return None
------>        return "SDO_GEOMETRY('%s',%s)" % (value.wkt, self.srid)
       return convert
       """convert value from database to a geometry object"""
               return None
               return asShape(sdo.Geometry(value))
       return convert
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...