[postgis-users] postgis & php

Paul Scott pscott at uwc.ac.za
Tue Apr 4 21:44:07 PDT 2006


On Tue, 2006-04-04 at 13:05 -0500, Nelson Guda wrote:
> 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).
> 
Pass the directory name to the function as a full path. i.e.
'/var/www/mydata/'

> 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.
> 
Pass in the directory name. In this version, the directory and file name
are pretty interchangeable. The updated code is a lot better at handling
the whole deal.

> Thanks so much for your help in this.
> 
> nelson
> 
> 
> 
> 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.
> >
> > HTH
> >
> > --Paul
> >
> > _______________________________________________
> > 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




More information about the postgis-users mailing list