Recent Earthquakes: map generated by GRASS on the fly with PHP

Software: Apache, PHP5, GRASS 6 (see here for PHP4 example; here for GRASS location with world map)

What happens here?
The server is fetching the current list of earthquakes from http://neic.usgs.gov/neis/gis/bulletin.asc and generates a vector map with attributes from that. The result is overlayed to the Blue Marble Next Generation map from May. // // Home: http://grass.itc.it/spearfish/php_grass_earthquakes.php // // Inspired by: // http://grass.itc.it/pipermail/grass5/2004-August/015106.html // // Possible improvements: // - use PHP session to avoid race conditions // (use session name as mapset etc) // - replace calls by GRASS-SWIG-PHP interface // Approach: // GRASS is simply a set of commands which need to be run in // a pre-defined environment. A map is generated as PNG file // and then visualized. // To keep it simple, we use system commands. Note the trailing // semicolon for each command, otherwise it won't work. // Prerequisites: // Add 'MONITOR: PNG' to the .grassrc6 file (see GISRC below). // Update also the path to the location. The GRASS location needs to be // recursively set to the owner/group of Web server (e.g. 'apache' user/group) // as the Web server software launches GRASS. Check for access permissions // also in the parent directories. // Also this directory where the files/map is generated, needs to // be owned by the Web server user. // // --------------------- // Here we go: // 1. define the environment settings (full path everywhere): $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("pipe", "w") // stderr is a file to write to ); $cwd = '.'; // define environment variables $env = array( 'GISBASE' => '/usr/local/grass-6.1.cvs', 'PATH' => '/bin:/usr/bin:/usr/local/bin:/usr/local/grass-6.1.cvs/bin:/usr/local/grass-6.1.cvs/scripts', // WARNING: probably protected in /etc/php.ini (see 'safe_mode_protected_env_vars') 'LD_LIBRARY_PATH' => '/lib:/usr/lib:/usr/local/grass-6.1.cvs/lib', // note that the GRASS location needs owner/group of Web server (e.g. 'apache' user/group) 'GISRC' => '/krokus0/www/grass/html/spearfish/.grassrc6_ll', // also the output files need owner/group of Web server: 'GRASS_PNGFILE' => 'earthquakes.png', 'GRASS_TRUECOLOR' => 'TRUE', 'GRASS_WIDTH' => '900', 'GRASS_PNG_COMPRESSION' => '1', 'HOME' => '/tmp', // better use posix_getpid(); 'GIS_LOCK' => '$$' ); // 3. fetch & polish earthquake data // GRASS session.... // watch out for quotes-in-quotes in SQL commands and $: $command = " lynx -dump http://neic.usgs.gov/neis/gis/bulletin.asc > /tmp/bulletin.tmp; cat /tmp/bulletin.tmp | sed 's+ ++g' | grep -v ',,' | grep -v '^$' > bulletin.asc; env > environment_vars2.txt; rm -f grasserrors2.txt grassmsgs2.txt; echo \$LD_LIBRARY_PATH >> grasserrors2.txt; echo \$PATH >> grasserrors2.txt; echo \$GISBASE >> grasserrors2.txt; echo \$GISRC >> grasserrors2.txt; echo \$GIS_LOCK >> grasserrors2.txt; g.region -d >> grasserrors2.txt; d.mon PNG 2>> grasserrors2.txt; cat bulletin.asc | v.in.ascii out=recent_earthquakes skip=1 fs=',' y=3 x=4 col='e_date date, e_time varchar(10), lat double precision, long double precision, magnitude double precision, depth double precision' --o >> grassmsgs2.txt 2>> grasserrors2.txt; v.db.addcol recent_earthquakes col='class integer' ; v.db.update recent_earthquakes col=class value=1 where='magnitude < 3' ; v.db.update recent_earthquakes col=class value=2 where='magnitude >=3 AND magnitude < 4' ; v.db.update recent_earthquakes col=class value=3 where='magnitude >=4 AND magnitude < 5' ; v.db.update recent_earthquakes col=class value=4 where='magnitude >=5 AND magnitude < 6' ; v.db.update recent_earthquakes col=class value=5 where='magnitude >=6 AND magnitude < 7' ; v.db.update recent_earthquakes col=class value=6 where='magnitude >=8' ; # center at Pacific g.region w=-18 e=-18 d.rast BMNG_May.rgb >> grassmsgs2.txt 2>> grasserrors2.txt; echo '

' ;
  d.vect.thematic recent_earthquakes column=class type=point themetype=graduated_points maxsize=20 nint=6 >> grassmsgs2.txt >> grassmsgs2.txt 2>> grasserrors2.txt;
  echo '
' ; d.mon stop=PNG >> grassmsgs2.txt 2>> grasserrors2.txt; g.remove vect=recent_earthquakes >> grassmsgs2.txt 2>> grasserrors2.txt; \$GISBASE/etc/clean_temp rm -rf /tmp/grass6-\$USER-\$GIS_LOCK rm -f earthquakes_small.jpg ; convert -geometry 75% earthquakes.png earthquakes_small.jpg; convert -geometry 40% earthquakes.png earthquakes_tiny.jpg; "; // command $process = proc_open($command, $descriptorspec, $pipes, $cwd, $env); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // 2 => readable handle connected to child stderr fwrite($pipes[0], ''); fclose($pipes[0]); echo stream_get_contents($pipes[1]); fclose($pipes[1]); echo stream_get_contents($pipes[2]); fclose($pipes[2]); $return_value = proc_close($process); //echo $return_value; } ?>
Earthquake classification (circles in map are sized by these classes)
Magnitude Classification Class for map
0 - 3 Micro 1
3 - 3.9 Minor 2
4 - 4.9 Light 3
5 - 5.9 Moderate 4
6 - 6.9 Strong 5
7 - 7.9 Major 6
8 and higher Great 7

Here a map should appear:

Oldest event from: UTC ; Latest event from: UTC

Latest events drawn last.

Auto-generated earthquake map (GRASS GIS/PHP based)
Click for high-res map

For earthquake details, visit Latest Earthquakes in the World - Last 7 days from USGS.


2006 Markus Neteler (based on comments from Sharyn Namnath and Glynn Clements)
Download PHP page source code
Download the script implemented as SHELL script code