This project has moved and is read-only. For the latest updates, please go here.

Unit conversions for tile based map

Topics: SharpMap v0.9 / v1.x
Dec 27, 2011 at 9:31 PM


I'm trying to get a back-end rendering engine built to pull map data from a local tile server and export a rendered map to a png file.  I've been able to get the basics in place except that I've had no luck figuring out the unit conversion.  How do you go about converting between lat/lon and the map units?

Below is the code I have right now which ends up rendering a section of the map I have no data for.


using System;
using System.Drawing;
using System.Drawing.Imaging;
using BruTile;
using BruTile.Cache;
using BruTile.PreDefined;
using BruTile.Web;
using SharpMap;
using SharpMap.Layers;

namespace TestMapGenerator
    class Program
        static void Main(string[] args)
            var myMap = new Map(new Size(400, 300));

            ITileSource tileSource =
                new TileSource(
                    new WebTileProvider(new TmsRequest(new Uri("http://localhost/OSM/"), "png"),
                                        new FileCache(@"c:\temp\filecache", "png")),
                    new SphericalMercatorInvertedWorldSchema());
            var tileLayer = new TileLayer(tileSource, "OSM");

            var myMapPoint = new SharpMap.Geometries.Point(-77.168, 38.901);
            var box = new SharpMap.Geometries.BoundingBox(-78.01, 38.42, -76.36, 39.46);

            myMap.BackColor = Color.White;
            myMap.Center = myMapPoint;

            var imgMap = myMap.GetMap();

            imgMap.Save(@"c:\temp\test.png", ImageFormat.Png);

Dec 28, 2011 at 1:20 PM

Found an answer over on the Proj.Net project after following a few breadcrumbs around.  Just required a formula for converting between Lat/Lon and Mercator.  Works great now with the code above.