[postgis-users] PHP tutorial

david techer davidtecher at yahoo.fr
Mon Oct 3 11:25:44 PDT 2005


Kjel is right!

Have a look at Paul's class...Very interesting wrapper

For my personal work, I use it a lot...Thanks Paul :-)

You need adding this code Paul's code:

function postgis($h_host, $h_port, $h_user, $h_password, $h_dbname)
{
    $this->host = $h_host;
    $this->port = $h_port;
    $this->user = $h_user;  
    $this->password = $h_password;   
    $this->dbname = $h_dbname; 
}

function IsAPostGISDataBase($dbname)
{
        $this->connect($dbname);
    $query = "SELECT COUNT(*)>0 AS present FROM pg_proc WHERE proname='postgis_full_version'::text";
    $rec = $this->exec($query);
    if($rec != 0)
    {
          $this->nextRow();  
          $arr = $this->farray(); 
          return $arr;
    }
    else
    {
        return $this->errorMsg();
    }
    $this->close();
  
}

Here is an example about using it in order to have:

- 1)PostgreSQL DB with PostGIS fuctions
- 2) PostgreSQL DB wihout  PostGIS functions: 

Screenshot available at http://techer.pascal.free.fr/postgis/Capture-1.png

==========================================================================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>	<head>
	<meta http-equiv="Pragma" content="no-cache">
	<meta http-equiv="expires" content="0">	<meta http-equiv="content-type" content="text/html;charset=UTF-8">	
	<link href="./configuration/main.css" rel="stylesheet" type="text/css">
	
	</head>
<BODY>
<table width=90%><tr><td align="left">
<fieldset><legend><font class='titre'>Informations Générales</font></legend>
<?php
// Inclusion de la class de Paul Scott: PostGIS Wrapper Php
include("./postgis_class_inc.php");
include("./getconf.php");

$Connexion = new postgis($conf_host,$conf_port,$conf_user,$conf_passwd,$conf_dbname);
//
// Ouverture de la connexion
//
 $Connexion->connect($Connexion->dbname);// Création d'in objet PostgreSQL
 // Ouverture d'une connexion
 //
 // ---> Requête pour lister les bases présentes
 //
/*
    Quelques informatios d'ordre générale:
     - version de php utilisé
     - version de postgresql
     - version de postgis
     - version de geos
     - version de proj

*/ 
function SendInfo($OnLeft,$OnRight)
{
   		echo "<div class='postwrapper'>
 			      <div class='posttopbar'>
  				     <div class='postname'>".$OnLeft."</div>
  				     <div class='postdate'>".$OnRight."</div>
               </div>
             </div>";
}


function SendInfoWithText($OnLeft,$OnRight,$Texte)
{
   		echo "<div class='postwrapper'>
 			        <div class='posttopbar'>
  				       <div class='postname'>".$OnLeft."</div>
  				       <div class='postdate'>".$OnRight."</div>
                 </div>
                  <div class='postcontent'>".$Texte."</div>
               </div>";
}

        $Connexion->exec("SHOW server_version");
        $Connexion->nextRow();  
        $arr = $Connexion->farray();  
        $PG_VERSION = $arr[0];
        $Connexion->exec("SHOW data_directory");
        $Connexion->nextRow();  
        $arr = $Connexion->farray();  
        $PG_DATA= $arr[0];
        
SendInfoWithText("<b>Informations générales</b>",
  			"Informations PostgreSQL",
         "PostgreSQL: Version = ".$PG_VERSION."<br>
           PostgreSQL: PGDATA  = ".$PG_DATA."<br>");  


  $Connexion->exec("SELECT pg_database.datname as Database FROM pg_database, pg_user
     WHERE pg_database.datdba = pg_user.usesysid and pg_database.datname not
     like 'template%' order by datname");   
   $Connexion->setRow(-1);
   $Listing_DB = array();
   $Listing_PostGIS_DB = array();
   $Check_Img_DB = array();
 if ( $Connexion->numRows() )
  {while ($Connexion->nextRow()) 
    { // Début de la boucle while
         $rs = $Connexion->fobject(); 
         
         $pg = new postgis($conf_host,$conf_port,$conf_user,$conf_passwd,$rs->database);
         
         $r = array($pg->IsAPostGISDataBase($pg->dbname));
         $Listing_DB[] = $rs->database;
       
        if ($r[0][0]=='t')
        {
           $Check_Img_DB[] = "<img src='./img/ok.png'/>";
           $Listing_PostGIS_DB[] = $rs->database;
        }
        else
        {
           $Check_Img_DB[] = "<img src='./img/delete.png'/>";
        }
     }// Fin de la boucle while!
     if (count($Listing_PostGIS_DB))// Il y a au moins une base PostGIS
     {
       $pg = new postgis($conf_host,$conf_port,$conf_user,$conf_passwd,$Listing_PostGIS_DB[0]);
       $pg->connect($Listing_PostGIS_DB[0]);
       $pg->exec("SELECT postgis_lib_version(),postgis_geos_version(),postgis_proj_version()");
       $pg->setRow(-1);
       $pg->nextRow(); 
       $rs = $pg->fobject(); 
       $Texte_Info_PostGIS =  "PostGIS: Version = ".$rs->postgis_lib_version."<br>
                               Geos: Version  = ".$rs->postgis_geos_version."<br>
                               Proj: Version  = ".$rs->postgis_proj_version;
     }
     else
     {
       $Texte_Info_PostGIS = "<b>Aucune base PostGIS trouvée</b>";
     }
     
SendInfoWithText("<b>Informations générales</b>",
  			        "Informations PostGIS",
                 $Texte_Info_PostGIS
                 ); 
                 
     for($IntDB=0;$IntDB<count($Listing_DB);$IntDB++)
     {
        SendInfo("<b>".intval($IntDB+1)." - base = ".$Listing_DB[$IntDB]."</b>",
  			                 $Check_Img_DB[$IntDB]
                         );
     }
    
  }
  else // Oh! Aucune base trouvée!
  {echo "Oups!...Apparement aucune base n'a été trouvée à part les bases habituelles
         template0 et template1. Il se peut aussi que votre serveur PostgreSQL soit pas actif ou mauvaise
          connection";}


$Connexion->close();
?>
</fieldset>
</td>
</tr>
</table>
</body></html>

==========================================================================================================================


--david;
-------------------------------------------------
Jean David TECHER
e-mail: davidtecher(at)yahoo(dot)fr
sites: http://www.postgis.fr
       http://techer.pascal.free.fr/postgis/
-------------------------------------------------



Abe Gillespie a écrit :

>Hi Kjel,
>
>You might want to try this project:
>
>http://avoir.uwc.ac.za/projects/postgis/
>
>At the very least you can look at the class's code and see how it does
>things with PostGIS.
>
>-Abe
>
>On 10/3/05, Kjel Anderson <kanderson at maponics.com> wrote:
>  
>
>>Hello list,
>>
>>I am just getting started using PostGIS. I need to find some good working PHP
>>examples. I am trying to figure out how to take an array of raw geometry and
>>turn it into an envelope. I have read the documentation, but I find it a bit
>>sparse. Any help appreciated,
>>
>>Thanks,
>>Kjel
>>_______________________________________________
>>postgis-users mailing list
>>postgis-users at postgis.refractions.net
>>http://postgis.refractions.net/mailman/listinfo/postgis-users
>>
>>    
>>
>_______________________________________________
>postgis-users mailing list
>postgis-users at postgis.refractions.net
>http://postgis.refractions.net/mailman/listinfo/postgis-users
>
>  
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20051003/d8b2d036/attachment.html>


More information about the postgis-users mailing list