├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── bench.js ├── bin └── inside.js ├── fixtures ├── multipoly-with-hole.geojson └── poly-with-hole.geojson ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | 27 | .DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.10 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Morgan Herlocker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED - replaced by [`@turf/boolean-point-in-polygon`](https://www.npmjs.com/package/@turf/boolean-point-in-polygon) 2 | 3 | # turf-inside 4 | 5 | [![build status](https://secure.travis-ci.org/Turfjs/turf-inside.png)](http://travis-ci.org/Turfjs/turf-inside) 6 | 7 | turf inside module 8 | 9 | 10 | ### `turf.inside(point, polygon)` 11 | 12 | Takes a Point and a Polygon or MultiPolygon and determines if the point resides inside the polygon. The polygon can 13 | be convex or concave. The function accounts for holes. 14 | 15 | 16 | ### Parameters 17 | 18 | | parameter | type | description | 19 | | --------- | --------------------------------- | ----------------------------- | 20 | | `point` | Feature\.\ | input point | 21 | | `polygon` | Feature\.\ | input polygon or multipolygon | 22 | 23 | 24 | ### Example 25 | 26 | ```js 27 | var pt1 = { 28 | "type": "Feature", 29 | "properties": { 30 | "marker-color": "#f00" 31 | }, 32 | "geometry": { 33 | "type": "Point", 34 | "coordinates": [-111.467285, 40.75766] 35 | } 36 | }; 37 | var pt2 = { 38 | "type": "Feature", 39 | "properties": { 40 | "marker-color": "#0f0" 41 | }, 42 | "geometry": { 43 | "type": "Point", 44 | "coordinates": [-111.873779, 40.647303] 45 | } 46 | }; 47 | var poly = { 48 | "type": "Feature", 49 | "properties": {}, 50 | "geometry": { 51 | "type": "Polygon", 52 | "coordinates": [[ 53 | [-112.074279, 40.52215], 54 | [-112.074279, 40.853293], 55 | [-111.610107, 40.853293], 56 | [-111.610107, 40.52215], 57 | [-112.074279, 40.52215] 58 | ]] 59 | } 60 | }; 61 | 62 | var features = { 63 | "type": "FeatureCollection", 64 | "features": [pt1, pt2, poly] 65 | }; 66 | 67 | //=features 68 | 69 | var isInside1 = turf.inside(pt1, poly); 70 | //=isInside1 71 | 72 | var isInside2 = turf.inside(pt2, poly); 73 | //=isInside2 74 | ``` 75 | 76 | 77 | **Returns** `Boolean`, `true` if the Point is inside the Polygon; `false` if the Point is not inside the Polygon 78 | 79 | ## Installation 80 | 81 | Requires [nodejs](http://nodejs.org/). 82 | 83 | ```sh 84 | $ npm install turf-inside 85 | ``` 86 | 87 | ## Tests 88 | 89 | ```sh 90 | $ npm test 91 | ``` 92 | 93 | 94 | -------------------------------------------------------------------------------- /bench.js: -------------------------------------------------------------------------------- 1 | var inside = require('./'); 2 | var Benchmark = require('benchmark'); 3 | var fs = require('fs'); 4 | var point = require('turf-point'); 5 | var polygon = require('turf-polygon'); 6 | 7 | var poly = polygon([[[0,0], [0,100], [100,100], [100,0]]]); 8 | var ptIn = point(50, 50); 9 | var ptOut = point(140, 150); 10 | 11 | var suite = new Benchmark.Suite('turf-inside'); 12 | suite 13 | .add('turf-inside',function () { 14 | inside(ptIn, poly); 15 | }) 16 | .on('cycle', function (event) { 17 | console.log(String(event.target)); 18 | }) 19 | .on('complete', function () { 20 | 21 | }) 22 | .run(); -------------------------------------------------------------------------------- /bin/inside.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var point = require('../'); 3 | var argv = require('minimist')(process.argv.slice(2)); 4 | 5 | var x,y; 6 | 7 | if(argv.h || argv.help){ 8 | docs(); 9 | } 10 | else { 11 | if(argv.point && argv.y){ 12 | x = parseFloat(process.argv[process.argv.indexOf('-x') + 1]); 13 | y = parseFloat(process.argv[process.argv.indexOf('-y') + 1]); 14 | } 15 | 16 | console.log(JSON.stringify(point(x, y))); 17 | } 18 | 19 | function docs(){ 20 | console.log('turf-inside\n===\n'); 21 | console.log('-h --help: show docs\n'); 22 | console.log('\nusage: \nturf-point [point_file] [polygon_file]\n\n') 23 | } -------------------------------------------------------------------------------- /fixtures/multipoly-with-hole.geojson: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | { 5 | "type": "Feature", 6 | "properties": {}, 7 | "geometry": { 8 | "type": "MultiPolygon", 9 | "coordinates": [ 10 | [ 11 | [ 12 | [ 13 | -86.76624298095703, 14 | 36.171278341935434 15 | ], 16 | [ 17 | -86.77362442016602, 18 | 36.2014818084173 19 | ], 20 | [ 21 | -86.74100875854492, 22 | 36.19607929145354 23 | ], 24 | [ 25 | -86.74238204956055, 26 | 36.170862616662134 27 | ], 28 | [ 29 | -86.76624298095703, 30 | 36.171278341935434 31 | ] 32 | ] 33 | ], 34 | [ 35 | [ 36 | [ 37 | -86.70478820800781, 38 | 36.23084281427824 39 | ], 40 | [ 41 | -86.73980712890625, 42 | 36.21062368007896 43 | ], 44 | [ 45 | -86.71371459960938, 46 | 36.173495506147 47 | ], 48 | [ 49 | -86.67526245117186, 50 | 36.17709826419592 51 | ], 52 | [ 53 | -86.67303085327148, 54 | 36.20910010895552 55 | ], 56 | [ 57 | -86.68041229248047, 58 | 36.230427405208005 59 | ], 60 | [ 61 | -86.70478820800781, 62 | 36.23084281427824 63 | ] 64 | ],[ 65 | [ 66 | -86.6934585571289, 67 | 36.217271643303604 68 | ], 69 | [ 70 | -86.71268463134766, 71 | 36.20771501855801 72 | ], 73 | [ 74 | -86.70238494873047, 75 | 36.19067640168397 76 | ], 77 | [ 78 | -86.68487548828125, 79 | 36.19691047217554 80 | ], 81 | [ 82 | -86.68264389038086, 83 | 36.20993115142727 84 | ], 85 | [ 86 | -86.6934585571289, 87 | 36.217271643303604 88 | ] 89 | ]] 90 | ] 91 | } 92 | } -------------------------------------------------------------------------------- /fixtures/poly-with-hole.geojson: -------------------------------------------------------------------------------- 1 | { 2 | "type": "Feature", 3 | "properties": {}, 4 | "geometry": { 5 | "type": "Polygon", 6 | "coordinates": [ 7 | [ 8 | [ 9 | -86.70478820800781, 10 | 36.23084281427824 11 | ], 12 | [ 13 | -86.73980712890625, 14 | 36.21062368007896 15 | ], 16 | [ 17 | -86.71371459960938, 18 | 36.173495506147 19 | ], 20 | [ 21 | -86.67526245117186, 22 | 36.17709826419592 23 | ], 24 | [ 25 | -86.67303085327148, 26 | 36.20910010895552 27 | ], 28 | [ 29 | -86.68041229248047, 30 | 36.230427405208005 31 | ], 32 | [ 33 | -86.70478820800781, 34 | 36.23084281427824 35 | ] 36 | ], 37 | [ 38 | [ 39 | -86.6934585571289, 40 | 36.217271643303604 41 | ], 42 | [ 43 | -86.71268463134766, 44 | 36.20771501855801 45 | ], 46 | [ 47 | -86.70238494873047, 48 | 36.19067640168397 49 | ], 50 | [ 51 | -86.68487548828125, 52 | 36.19691047217554 53 | ], 54 | [ 55 | -86.68264389038086, 56 | 36.20993115142727 57 | ], 58 | [ 59 | -86.6934585571289, 60 | 36.217271643303604 61 | ] 62 | ] 63 | ] 64 | } 65 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var invariant = require('turf-invariant'); 2 | 3 | // http://en.wikipedia.org/wiki/Even%E2%80%93odd_rule 4 | // modified from: https://github.com/substack/point-in-polygon/blob/master/index.js 5 | // which was modified from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html 6 | 7 | /** 8 | * Takes a {@link Point} and a {@link Polygon} or {@link MultiPolygon} and determines if the point resides inside the polygon. The polygon can 9 | * be convex or concave. The function accounts for holes. 10 | * 11 | * @module turf/inside 12 | * @category joins 13 | * @param {Feature} point input point 14 | * @param {Feature<(Polygon|MultiPolygon)>} polygon input polygon or multipolygon 15 | * @return {Boolean} `true` if the Point is inside the Polygon; `false` if the Point is not inside the Polygon 16 | * @example 17 | * var pt1 = { 18 | * "type": "Feature", 19 | * "properties": { 20 | * "marker-color": "#f00" 21 | * }, 22 | * "geometry": { 23 | * "type": "Point", 24 | * "coordinates": [-111.467285, 40.75766] 25 | * } 26 | * }; 27 | * var pt2 = { 28 | * "type": "Feature", 29 | * "properties": { 30 | * "marker-color": "#0f0" 31 | * }, 32 | * "geometry": { 33 | * "type": "Point", 34 | * "coordinates": [-111.873779, 40.647303] 35 | * } 36 | * }; 37 | * var poly = { 38 | * "type": "Feature", 39 | * "properties": {}, 40 | * "geometry": { 41 | * "type": "Polygon", 42 | * "coordinates": [[ 43 | * [-112.074279, 40.52215], 44 | * [-112.074279, 40.853293], 45 | * [-111.610107, 40.853293], 46 | * [-111.610107, 40.52215], 47 | * [-112.074279, 40.52215] 48 | * ]] 49 | * } 50 | * }; 51 | * 52 | * var features = { 53 | * "type": "FeatureCollection", 54 | * "features": [pt1, pt2, poly] 55 | * }; 56 | * 57 | * //=features 58 | * 59 | * var isInside1 = turf.inside(pt1, poly); 60 | * //=isInside1 61 | * 62 | * var isInside2 = turf.inside(pt2, poly); 63 | * //=isInside2 64 | */ 65 | module.exports = function(point, polygon) { 66 | invariant.featureOf(point, 'Point', 'inside'); 67 | var polys = polygon.geometry.coordinates; 68 | var pt = [point.geometry.coordinates[0], point.geometry.coordinates[1]]; 69 | // normalize to multipolygon 70 | if (polygon.geometry.type === 'Polygon') polys = [polys]; 71 | 72 | var insidePoly = false; 73 | var i = 0; 74 | while (i < polys.length && !insidePoly) { 75 | // check if it is in the outer ring first 76 | if(inRing(pt, polys[i][0])) { 77 | var inHole = false; 78 | var k = 1; 79 | // check for the point in any of the holes 80 | while(k < polys[i].length && !inHole) { 81 | if(inRing(pt, polys[i][k])) { 82 | inHole = true; 83 | } 84 | k++; 85 | } 86 | if(!inHole) insidePoly = true; 87 | } 88 | i++; 89 | } 90 | return insidePoly; 91 | }; 92 | 93 | // pt is [x,y] and ring is [[x,y], [x,y],..] 94 | function inRing (pt, ring) { 95 | var isInside = false; 96 | for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) { 97 | var xi = ring[i][0], yi = ring[i][1]; 98 | var xj = ring[j][0], yj = ring[j][1]; 99 | var intersect = ((yi > pt[1]) !== (yj > pt[1])) && 100 | (pt[0] < (xj - xi) * (pt[1] - yi) / (yj - yi) + xi); 101 | if (intersect) isInside = !isInside; 102 | } 103 | return isInside; 104 | } 105 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "turf-inside", 3 | "version": "1.1.4", 4 | "description": "turf inside module", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "tape test.js", 8 | "doc": "dox -r < index.js | doxme --readme > README.md" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/Turfjs/turf-inside.git" 13 | }, 14 | "keywords": [ 15 | "geojson", 16 | "polygon", 17 | "point", 18 | "inside", 19 | "bin", 20 | "gis" 21 | ], 22 | "author": "morganherlocker", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/Turfjs/turf-inside/issues" 26 | }, 27 | "homepage": "https://github.com/Turfjs/turf-inside", 28 | "bin": {}, 29 | "dependencies": { 30 | "minimist": "^1.1.0", 31 | "turf-invariant": "^1.0.3" 32 | }, 33 | "devDependencies": { 34 | "benchmark": "^1.0.0", 35 | "tape": "^3.5.0", 36 | "turf-point": "^2.0.0", 37 | "turf-polygon": "^1.0.2", 38 | "dox": "^0.6.1", 39 | "doxme": "^1.4.3" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape'); 2 | var inside = require('./'); 3 | var point = require('turf-point'); 4 | var polygon = require('turf-polygon'); 5 | var fs = require('fs'); 6 | 7 | test('bad type', function (t) { 8 | var poly = polygon([[[0,0], [0,100], [100,100], [100,0], [0,0]]]); 9 | 10 | t.throws(function() { 11 | inside(poly, poly); 12 | }, /Invalid input to inside: must be a Point, given Polygon/); 13 | 14 | t.end(); 15 | }); 16 | 17 | test('featureCollection', function (t) { 18 | // test for a simple polygon 19 | var poly = polygon([[[0,0], [0,100], [100,100], [100,0], [0,0]]]); 20 | var ptIn = point([50, 50]); 21 | var ptOut = point([140, 150]); 22 | 23 | t.true(inside(ptIn, poly), 'point inside simple polygon'); 24 | t.false(inside(ptOut, poly), 'point outside simple polygon'); 25 | 26 | // test for a concave polygon 27 | var concavePoly = polygon([[[0,0], [50, 50], [0,100], [100,100], [100,0], [0,0]]]); 28 | var ptConcaveIn = point([75, 75]); 29 | var ptConcaveOut = point([25, 50]); 30 | 31 | t.true(inside(ptConcaveIn, concavePoly), 'point inside concave polygon'); 32 | t.false(inside(ptConcaveOut, concavePoly), 'point outside concave polygon'); 33 | 34 | t.end(); 35 | }); 36 | 37 | test('poly with hole', function (t) { 38 | var ptInHole = point([-86.69208526611328, 36.20373274711739]); 39 | var ptInPoly = point([-86.72229766845702, 36.20258997094334]); 40 | var ptOutsidePoly = point([-86.75079345703125, 36.18527313913089]); 41 | var polyHole = JSON.parse(fs.readFileSync(__dirname + '/fixtures/poly-with-hole.geojson')); 42 | 43 | t.false(inside(ptInHole, polyHole)); 44 | t.true(inside(ptInPoly, polyHole)); 45 | t.false(inside(ptOutsidePoly, polyHole)); 46 | 47 | t.end(); 48 | }); 49 | 50 | test('multipolygon with hole', function (t) { 51 | var ptInHole = point([-86.69208526611328, 36.20373274711739]); 52 | var ptInPoly = point([-86.72229766845702, 36.20258997094334]); 53 | var ptInPoly2 = point([-86.75079345703125, 36.18527313913089]); 54 | var ptOutsidePoly = point([-86.75302505493164, 36.23015046460186]); 55 | var multiPolyHole = JSON.parse(fs.readFileSync(__dirname + '/fixtures/multipoly-with-hole.geojson')); 56 | 57 | t.false(inside(ptInHole, multiPolyHole)); 58 | t.true(inside(ptInPoly, multiPolyHole)); 59 | t.true(inside(ptInPoly2, multiPolyHole)); 60 | t.true(inside(ptInPoly, multiPolyHole)); 61 | t.false(inside(ptOutsidePoly, multiPolyHole)); 62 | 63 | t.end(); 64 | }); 65 | --------------------------------------------------------------------------------