forked from marcelreppi/shape2geohash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
108 lines (95 loc) · 2.76 KB
/
helpers.js
File metadata and controls
108 lines (95 loc) · 2.76 KB
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
const { default: turfBbox } = require("@turf/bbox")
const { lineString: turfLine } = require("@turf/helpers")
const { default: turfLineSplit } = require("@turf/line-split")
function switchBbox(bbox) {
const [y1, x1, y2, x2] = bbox
return [x1, y1, x2, y2]
}
function isMulti(coordinates) {
return Array.isArray(coordinates[0][0][0])
}
function isLine(coordinates) {
return !Array.isArray(coordinates[0][0])
}
function isPoint(coordinates) {
return !Array.isArray(coordinates[0])
}
function allRectangleEdgesWithin(polygon1, polygon2) {
const bbox = turfBbox(polygon1)
const edge = turfLine([
[bbox[0], bbox[3]], // Top edge
[bbox[2], bbox[3]], // Top edge
[bbox[2], bbox[3]], // Right edge
[bbox[2], bbox[1]], // Right edge
[bbox[2], bbox[1]], // Bottom edge
[bbox[0], bbox[1]], // Bottom edge
[bbox[0], bbox[1]], // Left edge
[bbox[0], bbox[3]], // Left edge
])
// Make sure the polygon does not split the line into separate segments
return turfLineSplit(edge, polygon2).features.length === 0
}
function extractCoordinatesFromGeoJSON(geoJSON) {
let result = []
if (!geoJSON.hasOwnProperty("type")) {
throw new Error("GeoJSON Error: GeoJSON object is missing type property")
}
const checkForCoordinates = [
"Polygon",
"MultiPolygon",
"LineString",
"MultiLineString",
"Point",
"MultiPoint",
]
if (checkForCoordinates.includes(geoJSON.type)) {
if (!geoJSON.hasOwnProperty("coordinates")) {
throw new Error(`GeoJSON Error: ${geoJSON.type} is missing "coordinates" property`)
}
}
switch (geoJSON.type) {
case "FeatureCollection":
geoJSON.features.forEach(f => {
const coordinates = extractCoordinatesFromGeoJSON(f)
result.push(...coordinates)
})
break
case "Feature":
if (!geoJSON.hasOwnProperty("geometry")) {
throw new Error("GeoJSON Error: Feature is missing geometry property")
}
const coordinates = extractCoordinatesFromGeoJSON(geoJSON.geometry)
result.push(...coordinates)
break
case "Polygon":
result.push(geoJSON.coordinates)
break
case "MultiPolygon":
if (!isMulti(geoJSON.coordinates)) {
throw new Error("GeoJSON Error: MultiPolygon is actually not a MultiPolygon")
}
result.push(...geoJSON.coordinates)
break
case "LineString":
result.push(geoJSON.coordinates)
break
case "MultiLineString":
result.push(...geoJSON.coordinates)
break
case "Point":
result.push(geoJSON.coordinates)
break
case "MultiPoint":
result.push(...geoJSON.coordinates)
break
}
return result
}
module.exports = {
switchBbox,
isPoint,
isLine,
isMulti,
allRectangleEdgesWithin,
extractCoordinatesFromGeoJSON,
}