-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathGeometryUtil.ts
402 lines (374 loc) · 15.9 KB
/
GeometryUtil.ts
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import buffer from '@turf/buffer';
import difference from '@turf/difference';
import intersect from '@turf/intersect';
import { featureCollection, flatten } from '@turf/turf';
import union from '@turf/union';
import {
Feature as GeoJSONFeature,
MultiPolygon as GeoJSONMultiPolygon,
Polygon as GeoJSONPolygon
} from 'geojson';
import isNil from 'lodash/isNil';
import OlFeature from 'ol/Feature';
import OlFormatGeoJSON from 'ol/format/GeoJSON';
import OlGeometry from 'ol/geom/Geometry';
import OlGeomLineString from 'ol/geom/LineString';
import OlGeomMultiLineString from 'ol/geom/MultiLineString';
import OlGeomMultiPoint from 'ol/geom/MultiPoint';
import OlGeomMultiPolygon from 'ol/geom/MultiPolygon';
import OlGeomPoint from 'ol/geom/Point';
import OlGeomPolygon from 'ol/geom/Polygon';
import { ProjectionLike } from 'ol/proj';
import polygonSplitter from 'polygon-splitter';
/**
* @template {OlGeomGeometry} T
* @param {OlFeature<T>|T} featureOrGeom
*/
function toGeom<Geom extends OlGeometry>(featureOrGeom: OlFeature<Geom> | Geom) {
if (featureOrGeom instanceof OlFeature) {
const geom = featureOrGeom.getGeometry();
if (geom === undefined) {
throw new Error('Feature has no geometry.');
}
return geom;
} else {
return featureOrGeom;
}
}
/**
* Helper class for the geospatial analysis. Makes use of
* [Turf.js](http://turfjs.org/).
*
* @class GeometryUtil
*/
class GeometryUtil {
/**
* The prefix used to detect multi geometries.
* @ignore
*/
static MULTI_GEOM_PREFIX = 'Multi';
/**
* Splits an OlFeature with/or ol.geom.Polygon by an OlFeature with/or ol.geom.LineString
* into an array of instances of OlFeature with/or ol.geom.Polygon.
* If the target polygon (first param) is of type ol.Feature it will return an
* array with ol.Feature. If the target polygon (first param) is of type
* ol.geom.Geometry it will return an array with ol.geom.Geometry.
*
* @param {OlFeature<OlGeomPolygon> | OlGeomPolygon} polygon The polygon geometry to split.
* @param {OlFeature<OlGeomLineString> | OlGeomLineString} line The line geometry to split the polygon
* geometry with.
* @param {import("ol/proj").ProjectionLike} projection The EPSG code of the input features.
* Default is to EPSG:3857.
* @returns {OlFeature[] | OlGeomPolygon[]} An array of instances of OlFeature
* with/or ol.geom.Polygon
*/
static splitByLine(
polygon: OlFeature<OlGeomPolygon> | OlGeomPolygon,
line: OlFeature<OlGeomLineString>,
projection: ProjectionLike = 'EPSG:3857'
): OlGeomPolygon[] | OlFeature<OlGeomPolygon>[] {
const returnFeature = polygon instanceof OlFeature;
const geometries = GeometryUtil.splitGeometryByLine(toGeom(polygon), toGeom(line), projection);
if (returnFeature) {
return geometries.map(geom => new OlFeature(geom));
} else {
return geometries;
}
}
/**
* Splits an ol.geom.Polygon by an ol.geom.LineString
* into an array of instances of ol.geom.Polygon.
*
* @param {OlGeomPolygon} polygon The polygon geometry to split.
* @param {OlGeomLineString} line The line geometry to split the polygon
* geometry with.
* @param {ProjectionLike} projection The EPSG code of the input features.
* Default is to EPSG:3857.
* @returns {OlGeomPolygon[]} An array of instances of ol.geom.Polygon
*/
static splitGeometryByLine(
polygon: OlGeomPolygon,
line: OlGeomLineString,
projection: ProjectionLike = 'EPSG:3857'
): OlGeomPolygon[] {
const geoJsonFormat = new OlFormatGeoJSON({
dataProjection: 'EPSG:4326',
featureProjection: projection
});
const polyJson = geoJsonFormat.writeGeometryObject(polygon);
const lineJson = geoJsonFormat.writeGeometryObject(line);
const result = polygonSplitter(polyJson, lineJson);
const flattened = flatten(result);
return flattened.features.map((geojsonFeature: any) => {
return geoJsonFormat.readGeometry(geojsonFeature.geometry) as OlGeomPolygon;
});
}
/**
* Adds a buffer to a given geometry.
*
* If the target is of type ol.Feature it will return an ol.Feature.
* If the target is of type ol.geom.Geometry it will return ol.geom.Geometry.
*
* @param {OlGeometry | OlFeature} geometryOrFeature The geometry.
* @param {number} radius The buffer to add in meters.
* @param {string} projection The projection of the input geometry as EPSG code.
* Default is to EPSG:3857.
*
* @returns {OlGeometry | OlFeature} The geometry or feature with the added buffer.
*/
static addBuffer(
geometryOrFeature: OlFeature<OlGeometry> | OlGeometry,
radius: number = 0,
projection: ProjectionLike = 'EPSG:3857'
) {
if (geometryOrFeature instanceof OlFeature) {
return new OlFeature(GeometryUtil.addGeometryBuffer(toGeom(geometryOrFeature), radius, projection));
} else {
return GeometryUtil.addGeometryBuffer(geometryOrFeature, radius, projection);
}
}
/**
* Adds a buffer to a given geometry.
*
* @param {OlGeometry} geometry The geometry.
* @param {number} radius The buffer to add in meters.
* @param {string} projection The projection of the input geometry as EPSG code.
* Default is to EPSG:3857.
*
* @returns {OlGeometry} The geometry with the added buffer.
*/
static addGeometryBuffer(geometry: OlGeometry, radius: number = 0, projection: ProjectionLike = 'EPSG:3857') {
if (radius === 0) {
return geometry;
}
const geoJsonFormat = new OlFormatGeoJSON({
dataProjection: 'EPSG:4326',
featureProjection: projection
});
const geoJson = geoJsonFormat.writeGeometryObject(geometry);
if (geoJson.type === 'GeometryCollection') {
return;
}
const buffered = buffer(geoJson, radius, {
units: 'meters'
});
return geoJsonFormat.readGeometry(buffered?.geometry);
}
/**
* Merges multiple geometries into one MultiGeometry.
*
* @param {(OlGeomMultiPoint|OlGeomPoint)[]|(OlGeomMultiPolygon|OlGeomPolygon)[]|
* (OlGeomMultiLineString|OlGeomLineString)[]} geometries An array of ol.geom.geometries;
* @returns {OlGeomMultiPoint|OlGeomMultiPolygon|OlGeomMultiLineString} A Multigeometry.
*/
static mergeGeometries<Geom extends OlGeometry>(geometries: Geom[]) {
// split all multi-geometries to simple ones if passed geometries are
// multi-geometries
const separateGeometries = GeometryUtil.separateGeometries(geometries);
if (separateGeometries[0] instanceof OlGeomPolygon) {
const multiGeom = new OlGeomMultiPolygon([]);
for (const geom of separateGeometries) {
multiGeom.appendPolygon(geom as OlGeomPolygon);
}
return multiGeom;
} else if (separateGeometries[0] instanceof OlGeomLineString) {
const multiGeom = new OlGeomMultiLineString([]);
for (const geom of separateGeometries) {
multiGeom.appendLineString(geom as OlGeomLineString);
}
return multiGeom;
} else {
const multiGeom = new OlGeomMultiPoint([]);
for (const geom of separateGeometries) {
multiGeom.appendPoint(geom as OlGeomPoint);
}
return multiGeom;
}
}
/**
* Splits an array of geometries (and multi geometries) or a single MultiGeom
* into an array of single geometries.
*
* @param {} geometry An (array of) ol.geom.geometries;
* @returns {(OlGeomPoint|OlGeomLineString|OlGeomPolygon)[]} An array of geometries.
*/
static separateGeometries(geometry: OlGeometry | OlGeometry[]): OlGeometry[] {
if (Array.isArray(geometry)) {
return geometry.flatMap(geom => GeometryUtil.separateGeometries(geom));
}
if (geometry instanceof OlGeomMultiPolygon) {
return geometry.getPolygons();
}
if (geometry instanceof OlGeomMultiLineString) {
return geometry.getLineStrings();
}
if (geometry instanceof OlGeomMultiPoint) {
return geometry.getPoints();
}
return [geometry]; // Return simple geometry as array
}
/**
* Takes two or more polygons and returns a combined (Multi-)polygon.
*
* @param {OlFeature<OlGeomPolygon>[] | OlFeature<OlGeomPolygon | OlGeomMultiPolygon>>[]} inputPolygonalObjects An
* array of ol.Feature or ol.geom.Geometry instances of type (Multi)-Polygon.
* @param {ProjectionLike} projection The projection of the input polygons as EPSG code.
* Default is to EPSG:3857.
* @returns {OlGeomMultiPolygon|OlGeomPolygon|OlFeature<OlGeomMultiPolygon|OlGeomPolygon>} A Feature or Geometry with
* the combined area of the (Multi-)polygons.
*/
static union(
inputPolygonalObjects: OlGeomPolygon[] | OlFeature<OlGeomPolygon | OlGeomMultiPolygon>[],
projection: ProjectionLike = 'EPSG:3857'
): OlGeomMultiPolygon | OlGeomPolygon | OlFeature<OlGeomMultiPolygon|OlGeomPolygon> {
const geometries = inputPolygonalObjects.map(toGeom) as OlGeomPolygon[] | OlGeomMultiPolygon[];
const unionGeometry = GeometryUtil.unionGeometries(geometries, projection);
if (inputPolygonalObjects[0] instanceof OlFeature) {
return new OlFeature(unionGeometry);
} else {
return unionGeometry;
}
}
/**
* Takes two or more polygons and returns a combined (Multi-)polygon.
*
* @param {OlGeomPolygon[]} polygons An array of ol.geom.Geometry instances of type (Multi-)polygon.
* @param {string} projection The projection of the input polygons as EPSG code.
* Default is to EPSG:3857.
* @returns {OlGeomMultiPolygon|OlGeomPolygon} A FGeometry with the combined area of the (Multi-)polygons.
*/
static unionGeometries(polygons: OlGeomPolygon[] | OlGeomMultiPolygon[], projection: ProjectionLike = 'EPSG:3857'):
OlGeomMultiPolygon | OlGeomPolygon
{
const geoJsonFormat = new OlFormatGeoJSON({
dataProjection: 'EPSG:4326',
featureProjection: projection
});
const pp = polygons
.map((p: OlGeomPolygon | OlGeomMultiPolygon) => {
if (p instanceof OlGeomMultiPolygon) {
return geoJsonFormat.writeFeatureObject(new OlFeature(p)) as GeoJSONFeature<GeoJSONMultiPolygon>;
} else {
return geoJsonFormat.writeFeatureObject(new OlFeature(p)) as GeoJSONFeature<GeoJSONPolygon>;
}
});
const unionGeometry = union(featureCollection<GeoJSONMultiPolygon | GeoJSONPolygon>(pp));
return (geoJsonFormat.readFeature(unionGeometry) as OlFeature).getGeometry() as OlGeomMultiPolygon | OlGeomPolygon;
}
/**
* Finds the difference between two polygons by clipping the second polygon from the first.
*
* If both polygons are of type ol.Feature it will return an ol.Feature.
* Else it will return an ol.geom.Geometry.
*
* @param {OlGeomPolygon|OlGeomMultiPolygon|OlFeature<OlGeomPolygon|OlGeomMultiPolygon>} polygon1
* @param {OlGeomPolygon|OlGeomMultiPolygon|OlFeature<OlGeomPolygon|OlGeomMultiPolygon>} polygon2
* @param {string} projection The projection of the input polygons as EPSG code.
* Default is to EPSG:3857.
*
* @returns {OlGeomPolygon|OlGeomMultiPolygon|OlFeature<OlGeomPolygon|OlGeomMultiPolygon>} A Feature or geometry
* with the area of polygon1 excluding the area of polygon2.
*/
static difference(
polygon1: OlFeature<OlGeomPolygon> | OlGeomPolygon,
polygon2: OlFeature<OlGeomPolygon> | OlGeomPolygon,
projection: ProjectionLike = 'EPSG:3857'
): OlGeomMultiPolygon | OlGeomPolygon | OlFeature<OlGeomMultiPolygon|OlGeomPolygon> {
const differenceGeometry = GeometryUtil.geometryDifference(toGeom(polygon1), toGeom(polygon2), projection);
if (polygon1 instanceof OlFeature && polygon2 instanceof OlFeature) {
return new OlFeature(differenceGeometry);
} else {
return differenceGeometry;
}
}
/**
* Finds the difference between two polygons by clipping the second polygon from the first.
*
* @param {OlGeomPolygon|OlGeomMultiPolygon} polygon1 An ol.geom.Geometry
* @param {OlGeomPolygon|OlGeomMultiPolygon} polygon2 An ol.geom.Geometry
* @param {string} projection The projection of the input polygons as EPSG code.
* Default is to EPSG:3857.
*
* @returns {OlGeomPolygon|OlGeomMultiPolygon} A with the area
* of polygon1 excluding the area of polygon2.
*/
static geometryDifference(
polygon1: OlGeomPolygon,
polygon2: OlGeomPolygon,
projection: ProjectionLike = 'EPSG:3857'
): OlGeomMultiPolygon | OlGeomPolygon {
const geoJsonFormat = new OlFormatGeoJSON<OlFeature<OlGeomPolygon>>({
dataProjection: 'EPSG:4326',
featureProjection: projection
});
const geojson1 = geoJsonFormat.writeFeatureObject(new OlFeature(polygon1)) as GeoJSONFeature<GeoJSONPolygon>;
const geojson2 = geoJsonFormat.writeFeatureObject(new OlFeature(polygon2)) as GeoJSONFeature<GeoJSONPolygon>;
const coll = featureCollection([geojson1, geojson2]);
const intersection = difference(coll);
const feature = geoJsonFormat.readFeature(intersection);
return (feature as OlFeature).getGeometry() as OlGeomMultiPolygon | OlGeomPolygon;
}
/**
* Takes two polygons and finds their intersection.
*
* If both polygons are of type ol.Feature it will return an ol.Feature.
* Else it will return an ol.geom.Geometry.
*
* @param {OlGeomPolygon|OlGeomMultiPolygon|OlFeature<OlGeomPolygon|OlGeomMultiPolygon>} polygon1
* @param {OlGeomPolygon|OlGeomMultiPolygon|OlFeature<OlGeomPolygon|OlGeomMultiPolygon>} polygon2
* @param {string} projection The projection of the input polygons as EPSG code.
* Default is to EPSG:3857.
*
* @returns {OlGeomPolygon|OlGeomMultiPolygon|OlFeature<OlGeomPolygon|OlGeomMultiPolygon>|null} A Feature or Geometry
* with the shared area of the two polygons or null if the polygons don't intersect.
*/
static intersection(
polygon1: OlFeature<OlGeomPolygon | OlGeomMultiPolygon> | OlGeomPolygon,
polygon2: OlFeature<OlGeomPolygon | OlGeomMultiPolygon> | OlGeomPolygon,
projection: ProjectionLike = 'EPSG:3857'
): OlGeomMultiPolygon | OlGeomPolygon | OlFeature<OlGeomPolygon|OlGeomMultiPolygon> | undefined {
const intersectionGeometry = GeometryUtil.geometryIntersection(toGeom(polygon1), toGeom(polygon2), projection);
if (isNil(intersectionGeometry)) {
return undefined;
}
if (polygon1 instanceof OlFeature && polygon2 instanceof OlFeature) {
return new OlFeature(intersectionGeometry);
} else {
return intersectionGeometry;
}
}
/**
* Takes two polygons and finds their intersection.
*
* @param {OlGeomPolygon|OlGeomMultiPolygon} polygon1 An ol.geom.Geometry
* @param {OlGeomPolygon|OlGeomMultiPolygon} polygon2 An ol.geom.Geometry
* @param {string} projection The projection of the input polygons as EPSG code.
* Default is to EPSG:3857.
*
* @returns {OlGeomPolygon|OlGeomMultiPolygon|null} A Geometry with the
* shared area of the two polygons or null if the polygons don't intersect.
*/
static geometryIntersection(
polygon1: OlGeomPolygon | OlGeomMultiPolygon,
polygon2: OlGeomPolygon | OlGeomMultiPolygon,
projection: ProjectionLike = 'EPSG:3857'
): OlGeomMultiPolygon | OlGeomPolygon | undefined {
const geoJsonFormat = new OlFormatGeoJSON({
dataProjection: 'EPSG:4326',
featureProjection: projection
});
const geojson1 = polygon1 instanceof OlGeomMultiPolygon ?
geoJsonFormat.writeFeatureObject(new OlFeature(polygon1)) as GeoJSONFeature<GeoJSONMultiPolygon> :
geoJsonFormat.writeFeatureObject(new OlFeature(polygon1)) as GeoJSONFeature<GeoJSONPolygon>;
const geojson2 = polygon2 instanceof OlGeomMultiPolygon ?
geoJsonFormat.writeFeatureObject(new OlFeature(polygon2)) as GeoJSONFeature<GeoJSONMultiPolygon> :
geoJsonFormat.writeFeatureObject(new OlFeature(polygon2)) as GeoJSONFeature<GeoJSONPolygon>;
const intersection = intersect(featureCollection<GeoJSONMultiPolygon | GeoJSONPolygon>([geojson1, geojson2]));
if (!intersection) {
return undefined;
}
const feature = geoJsonFormat.readFeature(intersection);
return (feature as OlFeature).getGeometry() as OlGeomMultiPolygon | OlGeomPolygon;
}
}
export default GeometryUtil;