oscarfonts / mapbox-gl-cordova-offline

Offline vector maps in Cordova using Mapbox GL JS
Other
60 stars 36 forks source link

Support raster-dem in offline #42

Open HarelM opened 5 years ago

HarelM commented 5 years ago

It would be extremely cool if raster-dem source will be supported while using offline to reduce the size of the hillshading database and allow cool hillshading capablilities. https://docs.mapbox.com/studio-manual/examples/hillshade/ It might take me sometime, but I might be able to contribute the implementation if I'll manage to make it work...

HarelM commented 5 years ago

Here's another thought about this, which I think is very elegant. Instead of deriving from each source and implementing a database call to fetch the relevant tile it might be easier to implement a scheme for the tiles address - for example a vector source:

           db: {
                type: "vector",
                tiles: ["mbtiles://mydb.mbtiles/{z}/{x}/{y}"]
            }

using the regular sources but hooking into the makeRequest method in ajax.js. The main problem here is that this request is made inside a worker and it's currently very difficult to use sqlite cordova plugin within a worker. I'll see if I can post a message to the main thread in order to open the database and get the data. If this doesn't work I'll try to derive from RasterDEMTileSource...

HarelM commented 5 years ago

Hi, I haven't fully tested RasterDEM, but the following commit to my branch is the implementation of the above idea of "mocking" the fetch http request with a call to the database. Since the database is not available in the worker the call is sent back to the main thread. https://github.com/IsraelHikingMap/mapbox-gl-js/commit/2cb233d612bc17fc1fd0cdf771dd7c250ef7e5bc Let me know if you want a PR. feel free to use this code.

michogar commented 5 years ago

Hi @HarelM thank you very much for your work.

Now I'm a bit busy, I'll try to review your contributions asap and I warn you to make a PR.

Thanks again.

HarelM commented 5 years ago

I'd be happy if you review it first and then decide how to make a PR since this repository isn't a fork of mapbox-gl and I made my changes on a fork of mapbox-gl. I'm using patch-package in my repository to add these changes to mapbox-gl package so it might be a different method of how you guys do it here. I'd love to understand how you do it though :-) I haven't tested RasterDEM yet, but I hope to test it soon, I'll report here once the tests are done.

HarelM commented 5 years ago

It seems that both vector and raster-dem work when using the mbtiles:// protocol :-)

HarelM commented 5 years ago

Further testing reveals that the performance of this implementation is not so good. The raster-dem layer is presented as expected and even rotates well when defined as hillshading but the database process seems to hold the main thread in a noticeable way causing the app to be sluggish. Have you guys encountered this? I'm basically using a vector layer and a raster-dem layer of Israel. It might be related to the fact that I didn't set any values to the cache so that the tiles are never cached, not sure, might be the implementation of SQLite-ext. Since I don't know how to make this work in a web worker I think I'll try and use PouchDB worker and hopefully this will allow the app to be less sluggish...

HarelM commented 5 years ago

In case anyone is interested, I have made further progress in this aspect. Instead of adjusting mabpox with new capabilities and keep creating a new version every time I change it, I have added to mapbox-gl a hook for a global getTilesFunction. This function alongside the custom:// protocol can fetch tiles however you write it and stores them if you want too. I was able to use PouchDB with worker-pouch adapter and get excellent performance. In case anyone is interested the following is the mabpox fork. https://github.com/IsraelHikingMap/mapbox-gl-js You can clone and build it, register to the getTilesFunction and add the custom:// tiles source to your style. Enjoy!

HarelM commented 5 years ago

Seems like there's a problem with PouchDB on large datasets (127K tiles) - PouchDB's performance degenerate significantly. I have switched to use Dexie and the performance is excellent now. Most of the code can be seen here: https://github.com/IsraelHikingMap/Site/blob/204aa92230b059bec4f0eae877d1545d5d846327/IsraelHiking.Web/sources/application/services/database.service.ts#L79 This issue can be closed in theory as I don't have any more plan to implement it besides the custom hook I did for mapbox...