Accessing WMS from Virtual Earth

This article is written for an old version of the Virtual Earth platform.

The Web Mapping Server (WMS) protocol was developed by the Open Geospatial Consortium (OGC) as a standard for accessing maps over the Web. WMS has been widely adopted within the Geographical Information Systems (GIS) community and there is a wealth of information available in WMS format on the Web. A catalogue of sites is available at http://wms-sites.com/.

Microsoft Virtual Earth provides the ability to overlay your own maps and charts using MapCruncher and custom tile servers but does not yet support WMS directly. This article explains how to build a gateway that allows you to include WMS in your Virtual Earth mash-ups.

Recap: Virtual Earth Servers

The Microsoft Virtual Earth servers deliver map tiles based on a quad tree algorithm which splits the world up into 256 x 256 squares at zoom levels 1 to 19. For more information see �Roll Your Own Tile Server� at �http://www.viawindowslive.com/Articles/VirtualEarth/RollYourOwnTileServer.aspx

Web Map Servers

Web Map Servers provide a REST interface to enable access to bitmap images of maps.

Here is a typical request:

http://www2.demis.nl/wms/wms.asp?wms=WorldMap&LAYERS=Coastlines&FORMAT=image/png&VERSION=1.1.1 &SERVICE=WMS&REQUEST=GetMap&CRS=EPSG:4326&BBOX=-180,-90,180,90&WIDTH=800&HEIGHT=600

As you can see the URL consists of a number of options and settings. We're primarily interested in WIDTH and HEIGHT which specify the size of the bitmap and the BBOX which specifies the map extents.

The full WMS protocol is described in a document called �OpenGIS� Web Map Service (WMS) Implementation Specification� available from the OGC web site at http://www.opengeospatial.org/standards/wms

Virtual Earth uses a WGS84 Mercator projection, but WMS servers need you to specify the projection. In this case we�ve specified a Coordinate Reference System (CRS) of EPSG:4326 which is a common geographic projection that is equivalent �to WGS84.

The WMS standard has a wide range of other features that we won�t consider here, including the ability to ask a server for its capabilities.

Building the Glue

To allow access to WMS, we need to find a way of converting Virtual Earth style tile requests into WMS requests and create the Virtual Earth tiles on-the-fly. Sounds complicated, but in fact most of the hard work is already done and described in the �Roll Your Own Tile Server� article at http://www.viawindowslive.com/Articles/VirtualEarth/RollYourOwnTileServer.aspx . We can take that code sample as a template for this project.

We just need to set up an HTTP handler to intercept Virtual Earth Tile requests and fulfil them with information from WMS � essentially a kind of proxy server.

Algorithm

The algorithm is as follows:

For each tile request...

Find the quad key from the URL and use this to determine a bounding box of coordinates in WGS84 latitude and longitude.

Build a request to the WMS server using this bounding box and specifying a bitmap size of 256x256. Make the request using an HTTP GET.

Return the resultant WMS bitmap as if it were a Virtual Earth Tile.

Code

The resultant code looks like this:


public void ProcessRequest(HttpContext context)
{
    // Extract the QuadKey from the URL
    string urlString = context.Request.Url.ToString();
    string quadKey = GetQuadKey(urlString);

    int zoomLevel = quadKey.Length;

    // Use the quadkey to determine a bounding box for the requested tile
    Box boundingBox = QuadKeyToBox(quadKey);

    // Get the lat longs of the corners of the box
    double lon = XToLongitudeAtZoom(boundingBox.x * TILE_SIZE, 18);
    double lat = YToLatitudeAtZoom(boundingBox.y * TILE_SIZE, 18);

    double lon2 = XToLongitudeAtZoom((boundingBox.x + boundingBox.width) * 256, 18) ;
    double lat2 = YToLatitudeAtZoom((boundingBox.y - boundingBox.height) * 256, 18);


    string url = string.Format(
    "http://www.mapsherpa.com/cgi-bin/wms_iodra?LAYERS=Bathymetry&FORMAT=image%2Fgif
    &VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.
    ogc.se_inimage&SRS=EPSG%3A4326&BBOX={0}&WIDTH=256&HEIGHT=256",
       string.Format("{0},{1},{2},{3}", lon, lat, lon2, lat2));


    Bitmap wmsBitmap = DownloadImage(url);

    Bitmap tileBitmap = new Bitmap(TILE_SIZE, TILE_SIZE);
    Graphics graphics = Graphics.FromImage(tileBitmap);

    graphics.DrawImage(wmsBitmap, 0, 0, TILE_SIZE, TILE_SIZE);

    graphics.Dispose();

    // Stream the image out in respnse to the HTTP request
    MemoryStream streamImage = new MemoryStream();
    tileBitmap.Save(streamImage, ImageFormat.Png);
    context.Response.Clear();
    context.Response.ContentType = "image/png";
    context.Response.AddHeader("content-length",
        System.Convert.ToString(streamImage.Length));
    context.Response.BinaryWrite(streamImage.ToArray());

    // Clean up
    tileBitmap.Dispose();
    streamImage.Dispose();
    wmsBitmap.Dispose();
}

Just replace the URL line with details of the WMS server you want to access.

Source Code

You can download the source here

Example

I attach a working example of the code with a simple web page and Virtual Earth control that can be used as a test.

The amazing thing is that this all works in 3D without adding any extra code:

WMS Sample

(Bathymetry courtesy of www.mapsherpa.com)

References

[1] http://wms-sites.com/ A catalog of WMS sites.

[2] Roll Your Own Tile Server, http://www.viawindowslive.com/Articles/VirtualEarth/RollYourOwnTileServer.aspx

[3] OpenGIS Web Map Service (WMS) Implementation Specification, http://www.opengeospatial.org/standards/wms

 

Rob Blackwell

Rob Blackwell is a director at Active Web Solutions Ltd (www.aws.net), a Microsoft Partner that specialises in Virtual Earth and Web GIS solutions in the UK and Europe.

Email rob.blackwell@aws.net� Blog: www.robblackwell.org.uk

Have you got something to contribute?

Copyright 2009. Sponsored by nsquared.   |  Terms Of Use  |  Privacy Statement
Content on this site is generated from the developer community and shared freely for your enjoyment and benefit. This site is run independently of Microsoft and does not express Microsoft's views in any way.