Skip to Content

requête SELECT: Postgis

  1. import psycopg2 #module le plus courant pour la connexion à PostgreSQL
  2. conn = psycopg2.connect("dbname='testpostgis'host='localhost' user='moi'")
  3. cur = conn.cursor()
  4. cur.execute("""SELECT astext(the_geom) from testpoly;""")
  5. listpoly = cur.fetchall()
  6. for i in listpoly:
  7. print i
  8. ('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))',)
  9. ('POLYGON((0.275460829493088 0.793364055299539,0.537027649769585 0.813640552995392,0.417396313364055 0.592626728110599,0.226797235023042 0.67778801843318,0.226797235023042 0.744700460829493,0.275460829493088 0.793364055299539))',)
  10. cur.close()
  11. cur = conn.cursor()
  12. query = "SELECT nom, AsText(the_geom), Srid(the_geom) FROM testpoly where nom='%s'" % 'test1'
  13. cur.execute(query)
  14. poly = cur.fetchone()
  15. print poly
  16. ('test1', '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))', 4326)