[GIS] Search for geojson polygon in mongodb implementing a custom vector tileserver

geojsonmongodbpolygon

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

Best Answer

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

Related Question