Hack 43. Map Earthquakes in (Nearly) Real Time

With an RSS feed and a bit of Flash, you can map earthquakes around the world, as they happen.

Originally, this hack was going to cover some pretty straightforward HTML screen scraping from the USGS Earthquake web site, but, lucky for us, times have changed. The happiest end for any screen-scraping application is when the screen scraper is no longer needed because the scrapee has made the data available in a machine-readable format. Yesss! In just such a case, the USGS has recently started publishing earthquake alerts in RSS 2.0, which makes mapping recent earthquakes in worldKit a snap.

The USGS's recent earthquake RSS feeds are listed at http://earthquake.usgs.gov/recenteqsww/rss.html, along with directions on how to subscribe to them with several popular RSS newsreaders. Several feeds are available, arranged by magnitude and age. For mapping applications, these RSS files make use of a couple RSS namespaces to include metadata on each quake. The latitude and longitude of each quake epicenter is included via the ICBM namespace (http://postneo.com/icbm/), and the integer value of the Richter Scale magnitude is included in the Dublin Core dc:subject element (http://dublincore.org/).

Here's an example of the format of an RSS item about an earthquake.

Loma Prieta Earthquake October 17, 1989 5:04 pmhttp://wrgis.wr.usgs.gov/dds/dds-29/ 37.04 -121.88 7

Mapping an RSS feed like this is easy with worldKit [Hack #39] . You can find the download, documentation, and examples at http://brainoff.com/worldkit/.

After worldKit is downloaded and installed, the next step is to configure the data feed and images in config.xml. The data feed is specified in the dataurl element, as one of two choices: you can either point the data feed directly to the USGS feed of your choice, or the RSS feed can be periodically downloaded to your server. The difference is primarily in their respective disadvantages. The first option will generate a security warning in the Flash plug-in, since, by default, the player can only make connections to the site where the SWF file is hosted. Keeping a local copy of the feed requires setting up a small cron job to download the file to your server. The entry in your crontab would look something like:

17 * * * * wget -O $HOME/public_html/quakemap/quake.rss http://earthquake.usgs.gov/ recenteqsww/eqs1day-M2.5.xml

The above command should be on one line in your crontab file. You would then point the dataurl element of config.xml at the file on your web server. The other downside of this, of course, is that your local copy of the feed will only be up to date once an hour (or however frequently you set the cron job to run).

As for imagery, the Blue Marble image packaged with worldKit will work just fine out of the box. If you're feeling inspired, you can find other Earth imagery at NASA's Earth Observatory site located at http://earthobservatory.nasa.gov/, and decent world topographic maps can be found at http://flatplanet.sourceforge.net/maps/topo.html. For that matter, any cylindrical-projection (e.g., Mercator) world map will suffice. Save the image on your server, and point worldKit to it in your config.xml file with the dayimg element; you should see something like Figure 4-14.

Figure 4-14. An excerpt from a worldKit earthquake map, showing the Western Hemisphere

Given that the USGS includes the magnitude of each quake in the dc:subject element, you can also configure worldKit to plot the location of each quake with a different size marker relative to its rating on the Richter Scale. Figure 4-14 shows part of a worldKit map made this way. All that's needed is an additional plotsize element in config.xml, which accepts subtags with names corresponding to dc:subject values. The values of the subtags establish the corresponding marker sizes. For earthquake feeds, you can expect Richter magnitudes ranging from 1 to 10, so that's what we'll feed into worldKit:

6 <1>2<2>4<3>6<4>8<5>10 <6>12<7>14<8>16<9>18<10>20

The default plotsize is given by the first value (in this case, 6), to be used when an item's dc:subject is missing or otherwise unusable.

worldKit's use of purely numeric names for XML tags is not exactly legal XMLbut worldKit handles them without complaint.

 

4.10.1. Hacking the Hack

What if you're primarily interested in quakes in your immediate neighborhood? In that case, you can specify a bounding box for your map in terms of latitude and longitude in your config.xml file. Here's an example that covers the San Francisco Bay Area:

38.20 37.30 -121.87 -123.00

If a bounding box is specified, worldKit will only display items in the feed within the bounding box. You only need to provide an image that corresponds to your location. If you don't have a map of your area handy, we can offer a couple of suggestions on how to obtain one easily.

First, you might try MapProxy (http://brainoff.com/worldkit/mapproxy/), a simple web interface to TerraServer, which can fetch U.S. topo maps or aerial photos, given a ZIP Code and distance. MapProxy generates the corresponding worldKit bounding box configuration for you, as well.

Failing that, you can take one of the world maps, and extract the area corresponding to your bounding box with Photoshop or ImageMagick. To convert the bounding box in latitude and longitude to x- and y-coordinates in the image, use the following formulas from Table 4-1.

Table 4-1. Formulas for converting a bounding box into image coordinates

Side

Conversion formula

Top

(90 - north) * height / 180

Bottom

(90 - south) * height / 180

Left

(180 + west) * width / 360

Right

(180 + east) * width / 360

Mikel Maron

Категории