Search code examples
node.jsmongodbredismemcachedgeocode

How to create geocoding service (find polygons that intersect a given point)


SITUATION

I have a database with 2,000,000 cities. All of them have coordinates of the city center and mostly all - GeoJSON boundaries. I'm trying to implement a geocoding service that would find cities that intersect with a given point using node.js, mongodb, redis, memcached (and golang, if that is necessary, cause I'm just totally new to it )

PROBLEM

I know how to work with points (lat and lng) since both MongoDB and Redis support geoindexes but I've never seen anything about polygons.

I guess MongoDB won't really help cause of its speed (since it work on disks), but any memory database should deal with this problem. The thing is I can't even think of any way to implement it.

I'll be happy if someone point me how to make it. Thanks.


Solution

  • You may implement a point-in-polygon algorithm yourself. I've done something similar on https://api.3geonames.org

    First do a bounding box to identify candidate polygons, then run a PIP. https://en.wikipedia.org/wiki/Point_in_polygon