Skip to content

Instantly share code, notes, and snippets.

@tianhuil
Last active June 1, 2020 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tianhuil/b4d3fb5fe996660da94bf163cb407364 to your computer and use it in GitHub Desktop.
Save tianhuil/b4d3fb5fe996660da94bf163cb407364 to your computer and use it in GitHub Desktop.
Practically solving the shape

Suppose we wish to understand if a given X/Y pair falls within a set of non-intersecting arbitrary polygons. For example, we are trying to see if a given lat,long falls within a Michigan minor civil division. There are 1500+ such divisions each one of which could be a polygon with hundreds or thousands of points. The following is a practical solution that is fast enough to return to a user.

  1. Break up Michigan into $N$ box oriented tiles. We can easily calculate if x, y will only fall within a tile based on the value (x % WIDTH, y % HEIGHT). Select $N$ such that most (e.g. ~60%?) tiles are fully contained within a polygon, and the vast majority (e.g. 95%) are in no more than 4 polygons. I imagine this would happen easy at $N$ less than 100,000.
  2. Preprocess the polygon shape files using Shapely to determine a mapping from the tile to the intersection of the tile with it's polygons. This will be a one-to-many mapping. Store this into a json blob.
  3. At run time, you can easily determine which tile a point is in. Using the mapping computed in #2, we can run a fairly a straightforward point in polygon typescript algorithm for the few polygon intersections which map be relevant. Once we determine the polygon intersection, we can then return the name of the polygon.

Steps #1 and #2 are only for performance. You can obviously brute force #3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment