[postgis-users] postgis & php

Nelson Guda nelsong at mail.utexas.edu
Tue Apr 4 11:05:35 PDT 2006

Hi paul,

I was just looking over this, and I have a quick couple of questions.

First - I need to run this php on a website hosted by a webhosting  
company.  What I don't quite get is what the directory address for  
the $filename should be  if the shapefiles are put into some folder  
within my account on that webserver (say for example the www folder).

Also - when I call one of the class functions in a php page, I assume  
that i need to call the function with the appropriate data inside the  
parens  [for example - shp2pgsql($filename,$dbname) ].  So, $filename  
and $dbname need to be defined within that php page.  And if I want  
all shapefiles in a directory converted, then $filename would  
actually be a directory.  Is that correct?  If so, is this directory  
address relative to my user space ('/www' ?) or to the server as a  
whole?  I'm very unclear on what that directory should be.

Thanks so much for your help in this.


On Apr 3, 2006, at 11:39 PM, Paul Scott wrote:

> 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.
> --Paul
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users

More information about the postgis-users mailing list