[GIS] How to improve speed of rendering polygons from Postgis in Leaflet

javascriptleafletpolygonpostgistilestache

I want to improve speed of rendering polygons in my Leaflet map.

Polygons are stored in Postgis, and are rendered with select query with bbox. I wonder can I cache somehow those polygons?
I read about Tilestache a bit, but I'm still not sure if that would help in speed performance.

Also, one of my problems is that I need to relate postgis tables with non-spatial tables in my postgresql. So, some of the attributes I need to show in the popup are in some other non-spatial tables in Postgresql.

Just to be more clear, that map is already developed and all works but I want to improve speed of polygons rendering. Can you give me some advices?

Best Answer

I am not sure if I completely understand what your application is doing. Are you sending a query to postGIS every time the bbox changes and then render the response as a vector layer? If that's the case, in my experience, there are no obvious solutions to the problem. No magic bullets yet, but still solutions can be found, here some ideas:

  • Maybe not what you need here, but it's always good to know about server-side solutions to the problem. One technique consists in rendering your polygons upfront as tiles and then serving them as a TMS layer from some fast static content server. This can be achieved in many ways, the one I prefer is using mapnik in combination with mapproxy. The issue of having other non spatial data to query is not a difficult problem to solve here. A click on the map could send an Ajax request to the server, where you could perform a fast point-in-polygon query an retrieve all you need.

  • If a vector layer is what you need, then consider using topojson. Is the whole dataset really huge? Can you try to convert it all to a topojson file and send it to the client? Also, have a look at vector-river-map, quite impressive. And have a read at this inspirational post from the author of tilestache, it does not give you production ready solutions, but worth a read for ideas.

  • Caching geojson on the client side is possible too, although depending on the exact specifications you are looking for, things could become difficult. I mean, JSON data can be easily stored on the client, in simple JavaScript objects or in more elaborate implementations such as backbone models and collections. To decide when to use the cache, when to do a new request and when to destroy it, that can be trickier.

Hope it helps.