Im using geodjango (postgis) to retrieve points contained inside a bounding box (made by leafletjs), it works so nice, but now I want to cache this query.

A tipical query is one like this:


This kind of query will never be cached, is there a way to cache this?, I mean, achieve some cache hits, assuming would appear more/less points in the viewport than would strictly correspond. Some ideas...

  1. removing decimal position in the numbers
  2. passing zoomlevel as a param should be better/mandatory?

Thanks for your thoughts.


  1. Use https://pypi.python.org/pypi/django-geojson to make a simple view which uses (x,y,z) as params to do filter your queryset.

  2. Use http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_numbers_to_lon..2Flat._5 to make the filter in the view

  3. Use https://github.com/glenrobertson/leaflet-tilelayer-geojson/ for making a layer in you map which makes your tile request to the previous view.
  • How about just zoom-level and center point rounded to some coarse precision? This keeps your parameter space small so as to make caching easier but also provides a more simple API.
    – Arthur
    Mar 10, 2014 at 13:30
  • This would make a great walk through on how to combine those three tidbits into a working solution. Is the tile numbers to lat/lon needed on the javascript side or the django view? Apr 16, 2014 at 17:23

1 Answer 1


Luckily a standardized series of bounding boxes already exists, the TMS tile scheme.

If your ok with making more requests for smaller amounts of data, splitting it into tiles can work very well, it caches well, and makes nice looking URLs. Once you have a tile coordinate(zoom/x/y) it can be converted to a bounding box, fetch the data, then depending on the size of your features either clip to the bounding box, or let features extend outside the box, which can result in the same feature on the map multiple times, but in many cases that is not a problem.

If your returning GeoJSON there is already a leaflet plugin that will handle the requests.

If your just fetching data from Postgis returning it as GeoJSON you could even use TileStache to handle the fetching, formatting, and caching.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.