Showing an offline OSM map file. Suggestion: an MB Tiles file with Js.library
Asked Answered
S

2

9

When online access to the internet is not possible, I would like the (offline) HTML5 app show an OSM map via an OSM file.

Can you give an example of how I can show in an offline Html5 app OSM tiles that are loaded from an offline OSM map file like Mapsforge / Geofabrik etc?

Example: via the openstreetmap.org I first exported a small part of a map. How can I show this downloaded OSM map in the Html5 offline webapp.

Sisk answered 2/5, 2017 at 16:38 Comment(2)
The answer to your question is "yes". Please be more specific; what have you tried? Which solutions have you considered?Epicalyx
I couldn't find a good solution yet. I hope a standard solution is possible, like using leafletjs with Mapforge files. Do you have a working solution?Sisk
S
3

How can we show a map using Leaflet? By default Leaflet works with raster images. Normally these tiles are retrieved via the internet.

How can we show a map using an offline file? E.g. because no internet connection is possible?

  1. Local tiles in an hierarchy structure. For example by using such a script. The disadvantage is that this is not a compact format. It requires some preparational work:

    L.tileLayer('/map-tiles/{z}/map_{x}_{y}.png', { attribution: 'Map data © ???', }).addTo(map);

  2. MBTiles file with raster tiles. Such a file can be shown via the Leaflet.TileLayer.MBTiles.js plugin. An example script is shown below.

  3. VectorGrid is a compact candidate for reading the vector data from the MBTiles file. See also this intruction.

  4. Mapbox GL JS offline. In that case you put locally your vector files. See the demo.

  5. mobac.sourceforge.net as suggested below by @JaakL.

Ad option 3: OpenMapTiles.com generates very compact MBTiles file with the Vector format. So, this solution is useful for option 3.

Ad option 2: When you have an MBTILES/Raster file, then the following code will work correctly. So, it is not working with the above MBTiles vector file.

After installing in about 1 minute with npm the package I ran the demo. The demo is under the 'node_modules\Leaflet.TileLayer.MBTiles\demo' folder. Works fine.

Then I tried to show the Amsterdam map. Alas, I couldn't get this (as a newbie) working. I am investigating this for a POC.

How can I update this source to get the Amsterdam map shown? Getting it done will give the +50 bounty.

<!DOCTYPE html>
<html>
<head>
<link href="https://unpkg.com/[email protected]/dist/leaflet.css" rel="stylesheet" type="text/css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet-src.js"></script>
<script src="https://unpkg.com/[email protected]/js/sql.js"></script>
<script src="../Leaflet.TileLayer.MBTiles.js"></script>
  <meta charset="utf-8">
  <title>Leaflet.TileLayer.MBTiles demo</title>
  <style>
    #map {
      width:600px;
      height:400px;
    }
  </style>
</head>
<body>
  <div id='map'></div>
  <script>
    var map = new L.Map('map');
    map.setView(new L.LatLng(52.361367, 4.923083), 18);
    var mb = L.tileLayer.mbTiles('./amsterdam_netherlands.mbtiles', {
        minZoom: 16,
        maxZoom: 20
    }).addTo(map);
    mb.on('databaseloaded', function(ev) {
        console.info('MBTiles DB loaded', ev);
    });
    mb.on('databaseerror', function(ev) {
        console.info('MBTiles DB error', ev);
    });
  </script>
</body>
</html>
Sisk answered 10/5, 2017 at 21:10 Comment(8)
I believe you'd need to serve the mbtiles file locally. Take a look at: github.com/chelm/mbtiles-serverHolusbolus
I think Leaflet.TileLayer.MBTiles plugin expects raster tiles, whereas OpenMapTiles website provides you with vector tiles.Schreibe
@MichaelYang - thank you for your suggestions (also the other answer). A local server on a mobile is a bit too heavy. Accessing a local file is better.Sisk
@Schreibe - tx. Is there a good Leaflet plugin that can read mbtiles (vector) from an OpenMapTiles file? Or is there a server serving (free) MBTiles as raster files?Sisk
I'm afraid there are no good Leaflet plugins which can render vector tiles on mobile fast enough, so it is usable for mobile. This is too hard work for mobile browser. Also I don't think you can find decent free raster tile mbtiles packages nowadays anymore, as the offline packages of vendors (openmaptiles, CARTO etc) are in vector and meant for native mobile clients. However, I suggest to check out mobac.sourceforge.net - with this you can create easily raster tile packages in mbtiles (or other) formats.Cappadocia
@tjm1706 I have vector tiles file(Format: MBTiles (PBF) ), do you know which approach to use to show it on leaflet? VectorGrid docs say "L.VectorGrid.Protobuf for displaying vector tiles from an online tile server". But I want to use them offline.Exalt
Finally I had (yes, a pity) to switch to OL because of missing functionality. I cannot answer your question. This may be a useful question for stackoverflow!Sisk
@tjm1706 I asked here: stackoverflow.com/questions/55932661/read-vector-tiles-offlineExalt
H
5

Yes, it's possible to do this in Leaflet, or any other mapping library. All you have to do is convert the OSM file into a set of tiles, then point your tile layer to the locally stored files (relative path). Or if it's possible (I'm not familiar with OSM rules), you could download the set of tiles for your area.

If you need to convert your OSM files into tiles, try a tool like Maperative and its tile generator.

You can also try the methods listed at this link: http://wiki.openstreetmap.org/wiki/Creating_your_own_tiles.

Once you have your tiles, set up the property directory structure, and point your tile layer at it.

Here's an example using Leaflet:

// the path may be different depending on how you have the tiles saved.
// you will have to define the options object

new L.tileLayer('/tiles/{z}/{x}/{y}.png', {}).addTo(map);

Keep in mind the file paths are relative, and you'll probably have to make sure the constraints are correct set up for your custom tiles.

Holusbolus answered 6/5, 2017 at 5:9 Comment(0)
S
3

How can we show a map using Leaflet? By default Leaflet works with raster images. Normally these tiles are retrieved via the internet.

How can we show a map using an offline file? E.g. because no internet connection is possible?

  1. Local tiles in an hierarchy structure. For example by using such a script. The disadvantage is that this is not a compact format. It requires some preparational work:

    L.tileLayer('/map-tiles/{z}/map_{x}_{y}.png', { attribution: 'Map data © ???', }).addTo(map);

  2. MBTiles file with raster tiles. Such a file can be shown via the Leaflet.TileLayer.MBTiles.js plugin. An example script is shown below.

  3. VectorGrid is a compact candidate for reading the vector data from the MBTiles file. See also this intruction.

  4. Mapbox GL JS offline. In that case you put locally your vector files. See the demo.

  5. mobac.sourceforge.net as suggested below by @JaakL.

Ad option 3: OpenMapTiles.com generates very compact MBTiles file with the Vector format. So, this solution is useful for option 3.

Ad option 2: When you have an MBTILES/Raster file, then the following code will work correctly. So, it is not working with the above MBTiles vector file.

After installing in about 1 minute with npm the package I ran the demo. The demo is under the 'node_modules\Leaflet.TileLayer.MBTiles\demo' folder. Works fine.

Then I tried to show the Amsterdam map. Alas, I couldn't get this (as a newbie) working. I am investigating this for a POC.

How can I update this source to get the Amsterdam map shown? Getting it done will give the +50 bounty.

<!DOCTYPE html>
<html>
<head>
<link href="https://unpkg.com/[email protected]/dist/leaflet.css" rel="stylesheet" type="text/css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet-src.js"></script>
<script src="https://unpkg.com/[email protected]/js/sql.js"></script>
<script src="../Leaflet.TileLayer.MBTiles.js"></script>
  <meta charset="utf-8">
  <title>Leaflet.TileLayer.MBTiles demo</title>
  <style>
    #map {
      width:600px;
      height:400px;
    }
  </style>
</head>
<body>
  <div id='map'></div>
  <script>
    var map = new L.Map('map');
    map.setView(new L.LatLng(52.361367, 4.923083), 18);
    var mb = L.tileLayer.mbTiles('./amsterdam_netherlands.mbtiles', {
        minZoom: 16,
        maxZoom: 20
    }).addTo(map);
    mb.on('databaseloaded', function(ev) {
        console.info('MBTiles DB loaded', ev);
    });
    mb.on('databaseerror', function(ev) {
        console.info('MBTiles DB error', ev);
    });
  </script>
</body>
</html>
Sisk answered 10/5, 2017 at 21:10 Comment(8)
I believe you'd need to serve the mbtiles file locally. Take a look at: github.com/chelm/mbtiles-serverHolusbolus
I think Leaflet.TileLayer.MBTiles plugin expects raster tiles, whereas OpenMapTiles website provides you with vector tiles.Schreibe
@MichaelYang - thank you for your suggestions (also the other answer). A local server on a mobile is a bit too heavy. Accessing a local file is better.Sisk
@Schreibe - tx. Is there a good Leaflet plugin that can read mbtiles (vector) from an OpenMapTiles file? Or is there a server serving (free) MBTiles as raster files?Sisk
I'm afraid there are no good Leaflet plugins which can render vector tiles on mobile fast enough, so it is usable for mobile. This is too hard work for mobile browser. Also I don't think you can find decent free raster tile mbtiles packages nowadays anymore, as the offline packages of vendors (openmaptiles, CARTO etc) are in vector and meant for native mobile clients. However, I suggest to check out mobac.sourceforge.net - with this you can create easily raster tile packages in mbtiles (or other) formats.Cappadocia
@tjm1706 I have vector tiles file(Format: MBTiles (PBF) ), do you know which approach to use to show it on leaflet? VectorGrid docs say "L.VectorGrid.Protobuf for displaying vector tiles from an online tile server". But I want to use them offline.Exalt
Finally I had (yes, a pity) to switch to OL because of missing functionality. I cannot answer your question. This may be a useful question for stackoverflow!Sisk
@tjm1706 I asked here: stackoverflow.com/questions/55932661/read-vector-tiles-offlineExalt

© 2022 - 2024 — McMap. All rights reserved.