├── .travis.yml ├── README.md ├── bench.js ├── index.js ├── npm-shrinkwrap.json ├── package.json └── test ├── fixtures ├── in │ ├── Intersect1.json │ ├── Intersect2.json │ └── armenia.json ├── no-overlap.geojson └── out │ ├── Intersect1.json │ ├── Intersect2.json │ └── armenia.json └── test.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.8" 4 | - "0.10" 5 | before_install: 6 | - npm install -g npm@~1.4.6 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED - replaced with [`@turf/intersect`](https://github.com/Turfjs/turf/tree/master/packages/turf-intersect) 2 | # turf-intersect 3 | 4 | [![build status](https://secure.travis-ci.org/Turfjs/turf-intersect.png)](http://travis-ci.org/Turfjs/turf-intersect) 5 | 6 | find the intersection of spatial features 7 | 8 | 9 | ### `turf.intersect(poly1, poly2)` 10 | 11 | Takes two Polygon|polygons and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined. 12 | 13 | 14 | ### Parameters 15 | 16 | | parameter | type | description | 17 | | --------- | -------------------- | ------------------ | 18 | | `poly1` | Feature\.\ | the first polygon | 19 | | `poly2` | Feature\.\ | the second polygon | 20 | 21 | 22 | ### Example 23 | 24 | ```js 25 | var poly1 = { 26 | "type": "Feature", 27 | "properties": { 28 | "fill": "#0f0" 29 | }, 30 | "geometry": { 31 | "type": "Polygon", 32 | "coordinates": [[ 33 | [-122.801742, 45.48565], 34 | [-122.801742, 45.60491], 35 | [-122.584762, 45.60491], 36 | [-122.584762, 45.48565], 37 | [-122.801742, 45.48565] 38 | ]] 39 | } 40 | } 41 | var poly2 = { 42 | "type": "Feature", 43 | "properties": { 44 | "fill": "#00f" 45 | }, 46 | "geometry": { 47 | "type": "Polygon", 48 | "coordinates": [[ 49 | [-122.520217, 45.535693], 50 | [-122.64038, 45.553967], 51 | [-122.720031, 45.526554], 52 | [-122.669906, 45.507309], 53 | [-122.723464, 45.446643], 54 | [-122.532577, 45.408574], 55 | [-122.487258, 45.477466], 56 | [-122.520217, 45.535693] 57 | ]] 58 | } 59 | } 60 | 61 | var polygons = { 62 | "type": "FeatureCollection", 63 | "features": [poly1, poly2] 64 | }; 65 | 66 | var intersection = turf.intersect(poly1, poly2); 67 | 68 | //=polygons 69 | 70 | //=intersection 71 | ``` 72 | 73 | 74 | **Returns** `Feature.,Feature.`, if `poly1` and `poly2` overlap, returns a Polygon feature representing the area they overlap; if `poly1` and `poly2` do not overlap, returns `undefined`; if `poly1` and `poly2` share a border, a MultiLineString of the locations where their borders are shared 75 | 76 | ## Installation 77 | 78 | Requires [nodejs](http://nodejs.org/). 79 | 80 | ```sh 81 | $ npm install @turf/intersect 82 | ``` 83 | 84 | ## Tests 85 | 86 | ```sh 87 | $ npm test 88 | ``` 89 | 90 | 91 | -------------------------------------------------------------------------------- /bench.js: -------------------------------------------------------------------------------- 1 | var intersect = require('./'); 2 | var Benchmark = require('benchmark'); 3 | var fs = require('fs'); 4 | 5 | var armenia = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/armenia.json')); 6 | var simple = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/Intersect1.json')); 7 | var suite = new Benchmark.Suite('turf-intersect'); 8 | suite 9 | .add('turf-intersect#simple',function () { 10 | intersect(simple[0], simple[1]); 11 | }) 12 | .add('turf-intersect#armenia',function () { 13 | intersect(armenia[0], armenia[1]); 14 | }) 15 | .on('cycle', function (event) { 16 | console.log(String(event.target)); 17 | }) 18 | .on('complete', function () { 19 | 20 | }) 21 | .run(); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // depend on jsts for now https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html 2 | var jsts = require('jsts'); 3 | 4 | /** 5 | * Takes two {@link Polygon|polygons} and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined. 6 | * 7 | * @module turf/intersect 8 | * @category transformation 9 | * @param {Feature} poly1 the first polygon 10 | * @param {Feature} poly2 the second polygon 11 | * @return {(Feature|undefined|Feature)} if `poly1` and `poly2` overlap, returns a Polygon feature representing the area they overlap; if `poly1` and `poly2` do not overlap, returns `undefined`; if `poly1` and `poly2` share a border, a MultiLineString of the locations where their borders are shared 12 | * @example 13 | * var poly1 = { 14 | * "type": "Feature", 15 | * "properties": { 16 | * "fill": "#0f0" 17 | * }, 18 | * "geometry": { 19 | * "type": "Polygon", 20 | * "coordinates": [[ 21 | * [-122.801742, 45.48565], 22 | * [-122.801742, 45.60491], 23 | * [-122.584762, 45.60491], 24 | * [-122.584762, 45.48565], 25 | * [-122.801742, 45.48565] 26 | * ]] 27 | * } 28 | * } 29 | * var poly2 = { 30 | * "type": "Feature", 31 | * "properties": { 32 | * "fill": "#00f" 33 | * }, 34 | * "geometry": { 35 | * "type": "Polygon", 36 | * "coordinates": [[ 37 | * [-122.520217, 45.535693], 38 | * [-122.64038, 45.553967], 39 | * [-122.720031, 45.526554], 40 | * [-122.669906, 45.507309], 41 | * [-122.723464, 45.446643], 42 | * [-122.532577, 45.408574], 43 | * [-122.487258, 45.477466], 44 | * [-122.520217, 45.535693] 45 | * ]] 46 | * } 47 | * } 48 | * 49 | * var polygons = { 50 | * "type": "FeatureCollection", 51 | * "features": [poly1, poly2] 52 | * }; 53 | * 54 | * var intersection = turf.intersect(poly1, poly2); 55 | * 56 | * //=polygons 57 | * 58 | * //=intersection 59 | */ 60 | module.exports = function(poly1, poly2) { 61 | var geom1, geom2; 62 | if(poly1.type === 'Feature') geom1 = poly1.geometry; 63 | else geom1 = poly1; 64 | if(poly2.type === 'Feature') geom2 = poly2.geometry; 65 | else geom2 = poly2; 66 | var reader = new jsts.io.GeoJSONReader(); 67 | var a = reader.read(JSON.stringify(geom1)); 68 | var b = reader.read(JSON.stringify(geom2)); 69 | var intersection = a.intersection(b); 70 | var parser = new jsts.io.GeoJSONParser(); 71 | 72 | intersection = parser.write(intersection); 73 | if(intersection.type === 'GeometryCollection' && intersection.geometries.length === 0) { 74 | return undefined; 75 | } else { 76 | return { 77 | type: 'Feature', 78 | properties: {}, 79 | geometry: intersection 80 | }; 81 | } 82 | }; 83 | -------------------------------------------------------------------------------- /npm-shrinkwrap.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "turf-intersect", 3 | "version": "1.4.2", 4 | "dependencies": { 5 | "jsts": { 6 | "version": "0.15.0", 7 | "from": "jsts@>=0.15.0 <0.16.0", 8 | "resolved": "https://registry.npmjs.org/jsts/-/jsts-0.15.0.tgz", 9 | "dependencies": { 10 | "javascript.util": { 11 | "version": "0.12.5", 12 | "resolved": "https://registry.npmjs.org/javascript.util/-/javascript.util-0.12.5.tgz" 13 | } 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "turf-intersect", 3 | "version": "1.4.2", 4 | "description": "find the intersection of spatial features", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test/test.js", 8 | "doc": "dox -r < index.js | doxme --readme > README.md" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/Turfjs/turf-intersect.git" 13 | }, 14 | "keywords": [ 15 | "turf", 16 | "gis", 17 | "intersect" 18 | ], 19 | "author": "Morgan Herlocker", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/Turfjs/turf-intersect/issues" 23 | }, 24 | "homepage": "https://github.com/Turfjs/turf-intersect", 25 | "devDependencies": { 26 | "benchmark": "^1.0.0", 27 | "glob": "~4.3.5", 28 | "tape": "~3.5.0", 29 | "dox": "^0.6.1", 30 | "doxme": "^1.4.3" 31 | }, 32 | "dependencies": { 33 | "jsts": "~0.15.0" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/fixtures/in/Intersect1.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "type": "Feature", 4 | "properties": {}, 5 | "geometry": { 6 | "type": "Polygon", 7 | "coordinates": [ 8 | [ 9 | [ 10 | -79.88571166992188, 11 | 32.887659962078956 12 | ], 13 | [ 14 | -80.09788513183594, 15 | 32.927436533285565 16 | ], 17 | [ 18 | -80.15350341796875, 19 | 32.82825010814964 20 | ], 21 | [ 22 | -80.00312805175781, 23 | 32.69428812316933 24 | ], 25 | [ 26 | -79.89395141601562, 27 | 32.75551989829049 28 | ], 29 | [ 30 | -79.88571166992188, 31 | 32.887659962078956 32 | ] 33 | ] 34 | ] 35 | } 36 | }, 37 | { 38 | "type": "Feature", 39 | "properties": {}, 40 | "geometry": { 41 | "type": "Polygon", 42 | "coordinates": [ 43 | [ 44 | [ 45 | -79.92141723632812, 46 | 32.953944317478246 47 | ], 48 | [ 49 | -79.97428894042969, 50 | 32.83690450361482 51 | ], 52 | [ 53 | -79.97360229492188, 54 | 32.76071688548088 55 | ], 56 | [ 57 | -79.93034362792969, 58 | 32.76475877693074 59 | ], 60 | [ 61 | -79.93789672851562, 62 | 32.74108223150125 63 | ], 64 | [ 65 | -79.80537414550781, 66 | 32.7231762754146 67 | ], 68 | [ 69 | -79.81773376464844, 70 | 32.923402043498875 71 | ], 72 | [ 73 | -79.92141723632812, 74 | 32.953944317478246 75 | ] 76 | ] 77 | ] 78 | } 79 | } 80 | ] -------------------------------------------------------------------------------- /test/fixtures/in/Intersect2.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "type": "Feature", 4 | "properties": {}, 5 | "geometry": { 6 | "type": "Polygon", 7 | "coordinates": [ 8 | [ 9 | [ 10 | -79.92141723632812, 11 | 32.953944317478246 12 | ], 13 | [ 14 | -80.068359375, 15 | 32.88189375925038 16 | ], 17 | [ 18 | -80.01686096191406, 19 | 32.87266705436184 20 | ], 21 | [ 22 | -79.97360229492188, 23 | 32.76071688548088 24 | ], 25 | [ 26 | -79.93034362792969, 27 | 32.76475877693074 28 | ], 29 | [ 30 | -79.93789672851562, 31 | 32.74108223150125 32 | ], 33 | [ 34 | -79.80537414550781, 35 | 32.7231762754146 36 | ], 37 | [ 38 | -79.81773376464844, 39 | 32.923402043498875 40 | ], 41 | [ 42 | -79.92141723632812, 43 | 32.953944317478246 44 | ] 45 | ] 46 | ] 47 | } 48 | }, 49 | { 50 | "type": "Feature", 51 | "properties": {}, 52 | "geometry": { 53 | "type": "Polygon", 54 | "coordinates": [ 55 | [ 56 | [ 57 | -80.10543823242188, 58 | 32.94760622243483 59 | ], 60 | [ 61 | -80.14389038085938, 62 | 32.8149783969858 63 | ], 64 | [ 65 | -80.07453918457031, 66 | 32.85536439443039 67 | ], 68 | [ 69 | -79.99351501464844, 70 | 32.84440429734253 71 | ], 72 | [ 73 | -79.98184204101562, 74 | 32.90495631913751 75 | ], 76 | [ 77 | -80.10543823242188, 78 | 32.94760622243483 79 | ] 80 | ] 81 | ] 82 | } 83 | } 84 | ] -------------------------------------------------------------------------------- /test/fixtures/in/armenia.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "type": "Feature", 4 | "id": "ARM", 5 | "properties": { 6 | "name": "Armenia" 7 | }, 8 | "geometry": { 9 | "type": "Polygon", 10 | "coordinates": [ 11 | [ 12 | [ 13 | 43.582746, 14 | 41.092143 15 | ], 16 | [ 17 | 44.97248, 18 | 41.248129 19 | ], 20 | [ 21 | 45.179496, 22 | 40.985354 23 | ], 24 | [ 25 | 45.560351, 26 | 40.81229 27 | ], 28 | [ 29 | 45.359175, 30 | 40.561504 31 | ], 32 | [ 33 | 45.891907, 34 | 40.218476 35 | ], 36 | [ 37 | 45.610012, 38 | 39.899994 39 | ], 40 | [ 41 | 46.034534, 42 | 39.628021 43 | ], 44 | [ 45 | 46.483499, 46 | 39.464155 47 | ], 48 | [ 49 | 46.50572, 50 | 38.770605 51 | ], 52 | [ 53 | 46.143623, 54 | 38.741201 55 | ], 56 | [ 57 | 45.735379, 58 | 39.319719 59 | ], 60 | [ 61 | 45.739978, 62 | 39.473999 63 | ], 64 | [ 65 | 45.298145, 66 | 39.471751 67 | ], 68 | [ 69 | 45.001987, 70 | 39.740004 71 | ], 72 | [ 73 | 44.79399, 74 | 39.713003 75 | ], 76 | [ 77 | 44.400009, 78 | 40.005 79 | ], 80 | [ 81 | 43.656436, 82 | 40.253564 83 | ], 84 | [ 85 | 43.752658, 86 | 40.740201 87 | ], 88 | [ 89 | 43.582746, 90 | 41.092143 91 | ] 92 | ] 93 | ] 94 | } 95 | }, 96 | { 97 | "type": "Feature", 98 | "id": "ARM", 99 | "properties": { 100 | "name": "Armenia" 101 | }, 102 | "geometry": { 103 | "type": "Polygon", 104 | "coordinates": [ 105 | [ 106 | [ 107 | 43.582746, 108 | 41.092143 109 | ], 110 | [ 111 | 44.97248, 112 | 41.248129 113 | ], 114 | [ 115 | 45.179496, 116 | 40.985354 117 | ], 118 | [ 119 | 45.560351, 120 | 40.81229 121 | ], 122 | [ 123 | 45.359175, 124 | 40.561504 125 | ], 126 | [ 127 | 45.891907, 128 | 40.218476 129 | ], 130 | [ 131 | 45.610012, 132 | 39.899994 133 | ], 134 | [ 135 | 46.034534, 136 | 39.628021 137 | ], 138 | [ 139 | 46.483499, 140 | 39.464155 141 | ], 142 | [ 143 | 46.50572, 144 | 38.770605 145 | ], 146 | [ 147 | 46.143623, 148 | 38.741201 149 | ], 150 | [ 151 | 45.735379, 152 | 39.319719 153 | ], 154 | [ 155 | 45.739978, 156 | 39.473999 157 | ], 158 | [ 159 | 45.298145, 160 | 39.471751 161 | ], 162 | [ 163 | 45.001987, 164 | 39.740004 165 | ], 166 | [ 167 | 44.79399, 168 | 39.713003 169 | ], 170 | [ 171 | 44.400009, 172 | 40.005 173 | ], 174 | [ 175 | 43.656436, 176 | 40.253564 177 | ], 178 | [ 179 | 43.752658, 180 | 40.740201 181 | ], 182 | [ 183 | 43.582746, 184 | 41.092143 185 | ] 186 | ] 187 | ] 188 | } 189 | }, 190 | { 191 | "type": "Feature", 192 | "properties": {}, 193 | "geometry": { 194 | "type": "Polygon", 195 | "coordinates": [ 196 | [ 197 | [ 198 | 45.1318359375, 199 | 40.1452892956766 200 | ], 201 | [ 202 | 45.1318359375, 203 | 43.197167282501276 204 | ], 205 | [ 206 | 53.3935546875, 207 | 43.197167282501276 208 | ], 209 | [ 210 | 53.3935546875, 211 | 40.1452892956766 212 | ], 213 | [ 214 | 45.1318359375, 215 | 40.1452892956766 216 | ] 217 | ] 218 | ] 219 | } 220 | } 221 | ] -------------------------------------------------------------------------------- /test/fixtures/no-overlap.geojson: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "type": "Feature", 4 | "properties": {}, 5 | "geometry": { 6 | "type": "Polygon", 7 | "coordinates": [ 8 | [ 9 | [ 10 | 92.6806640625, 11 | 53.4357192066942 12 | ], 13 | [ 14 | 92.6806640625, 15 | 53.51418452077113 16 | ], 17 | [ 18 | 93.0322265625, 19 | 53.51418452077113 20 | ], 21 | [ 22 | 93.0322265625, 23 | 53.4357192066942 24 | ], 25 | [ 26 | 92.6806640625, 27 | 53.4357192066942 28 | ] 29 | ] 30 | ] 31 | } 32 | }, 33 | { 34 | "type": "Feature", 35 | "properties": {}, 36 | "geometry": { 37 | "type": "Polygon", 38 | "coordinates": [ 39 | [ 40 | [ 41 | 93.47854614257812, 42 | 53.628353173374194 43 | ], 44 | [ 45 | 93.47854614257812, 46 | 53.74140157486066 47 | ], 48 | [ 49 | 93.680419921875, 50 | 53.74140157486066 51 | ], 52 | [ 53 | 93.680419921875, 54 | 53.628353173374194 55 | ], 56 | [ 57 | 93.47854614257812, 58 | 53.628353173374194 59 | ] 60 | ] 61 | ] 62 | } 63 | } 64 | ] -------------------------------------------------------------------------------- /test/fixtures/out/Intersect1.json: -------------------------------------------------------------------------------- 1 | {"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-79.94623496447946,32.89900638172028],[-79.88571166992188,32.887659962078956],[-79.89395141601562,32.75551989829049],[-79.92322780260464,32.73910022106017],[-79.93789672851562,32.74108223150125],[-79.93034362792969,32.76475877693074],[-79.97360229492188,32.76071688548088],[-79.97428894042969,32.83690450361482],[-79.94623496447946,32.89900638172028]]]}} -------------------------------------------------------------------------------- /test/fixtures/out/Intersect2.json: -------------------------------------------------------------------------------- 1 | {"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-80.0066252126598,32.84617770697059],[-80.01686096191406,32.87266705436184],[-80.068359375,32.88189375925038],[-80.0050160405751,32.91295307720083],[-79.98184204101562,32.90495631913751],[-79.99351501464844,32.84440429734253],[-80.0066252126598,32.84617770697059]]]}} -------------------------------------------------------------------------------- /test/fixtures/out/armenia.json: -------------------------------------------------------------------------------- 1 | {"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[43.582746,41.092143],[44.97248,41.248129],[45.179496,40.985354],[45.560351,40.81229],[45.359175,40.561504],[45.891907,40.218476],[45.610012,39.899994],[46.034534,39.628021],[46.483499,39.464155],[46.50572,38.770605],[46.143623,38.741201],[45.735379,39.319719],[45.739978,39.473999],[45.298145,39.471751],[45.001987,39.740004],[44.79399,39.713003],[44.400009,40.005],[43.656436,40.253564],[43.752658,40.740201],[43.582746,41.092143]]]}} -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var intersect = require('../'), 2 | test = require('tape'), 3 | glob = require('glob'), 4 | fs = require('fs'); 5 | 6 | var REGEN = true; 7 | 8 | test('intersect -- features', function(t){ 9 | glob.sync(__dirname + '/fixtures/in/*.json').forEach(function(input) { 10 | var features = JSON.parse(fs.readFileSync(input)); 11 | var output = intersect(features[0], features[1]); 12 | if (REGEN) fs.writeFileSync(input.replace('/in/', '/out/'), JSON.stringify(output)); 13 | t.deepEqual(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/'))), input); 14 | }); 15 | t.end(); 16 | }); 17 | 18 | test('intersect -- geometries', function(t){ 19 | glob.sync(__dirname + '/fixtures/in/*.json').forEach(function(input) { 20 | var features = JSON.parse(fs.readFileSync(input)); 21 | var output = intersect(features[0].geometry, features[1].geometry); 22 | if (REGEN) fs.writeFileSync(input.replace('/in/', '/out/'), JSON.stringify(output)); 23 | t.deepEqual(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/'))), input); 24 | }); 25 | t.end(); 26 | }); 27 | 28 | test('intersect -- no overlap', function(t){ 29 | var noOverlap = JSON.parse(fs.readFileSync(__dirname+'/fixtures/no-overlap.geojson')); 30 | var output = intersect(noOverlap[0].geometry, noOverlap[1].geometry); 31 | t.deepEqual(output, undefined); 32 | t.end(); 33 | }); --------------------------------------------------------------------------------