chore: replace point-in-geopolygon by @turf for improved performances (×3)

This commit is contained in:
Basile Bruneau
2020-05-09 11:39:17 +02:00
parent 05859e2463
commit ec79e1201d
3 changed files with 39 additions and 11 deletions
+13 -2
View File
@@ -1,5 +1,5 @@
const proj4 = require('proj4');
const inside = require('point-in-geopolygon');
const inside = require('@turf/boolean-point-in-polygon').default;
const geojsonWorld = require('./countries.geo.json');
const geojsonByCountry = geojsonWorld.features.reduce((countries, feature) => {
@@ -7,6 +7,15 @@ const geojsonByCountry = geojsonWorld.features.reduce((countries, feature) => {
return countries;
}, {});
const geojsonToMultiPolygons = (geojson) => {
const coordinates = geojson.features.reduce(
(poly, feature) =>
poly.concat(feature.geometry.type === 'Polygon' ? [feature.geometry.coordinates] : feature.geometry.coordinates),
[],
);
return { type: 'Feature', geometry: { type: 'MultiPolygon', coordinates } };
};
const CACHE = {};
const DEFAULT_WORLD_REGION = {
@@ -70,6 +79,8 @@ function DottedMap({ height = 0, width = 0, countries = [], region, grid = 'vert
region = DEFAULT_WORLD_REGION;
}
const poly = geojsonToMultiPolygons(geojson);
const cacheKey = [JSON.stringify(region), grid, height, width, JSON.stringify(countries)].join(' ');
const [X_MIN, Y_MIN] = proj4(proj4.defs('GOOGLE'), [region.lng.min, region.lat.min]);
@@ -95,7 +106,7 @@ function DottedMap({ height = 0, width = 0, countries = [], region, grid = 'vert
const pointGoogle = [(localx / width) * X_RANGE + X_MIN, Y_MAX - (localy / height) * Y_RANGE];
const wgs84Point = proj4(proj4.defs('GOOGLE'), proj4.defs('WGS84'), pointGoogle);
if (inside.feature(geojson, wgs84Point) !== -1) {
if (inside(wgs84Point, poly)) {
points[[x, y].join(';')] = { x: localx, y: localy };
}
}