This project is read-only.

Load mbtiles

Sep 16, 2014 at 12:28 PM
Hello!!

Can anybody please post a code snippet or "how-to" mechanism to load mbtiles in a map control(offline)

Thanks
Sep 16, 2014 at 2:14 PM
Edited Sep 16, 2014 at 2:15 PM
Have a look at https://github.com/FObermaier/DotSpatial.Plugins
var config = new DotSpatial.Plugins.BruTileLayer.Configuration.MbTilesConfiguration(/*your path goes here*/);
var layer = new DotSpatial.Plugins.BruTileLayer(config);
The plugin has (limited) reprojection capabilities for the tiles. It has a user interface to allow adding new layers. It might solve your TMS question as well.

NOTE: You will have to compile the plugin yourself!
Sep 16, 2014 at 4:50 PM
Hi,Is there any way to fit a wgs84 on BruTileLayer?Does it need any reprojection?whn I set it it show in wrong place
Sep 16, 2014 at 6:59 PM
Iirc you must not set the Layer.Projection, you need to call Layer.Reproject(...). But I have very limited experience with DotSpatial.
Sep 16, 2014 at 8:12 PM
FObermaier wrote:
Iirc you must not set the Layer.Projection, you need to call Layer.Reproject(...). But I have very limited experience with DotSpatial.
thanks,So you mean I have to change all of my layers projections to google maps(Brutile) projection?right?
Sep 17, 2014 at 8:35 AM
Edited Sep 17, 2014 at 8:47 AM
If you add a BruTileLayer as backgroundlayer, you'll get best results if you reproject all your other layers to the projection of the BruTileLayer.

Same applies to WebMap plugin, I suppose.
Sep 17, 2014 at 8:50 PM
Thank you so much for your helps.But I could not find backgroundlayer property in map control.when I use reproject my layers disappear and could not be found :(
Sep 19, 2014 at 10:14 AM
Edited Sep 19, 2014 at 10:15 AM
This works for me:
private static void Reproject(DotSpatial.Controls.Map map, DotSpatial.Projections.ProjectionInfo proj)
{
    LogManager.DefaultLogManager.LogMessage(string.Format("Reprojecting from '{0}' to '{1};'", map.Projection.Name, proj.Name), DialogResult.OK);
    
    var extents = map.ViewExtents;
    var oldProjection = map.Projection;
    map.Projection = proj;
    var newExtents = Reproject(extents, oldProjection, map.Projection);

    foreach (var layer in map.Layers)
    {
        layer.Reproject(map.Projection);
    }

    map.ViewExtents = newExtents;
    map.Invalidate();
}

static Extent Reproject(Extent extent, ProjectionInfo source, ProjectionInfo target, int depth = 0)
{
    var xy = ToSequence(extent);
    DotSpatial.Projections.Reproject.ReprojectPoints(xy, null, source, target, 0, xy.Length / 2);
    var res = ToExtent(xy);

    return res;
}

static double[] ToSequence(Extent extent)
{
    const int horizontal = 72;
    const int vertical = 36;
    var res = new double[horizontal * vertical * 2];

    var dx = extent.Width / (horizontal - 1);
    var dy = extent.Height / (vertical - 1);

    var minY = extent.MinY;
    var k = 0;
    for (var i = 0; i < vertical; i++)
    {
        var minX = extent.MinX;
        for (var j = 0; j < horizontal; j++)
        {
            res[k++] = minX;
            res[k++] = minY;
            minX += dx;
        }
        minY += dy;
    }

    return res;
}

private static Extent ToExtent(double[] xyOrdinates)
{
    double minX = double.MaxValue, maxX = double.MinValue;
    double minY = double.MaxValue, maxY = double.MinValue;

    var i = 0;
    while (i < xyOrdinates.Length)
    {
        if (!double.IsNaN(xyOrdinates[i]) &&
            (double.MinValue < xyOrdinates[i] && xyOrdinates[i] < double.MaxValue))
        {
            if (minX > xyOrdinates[i]) minX = xyOrdinates[i];
            if (maxX < xyOrdinates[i]) maxX = xyOrdinates[i];
        }
        i += 1;
        if (!double.IsNaN(xyOrdinates[i]) &&
            (double.MinValue < xyOrdinates[i] && xyOrdinates[i] < double.MaxValue))
        {
            if (minY > xyOrdinates[i]) minY = xyOrdinates[i];
            if (maxY < xyOrdinates[i]) maxY = xyOrdinates[i];
        }
        i += 1;
    }
    return new Extent(minX, minY, maxX, maxY);
}
Sep 19, 2014 at 4:17 PM
Dear FObermaier,
Thanks very much for your code.I'll try it,I hope it workes fine.I'll let you know the result
Sep 19, 2014 at 9:15 PM
I am unable to get the DotSpatial.Plugins.BruTileLayer to compile due to a missing reference to WmtsTileSource. Is does not appear in the latest BruTile source download.

Thanks
Sep 20, 2014 at 9:36 AM
That is right, the Version of BruTileLayer relies on functionality of BruTile that has not yet been released as nuget package.
For the time being exclude Wmts related stuff from BruTileLayer.
Nov 5, 2014 at 9:55 AM
Does BruTileLayer plugin supports google maps?How can I add it by code?I just see Bingmaps