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

Not working with odd shaped polygon #42

Closed green3g closed 6 years ago

green3g commented 6 years ago

The point generated falls outside of this polygon:

{
    "type": "Polygon",
    "coordinates": [
        [
            [-93.02957807480158, 44.861837817995564],
            [-93.02959954453688, 44.86892493940014],
            [-93.0280974715503, 44.86877290567057],
            [-93.02829060933638, 44.869153052906924],
            [-93.0298784714326, 44.86912268451289],
            [-93.02992141090319, 44.86551834530052],
            [-93.03996358763281, 44.86551834530052],
            [-93.04249557909264, 44.86548797498883],
            [-93.04513491922891, 44.86503171802141],
            [-93.04513491922891, 44.86354124914875],
            [-93.04599316965135, 44.86284162150566],
            [-93.0443409881808, 44.86136631401641],
            [-93.0433968588172, 44.859586789464856],
            [-93.0427531260846, 44.857852803290726],
            [-93.0422595916675, 44.856818474236],
            [-93.0414441908841, 44.856240456857805],
            [-93.04043565231463, 44.85555596011782],
            [-93.0401567254189, 44.85552558455064],
            [-93.0401567254189, 44.85596669734617],
            [-93.04183037662474, 44.85695538396923],
            [-93.04234538077714, 44.857837584275984],
            [-93.04294617403916, 44.859678037043125],
            [-93.04382598402843, 44.86150321293619],
            [-93.04513491922891, 44.862735161220904],
            [-93.0445126562316, 44.86326746067597],
            [-93.04457697560595, 44.86483395885438],
            [-93.0424312597183, 44.86522941283937],
            [-93.03916965658472, 44.865275064010014],
            [-93.03000720001282, 44.865244629900275],
            [-93.02989994116788, 44.86189862612768],
            [-93.02957807480158, 44.861837817995564]
        ]
    ]
}

The generated point appears to be these coordinates:

{"x":-93.03840446234251,"y":44.863239663088805,"spatialReference":{"wkid":4326}}

The dark solid gray point is the generated point:

image

import polylabel from 'polylabel';
import {arcgisToGeoJSON, geojsonToArcGIS} from '@esri/arcgis-to-geojson-utils';
import {project} from 'esri/geometry/support/webMercatorUtils';
import {WGS84, WebMercator} from 'esri/geometry/SpatialReference';
import Point from 'esri/geometry/Point';

export default function(graphic, event, vm) {
    let polygon = project(graphic.geometry, WGS84);
    polygon = arcgisToGeoJSON(polygon.toJSON());
    const point = geojsonToArcGIS({
        type: 'Point',
        coordinates: polylabel(polygon.coordinates),
    });
    return new Point(project(point, WebMercator));
}
talaj commented 6 years ago

Have you tried smaller precision parameter? Given your values, I would try 0.001.

green3g commented 6 years ago

I will try that! Thank you.

green3g commented 6 years ago

Nice! That did it. Thanks again!

image