namazu-ts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/.prettierignore +4 -0
  2. package/.prettierrc +6 -0
  3. package/README.md +13 -0
  4. package/dist/browser/index.css +793 -0
  5. package/dist/browser/index.js +29829 -0
  6. package/dist/client.d.ts +41 -0
  7. package/dist/index.d.ts +5 -0
  8. package/dist/maputils.d.ts +13 -0
  9. package/dist/node/index.css +788 -0
  10. package/dist/node/index.js +42188 -0
  11. package/dist/sismomap.d.ts +21 -0
  12. package/dist/types.d.ts +197 -0
  13. package/dist/utils.d.ts +4 -0
  14. package/docs/.nojekyll +1 -0
  15. package/docs/assets/hierarchy.js +1 -0
  16. package/docs/assets/highlight.css +71 -0
  17. package/docs/assets/icons.js +18 -0
  18. package/docs/assets/icons.svg +1 -0
  19. package/docs/assets/main.js +60 -0
  20. package/docs/assets/navigation.js +1 -0
  21. package/docs/assets/search.js +1 -0
  22. package/docs/assets/style.css +1633 -0
  23. package/docs/classes/Client.html +31 -0
  24. package/docs/classes/LngLatBounds.html +120 -0
  25. package/docs/classes/SismoMap.html +12 -0
  26. package/docs/functions/EventToEventGeoJSON.html +1 -0
  27. package/docs/functions/createMap.html +6 -0
  28. package/docs/hierarchy.html +1 -0
  29. package/docs/index.html +9 -0
  30. package/docs/modules.html +1 -0
  31. package/docs/types/Answer.html +5 -0
  32. package/docs/types/City.html +7 -0
  33. package/docs/types/CleanedFDSNQueryOptions.html +1 -0
  34. package/docs/types/EventFeature.html +5 -0
  35. package/docs/types/EventGeoJSON.html +3 -0
  36. package/docs/types/EventGeoJSONProperties.html +11 -0
  37. package/docs/types/EventGeojsonDescriptionProperty.html +3 -0
  38. package/docs/types/FDSNQueryOptions.html +29 -0
  39. package/docs/types/Form.html +7 -0
  40. package/docs/types/Magnitude.html +16 -0
  41. package/docs/types/Origin.html +27 -0
  42. package/docs/types/Question.html +12 -0
  43. package/docs/types/QuestionChoice.html +5 -0
  44. package/docs/types/QuestionCondition.html +1 -0
  45. package/docs/types/QuestionGroup.html +3 -0
  46. package/docs/types/SisEvent.html +12 -0
  47. package/docs/types/Street.html +4 -0
  48. package/docs/types/Survey.html +5 -0
  49. package/docs/types/Testimony.html +17 -0
  50. package/docs/variables/eventTypes.html +1 -0
  51. package/docs/variables/mapLayers.html +1 -0
  52. package/package.json +54 -0
  53. package/src/client.ts +552 -0
  54. package/src/index.ts +5 -0
  55. package/src/maputils.ts +116 -0
  56. package/src/sismomap.ts +108 -0
  57. package/src/types.ts +237 -0
  58. package/src/utils.ts +20 -0
  59. package/typedoc.json +1 -0
@@ -0,0 +1,108 @@
1
+ import maplibregl from 'maplibre-gl';
2
+ import { EventGeoJSON, mapLayers, SisEvent } from './types';
3
+ import { isEventGeoJSONProperties, isEventGeoJSON } from './utils';
4
+ import { bbox } from '@turf/turf';
5
+ import { EventToEventGeoJSON } from './maputils';
6
+
7
+ export class SismoMap {
8
+ name: string;
9
+ map: maplibregl.Map;
10
+ descPopup: maplibregl.Popup;
11
+
12
+ /**
13
+ * Not meant to use, use createMap instead
14
+ */
15
+ constructor(
16
+ name: string,
17
+ map: maplibregl.Map,
18
+ descPopup: maplibregl.Popup
19
+ ) {
20
+ this.name = name;
21
+ this.map = map;
22
+ this.descPopup = descPopup;
23
+ }
24
+
25
+ /**
26
+ * Removes all user-made layers
27
+ */
28
+ clear() {
29
+ mapLayers.forEach((title) => {
30
+ if (this.map.getLayer(title)) {
31
+ this.map.removeLayer(title);
32
+ }
33
+ if (this.map.getSource('src')) {
34
+ this.map.removeSource('src');
35
+ }
36
+ });
37
+ }
38
+
39
+ /**
40
+ * @param eventOrList Can either be a single Event (via /events API) or a FDSN GeoJSON
41
+ * @param moveView True = The view moves on the events' bounding box while displaying (does not affect user's possible behaviour on the map)
42
+ * @param clear True = The map is cleared before displaying the events
43
+ */
44
+ displayEvents(
45
+ eventOrList: SisEvent | EventGeoJSON,
46
+ moveView: boolean = true,
47
+ clear: boolean = true
48
+ ) {
49
+ let eventList: EventGeoJSON;
50
+
51
+ // Type guard to disting the overloading cases
52
+ if (isEventGeoJSON(eventOrList)) {
53
+ eventList = eventOrList;
54
+ } else {
55
+ eventList = EventToEventGeoJSON(eventOrList as SisEvent);
56
+ }
57
+
58
+ if (clear) {
59
+ this.clear();
60
+ }
61
+ if (eventList.features.length == 0) return;
62
+
63
+ this.map.addSource('src', { type: 'geojson', data: eventList as any });
64
+ eventList.features.forEach((feature) => {
65
+ let eventType: string;
66
+ let typeName: string;
67
+ if (!isEventGeoJSONProperties(feature.properties)) {
68
+ eventType = feature.properties.eventType;
69
+ typeName = 'eventType';
70
+ } else {
71
+ eventType = feature.properties.type;
72
+ typeName = 'type';
73
+ }
74
+ let layerName = eventType == null ? 'event' : eventType;
75
+ if (this.map.getLayer(layerName)) return;
76
+ this.map.addLayer({
77
+ id: layerName,
78
+ source: 'src',
79
+ type: 'circle',
80
+ paint: {
81
+ // Inspired by https://renass.unistra.fr/js/events.js
82
+ 'circle-radius': ['*', 8, ['ln', ['+', 1, ['get', 'mag']]]],
83
+ 'circle-color': '#B42222',
84
+ 'circle-opacity': 0.8,
85
+ 'circle-stroke-width': 1,
86
+ 'circle-stroke-opacity': 0.9,
87
+ },
88
+ filter: ['==', eventType, ['get', typeName]],
89
+ });
90
+ });
91
+ if (moveView) {
92
+ if (eventList.features.length == 1) {
93
+ let lng: number = eventList.features[0].geometry.coordinates[0];
94
+ let lat: number = eventList.features[0].geometry.coordinates[1];
95
+ this.map.flyTo({ center: [lng, lat], zoom: 6 });
96
+ } else {
97
+ // Inter-library operability is annoying, this line will always work with the types i'll give it
98
+ let bounds = bbox(eventList as any);
99
+ let margin = [-0.5, -0.5, 0.5, 0.5];
100
+ for (let i = 0; i < 4; i++) {
101
+ bounds[i] += margin[i];
102
+ }
103
+
104
+ this.map.fitBounds(bounds as any);
105
+ }
106
+ }
107
+ }
108
+ }
package/src/types.ts ADDED
@@ -0,0 +1,237 @@
1
+ import maplibregl from 'maplibre-gl';
2
+ import 'maplibre-gl/dist/maplibre-gl.css';
3
+
4
+ export const mapLayers: Array<string> = [
5
+ 'event',
6
+ 'earthquake',
7
+ 'quarry blast',
8
+ 'thunder',
9
+ 'mining-explosion',
10
+ 'chemical explosion',
11
+ 'building collapse',
12
+ 'nuclear explosion',
13
+ 'mine collapse',
14
+ 'sonic boom',
15
+ 'rockslide',
16
+ 'ice quake',
17
+ 'outside of network interest',
18
+ 'landslide',
19
+ 'explosion',
20
+ 'induced',
21
+ 'stations-layer',
22
+ ];
23
+
24
+ export type EventGeojsonDescriptionProperty = {
25
+ fr: string;
26
+ en: string;
27
+ };
28
+
29
+ export type EventGeoJSONProperties = {
30
+ type: string;
31
+ time: Date;
32
+ description: EventGeojsonDescriptionProperty;
33
+ depth: number;
34
+ url: string[];
35
+ automatic: boolean;
36
+ latitude: number;
37
+ longitude: number;
38
+ mag: number;
39
+ magType: string;
40
+ };
41
+
42
+ export type EventFeature<T = EventGeoJSONProperties | SisEvent> = {
43
+ id: string;
44
+ type: string;
45
+ properties: T;
46
+ geometry: {
47
+ type: string;
48
+ coordinates: [number, number];
49
+ };
50
+ };
51
+
52
+ export type EventGeoJSON = {
53
+ type: string;
54
+ features: EventFeature[];
55
+ };
56
+
57
+ export type Origin = {
58
+ id: number;
59
+ eventID: number;
60
+ eventPreferred: boolean;
61
+ time: Date;
62
+ timeUncertainty: number;
63
+ timeConfidenceLevel: number;
64
+ latitude: number;
65
+ latitudeUncertainty: number;
66
+ latitudeConfidenceLevel: number;
67
+ longitude: number;
68
+ longitudeUncertainty: number;
69
+ longitudeConfidenceLevel: number;
70
+ depth: number;
71
+ depthUncertainty: number;
72
+ depthUsed: number;
73
+ depthType: number;
74
+ methodID: string;
75
+ earthmodelID: string;
76
+ automatic: boolean;
77
+ evaluationStatus: string;
78
+ originType: string;
79
+ agency: string;
80
+ author: string;
81
+ createdAt: Date;
82
+ insertedAt: Date;
83
+ updatedAt: Date;
84
+ };
85
+
86
+ export type Magnitude = {
87
+ id: number;
88
+ eventID: number;
89
+ originID: number;
90
+ eventPreferred: boolean;
91
+ magnitude: number;
92
+ magnitudeUncertainty: number;
93
+ magnitudeType: number;
94
+ methodID: string;
95
+ stationCount: number;
96
+ evaluationStatus: string;
97
+ agency: string;
98
+ author: string;
99
+ createdAt: Date;
100
+ insertedAt: Date;
101
+ updatedAt: Date;
102
+ };
103
+
104
+ export type Survey = {
105
+ name: string;
106
+ key: string;
107
+ eventPublicID: string;
108
+ opened: boolean;
109
+ };
110
+
111
+ // Had to change the name from Event to SisEvent because Event is already
112
+ // taken by TS and it messed with the compiler and LSPs
113
+ export type SisEvent = {
114
+ id: number;
115
+ zoneID: number;
116
+ publicid: string;
117
+ eventType: string;
118
+ localtime: Date;
119
+ timezone: string;
120
+ felt: boolean;
121
+ preferredOrigin: Origin;
122
+ preferredMagnitude: Magnitude;
123
+ insertedAt: Date;
124
+ updatedAt: Date;
125
+ };
126
+
127
+ export type FDSNQueryOptions = {
128
+ starttime?: Date;
129
+ endtime?: Date;
130
+ minlatitude?: number;
131
+ maxlatitude?: number;
132
+ minlongitude?: number;
133
+ maxlongitude?: number;
134
+ latitude?: number;
135
+ longitude?: number;
136
+ minradius?: number;
137
+ maxradius?: number;
138
+ mindepth?: number;
139
+ maxdepth?: number;
140
+ minmagnitude?: number;
141
+ maxmagnitude?: number;
142
+ magnitudetype?: string;
143
+ eventtype?: string;
144
+ includeallorigins?: boolean;
145
+ includeallmagnitudes?: boolean;
146
+ includearrivals?: boolean;
147
+ eventid?: string;
148
+ limit?: number;
149
+ offset?: number;
150
+ orderby?: string;
151
+ catalog?: string;
152
+ contributor?: string;
153
+ updatedafter?: string;
154
+ format?: string;
155
+ nodata?: string;
156
+ };
157
+
158
+ // This is called typescript branding and makes FDSN... not usable when CleanedFDSN... is expected
159
+ export type CleanedFDSNQueryOptions = FDSNQueryOptions & { __brand: 'Cleaned' };
160
+
161
+ export type City = {
162
+ latitude: number;
163
+ longitude: number;
164
+ name: string;
165
+ id: number;
166
+ population: number;
167
+ administrativeCode: string;
168
+ };
169
+
170
+ export type Street = {
171
+ latitude: number;
172
+ longitude: number;
173
+ name: string;
174
+ };
175
+
176
+ export type Testimony = {
177
+ key: string;
178
+ eventPublicID: string;
179
+ formID: number;
180
+ naiveFeltTime: Date;
181
+ feltTime: Date;
182
+ timezone: String;
183
+ highlighted: boolean;
184
+ utcOffset: number;
185
+ quality: number;
186
+ userEvaluationStatus: string;
187
+ source: string;
188
+ administrativeCodeType: string;
189
+ administrativeCode: string;
190
+ geocodingType: string;
191
+ longitude: number;
192
+ latitude: number;
193
+ };
194
+
195
+ export type QuestionGroup = {
196
+ id: number;
197
+ text: string;
198
+ };
199
+
200
+ export type QuestionCondition = number[];
201
+
202
+ export type QuestionChoice = {
203
+ id: number;
204
+ value: string;
205
+ url?: string;
206
+ order: number;
207
+ };
208
+
209
+ export type Question = {
210
+ id: number;
211
+ groupID: number;
212
+ type: string;
213
+ text: string;
214
+ subtext: string;
215
+ answerType: string;
216
+ required: boolean;
217
+ order: number;
218
+ multiple: boolean;
219
+ conditions: QuestionCondition[];
220
+ choices: QuestionChoice[];
221
+ };
222
+
223
+ export type Answer = {
224
+ id: number;
225
+ question_id: number;
226
+ value: string;
227
+ choices: number[];
228
+ };
229
+
230
+ export type Form = {
231
+ default: boolean;
232
+ id: number;
233
+ name: string;
234
+ type: string;
235
+ groups: QuestionGroup[];
236
+ questions: Question[];
237
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { SisEvent, EventGeoJSON, EventGeoJSONProperties } from './types';
2
+ // This file is for library-only functions, it should not be exported outside of the module
3
+
4
+ export function formatString(str: string): string {
5
+ return str
6
+ .normalize('NFD')
7
+ .replace(/[\u0300-\u036f]/g, '')
8
+ .replace(/\s+/g, '-')
9
+ .toLowerCase()
10
+ .normalize('NFC');
11
+ }
12
+
13
+ export function isEventGeoJSONProperties(
14
+ obj: SisEvent | EventGeoJSONProperties
15
+ ): obj is EventGeoJSONProperties {
16
+ return !((obj as SisEvent).eventType !== undefined);
17
+ }
18
+ export function isEventGeoJSON(obj: SisEvent | EventGeoJSON): obj is EventGeoJSON {
19
+ return (obj as EventGeoJSON).features !== undefined;
20
+ }
package/typedoc.json ADDED
@@ -0,0 +1 @@
1
+ {}