question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does this support multiple polygons?

See original GitHub issue

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)?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rowanwinscommented, Feb 24, 2021

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 😃

0reactions
rowanwinscommented, Jun 2, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple polygon features (Polygon)—ArcGIS Pro
In a group template, the Multiple polygon features builder generates a polygon feature that is coincident with a polygon feature you create in...
Read more >
Understanding difference between Polygon and Multipolygon ...
A Polygon is a planar Surface defined by 1 exterior boundary and 0 or ... If a shape has multiple rings, they can...
Read more >
Group Multiple Polygons Into a Single Multi-Part Polygon
The options to group multiple polygons into a single multi-part polygon can then be found under the Advanced Feature Creation Options submenu.
Read more >
Multipolygon support for elastic search - Elasticsearch
Although Elasticsearch allows to have polygons intersecting each other in a multi-polygon, it is actually a bad idea and in many other tools...
Read more >
Plot one or more filled polygonal regions - MATLAB patch
Specifying only unique vertices and their connection matrix can reduce the size of the data when there are many polygons. Specify one vertex...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found