Tiled WMS overlay in Windows Phone 7

 

  Update 13/03/2013, here is new  blog about Tiled WMS overlay for Windows Phone 8 :https://blog.sumbera.com/2013/03/10/tiled-wms-overlay-in-windows-phone-8/

This post [https://blog.sumbera.com/2010/02/25/overlay-wms-on- google-in-silverlight-bing/ ] talked about how to overlay over the Bing maps Silverlight control Google tiles  and tiled WMS. It was quite easy to port this web sample into the Windows Phone 7 emulator. I took a smaple code from Training kit called “Ex1-TheBingMapControl” and extend it with the 2 classes : GoogleTile and WmsTile, with slight changes to the namespace using Microsoft.Phone.Controls.Maps (original for Silverlight was  Microsoft.Maps.MapControl). Then extend MainPage.XAML with :

                    <my:MapTileLayer Opacity=”1″>
                        <my:MapTileLayer.TileSources>
                            <local:GoogleTile></local:GoogleTile>
                        </my:MapTileLayer.TileSources>
                    </my:MapTileLayer>
                    <my:MapTileLayer Opacity=”1″>
                        <my:MapTileLayer.TileSources>
                            <local:WMSTiledOverlay></local:WMSTiledOverlay>
                        </my:MapTileLayer.TileSources>
                    </my:MapTileLayer>
 
 

Extended sample code availabel for download here : http://www.sumbera.com/lab/wp7/TheBingMapControlWithWMSandGoogle.zip

Update: I found the way calculating BoundingBox from the quadkey somehow limiting (the DeepEarth weay) since it worked only till the level 18. So here is updated class fro getting correct bounding box of the tiled WMS request in the Bing Silverlight control:

            public static double TileToWorldPosX(double tile_x, int zoom) {
return  (float)((tile_x / Math.Pow(2.0, zoom) * 360.0) – 180.0);
}
public static double TileToWorldPosY( double tile_y, int zoom) {
double n = Math.PI – ((2.0 * Math.PI * tile_y) / Math.Pow(2.0, zoom));
return   (float)(180.0 / Math.PI * Math.Atan(Math.Sinh(n)));
}
public override Uri GetUri(int tilePositionX, int tilePositionY, int tileLevel) {
                int zoom = tileLevel; //SSU tileLevel would be same as zoom in Bing control
double deltaX = 0;// 0.00135; //SSU deltaX for SLP WMS
double deltaY = 0;// 0.00058; //SSU deltaY for SLP WMS
double minXLon = TileToWorldPosX(tilePositionX, zoom) + deltaX;
double minYLat = TileToWorldPosY(tilePositionY + 1, zoom) + deltaY;
double maxXLon = TileToWorldPosX(tilePositionX + 1, zoom) + deltaX;
double maxYLat = TileToWorldPosY(tilePositionY, zoom) + deltaY;
string wmsUrl = string.Format(this.UriFormat, minXLon, minYLat, maxXLon, maxYLat, 256);
return new Uri(wmsUrl);
}
}
   

 

Overlay WMS on Google in OpenLayers

[Note: therea are related post: overlyaing tiled WMS over the new Google Map v3 https://blog.sumbera.com/2010/11/02/tiled-wms-overlay-on-google-map-v3/ and overlying tiled WMS over the Silverlight Bing map https://blog.sumbera.com/2010/02/25/overlay-wms-on-google-in-silverlight-bing/ ]

Is it possible to display WMS (EPSG:4326) over the Google (EPSG:900913) in Openlayers ? Yes ! thanks to the great img ‘feature’ that enables you to shrink/expand your return image based on defined image size. That means that if your map view is rectangular or you request WMS as tiles (rectangular too) you get proper overlay of EPSG:4326 on EPSG:900913) . Example of various image sizes follows (these are actually WMS GetMap requests):

256 x 160
256 x 256

Here is the way how to implement it in OpenLayers – very simplified:

1. read this post http://docs.openlayers.org/library/spherical_mercator.html and create your Google map:

var options = {
projection: new OpenLayers.Projection(“EPSG:900913”),
displayProjection: new OpenLayers.Projection(“EPSG:4326”),
units: “m”,
numZoomLevels: 22,
maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
20037508, 20037508.34)
};
map = new OpenLayers.Map(‘map’, options);
            // create Google Mercator layers
            var ghyb = new OpenLayers.Layer.Google(
“Google Hybrid”,
{ type: G_HYBRID_MAP, ‘sphericalMercator’: true }
);

2. add your WMS layer

var gwms = new OpenLayers.Layer.TMS(“SLP”, “http://mapserver-slp.mendelu.cz/cgi-bin/mapserv?map=/var/local/slp/krtinyWMS.map&”,
{
layers: ‘obrys,typologie,hm2003’,
type: ‘png’,
visibility: true,
getURL: get_wms_url,
format: “image/png”,
opacity: 1,
isBaseLayer: false,
deltaX: 0.0013,
deltaY: 0.00058
});

3. include support for reprojection before you include OpenLayers:

<script src =”proj4js/lib/proj4js-combined.js”>script>

4. handle WMS as TMS tiles as this:

function get_wms_url(bounds) {

// recalculate bounds from Google to WGS
var proj = new OpenLayers.Projection(“EPSG:4326”);
bounds.transform(map.getProjectionObject(), proj);

// this is not necessary for most servers display overlay correctly,
//but in my case the WMS  has been slightly shifted, so I had to correct this with this delta shift

bounds.left += this.deltaX;
bounds.right += this.deltaX;
bounds.top += this.deltaY;
bounds.bottom += this.deltaY;

            //construct WMS request

var url = this.url;
url += “&REQUEST=GetMap”;
url += “&SERVICE=WMS”;
url += “&VERSION=1.1.1”;
url += “&LAYERS=” + this.layers;
url += “&FORMAT=” + this.format;
url += “&TRANSPARENT=TRUE”;
url += “&SRS=” + “EPSG:4326”;
url += “&BBOX=” + bounds.toBBOX();
url += “&WIDTH=” + this.tileSize.w;
url += “&HEIGHT=” + this.tileSize.h;
return url;

}

That is, live example you can see here http://www.sumbera.com/lab/wms/getcapWGS.htm

or in MapShake here : http://www.mapshake.cz/mapfs.aspx?i=464