-
Notifications
You must be signed in to change notification settings - Fork 215
/
layers.js
150 lines (134 loc) · 4.04 KB
/
layers.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
import L from 'leaflet'
import { castArray } from 'lodash'
// Is the granule coordinate system cartesian?
export function isCartesian(metadata = {}) {
return metadata.coordinate_system === 'CARTESIAN'
}
// Parse a spatial string into an array of LatLng points
export function parseSpatial(str) {
if (!str) return null
let newStr = str
if (newStr instanceof Array) {
[newStr] = str
}
const coords = newStr.split(' ')
const len = coords.length - 1
return (() => {
const result = []
for (let i = 0, end = len; i < end; i += 2) {
result.push(new L.LatLng(coords[i], coords[i + 1]))
}
return result
})()
}
// Pull points out of metadata
export function getPoints(metadata = {}) {
let points = []
// eslint-disable-next-line no-underscore-dangle
if ((metadata._points == null) && (metadata.points != null)) {
const merged = []
points = merged.concat(...metadata.points.map(parseSpatial))
}
return points
}
// Pull polygons out of metadata
export function getPolygons(metadata = {}) {
const { polygons: metadataPolygons } = metadata
let polygons = []
if (metadataPolygons && metadataPolygons !== null) {
polygons = metadataPolygons.map(p => p.map(s => parseSpatial(s)))
return polygons
}
return polygons
}
// Pull lines out of metadata
export function getLines(metadata = {}) {
let lines = []
// eslint-disable-next-line no-underscore-dangle
if ((metadata._lines == null) && (metadata.lines != null)) {
lines = metadata.lines.map(parseSpatial)
}
return lines
}
// Pull rectangles out of metadata
export function getRectangles(metadata = {}) {
const rects = []
// eslint-disable-next-line no-underscore-dangle
if ((metadata._rects == null) && (metadata.boxes != null)) {
metadata.boxes.map(parseSpatial).forEach((rect) => {
let divided
if (rect[0].lng > rect[1].lng) {
divided = [
[rect[0], L.latLng(rect[1].lat, 180)],
[L.latLng(rect[0].lat, -180), rect[1]]
]
} else {
divided = [rect]
}
divided.forEach((box) => {
rects.push([L.latLng(box[0].lat, box[0].lng), L.latLng(box[0].lat, box[1].lng),
L.latLng(box[1].lat, box[1].lng), L.latLng(box[1].lat, box[0].lng),
L.latLng(box[0].lat, box[0].lng)
])
})
})
}
return rects
}
export const buildLayer = (options, metadata) => {
const layer = new L.FeatureGroup()
const points = getPoints(metadata)
const polygons = getPolygons(metadata)
const lines = getLines(metadata)
const rectangles = getRectangles(metadata)
const cartesian = isCartesian(metadata)
if (points) {
castArray(points).forEach((point) => {
layer.addLayer(L.circleMarker(point, options))
})
}
if (polygons) {
castArray(polygons).forEach((polygon) => {
let polyLayer
if (cartesian) {
polyLayer = L.polygon(polygon)
// eslint-disable-next-line no-underscore-dangle
polyLayer._interpolationFn = 'cartesian'
} else {
polyLayer = L.sphericalPolygon(polygon, options)
}
layer.addLayer(polyLayer)
const bounds = L.latLngBounds(polygon)
if (
bounds.getNorth() - bounds.getSouth() < 0.5
&& bounds.getWest() - bounds.getEast() < 0.5
) {
layer.addLayer(L.marker(bounds.getCenter()))
}
})
}
if (lines) {
castArray(lines).forEach((line) => {
const lineLayer = L.polyline(line, options)
// eslint-disable-next-line no-underscore-dangle
if (cartesian) lineLayer._interpolationFn = 'cartesian'
})
}
if (rectangles) {
castArray(rectangles).forEach((rect) => {
const shape = L.polygon(rect, options)
// eslint-disable-next-line no-underscore-dangle
shape._interpolationFn = 'cartesian'
layer.addLayer(shape)
const bounds = L.latLngBounds(rect)
if (
bounds.getNorth() - bounds.getSouth() < 0.5
&& bounds.getWest() - bounds.getEast() < 0.5
) {
layer.addLayer(L.marker(bounds.getCenter()))
}
})
}
return layer
}
export default buildLayer