rowanwins/point-in-polygon-hao

Does this support multiple polygons?

kendallroth opened this issue · 5 comments

I noticed that the input array was a relatively strange format (doubly nested array) and was curious if this meant that arrays of multiple polygons were supported? Is this implicitly supported because of the hole support?

// Input shape(s)
const polygon = [
  [
    [0,0], [1,0], [1, 1], [0,1], [0,0]
  ],
  [
    [2,2], [3,2], [3,3], [2,3], [2,2]
  ]
]

// Tests
const tests = [
  [0.5, 0.5],
  [0,0],
  [0.5, 0],
  [2, 0],

  [2,2],
  [2.5, 2.5],
  [2.5, 2],
  [2, 1]
];

// Verification
tests.forEach((test) => {
  let isInside = inside(test, polygon);
  isInside = isInside === true || isInside === 0;

  console.log(isInside, test);
})

It also appears to work as expected if the two "shape" subarrays are combined (assuming both are closed properly)?

Hi @kendallroth

That's a good question, I hadn't actually thought about it!

The choice of doubly-nested arrays was to align to how geojson structures things (I mainly deal with mapping problems). Using your structure above is how we'd normally handle holes in geojson.

Below are some examples of geojson coordinate stucture


const geojsonPolygonWithHole = [
  [
    [0,0], [1,0], [1, 1], [0,1], [0,0]
  ],
  [
    [0.1,0.1], [0.1,0.9], [0.9,0.9], [0.9,0.1], [0.1,0.1]
  ]
]

// Note for multipoly we wrap the coordinates another level deeper
const geojsonMultiPolygon = [
  [
    [
      [0,0], [1,0], [1, 1], [0,1], [0,0]
    ]
  ],
  [
    [
      [2,2], [3,2], [3,3], [2,3], [2,2]
    ]
  ]
]

Note that using the geojson multipolygon approach currently this wouldn't work with the library.

So I probably ought to be more explicit in describing the expected input format! Although it's interesting that it works with your approach, although obviously your approach also means your multipolys can't have holes.

Open to suggestions/thoughts at least around documenting expected behaviour :)

Ah, interesting, I was not aware that this was intended to support holes! Does it handle a polygon with multiple holes?

As far as documentation goes, I think giving a few more examples (such as the first one here) may be beneficial. I know it was a bit unclear to me what was all supported/intended by the library, and that could help clear things up from a glance at the README.

Multiple holes seems to be supported by GeoJSON "standard" as evidenced by this stackoverflow issue. I will be doing some quick tests to determine whether this works with this library.

P.S. I'm curious (like you) as to why my original example (using the hole support for another polygon) works...

This REPL seems to indicate that the library does handle multiple holes properly. Could this maybe be documented as well (or an example given)?

I've update the readme to indicate that the input is in the geojson format.