-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
73 lines (69 loc) · 1.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// look here for help http://svn.osgeo.org/grass/grass/branches/releasebranch_6_4/vector/v.overlay/main.c
//must be array of polygons
// depend on jsts for now https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html
var jsts = require('jsts');
/**
* Takes two {@link Polygon|polygons} and returns a combined polygon. If the input polygons are not contiguous, this function returns a {@link MultiPolygon} feature.
*
* @module turf/union
* @category transformation
* @param {Feature<Polygon>} poly1 input polygon
* @param {Feature<Polygon>} poly2 another input polygon
* @return {Feature<(Polygon|MultiPolygon)>} a combined {@link Polygon} or {@link MultiPolygon} feature
* @example
* var poly1 = {
* "type": "Feature",
* "properties": {
* "fill": "#0f0"
* },
* "geometry": {
* "type": "Polygon",
* "coordinates": [[
* [-82.574787, 35.594087],
* [-82.574787, 35.615581],
* [-82.545261, 35.615581],
* [-82.545261, 35.594087],
* [-82.574787, 35.594087]
* ]]
* }
* };
* var poly2 = {
* "type": "Feature",
* "properties": {
* "fill": "#00f"
* },
* "geometry": {
* "type": "Polygon",
* "coordinates": [[
* [-82.560024, 35.585153],
* [-82.560024, 35.602602],
* [-82.52964, 35.602602],
* [-82.52964, 35.585153],
* [-82.560024, 35.585153]
* ]]
* }
* };
* var polygons = {
* "type": "FeatureCollection",
* "features": [poly1, poly2]
* };
*
* var union = turf.union(poly1, poly2);
*
* //=polygons
*
* //=union
*/
module.exports = function(poly1, poly2) {
var reader = new jsts.io.GeoJSONReader();
var a = reader.read(JSON.stringify(poly1.geometry));
var b = reader.read(JSON.stringify(poly2.geometry));
var union = a.union(b);
var parser = new jsts.io.GeoJSONParser();
union = parser.write(union);
return {
type: 'Feature',
geometry: union,
properties: poly1.properties
};
};