AliFlux / VectorTileRenderer

A comprehensive Vector Map Tile Renderer for .Net/C#
MIT License
181 stars 51 forks source link

Caching of tiles? #26

Open kezara opened 2 years ago

kezara commented 2 years ago

I have only 1 year of professional experience in programming, so this is maybe stupid one, why not caching tiles in Dictionary<string, BitmapSource>, where string would be fileName, and then limit number of entries on, let's say 5000, and then create new instance of dictionary? I actually tried this (only a few times), in Render class I implemented static dictionary and in RenderCached method I create instance of it, but with mbtiles file of only 153 MB and it worked quite well... I'd like to use this library, but with extracts, I'm not sure, but probably in size of the city, maybe country, so would this be possible? As I said, I do not have much experience, so I do not know how will this impact on memory and CPU consumption, but is this plausible?

rbrundritt commented 2 years ago

There are lots of different ways to cache tiles depending on the app/service you are creating. This library is primarily focused on the rendering aspect of vector tiles, so built in caching should be limited as there may be scenarios where it isn't wanted/needed (i.e. dynamic tile service that needs to always render the latest version of a tile that might change several times a minute).

Using a dictionary is a good simple cache solution for smaller tile sets and apps that run on a client. Memory usage would need to be watched as that's where these tiles will be cached. In a server side solution this may not work well if there is a large number of tiles in the data set, and you have a lot of users accessing your server.

Another solution for larger data sets is cache the tiles in a sqlite database. There is a common specification used for storing tiles (raster and vector) in such a database called MBTiles (this basically just tells you two tables to have in the database so that there is consistency between sqlite databases storing tiles for easier reuse and tooling creation). Using this solution, will be slightly slower at reading (milliseconds), but will use a lot less memory. Writing to a sqlite database is a lot faster than writing to disk as well.

When it comes to server side caching with multiple servers spread out globally, there are much more advance caching strategies that can be used.

AliFlux commented 2 years ago

Yeah, in-memory caching can easily hog up the RAM. There are various options for caching such as:

Depending on your use-case, you may modify the code to implement that specific strategy. Look at the RenderCached function in Renderer.cs for headstart

ShawnStoddard commented 2 years ago

Where are the tiles being generated? It is amazing how this general problem has crept in. I’m working on a project needs to access cached copy of raster map tiles. I’m trying to cache images plus generate them. To work around generating the letting open street maps. Once we retrieve the original image we can stop accessing them. A background process can update these.

On Fri, Oct 22, 2021, at 13:10, Borisav Ignjatov wrote:

I have only 1 year of professional experience in programming, so this is maybe stupid one, why not caching tiles in Dictionary<string, BitmapSource>, where string would be fileName, and then limit number of entries on, let's say 5000, and then create new instance of dictionary? I actually tried this (only a few times), in Render class I implemented static dictionary and in RenderCached method I create instance of it, but with mbtiles file of only 153 MB and it worked quite well... I'd like to use this library, but with extracts, I'm not sure, but probably in size of the city, maybe country, so would this be possible? As I said, I do not have much experience, so I do not know how will this impact on memory and CPU consumption, but is this plausible?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/AliFlux/VectorTileRenderer/issues/26, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARTX7JASLVDPNSXEQLJ3ODUIGLJFANCNFSM5GRACQRA.

AliFlux commented 2 years ago

The caching system is already implemented. The RenderCached function does exactly that. It renders a map once, saves it to a file. And when its invoked again it reads the rendered copy instead instead of re-rendering it.

You can specify where you want the cached images saved. Simply set the cachePath parameter of this function.

P.S. In Mapsui and Gmap, you can check the demo source codes to see where the cache path is specified.

kezara commented 2 years ago

Where are the tiles being generated? It is amazing how this general problem has crept in. I’m working on a project needs to access cached copy of raster map tiles. I’m trying to cache images plus generate them. To work around generating the letting open street maps. Once we retrieve the original image we can stop accessing them. A background process can update these. On Fri, Oct 22, 2021, at 13:10, Borisav Ignjatov wrote: I have only 1 year of professional experience in programming, so this is maybe stupid one, why not caching tiles in Dictionary<string, BitmapSource>, where string would be fileName, and then limit number of entries on, let's say 5000, and then create new instance of dictionary? I actually tried this (only a few times), in Render class I implemented static dictionary and in RenderCached method I create instance of it, but with mbtiles file of only 153 MB and it worked quite well... I'd like to use this library, but with extracts, I'm not sure, but probably in size of the city, maybe country, so would this be possible? As I said, I do not have much experience, so I do not know how will this impact on memory and CPU consumption, but is this plausible? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#26>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARTX7JASLVDPNSXEQLJ3ODUIGLJFANCNFSM5GRACQRA.

I need maps for offline desktop application, and maps are smaller part of it, so I think to use ready made solutions, so I first make extracts with osmium tool and for generation of tiles I have 2 options in mind, one is TileMaker which generates vector mbtiles, and second is Maperitive which makes raster mbtiles or png tiles....