point-in-polygon-hao
Advanced tools
+2
-1
@@ -7,4 +7,5 @@ { | ||
| "prefer-arrow-callback": 0, | ||
| "no-unneeded-ternary": 0 | ||
| "no-unneeded-ternary": 0, | ||
| "arrow-parens": 0 | ||
| }, | ||
| } |
@@ -27,2 +27,7 @@ (function (global, factory) { | ||
| const contour = polygon[i]; | ||
| // This helps to ensure that points on the left edges pass... | ||
| // Not 100% sure if it's legit | ||
| contour.push(contour[1]); | ||
| currentP = contour[0]; | ||
@@ -68,2 +73,3 @@ v1 = currentP[0] - x; | ||
| } | ||
| } | ||
@@ -70,0 +76,0 @@ |
@@ -1,1 +0,1 @@ | ||
| !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self).pointInPolygon=n()}(this,function(){"use strict";return function(e,n){let f=0,t=0,i=0,r=0,o=0,l=0,u=0,s=0,c=null,d=null;const p=e[0],y=e[1],g=n.length;for(;f<g;f++){t=0;const g=n[f].length-1,h=n[f];for(l=(c=h[0])[0]-p,o=c[1]-y;t<g;t++)if(s=(d=h[t+1])[1]-y,l<0&&s<0||l>0&&s>0)l=s,o=(c=d)[0]-p;else{if(u=d[0]-e[0],s>0&&l<=0){if((r=o*s-u*l)>0)i+=1;else if(0===r)return 0}else if(l>0&&s<=0){if((r=o*s-u*l)<0)i+=1;else if(0===r)return 0}else if(0===s&&l<0){if(0==(r=o*s-u*l))return 0}else if(0===l&&s<0){if(0==(r=o*s-u*l))return 0}else if(0===l&&0===s){if(u<=0&&o>=0)return 0;if(o<=0&&u>=0)return 0}c=d,l=s,o=u}}return i%2!=0}}); | ||
| !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self).pointInPolygon=n()}(this,function(){"use strict";return function(e,n){let f=0,t=0,i=0,r=0,o=0,u=0,l=0,s=0,c=null,d=null;const p=e[0],h=e[1],y=n.length;for(;f<y;f++){t=0;const y=n[f].length-1,g=n[f];for(g.push(g[1]),u=(c=g[0])[0]-p,o=c[1]-h;t<y;t++)if(s=(d=g[t+1])[1]-h,u<0&&s<0||u>0&&s>0)u=s,o=(c=d)[0]-p;else{if(l=d[0]-e[0],s>0&&u<=0){if((r=o*s-l*u)>0)i+=1;else if(0===r)return 0}else if(u>0&&s<=0){if((r=o*s-l*u)<0)i+=1;else if(0===r)return 0}else if(0===s&&u<0){if(0==(r=o*s-l*u))return 0}else if(0===u&&s<0){if(0==(r=o*s-l*u))return 0}else if(0===u&&0===s){if(l<=0&&o>=0)return 0;if(o<=0&&l>=0)return 0}c=d,u=s,o=l}}return i%2!=0}}); |
+2
-1
| { | ||
| "name": "point-in-polygon-hao", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "description": "A point in polygon based on the paper Optimal Reliable Point-in-Polygon Test and Differential Coding Boolean Operations on Polygons", | ||
@@ -22,2 +22,3 @@ "main": "dist/pointInPolygon.js", | ||
| "esm": "^3.2.20", | ||
| "jsts": "^2.0.3", | ||
| "load-json-file": "^5.2.0", | ||
@@ -24,0 +25,0 @@ "point-in-polygon": "^1.0.1", |
+11
-5
@@ -8,12 +8,16 @@ Based on the paper [Optimal Reliable Point-in-Polygon Test and | ||
| ### Usage | ||
| Install via `npm install point-in-polygon-hao` | ||
| ```` | ||
| const inside = require('point-in-polygon-hao') | ||
| const polygon = [ [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ], [ 1, 1 ] ] ]; | ||
| inside([ 1.5, 1.5 ], polygon) | ||
| // when inside returns true | ||
| // returns true | ||
| inside([ 4.9, 1.2 ], polygon) | ||
| // when outside returns false | ||
| // returns false | ||
| inside([1, 2], polygon) | ||
| // when on edge returns 0 | ||
| // returns 0 to indicate on edge | ||
| ```` | ||
@@ -23,4 +27,4 @@ | ||
| ### Comparisons | ||
| Some rough comparisons to similar libraries although | ||
| `point-in-polygon` & `robust-point-in-polygon` do not support polygons with holes. So while `point-in-polygon` is slightly faster in most cases it supports fewer use cases. | ||
| Some rough comparisons to similar libraries. | ||
| While `point-in-polygon` is slightly faster in most cases it does not support polygons with holes or degenerate polygons. | ||
@@ -33,3 +37,5 @@ ```` | ||
| robust-point-in-polygon x 18,344,949 ops/sec ±1.69% (83 runs sampled) | ||
| ```` | ||
| ```` | ||
| // For a point in a much larger geometry (700+ vertices) | ||
@@ -36,0 +42,0 @@ point-in-poly-hao x 449,670 ops/sec ±0.80% (90 runs sampled) |
+6
-0
@@ -21,2 +21,7 @@ export default function pointInPolygon(p, polygon) { | ||
| const contour = polygon[i] | ||
| // This helps to ensure that points on the left edges pass... | ||
| // Not 100% sure if it's legit | ||
| contour.push(contour[1]) | ||
| currentP = contour[0] | ||
@@ -62,2 +67,3 @@ v1 = currentP[0] - x | ||
| } | ||
| } | ||
@@ -64,0 +70,0 @@ |
| const Benchmark = require('benchmark') | ||
| const pipHao = require('../dist/pointInPolygon.js') | ||
| const inside = require('point-in-polygon') | ||
| const turfPip = require('@turf/boolean-point-in-polygon').default | ||
| const robustPip = require('robust-point-in-polygon') | ||
| const loadJsonFile = require('load-json-file') | ||
| const path = require('path') | ||
| const insideSuite = new Benchmark.Suite(); | ||
| const geojsonPoly = [[[1, 1], [1, 2], [2, 2], [2, 1], [1, 1]]]; | ||
| const polygon = [[1, 1], [1, 2], [2, 2], [2, 1]]; | ||
| insideSuite | ||
| .add('point-in-poly-hao', function() { | ||
| pipHao([1.5, 1.5], geojsonPoly) | ||
| }) | ||
| .add('turf-point-in-polygon', function() { | ||
| turfPip({type: 'Point', coordinates: [1.5, 1.5]}, {type: 'Polygon', coordinates: geojsonPoly}) | ||
| }) | ||
| .add('point-in-polygon', function() { | ||
| inside([1.5, 1.5], polygon) | ||
| }) | ||
| .add('robust-point-in-polygon', function() { | ||
| robustPip(polygon, [1.5, 1.5]) | ||
| }) | ||
| .on('cycle', function(event) { | ||
| console.log(String(event.target)) | ||
| }) | ||
| .on('complete', function() { | ||
| console.log('Fastest is ' + this.filter('fastest').map('name')); | ||
| }) | ||
| .run() | ||
| const largePolySuite = new Benchmark.Suite(); | ||
| const switzerland = loadJsonFile.sync(path.join(__dirname, 'fixtures', 'simple', 'switzerland.geojson')) | ||
| const switzCoords = switzerland.geometry.coordinates | ||
| const mainSwissRing = switzCoords[0] | ||
| largePolySuite | ||
| .add('point-in-poly-hao', function() { | ||
| pipHao([8, 46.5], switzCoords) | ||
| }) | ||
| .add('turf-point-in-polygon', function() { | ||
| turfPip({type: 'Point', coordinates: [8, 46.5]}, switzerland) | ||
| }) | ||
| .add('point-in-polygon', function() { | ||
| inside([8, 46.5], mainSwissRing) | ||
| }) | ||
| .add('robust-point-in-polygon', function() { | ||
| robustPip(mainSwissRing, [8, 46.5]) | ||
| }) | ||
| .on('cycle', function(event) { | ||
| console.log(String(event.target)) | ||
| }) | ||
| .on('complete', function() { | ||
| console.log('Fastest is ' + this.filter('fastest').map('name')); | ||
| }) | ||
| .run() | ||
| import test from 'ava' | ||
| const loadJsonFile = require('load-json-file') | ||
| const path = require('path') | ||
| import inside from '../src/index' | ||
| const switzerland = loadJsonFile.sync(path.join(__dirname, 'fixtures', 'simple', 'switzerland.geojson')) | ||
| const switzCoords = switzerland.geometry.coordinates | ||
| test('is inside', t => { | ||
| t.true(inside([ 8, 46.5 ], switzCoords)) | ||
| }); | ||
| test('is outside', t => { | ||
| t.false(inside([ 8, 44 ], switzCoords)) | ||
| }); | ||
| const switzerlandKinked = loadJsonFile.sync(path.join(__dirname, 'fixtures', 'notSimple', 'switzerlandKinked.geojson')) | ||
| const switzKinkedCoords = switzerlandKinked.geometry.coordinates | ||
| test('is inside kinked', t => { | ||
| t.true(inside([ 8, 46.5 ], switzKinkedCoords)) | ||
| }); | ||
| test('is outside kinked', t => { | ||
| t.false(inside([ 8, 44 ], switzKinkedCoords)) | ||
| }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
| import test from 'ava'; | ||
| import inside from '../src/index' | ||
| const polygon = [ [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ], [ 1, 1 ] ] ]; | ||
| test('is inside', t => { | ||
| t.true(inside([ 1.5, 1.5 ], polygon)) | ||
| }); | ||
| test('is outside', t => { | ||
| t.false(inside([ 4.9, 1.2 ], polygon)) | ||
| }); | ||
| test('is on edge', t => { | ||
| t.is(inside([1, 2], polygon), 0) | ||
| }); | ||
| const polygonWithHole = [ [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ], [ 1, 1 ] ], | ||
| [ [ 1.5, 1.5 ], [ 1.5, 1.7 ], [ 1.7, 1.7 ], [ 1.7, 1.5 ], [ 1.5, 1.5 ] ] ]; | ||
| test('is inside with hole', t => { | ||
| t.true(inside([ 1.2, 1.2 ], polygonWithHole)) | ||
| }); | ||
| test('is outside with hole', t => { | ||
| t.false(inside([ 4.9, 1.2 ], polygonWithHole)) | ||
| }); | ||
| test('is in the hole', t => { | ||
| t.false(inside([ 1.6, 1.6 ], polygonWithHole)) | ||
| }); | ||
| test('is on edge with hole', t => { | ||
| t.is(inside([1.5, 1.5], polygonWithHole), 0) | ||
| }); | ||
| test('is on edge of the outside', t => { | ||
| t.is(inside([1.2, 1], polygonWithHole), 0) | ||
| }); |
44
15.79%8233
-92.82%12
9.09%7
-41.67%144
-39.75%