Calling Python From PHP: A GIS Example

12 Nov

I showed in a past post how you could use Python and CherrPy to make web based GIS applications. While I think this is a great way to develop, I currently have WAMP installed and can run PHP through Apache without any extra configuration. I decided to take a look at how I could run my Python GIS Scripts through a PHP based website. It turns out that it is a rather simple task:

<?php exec(“C:\python25\python”); ?>

That is all there is to it. Now let me give you an example of how I would use this:

  1. I want to create a website where a person can select one of my shapefiles and a toolbox and then have the output sent back to them. If you want you could even let them upload their own file then you run the tools on it – if they wanted the results for their ArcGIS Online maps and lacked processing tools.
  2. For simplicity, I will default to the Gi* toolbox.
  3. When they click submit, I will generate the Python code in the PHP and then run the script. Doing this allows me to customize all of my Python scripts based on user preference. I can plug user input in to all of my functions.
  4. Lastly, the website will hand back the zipped files.

Here is the code for the form.

<H3>Enter a file to run Gi*</H3>
<form name=”input” action=”gi.php” method=”get”>
Shapefile: <input type=”text” name=”shp”><br>
<input type=”submit” value=”Submit”>

Simply a text box to enter a file name and a submit button. The for calls gi.php, which contains:

<H1>Running Gi*</H1>
<h2>On file <?php echo $_GET[“shp”];  ?>

$py= fopen(‘’, ‘w+’);
$header=’import arcgisscripting’.”\n”.’import zipfile’.”\n”;
$gp = ‘gp = arcgisscripting.create()’.”\n”.’gp.workspace = “C:\wamp\www\output”‘.”\n”;
$hs=’hs = gp.HotSpots(“‘.$_GET[“shp”].'”, “Score”, “HotSpots.shp”, “Fixed Distance Band”, “Euclidean Distance”, “None”, “2500”, “#”, “#”)’.”\n”;
$z=’zipped = zipfile.ZipFile(“C:\wamp\www\output\”, mode=”w”)’.”\n”.’zipped.write(“C:\wamp\www\output\hotspots.shp”)’.”\n”.’zipped.write(“C:\wamp\www\output\hotspots.shx”)’.”\n”.’zipped.write(“C:\wamp\www\output\hotspots.prj”)’.”\n”.’zipped.write(“C:\wamp\www\output\hotspots.shp.xml”)’.”\n”.’zipped.write(“C:\wamp\www\output\hotspots.sbx”)’.”\n”.’zipped.write(“C:\wamp\www\output\hotspots.sbn”)’.”\n”.’zipped.write(“C:\wamp\www\output\hotspots.dbf”)’.”\n”.’zipped.close()’;

<H3>Wrote Python….Executing</H3>

<?php exec(“C:\python25\python”); ?>


<a href=”/output/”></a>


The first thing the script does is print the name of the file they entered as an <H2>. Then the PHP opens a python file and writes the code. It then executes the code and returns an HREF to the zipfile. To make the actual Python code clear, I will include it:

import arcgisscripting
import zipfile

gp = arcgisscripting.create()
gp.workspace = “C:\wamp\www\output”
hs = gp.HotSpots(“Input.shp”, “Score”, “HotSpots.shp”, “Fixed Distance Band”, “Euclidean Distance”, “None”, “2500”, “#”, “#”)

zipfile.ZipFile(“C:\wamp\www\output\”, mode=”w”)

I am still on Arc 9.3. That’s why you see the import of arcgisscripting. I believe in 10 it’s arcpy.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: