-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (196 loc) · 5.71 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const PythonShell = require('python-shell')
/**
* Boolean Shapely helper script
* https://en.wikipedia.org/wiki/DE-9IM
*
* @private
* @param {string} operation ("crosses", "contains")
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const operation = 'crosses';
* const feature1 = lineString([[-2, 2], [1, 1]]);
* const feature2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely(operation, feature1, feature2)
* .then(result => console.log(result));
*/
function shapely(operation, feature1, feature2) {
// Convert Feature to Geometry
if (feature1.geometry) feature1 = feature1.geometry;
if (feature2.geometry) feature2 = feature2.geometry;
const options = {
scriptPath: __dirname,
args: [
operation,
JSON.stringify(feature1),
JSON.stringify(feature2)
]
};
return new Promise((resolve, reject) => {
const pyshell = new PythonShell('index.py', options);
pyshell.on('message', message => {
return resolve(message === 'True');
});
pyshell.end(error => {
if (error) throw error;
return reject(error);
});
});
};
/**
* Shapely Crosses
*
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const line1 = lineString([[-2, 2], [1, 1]]);
* const line2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely.crosses(line1, line2)
* .then(result => console.log(result));
*/
function crosses(feature1, feature2) {
return shapely('crosses', feature1, feature2)
}
/**
* Shapely Contains
*
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const line1 = lineString([[-2, 2], [1, 1]]);
* const line2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely.contains(line1, line2)
* .then(result => console.log(result));
*/
function contains(feature1, feature2) {
return shapely('contains', feature1, feature2)
}
/**
* Shapely Within
*
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const line1 = lineString([[-2, 2], [1, 1]]);
* const line2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely.within(line1, line2)
* .then(result => console.log(result));
*/
function within(feature1, feature2) {
return shapely('within', feature1, feature2)
}
/**
* Shapely Equals
*
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const line1 = lineString([[-2, 2], [1, 1]]);
* const line2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely.equals(line1, line2)
* .then(result => console.log(result));
*/
function equals(feature1, feature2) {
return shapely('equals', feature1, feature2)
}
/**
* Shapely Touches
*
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const line1 = lineString([[-2, 2], [1, 1]]);
* const line2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely.touches(line1, line2)
* .then(result => console.log(result));
*/
function touches(feature1, feature2) {
return shapely('touches', feature1, feature2)
}
/**
* Shapely Disjoint
*
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const line1 = lineString([[-2, 2], [1, 1]]);
* const line2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely.disjoint(line1, line2)
* .then(result => console.log(result));
*/
function disjoint(feature1, feature2) {
return shapely('disjoint', feature1, feature2)
}
/**
* Shapely Intersects
*
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const line1 = lineString([[-2, 2], [1, 1]]);
* const line2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely.intersects(line1, line2)
* .then(result => console.log(result));
*/
function intersects(feature1, feature2) {
return shapely('intersects', feature1, feature2)
}
/**
* Shapely Overlaps
*
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const line1 = lineString([[-2, 2], [1, 1]]);
* const line2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely.overlaps(line1, line2)
* .then(result => console.log(result));
*/
function overlaps(feature1, feature2) {
return shapely('overlaps', feature1, feature2)
}
/**
* Shapely Covers
*
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature
* @returns {Promise<Boolean>} true/false
* @example
* const line1 = lineString([[-2, 2], [1, 1]]);
* const line2 = lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
*
* shapely.covers(line1, line2)
* .then(result => console.log(result));
*/
function covers(feature1, feature2) {
return shapely('covers', feature1, feature2)
}
module.exports = {
crosses,
contains,
within,
equals,
touches,
disjoint,
intersects,
overlaps,
covers
}