chore: replace point-in-geopolygon by @turf for improved performances (×3)
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
const proj4 = require('proj4');
|
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 geojsonWorld = require('./countries.geo.json');
|
||||||
|
|
||||||
const geojsonByCountry = geojsonWorld.features.reduce((countries, feature) => {
|
const geojsonByCountry = geojsonWorld.features.reduce((countries, feature) => {
|
||||||
@@ -7,6 +7,15 @@ const geojsonByCountry = geojsonWorld.features.reduce((countries, feature) => {
|
|||||||
return countries;
|
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 CACHE = {};
|
||||||
|
|
||||||
const DEFAULT_WORLD_REGION = {
|
const DEFAULT_WORLD_REGION = {
|
||||||
@@ -70,6 +79,8 @@ function DottedMap({ height = 0, width = 0, countries = [], region, grid = 'vert
|
|||||||
region = DEFAULT_WORLD_REGION;
|
region = DEFAULT_WORLD_REGION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const poly = geojsonToMultiPolygons(geojson);
|
||||||
|
|
||||||
const cacheKey = [JSON.stringify(region), grid, height, width, JSON.stringify(countries)].join(' ');
|
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]);
|
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 pointGoogle = [(localx / width) * X_RANGE + X_MIN, Y_MAX - (localy / height) * Y_RANGE];
|
||||||
const wgs84Point = proj4(proj4.defs('GOOGLE'), proj4.defs('WGS84'), pointGoogle);
|
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 };
|
points[[x, y].join(';')] = { x: localx, y: localy };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+24
-7
@@ -1,19 +1,36 @@
|
|||||||
{
|
{
|
||||||
"name": "dotted-map-generator",
|
"name": "dotted-map",
|
||||||
"version": "1.0.0",
|
"version": "1.1.1",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@turf/boolean-point-in-polygon": {
|
||||||
|
"version": "6.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@turf/boolean-point-in-polygon/-/boolean-point-in-polygon-6.0.1.tgz",
|
||||||
|
"integrity": "sha512-FKLOZ124vkJhjzNSDcqpwp2NvfnsbYoUOt5iAE7uskt4svix5hcjIEgX9sELFTJpbLGsD1mUbKdfns8tZxcMNg==",
|
||||||
|
"requires": {
|
||||||
|
"@turf/helpers": "6.x",
|
||||||
|
"@turf/invariant": "6.x"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"@turf/helpers": {
|
||||||
|
"version": "6.1.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.1.4.tgz",
|
||||||
|
"integrity": "sha512-vJvrdOZy1ngC7r3MDA7zIGSoIgyrkWcGnNIEaqn/APmw+bVLF2gAW7HIsdTxd12s5wQMqEpqIQrmrbRRZ0xC7g=="
|
||||||
|
},
|
||||||
|
"@turf/invariant": {
|
||||||
|
"version": "6.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@turf/invariant/-/invariant-6.1.2.tgz",
|
||||||
|
"integrity": "sha512-WU08Ph8j0J2jVGlQCKChXoCtI50BB3yEH21V++V0T4cR1T27HKCxkehV2sYMwTierfMBgjwSwDIsxnR4/2mWXg==",
|
||||||
|
"requires": {
|
||||||
|
"@turf/helpers": "6.x"
|
||||||
|
}
|
||||||
|
},
|
||||||
"mgrs": {
|
"mgrs": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/mgrs/-/mgrs-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/mgrs/-/mgrs-1.0.0.tgz",
|
||||||
"integrity": "sha1-+5FYjnjJACVnI5XLQLJffNatGCk="
|
"integrity": "sha1-+5FYjnjJACVnI5XLQLJffNatGCk="
|
||||||
},
|
},
|
||||||
"point-in-geopolygon": {
|
|
||||||
"version": "1.0.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/point-in-geopolygon/-/point-in-geopolygon-1.0.1.tgz",
|
|
||||||
"integrity": "sha1-InEbCCna4qpXyTk/Mw2//jEfb90="
|
|
||||||
},
|
|
||||||
"prettier": {
|
"prettier": {
|
||||||
"version": "2.0.5",
|
"version": "2.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz",
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "dotted-map",
|
"name": "dotted-map",
|
||||||
"version": "1.1.1",
|
"version": "1.2.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"point-in-geopolygon": "^1.0.1",
|
"@turf/boolean-point-in-polygon": "^6.0.1",
|
||||||
"proj4": "^2.6.1"
|
"proj4": "^2.6.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
Reference in New Issue
Block a user