pelias / polygon-lookup

Fast point-in-polygon intersection for large numbers of polygons.
72 stars 17 forks source link

Clarification of "works best for polygons with little overlap" #46

Closed maxhanglin closed 4 years ago

maxhanglin commented 4 years ago

Hi there,

This is actually not a feature request, but a feature clarification question.

I would like to know the meaning of "works best for polygons with little overlap".

We need to find the set of polygons that a point exists within from a set of thousands of geographic polygons, many of which are layered on top if one another. So I was wondering if we could see problems of reliability, speed, or what issue when using your library this way.

Thank you very much in advance.

missinglink commented 4 years ago

Hi Max,

I believe that comment is there because the PolygonLookup.search() method has a default limit of 1. What this means is that it'll only return a single match, I believe if you set that to -1 then it won't be an issue.

Under the hood it's using npm rbush which you may find suits your needs if you're not using Pelias specificially.

Generally speaking, the method I have found which provides the best performance point-in-polygon is:

For the above to be fast you'll likely want to load all the data into RAM to avoid disk I/O, which is what this library does.

For bonus points, you can optimize this further:

This method will give you a guarantee of linear <1ms performance even on large complex polygons (such as New Zealand).

maxhanglin commented 4 years ago

Thank you very much for your great and quick response @missinglink

missinglink commented 4 years ago

@maxhanglin I'm working on a fast general purpose Spatial index, it works great but isn't well documented yet: https://github.com/pelias/spatial

If you're interested in building a high performance PIP service for work, then maybe you might want to check it out.