Mapping shapefile polygons
May 10, 2014
Above is a static image captured of a map produced using code in the exercise below, which displays warning polygons. The image above also showed locations of tweets within that same thirty minute timeframe, which mentioned certain weather terms. The map produced in this exercise will only deal with displaying the warnings, and will be interactive.
The goal of this exercise is to produce an embedded, interactive map on a web page, which will display the latest warnings issued by the National Weather Service. These warnings are updated by the NWS every minute, 24 hours a day, 365 days a year. You can read more about these files here.
How it Works
Running the ruby script will:
- Download the latest warnings issued by the National Weather Service.
- Extract any warning polygons of the type you specify. There are four available: SVR (Severe Thunderstorm), TOR (Tornado), FFW (Flash Flood), and SMW (Special Marine Warnings).
- Create a file called polygons.js, which will be used to display them on a map embedded in a web page.
The web page will display a map – in this example centered on the continental US – and show any warnings that might exist at the time the ruby script was run to generate them.
In the end, an automated process could be set up so that the ruby script was executed every n minutes (ie. with launchd on a Mac), and the map would be periodically updated with the latest weather information.
There are a few things you will need to have installed on your system before we can begin. The instructions for this are assuming a user on OS X.
First, you should install GEOS(Geometry Engine, Open Source). If you have brew installed on your system, this is a simple process:
brew install GEOS
Next, you will want to install a few necessary gems if they are not already part your system. You should begin with rubyzip, which will allow us to work with our downloaded zip file.
gem install rubyzip
Next, you will want to install the two gems which will allow us to extract the polygon information we need for displaying on the web. You will want to specify where GEOS was installed. The following examples are for our installation with brew.
gem install rgeo -- --with-geos-dir=/usr/local/lib gem install rgeo-shapefile -- --with-geos-dir=/usr/local/lib
Note: if your geos is installed in another location, you can check where by using:
sudo find / -name 'libgeos*'
Note: depending on your setup, you might be required to sudo when installing the gems.
The Code to Process Current Warnings
Now, below is the Ruby code for the script which will be used to build the polygon.js file to display any current warnings on our map.
Near the bottom of the code, you will find a section noted where there are three items you must set before you run it:
- @targetshapefiles - the location where you want to unzip the warning files
- warningtype – choose the type of warnings you are interested in displaying
The Code to Display the Map
Download leaflet from their website here: http://leafletjs.com/download.html
Here is the code for the HTML page. You may have to alter the references to resources as needed.
That’s it. You’re now ready to display your map. Enjoy!