Export/Import of mapcache
Like you write in the documentation about map tiles:
Technically, tiles are stored locally in IndexedDB blobs. There is currently no support to sync them between devices.
However, it would be very nice if this was possible.
Since it is hard to access the IndexedDB outside of the application that created the DB, it would be nice if the functionality could be built into this plugin.
My suggestion would be to use sql.js https://github.com/sql-js/sql.js/ and simply dump out the tiles from the indexdDB to a sqlite database in the browser and then download the database as a javascript typed array. Then it would be possible to import this database in another device and repopulate the indexdDB on that device.
With this approach, it would be possible to also add the possible to import raster tiles from an existing mbtiles file, that also is an sqlite database.
This sounds great, and gaining mbtiles support as a side-effect is an amazing idea. I'd love help if you're up to it. On a functional note, it would be best to have the user select a map source to export rather than dump all the IndexedDB. When importing, there needs to be a way to tie the file into a map source (perhaps also adding support for map sources that are completely offline, not just for caching purposes?) On a technical note, the part I was most unsure about when considering an export functionality, is how to dump big binary files outside of Obsidian. The Obsidian API doesn't seem to support writing big files (that can't be stored as a string in RAM), which would require workarounds of some sort.
I started to look into the code, and I have to admit that typescript isn't my native language. But I had another idea, and verified that it is possible to access files outside of the current vault, so it would be possible to use https://github.com/protomaps/protomaps-leaflet and point it to a local PMTiles file on disk. It should be possible to implement some logic that keeps using leaflet offline as usual if you point to a url ending in .png/etc, but if you point it to a local PMTiles file (or url) it would use protomaps-leaflet instead. If we could have a discussion about how to do that it would be great.
While I'm very interested in supporting vector tiles, I think that achieving what you intend through this way will be a very bumpy road; it means completely changing the offline tiles and caching mechanism. I think we would benefit the most from the ability to export tiles of a layer in some format and then import them to be used offline as part of the existing mechanism, fluently and transparently along with tile layers. Support for mbtiles and vectors are side-effect that can be achieved as part of different tasks :) It's easy (with the current infrastructure) to iterate over images of tile layer in the database, and if there's a good way to write big binary files to disk, the obstacle of reading/writing is really no more than an annoyance. I really want this feature too, so will probably do it either way even if nobody steps up and works on it, just can't say much about when I'll be able to get to it 🙃
I get your point. Having tought about it a bit more and got a bit more of understanding of how Obsidian can access data. The sqlite based approach I first suggested definitely has some drawback since there isn't support for reading/writing sqlite direct from disk. Because of this I would suggest that you use PMtiles raster as the storage format for import export. (The tool pmtiles cli can quite easily convert to and from mbtiles.)
To read raster tiles stored in a pmtiles file and store them in the offline cache should be quite straight forward. The code to get the tiles from the PMtiles file can be found here: https://github.com/protomaps/PMTiles/blob/main/js/src/index.ts And then it should be quite straight forward to add them to the storage that leaflet.offline using saveTile.
Writing a pmtiles file should be quite straight forward since it is some header data and then just blobs of the images in a binary file. There isn't any code to do this in js/ts what I can find, but based on the ts code i linked to above, it should be quite straight forward to convert https://github.com/protomaps/PMTiles/blob/main/python/pmtiles/writer.py to TypeScript. (I'll see if I can have a go at that.)
I have made a separate plugin that can import raster tiles from pmtiles files. The code is messy, but I'm happy to provide it to you if you are interested. Still haven't got to the part to export data.
Thank you, will look into it 🙏
Could you ping me an email, my address should be on my profile, and I'll send you my code.