ViewExtents not being correctly set

Apr 25, 2014 at 10:35 AM
Hi

My application works with OSM tiles, and I want to overlay shapefiles using DotSpatial. I stitch together OSM tiles to create my map window in WinForms. For various reasons, my design is to generate overlay "tiles" corresponding to standard OSM tiles containing the relevant portion of the Shapefile image, and cache them to avoid redrawing, and then overlay as desired.

I would have assumed the following code would work, but it doesn't :(

Bitmap GetShapeImage(int tileX, int tileY, int zoom, string shapefile)
{
var dsMap = new DotSpatial.Controls.Map();
dsMap.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;

// Calculate bounds of my tile in lat/long
int n = (int)Math.Pow(2, zoom);
double nwLong = (double)(tileX) / n * 360 - 180;
double nwLat = Math.Atan(Math.Sinh(Math.PI * (1 - 2 * (double)(tileY) / n))) * 180 / Math.PI;
double seLong = (tileX + 1.0) / n * 360 - 180;
double seLat = Math.Atan(Math.Sinh(Math.PI * (1 - 2 * (tileY + 1.0) / n))) * 180 / Math.PI;

var fs = Shapefile.OpenFile(shapefile);

// I need this as I have no prj file with my shapefile data
fs.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;

dsMap.MapFrame.Add(fs);
dsMap.MapFrame.ViewExtents = new Extent(nwLong, seLat, seLong, nwLat);
dsMap.Refresh();
dsMap.DrawToBitmap(bmp, new Rectangle(0, 0, 256, 256));

return bmp;
}

What I find is that ViewExtents is not correctly updated with my calculated tile extent. The result is that my bitmap is of the entire shapefile rather than the programmatically selected geographic region, which should contain some/all/none of the shapefile extent.

Have I done something silly?
Tim