Using .mbtiles file with SharpMap

Topics: SharpMap v0.9 / v1.x
Oct 13, 2013 at 4:38 PM
I am trying to load a .mbtiles based TileLayer in SharpMap.

In a WinForms project, I have referenced the SharpMap.Ui package and I have placed a MapBox control on my form.

Using the following code I can see OpenStreetMap - everything is ok:
mapBox1.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer(new BruTile.Web.OsmTileSource(), "OSM"));

mapBox1.Map.ZoomToExtents();
mapBox1.Refresh();
mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
Using the following code I cannot see anything in the map control:
FileInfo exePath = new FileInfo(Application.ExecutablePath);
string path = Path.Combine(exePath.Directory.FullName, "srtm.mbtiles");
MbTilesTileSource tiles = new BruTile.MbTilesTileSource(path);
mapBox1.Map.BackgroundLayer.Add(new SharpMap.Layers.TileLayer(tiles, "srtm"));

mapBox1.Map.ZoomToExtents();
mapBox1.Refresh();
mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
I have uploaded my .mbtiles file in MapBox and it works - I can see its contents.

Any pointers?


Thanks,
George J.
Developer
Oct 13, 2013 at 7:13 PM
why you use a TileLayer in second piece of code and a TileAsyncLayer in the first?
Oct 15, 2013 at 10:20 AM
For no apparent reason - I have tried TileAsyncLayer() with the same results.


George J.
Coordinator
Oct 15, 2013 at 11:57 AM
Edited Oct 15, 2013 at 1:49 PM
UPDATE: There seems to be an issue with MbTiles files that don't provide information for every zoom level.
For the time being, please grab the source from http://brutile.codeplex.com/SourceControl/latest (Branch 0.7.4) and compile yourself.

I've just checked that it works if your MbTiles file covers the whole extent of global mercator. If you have just a part, say the aegean, you must not call
map.ZoomToExtents();
but instead
var envelopeAegean = new Envelope(...);
map.ZoomToBox(envelopeAegean);
Please note: There is a threading issue with the provided MbTilesSource. If you want to avoid that, you need to compile it yourself, using not CSharpSqlite as sqlite provider but System.Data.SQLite.

Hth FObermaier
Oct 17, 2013 at 10:16 AM
I have compiled Brutile 0.7.4 branch locally, updated references to the latest System.Data.Sqlite library, and refreshed the existing nugget package. I have noticed, the new package requires System.Data.Sqlite!

I have used the following code to get the bounding box for my layer:
mapBox1.Map.ZoomToBox(mapBox1.Map.BackgroundLayer[0].Envelope);
mapBox1.Refresh();
But, still I cannot see the contents of the layer... Could my .mbtiles file have the problem?


George J.
Coordinator
Oct 17, 2013 at 11:24 AM
Edited Oct 17, 2013 at 12:21 PM
gcapnias wrote:
I have uploaded my .mbtiles file in MapBox and it works - I can see its contents.
Could you provide an URL:

This works for me with the Haiti example
public static SharpMap.Map InitializeMap(string[] mbTiles)
{
    if (mbTiles == null || mbTiles.Length == 0) return null;

    var map = new SharpMap.Map();

    var tileSource = new BruTile.MbTilesTileSource(mbTiles[0]);
    var tileLayer = new SharpMap.Layers.TileAsyncLayer(tileSource, 
                string.Format("TileLayer - {0}", Path.GetFileName(mbTiles[0])));
    map.BackgroundLayer.Add(tileLayer);
    //NOTE: you won't have the extent property on the tileSource unless you've compiled
    //      BruTile yourself!
    map.ZoomToBox(new Envelope(tileSource.Extent.MinX, tileSource.Extent.MaxX, 
                               tileSource.Extent.MinY, tileSource.Extent.MaxY));
            
    return map;
}
Oct 18, 2013 at 1:59 PM
I have downloaded and compiled the latest source in 0.7.4 branch (0.7.4.1 - commit d0f418b04b85); there is no Extent property in MbTilesTileSource. Am I downloading the correct commit?

You can find my .mbtile files here: http://sdrv.ms/19TqpVq


George J.
Coordinator
Oct 18, 2013 at 3:11 PM
After clicking on the sources tab there is a combo box which says "master".
Change it to "0.7.4"
Oct 18, 2013 at 6:56 PM
Edited Oct 18, 2013 at 7:02 PM
Hi, this is what I have done - can you verify that "commit d0f418b04b85" is the version that you are referring to?

Image


Regards,
George J.
Oct 19, 2013 at 8:39 AM
Hi again,

Fooling around, I found the .mbtiles spec. The Haiti .mbtiles example file you sent me does not conform with these specs (the SQLite db should have two table/views with the names "metadata" & "tiles"; Haiti sample has no "tiles" table/view). The files I provided are conforming with these specs (they were created with Global Mapper).

Can this be the issue that prevents Brutile from working?


George J.
Coordinator
Oct 19, 2013 at 3:17 PM
You are right, the haiti example does not adhere to the standard. But that is not the problem.
The problem is my ignorance on GIT :). I only commited to my local repository and did not push the changes.
That should be done by now. The changeset is 614f79f50f9a

Sorry
Marked as answer by gcapnias on 10/20/2013 at 2:27 AM
Oct 20, 2013 at 9:27 AM
Yes, I can see the map now.


Thanks,
George J.