mapbox / polylabel

A fast algorithm for finding the pole of inaccessibility of a polygon (in JavaScript and C++)
Other
1.44k stars 151 forks source link

how to use in browser? #39

Closed rjcarr closed 6 years ago

rjcarr commented 6 years ago

With the source using require I'm guessing this is supposed to be an npm component? Can it be run in the browser? I went and found tinyqueue and added it to my test html page (along with a few edits to remove request and module, etc), then ran this:

var polygon = [[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]];
var p = polylabel(polygon, 1.0);

But the result is [NaN, NaN], with no errors at all. Note I also tried a non-closed polygon and got the same result.

Any suggestions?

mourner commented 6 years ago

Regarding the error — you need to wrap your input with another array, since this is the polygon GeoJSON format (multiple rings where the first is outer ring and others are holes):

var polygon = [[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]];
var p = polylabel(polygon, 1.0);

There's no browser build in the repo currently — I'll need to add one. Meanwhile, you try using this bundle — it already includes all dependencies tinyqueue: https://bundle.run/polylabel@1.0.2

rjcarr commented 6 years ago

Thanks so much for the quick response, and you're right, that fixed it!

Sorry for overlooking that; when you said a geo-json polygon I looked it up on wikipedia and thought you wanted the whole object. Then I determined you just wanted the coordinate array, and I somehow stripped off the outer array when I was stripping away the object. Ooops!

If I end up using your algorithm I might port it to java, and if I do, then I'll be sure it makes its way back to you. Thanks again!

I'll close this issue if I can.