Tag Archives: WAMP

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 mygi.py”); ?>

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.

<html>
<head></head>
<body>
<H1>Gi*</H1>
<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”>
</form>
</body>
</html>

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

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

<?php
$py= fopen(‘mygi.py’, ‘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\hotspots.zip”, 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()’;
fwrite($py,$header);
fwrite($py,$gp);
fwrite($py,$hs);
fwrite($py,$z);
?>

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

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

<H3>Completed…..</H3>

<p>
<a href=”/output/HotSpots.zip”>hotspots.zip</a>
</p>

</body>
</html>

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\hotspots.zip”, mode=”w”)
zipped.write(“C:\wamp\www\output\hotspots.shp”)
zipped.write(“C:\wamp\www\output\hotspots.shx”)
zipped.write(“C:\wamp\www\output\hotspots.prj”)
zipped.write(“C:\wamp\www\output\hotspots.shp.xml”)
zipped.write(“C:\wamp\www\output\hotspots.sbx”)
zipped.write(“C:\wamp\www\output\hotspots.sbn”)
zipped.write(“C:\wamp\www\output\hotspots.dbf”)
zipped.close()

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

Advertisements

Modify Revit Model Data Through the Web

10 Jan

In my post on Programming for Architects I mention how I can use PHP to edit Revit model data.  In this post, I will briefly explain how this is done and point you to a very simple website demonstrating the results.

1) Export your Revit model to a datababse using RevitDBLink. If you don’t care about bringing the results back in you can export a single schedule to CSV and create a database table from that, or export the whole model any way you wish.

2) Download a WAMP stack. WAMP stands for Windows, Apache, MySQL and PHP. You can get the entire stack here:  WAMP Server.

3) Create a webpage in the WWW directory that has a form and whatever else you want to display. The form should call a PHP file. Mine calls insert.php to change the values and a popup.php file to display data. Name the HTML file index.html.

4) Create a PHP file that updates the database.  This is where you need to be able to code, or at least copy and modify code from the web.

5) Start Apache and browse to http://localhost/           to see it in action.

I have a very simple example running on my website. Click on a room to see the current properties. Change the values and click submit. Reclick on a room to see the changes. Currently, this edits the database in real time but requires a user to open Revit and bring the database back in. I see this as a good thing because it adds a layer of protection. It allows the user to import a specific table (rooms) and preview the changes prior to importing.

Happy Hacking.