I am building a vector tileserver in node.js using mongo 2.4.7 as database.

My database contains geojson data from multiple sources and they are in many layers. It is important to render the layers in the correct sort order.

Since I have little, or no control of the client rendering the individual tiles I have to clip the geojson data before returning it to the client so one “lower lying” polygon does not hide another higher lying tile in an adjacent tile.

I have created an simple rectangle ->polygon clipper that works for now - but this should be improved later on. If anyone knows an efficient clipper library that is usable from node.js please let me know. This is, however, not the issue.

My real challenge is the finding the correct polygons within the database. I am currently using both $geoIntersects and $geoWithin in my query.

Let say that we have a polygon A that in zoom level x just fits within one tile bounding box (tile A). Here the Polygon will be returned by the $geoWithin clause and all is well.

Going several zoom levels in, the area from the tile A now fits within 9 tiles in a square. How do I find Polygon A when the server needs to render tile number 5 (the middle one)? And yes - in this situation I would like to render a square covering the complete tile due to the clipping.

I have created a drawing to illustrate my challenge.

enter image description here

  • Did you look into into TileStache server implementation code, a Python vector (and raster) tiles server tilestache.org It may help for your need
    – ThomasG77
    Nov 14, 2013 at 0:55
  • Thanks Thomas! - I have had a look, but my ability to read python is limited. I will give it another try.
    – Lars Buur
    Nov 14, 2013 at 7:17
  • My previous comment was really not good for your use case >> I made a better answer
    – ThomasG77
    Nov 14, 2013 at 20:07

1 Answer 1


It seems that MongoDB support for spatial operation is quite limited e.g http://docs.mongodb.org/manual/reference/operator/query-geospatial/

You should better rely on jsts, a port of JTS (Java Topology Suite) for JavaScript. You will have really all the required operations available you need. For this, you should use the Geometry component with
coveredBy or covers and the GeoJSON reader

You will not find a lot of examples but you can maybe take a look on http://bjornharrtell.github.io/jsts/examples/overlay.html or http://bl.ocks.org/christophermanning/2271944 for inspirations

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.