[postgis-users] postgis & php
Paul Scott
pscott at uwc.ac.za
Mon Apr 3 21:39:10 PDT 2006
Hi Nelsong,
This code looks vaguely familiar :) although really dated, it will still
work...
On Mon, 2006-04-03 at 14:52 -0500, Nelson Guda wrote:
> 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?
>
check out http://avoir.uwc.ac.za/projects/postgis in the CVS section for
a more updated version. That being said, lets fix up your problem...
> //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);
> }
This function basically does all the magic, but should be a private
function. Which leaves us with the function below:
>
> 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
>
Here the function will take a directory and go through the entire
directory and convert all shapefiles to postgis sql statements. It can
also execute the sql...
A couple of things you should look out for though:
1. Make sure that your webserver user has access to the database (in my
case its www-data.
2. Make sure that your shapefiles are readable by PHP/Web user.
Then all should be OK.
HTH
--Paul
More information about the postgis-users
mailing list