-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.ts
153 lines (129 loc) · 5.84 KB
/
task.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
import { Type, TSchema, Static } from '@sinclair/typebox';
import { Feature } from '@tak-ps/node-cot';
import type { Event } from '@tak-ps/etl';
import ETL, { SchemaType, handler as internal, local, env } from '@tak-ps/etl';
import { fetch } from '@tak-ps/etl';
import { coordEach } from '@turf/meta';
const Env = Type.Object({
ShareId: Type.String({
description: 'CalTopo Share ID',
}),
'DEBUG': Type.Boolean({
default: false,
description: 'Print results in logs'
})
});
const Output = Type.Object({
title: Type.String(),
description: Type.Optional(Type.String()),
class: Type.String(),
creator: Type.String(),
updated: Type.Number(),
'marker-symbol': Type.Optional(Type.String()),
'marker-rotation': Type.Optional(Type.Union([Type.String(), Type.Null()])),
'marker-color': Type.Optional(Type.String()),
'marker-size': Type.Optional(Type.String()),
stroke: Type.Optional(Type.String()),
'stroke-opacity': Type.Optional(Type.Number()),
'stroke-width': Type.Optional(Type.Number()),
pattern: Type.Optional(Type.String()),
fill: Type.Optional(Type.String()),
'fill-opacity': Type.Optional(Type.Number()),
folderId: Type.Optional(Type.String()),
visible: Type.Optional(Type.Boolean()),
labelVisible: Type.Optional(Type.Boolean()),
});
export default class Task extends ETL {
async schema(type: SchemaType = SchemaType.Input): Promise<TSchema> {
if (type === SchemaType.Input) {
return Env;
} else {
return Output;
}
}
async control(): Promise<void> {
const env = await this.env(Env);
let features: Static<typeof Feature.InputFeature>[] = [];
console.log(`ok - requesting ${env.ShareId}`);
const url = new URL(`/api/v1/map/${env.ShareId}/since/-500`, 'https://caltopo.com/')
const res = await fetch(url);
const body = await res.typed(Type.Object({
status: Type.String(),
timestamp: Type.Integer(),
result: Type.Object({
state: Type.Object({
type: Type.String({ const: 'FeatureCollection' }),
features: Type.Array(Type.Object({
id: Type.String(),
type: Type.Literal('Feature'),
properties: Output,
geometry: Type.Optional(Type.Any())
}))
}),
timestamp: Type.Integer(),
}),
}));
const folders: Map<string, Static<typeof Output>> = new Map();
features = body.result.state.features
.filter((feat) => {
if (feat.properties.class === 'Folder') {
folders.set(feat.id, feat.properties);
return false;
} else {
// SARTopo will send "features" like "Operational Periods" which do not have geometry
return !!feat.geometry;
}
})
.map((calFeat) => {
const feat: Static<typeof Feature.InputFeature> = {
id: calFeat.id,
type: 'Feature',
properties: {
metadata: calFeat.properties
},
geometry: calFeat.geometry
};
feat.properties.callsign = String(calFeat.properties.title);
feat.properties.remarks = calFeat.properties.description ? String(calFeat.properties.description) : '';
if (feat.properties.metadata.fill !== undefined) feat.properties.fill = String(feat.properties.metadata.fill);
if (feat.properties.metadata['fill-opacity'] !== undefined) feat.properties['fill-opacity'] = Number(feat.properties.metadata['fill-opacity']);
if (feat.properties.metadata.stroke !== undefined) feat.properties.stroke = String(feat.properties.metadata.stroke);
if (feat.properties.metadata['stroke-opacity'] !== undefined) feat.properties['stroke-opacity'] = Number(feat.properties.metadata['stroke-opacity']);
if (feat.properties.metadata['stroke-width'] !== undefined) feat.properties['stroke-width'] = Number(feat.properties.metadata['stroke-width']);
if (feat.properties.metadata.ico !== undefined) feat.properties.icon = String(feat.properties.metadata.icon);
// CalTopo returns points with 4+ coords
coordEach(feat.geometry, (coord) => {
return coord.splice(3)
});
feat.properties.archived = true;
if (feat.geometry.type === 'Point') {
feat.properties.type = 'u-d-p';
if (feat.properties.metadata['marker-color']) {
feat.properties['marker-color'] = `#${feat.properties.metadata['marker-color']}`;
delete feat.properties.metadata['marker-color'];
feat.properties['marker-opacity'] = 1;
}
}
return feat;
});
// After all Features/Folders have been seperated, apply folder => path transform
features = features.map((feat) => {
if (feat.properties.metadata.folderId && typeof feat.properties.metadata.folderId === 'string') {
const folder = folders.get(feat.properties.metadata.folderId);
if (folder) {
feat.path = `/${folder.title}`;
}
}
return feat;
});
await this.submit({
type: 'FeatureCollection',
features: features
});
}
}
env(import.meta.url)
await local(new Task(), import.meta.url);
export async function handler(event: Event = {}) {
return await internal(new Task(), event);
}