[postgis-users] postgis & php

Nelson Guda nelsong at mail.utexas.edu
Mon Apr 3 12:52:53 PDT 2006


Hi all,

I am relatively new to postgis.  I've got a database up and running,  
and have used much of the language already.  I am looking for a php  
protocol that can run shp2pgsql and pgsql2shp so that i can use these  
functions via a web interface.

Unfortunately my understanding of programming languages is very  
limited - mostly I have used postgis through pgadmin.

I found a set of php classes on the web that include a function for  
shp2pgsql, but I cannot seem to get it working.  I'll copy those  
functions below.  I don't understand what I might need to change to  
make this work.  Can anyone help me out with this?

Thanks much,
nelsong




-- -- -- -- -- -- -- Postgis classes -- -- -- -- -- -- -- 
/**
* @author Paul Scott
* @copyright Paul Scott
* GPL Package
* Please include credit to me is you use this class!
*/

function connect()
{
        $dbname = 'cdsm';
        $port = '5432';
        $host = 'localhost';

       $dbconn = pg_connect("host=$host port=$port dbname=$dbname");
       if($dbconn)
       {
       return true;
       }
       else
       {
       return false;
       }
}

function close()
{
     $shutdown = pg_close($dbconn);
     if($shutdown)
     {
     return true;
     }
     else
     {
         return false;
     }
}

//function to insert shapefiles (ESRI) into PostGIS DB
function create_sql($filename,$dbname)
{
	//create the tablename from the filename (replace the .shp with shp)
	$tablename1 = str_replace('.','',$filename);
	//Remove last three chars from table name ('shp' is discarded)
	$tablename = substr($tablename1,0,-3);
	//Give the log file a name by tacking .log onto the table name
	$logfile = $tablename.".log";
	//execute the shell script that does all the real work (thanks  
refractions.net)
	$ret = shell_exec("/usr/local/pgsql/bin/shp2pgsql $filename  
$tablename|/usr/local/pgsql/bin/psql -d $dbname 2>&1");
	echo $ret; //debug only (displays the results)
	//open a file handle for binary safe write - this creates a  
logfile...Use it or leave it, just an extra
	$handle = fopen($logfile,'w');
	//Write the log data to a file
	fwrite($handle, $ret);
	//close the file handle
	fclose($handle);
}

function shp2pgsql($filename,$dbname)
{
//loop through all the shapefiles and perform the function on them
foreach (glob("*.shp") as $filename)
{
   //display the file size and name that we are currently working on
   //echo "$filename size " . filesize($filename) . "<br><br>\n";
   //do the sql-ize and insert the data, also write a log
   create_sql($filename,$dbname);
}//end foreach
}//end function




More information about the postgis-users mailing list