1 | import * as uriproj from 'uriproj'
|
2 | import { COVJSON_DATATYPE_TUPLE, COVJSON_DATATYPE_POLYGON } from '../constants.js'
|
3 |
|
4 | const OPENGIS_CRS_PREFIX = 'http://www.opengis.net/def/crs/'
|
5 |
|
6 |
|
7 | const EPSG4979 = OPENGIS_CRS_PREFIX + 'EPSG/0/4979'
|
8 |
|
9 |
|
10 | const EPSG4326 = OPENGIS_CRS_PREFIX + 'EPSG/0/4326'
|
11 |
|
12 |
|
13 | const CRS84 = OPENGIS_CRS_PREFIX + 'OGC/1.3/CRS84'
|
14 |
|
15 |
|
16 | const GeographicCRSs = [EPSG4979, EPSG4326, CRS84]
|
17 |
|
18 |
|
19 | const LongitudeAxisIndex = {
|
20 | [EPSG4979]: 1,
|
21 | [EPSG4326]: 1,
|
22 | [CRS84]: 0
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export function getReferenceObject (domain, coordinateId) {
|
30 | let ref = domain.referencing.find(ref => ref.coordinates.indexOf(coordinateId) !== -1)
|
31 | return ref
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | export function getHorizontalCRSReferenceObject (domain) {
|
41 | let isHorizontal = ref =>
|
42 | ['GeodeticCRS', 'GeographicCRS', 'GeocentricCRS', 'ProjectedCRS'].indexOf(ref.system.type) !== -1
|
43 | let ref = domain.referencing.find(isHorizontal)
|
44 | return ref
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | export function isEllipsoidalCRS (rs) {
|
52 | return rs.type === 'GeographicCRS' || GeographicCRSs.indexOf(rs.id) !== -1
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | export function getProjection (domain) {
|
74 | let isEllipsoidal = domain.referencing.some(ref => isEllipsoidalCRS(ref.system))
|
75 | if (isEllipsoidal) {
|
76 | return getLonLatProjection(domain)
|
77 | }
|
78 |
|
79 |
|
80 | let ref = getHorizontalCRSReferenceObject(domain)
|
81 | if (!ref) {
|
82 | throw new Error('No horizontal CRS found in coverage domain')
|
83 | }
|
84 |
|
85 | let uri = ref.system.id
|
86 | let proj = uriproj.get(uri)
|
87 | if (!proj) {
|
88 | throw new Error('Projection ' + uri + ' not cached in uriproj, use loadProjection() instead')
|
89 | }
|
90 | return wrapProj4(proj)
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | export function loadProjection (domain) {
|
102 | try {
|
103 |
|
104 | return getProjection(domain)
|
105 | } catch (e) {}
|
106 |
|
107 |
|
108 | let ref = getHorizontalCRSReferenceObject(domain)
|
109 | if (!ref) {
|
110 | throw new Error('No horizontal CRS found in coverage domain')
|
111 | }
|
112 |
|
113 | let uri = ref.system.id
|
114 | return uriproj.load(uri).then(proj => wrapProj4(proj))
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | export function getHorizontalCRSComponents (domain) {
|
125 | return getHorizontalCRSCoordinateIDs(domain)
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | export function getHorizontalCRSCoordinateIDs (domain) {
|
135 | let ref = getHorizontalCRSReferenceObject(domain)
|
136 | return ref.coordinates
|
137 | }
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | function wrapProj4 (proj) {
|
143 | return {
|
144 | project: ({lon, lat}) => {
|
145 | let [x, y] = proj.forward([lon, lat])
|
146 | return {x, y}
|
147 | },
|
148 | unproject: ({x, y}) => {
|
149 | let [lon, lat] = proj.inverse([x, y])
|
150 | return {lon, lat}
|
151 | }
|
152 | }
|
153 | }
|
154 |
|
155 | function getLonLatProjection (domain) {
|
156 | let ref = domain.referencing.find(ref => isEllipsoidalCRS(ref.system))
|
157 | let lonIdx = LongitudeAxisIndex[ref.system.id]
|
158 | if (lonIdx > 1) {
|
159 |
|
160 | throw new Error()
|
161 | }
|
162 |
|
163 | let lonComponent = ref.coordinates[lonIdx]
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | let lonMin, lonMax
|
170 | if (domain.axes.has(lonComponent)) {
|
171 |
|
172 | let lonAxisName = lonComponent
|
173 | let lonAxisVals = domain.axes.get(lonAxisName).values
|
174 | lonMin = lonAxisVals[0]
|
175 | lonMax = lonAxisVals[lonAxisVals.length - 1]
|
176 | if (lonMin > lonMax) {
|
177 | [lonMin, lonMax] = [lonMax, lonMin]
|
178 | }
|
179 | } else {
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | let axes = [...domain.axes.values()]
|
186 | let axis = axes.find(axis => axis.coordinates.indexOf(lonComponent) !== -1)
|
187 | let lonCompIdx = axis.coordinates.indexOf(lonComponent)
|
188 |
|
189 |
|
190 | lonMin = Infinity
|
191 | lonMax = -Infinity
|
192 | if (axis.dataType === COVJSON_DATATYPE_TUPLE) {
|
193 | for (let tuple of axis.values) {
|
194 | let lon = tuple[lonCompIdx]
|
195 | lonMin = Math.min(lon, lonMin)
|
196 | lonMax = Math.max(lon, lonMax)
|
197 | }
|
198 | } else if (axis.dataType === COVJSON_DATATYPE_POLYGON) {
|
199 | for (let poly of axis.values) {
|
200 | for (let ring of poly) {
|
201 | for (let point of ring) {
|
202 | let lon = point[lonCompIdx]
|
203 | lonMin = Math.min(lon, lonMin)
|
204 | lonMax = Math.max(lon, lonMax)
|
205 | }
|
206 | }
|
207 | }
|
208 | } else {
|
209 | throw new Error('Unsupported data type: ' + axis.dataType)
|
210 | }
|
211 | }
|
212 |
|
213 | let lonMid = (lonMax + lonMin) / 2
|
214 | let lonMinExtended = lonMid - 180
|
215 | let lonMaxExtended = lonMid + 180
|
216 |
|
217 | return {
|
218 | project: ({lon, lat}) => {
|
219 | let lonProjected
|
220 | if (lonMinExtended <= lon && lon <= lonMaxExtended) {
|
221 |
|
222 | lonProjected = lon
|
223 | } else {
|
224 | lonProjected = ((lon - lonMinExtended) % 360 + 360) % 360 + lonMinExtended
|
225 | }
|
226 |
|
227 | let [x, y] = lonIdx === 0 ? [lonProjected, lat] : [lat, lonProjected]
|
228 | return {x, y}
|
229 | },
|
230 | unproject: ({x, y}) => {
|
231 | let [lon, lat] = lonIdx === 0 ? [x, y] : [y, x]
|
232 | return {lon, lat}
|
233 | }
|
234 | }
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | export function reprojectCoords (pos, fromProjection, toProjection) {
|
241 | return toProjection.project(fromProjection.unproject(pos))
|
242 | }
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | export function getLongitudeWrapper (domain, axisName) {
|
263 |
|
264 |
|
265 |
|
266 | if (!isLongitudeAxis(domain, axisName)) {
|
267 | throw new Error(`'${axisName}' is not a longitude axis`)
|
268 | }
|
269 |
|
270 | let vals = domain.axes.get(axisName).values
|
271 | let lon_min = vals[0]
|
272 | let lon_max = vals[vals.length - 1]
|
273 | if (lon_min > lon_max) {
|
274 | [lon_min, lon_max] = [lon_max, lon_min]
|
275 | }
|
276 |
|
277 | let x_mid = (lon_max + lon_min) / 2
|
278 | let x_min = x_mid - 180
|
279 | let x_max = x_mid + 180
|
280 |
|
281 | return lon => {
|
282 | if (x_min <= lon && lon <= x_max) {
|
283 |
|
284 | return lon
|
285 | } else {
|
286 | return ((lon - x_min) % 360 + 360) % 360 + x_min
|
287 | }
|
288 | }
|
289 | }
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | export function isLongitudeAxis (domain, axisName) {
|
297 | let ref = getReferenceObject(domain, axisName)
|
298 | if (!ref) {
|
299 | return false
|
300 | }
|
301 |
|
302 | let crsId = ref.system.id
|
303 |
|
304 | if (GeographicCRSs.indexOf(crsId) === -1) {
|
305 |
|
306 | return false
|
307 | }
|
308 |
|
309 | let compIdx = ref.coordinates.indexOf(axisName)
|
310 | let isLongitude = LongitudeAxisIndex[crsId] === compIdx
|
311 | return isLongitude
|
312 | }
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | export function isISODateAxis (domain, axisName) {
|
319 | let val = domain.axes.get(axisName).values[0]
|
320 | if (typeof val !== 'string') {
|
321 | return false
|
322 | }
|
323 | return !isNaN(new Date(val).getTime())
|
324 | }
|
325 |
|
326 | export function asTime (inp) {
|
327 | let res
|
328 | let err = false
|
329 | if (typeof inp === 'string') {
|
330 | res = new Date(inp).getTime()
|
331 | } else if (inp instanceof Date) {
|
332 | res = inp.getTime()
|
333 | } else {
|
334 | err = true
|
335 | }
|
336 | if (isNaN(res)) {
|
337 | err = true
|
338 | }
|
339 | if (err) {
|
340 | throw new Error('Invalid date: ' + inp)
|
341 | }
|
342 | return res
|
343 | }
|