GIS and Media fusion

"The explosive growth of the GeoWeb and geographic information has made GIS powerful media for the general public to communicate, but perhaps more importantly, GIS have also become media for constructive dialogs and interactions about social issues." - Sui & Goodchild

User Tools

Site Tools


ogo14:postgisbasics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
ogo14:postgisbasics [2014/05/07 23:33]
oertz created
ogo14:postgisbasics [2018/05/16 10:05] (current)
Line 1: Line 1:
 ====== PostGIS, les bases ====== ====== PostGIS, les bases ======
 +Voyons comment utiliser l'​extension PostGIS de Postgresql pour stocker et gérer de l'​information géographique dans un SGBD.
 +
 +Ne pas oublier l'​indispensable [[http://​postgis.refractions.net/​documentation/​manual-1.5/​|documentation]] avec son chapitre [[http://​postgis.refractions.net/​documentation/​manual-1.5/​reference.html|PostGIS Reference]]
 +
   * Créer une //​géotable//​   * Créer une //​géotable//​
 <​code>​ <​code>​
Line 5: Line 9:
 SELECT AddGeometryColumn( '​my4capitals',​ '​the_geom',​ 4326, '​POINT',​ 2 ); SELECT AddGeometryColumn( '​my4capitals',​ '​the_geom',​ 4326, '​POINT',​ 2 );
 </​code>​ </​code>​
 +  * Alimenter la table
 +    * http://​fr.wikipedia.org/​wiki/​Well-known_text
 +<​code>​
 +INSERT INTO my4capitals VALUES ( nextval('​my4capitals_id_seq'​),​ '​Paris',​ ST_GeometryFromText( '​POINT(2.333 48.867)',​ 4326)); ​
 +INSERT INTO my4capitals VALUES ( nextval('​my4capitals_id_seq'​),​ '​Bern',​ ST_GeometryFromText( '​POINT(7.433 46.95)',​ 4326));
 +INSERT INTO my4capitals VALUES ( nextval('​my4capitals_id_seq'​),​ '​Rome',​ ST_GeometryFromText( '​POINT(12.5 41.883)',​ 4326));
 +INSERT INTO my4capitals VALUES ( nextval('​my4capitals_id_seq'​),​ '​Madrid',​ ST_GeometryFromText( '​POINT(-3.71 40.41)',​ 4326));
 +</​code>​
 +  * Lire la table et ses données géographiques
 +<​code>​
 +SELECT ST_AsEWKT(the_geom) FROM my4capitals;​
 +SELECT ST_AsGeoJSON(the_geom) FROM my4capitals;​
 +</​code>​
 +  * Transformer des coordonnées d'une projection à une autre
 +<​code>​
 +SELECT ST_AsEWKT(ST_Transform(the_geom,​ 21781)) FROM my4capitals;​
 +</​code>​
 +
 +===== Le géoservice Get4Capitals.php =====
 +Cet exemple montre comment créer un service sur mesure qui va //publier// l'​information géographique de la géotable. Il est notamment faire usage du format GeoJSON pour transmettre ces données au client. OpenLayers saura par exemple l'​exploiter facilement.
 +
 +On a d'​abord besoin d'un GeoManager (GeoManager.php) qui sait communiquer avec la geodatabase.
 +<​code>​
 +<?php
 +class Connection {
 +
 +    private static $CONFIG = "​host=172.16.213.132 port=5432 dbname=ogo user=postgres password=postgres";​
 +    private $CONN;
 +
 +    function __construct() {
 +        return $this->​CONN = pg_connect(self::​$CONFIG) or die('​connection failed'​);​
 +    }
 +
 +    function selectQuery($query) {
 +        $result = pg_query($this->​CONN,​ $query);
 +        if (!$result) throw new ErrorException($query);​
 +        return $result;
 +    }
 +
 +    function insertQuery($query) {
 +        pg_query($this->​CONN,​ "BEGIN WORK"​);​
 +        $result = pg_query($this->​CONN,​ $query);
 +        if (!$result) {
 +            pg_query($this->​CONN,​ "​ROLLBACK"​);​
 +        } else {
 +            pg_query($this->​CONN,​ "​COMMIT"​);​
 +        }
 +    }
 +}
 +
 +class Feature
 +{
 + var $type;
 + var $geometry;
 + var $id;
 + var $properties;​
 +
 + function Feature($id,​$geom,​$properties) {
 + $this->​type = "​Feature";​
 + $this->​geometry = $geom;
 + $this->​id = $id;
 + $this->​properties = $properties;​
 + }
 +}
 +
 +class FeatureCollection
 +{
 + var $type;
 + var $features;
 +
 + function FeatureCollection()
 + {
 + $this->​type = "​FeatureCollection";​
 + $this->​features = array();
 + }
 +
 + function addFeature($feature) {
 + array_push($this->​features,​$feature);​
 +
 +}
 +?>
 +</​code>​
 +
 +Et voici le service Get4Capitals.php qui pilote le GeoManager selon ses besoins :
 +<​code>​
 +<?php
 +require_once '​GeoManager.php';​
 +$conn = new Connection();​
 +
 +$query = "​SELECT name, ST_AsGeoJSON(the_geom) from my4capitals";​
 +$result = $conn->​selectQuery($query);​
 +
 +$i = 0;
 +$fc = new FeatureCollection();​
 +while ($row = pg_fetch_row($result)) {
 +    $fc->​addFeature(new Feature($i++,​ (json_decode($row[1])),​ array("​name"​ => $row[0])));
 +}
 +echo json_encode($fc);​
 +?>
 +</​code>​
 +
 +__TODO__
 +  - Tester cet exemple et analyser les différentes étapes du processus
 +  - Terminer l'​exemple en exploitant le géoservice dans OpenLayers avec une OpenLayers.Layer.Vector
 +
ogo14/postgisbasics.1399498384.txt.gz · Last modified: 2018/05/16 10:05 (external edit)