Using Virtual Earth with OpenLayers
This article shows you how to use Virtual Earth with OpenLayers. Our example is based on the maps used by the EcoMap Costa Rica project ( http://www.ecomapcostarica.com ), and will overlay external data on a Virtual Earth base map. Of course Virtual Earth has the ability to display a variety of formats on a base map. However OpenLayers adds many more options. For example, our overlay will be a shapefile that is supplied by MapServer as a series of WMS tiles. OpenLayers also allows us to use different base maps as appropriate. For the EcoMap Costa Rica project, Virtual Earth does an excellent job of visualizing Costa Rica and Central America, but does not currently have the detail required for the main area of study which is less than 1km in extent. By using OpenLayers we can easily re-arrange the layers to meet our specific needs.
MapServer is an open source environment for 'building spatially-enabled internet applications' - i.e. web sites. It was originally developed by the University of Minnesota, but it is now managed by the Open Source Geospatial Foundation. The MapServer website is at http://mapserver.gis.umn.edu.
MapServer can run on a wide range of different systems. Although the project uses it in a FreeBSD / Apache environment, I have successfully managed to install it on a Windows XP system in order to use some of the offline data tools. MapServer can be controlled in a variety of ways including: as a CGI script that inserts images into web pages; scripted with PHP/Python/PERL; and as a WMS server. We shall be using it as a WMS server.
Installing MapServer is beyond the scope of this article, but I shall show you how to use it as a WMS server. For installation instructions, it is recommended that you read the tutorials on the MapServer website.
Let's start coding! First, download OpenLayers and install it on your web server. The following HTML page assumes that OpenLayers.js is in your local directory, and the other production elements (eg. themes and styles) are in sub-directories. This is slightly non-standard: OpenLayers.js is usually shipped in the build sub-directory.
Here is the HTML. The comments should make it self explanatory.
A working example can be found at http://www.ecomapcostarica.com/map/map_ve.html.
Virtual Earth supplies the aerial base map. The MapServer-supplied data appears as a cyan river overlay.
Next we need to configure MapServer. It is assumed that you have a compiled and installed MapServer, and have a working mapserv.cgi. This is configured using a MapScript (.MAP) file. These files are used by MapServer to describe the maps that are produced. They define map properties such as data sources, projection, colors, and visibility.
Our data source is a set of 'Synthetic Stream' data for Costa Rica, available from the
Inter American Biodiversity Information Network
here. This river data is derived from digital elevation (DEM) data, so it indicates the theoretical locations of rivers and creeks. This works well in mountainous and steep areas, but the stream locations tend to be poor in flat plains. You will note that streams are plotted as straight lines across reservoirs and lakes. This data is a standard ESRI shape (SHP) file.
First check that your mapserv.cgi supports WMS. On the command line, type 'mapserv -v' (or './mapserv -v' on a Unix system) to check this. You should see a "SUPPORTS=WMS_SERVER" in the list of supported capabilities. If not, you will need to recompile MapServer.
Here is our MapScript file ('streams.map' in the above example):
That is all that is required. Here are the results:
OpenLayers is easy to use. At the moment the biggest limitation is that it only works with Virtual Earth v3 and v4. A ticket has been filed for support for Virtual Earth v6.1, so there is a good chance that the next version will include support for v6.1 before v4 is retired later this year.
MapServer is also easy to use after it has been installed, but building it can be a challenge on some systems. Luckily there are a lot of how-to articles available on the MapServer website.
Useful Websites and Books
MapServer How-To: WMS Server
Inter American Biodiversity Information Network's Digital Elevation (DEM) derived data files
Production Examples of OpenLayers, MapServer and Virtual Earth in use together
"Beginning MapServer Open Source GIS Development", by Bill Kropla; published by Apress 2005.
Discuss this story in the forum.
Author: Richard Marsden
Richard Marsden is the proprietor of Winwaed Software Technology, LLC
which provides software consulting and development services,
specializing in both MapPoint and online mapping applications. He
operates the Mapping-Tools.com
Website for MapPoint Tools and Utilities, and recently launched
the GeoWeb Guru a community
website for developers of the geospatial web.
In 2008, Richard was awarded Virtual Earth MVP status by Microsoft.
Prior to Winwaed, Richard worked as a software developer working on
seismic processing algorithms for the oil exploration industry. He holds
geology and geophysics degrees from the University of Cambridge
(Churchill College), and the University of Durham; and an
interdisciplinary MBA from the University of Dallas.