from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from geoalchemy import *
#création de l'"engine"
engine = create_engine('postgres://moi@localhost/testpostgis') # l'instruction ",echo=True"
#placée ici, aurait le même effet que la commande de débogage de SQLObject
# ouverture d'une session de travail
session = sessionmaker(bind=engine)()
# métadonnées
metadata = MetaData(engine)
Base = declarative_base(metadata=metadata)
#récupération de la table testpoly dans la classe Testpoly
class Testpoly(Base):
__tablename__= 'testpoly'
__table_args = {'autoload': True}
the_geom = GeometryColumn(Polygon(2))
#requête
s = session.query(Testpoly).get(1)
#ou
s = session.query(Testpoly).first()
#géométrie
print session.scalar(s.geom.geometry_type)
'ST_Polygon'
# géométrie en texte wkt
print session.scalar(s.geom.wkt)
'POLYGON((0.09094470046083 0.807557603686636,0.141635944700461 0.80147465437788,0.360622119815668 0.702119815668203,0.240990783410138 0.548018433179723,0.086889400921659 0.584516129032258,0.054447004608295 0.742672811059908,0.09094470046083 0.807557603686636))'
#géométrie en gml
print session.scalar(s.geom.gml)
'<gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>0.09094470046083,0.807557603686636 0.141635944700461,0.80147465437788 0.360622119815668,0.702119815668203 0.240990783410138,0.548018433179723 0.086889400921659,0.584516129032258 0.054447004608295,0.742672811059908 0.09094470046083,0.807557603686636</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon>'
#géométrie en kml
print session.scalar(s.geom.kml)
'<Polygon><outerBoundaryIs><LinearRing><coordinates>0.09094470046083,0.807557603686636 0.141635944700461,0.80147465437788 0.360622119815668,0.702119815668203 0.240990783410138,0.548018433179723 0.086889400921659,0.584516129032258 0.054447004608295,0.742672811059908 0.09094470046083,0.807557603686636</coordinates></LinearRing></outerBoundaryIs></Polygon>'