Skip to Content

requête avec SQLAlchemy - GeoAlchemy

  1. from sqlalchemy import *
  2. from sqlalchemy.orm import *
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from geoalchemy import *
  5.  
  6. #création de l'"engine"
  7. engine = create_engine('postgres://moi@localhost/testpostgis') # l'instruction ",echo=True"
  8. #placée ici, aurait le même effet que la commande de débogage de SQLObject
  9.  
  10. # ouverture d'une session de travail
  11. session = sessionmaker(bind=engine)()
  12.  
  13. # métadonnées
  14. metadata = MetaData(engine)
  15. Base = declarative_base(metadata=metadata)
  16.  
  17.  
  18. #récupération de la table testpoly dans la classe Testpoly
  19. class Testpoly(Base):
  20. __tablename__= 'testpoly'
  21. __table_args = {'autoload': True}
  22. the_geom = GeometryColumn(Polygon(2))
  23.  
  24. #requête
  25. s = session.query(Testpoly).get(1)
  26. #ou
  27. s = session.query(Testpoly).first()
  28. #géométrie
  29. print session.scalar(s.geom.geometry_type)
  30. 'ST_Polygon'
  31. # géométrie en texte wkt
  32. print session.scalar(s.geom.wkt)
  33. '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))'
  34.  
  35. #géométrie en gml
  36. print session.scalar(s.geom.gml)
  37. '<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>'
  38.  
  39. #géométrie en kml
  40. print session.scalar(s.geom.kml)
  41. '<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>'