-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathosm.ts
More file actions
545 lines (441 loc) · 13.9 KB
/
Copy pathosm.ts
File metadata and controls
545 lines (441 loc) · 13.9 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import parseOsmChangeXml from '@osmcha/osmchange-parser';
import type { FeatureCollection, Point } from 'geojson';
import {
BaseHttpClient,
BaseHttpClientError,
type FetchConfig,
type HttpBody,
} from '~/services/http';
import * as xml from '~/util/xml';
import type { ICancelableClient } from '~/services/loading';
import type { TdeiClient } from '~/services/tdei';
import { OSMCHANGE_ACTION_TYPES } from '~/types/osm';
import type {
OsmChange,
OsmChangeset,
OsmChangesetComment,
OsmElement,
OsmNode,
OsmNote,
OsmWay,
} from '~/types/osm';
import type { WorkspaceId } from '~/types/workspaces';
function formatFeatureIdPlaceholder(attributeName: string, feature: Element) {
const id = feature.getAttribute(attributeName);
if (id && id[0] !== '-') {
feature.setAttribute(attributeName, '-' + id);
}
}
function formatFeatureIdPlaceholders(feature: Element) {
// OSM-based APIs generally expect negative IDs for insertions:
formatFeatureIdPlaceholder('id', feature);
if (feature.tagName === 'node') {
// Nodes are by far the most common features--exit early to avoid
// an extra string comparison:
return
}
if (feature.tagName === 'way') {
for (const child of feature.children) {
if (child.tagName === 'nd') {
formatFeatureIdPlaceholder('ref', child);
}
}
} else if (feature.tagName === 'relation') {
for (const child of feature.children) {
if (child.tagName === 'member') {
formatFeatureIdPlaceholder('ref', child);
}
}
}
}
function notesGeoJsonToEntities(geoJson: FeatureCollection): OsmNote[] {
const notes = [];
for (const feature of geoJson.features) {
const geometry = feature.geometry as Point;
const properties = feature.properties ?? { };
for (const comment of properties.comments) {
comment.date = new Date(comment.date);
}
notes.push({
id: properties.id,
status: properties.status,
lat: geometry.coordinates[1] ?? 0,
lon: geometry.coordinates[0] ?? 0,
created_at: new Date(properties.date_created),
comments: properties.comments,
});
}
return notes;
}
function cleanOscForDemo(features: Element[]) {
const nodeIds = new Set();
const ways = [];
const wayIds = new Set();
const relations = [];
for (const node of features) {
if (node.tagName === 'node') {
nodeIds.add(node.getAttribute('id'));
} else if (node.tagName === 'way') {
ways.push(node);
} else if (node.tagName === 'relation') {
relations.push(node);
}
}
let orphanCounter = 0;
for (const way of ways) {
let totalNodes = 0;
let nodesRemoved = 0;
for (const child of [...way.children]) {
if (child.tagName === 'nd') {
totalNodes++;
if (!nodeIds.has(child.getAttribute('ref'))) {
child.remove();
nodesRemoved++;
}
}
}
if (nodesRemoved > 0) {
orphanCounter += nodesRemoved;
if (totalNodes - nodesRemoved <= 1) {
way.remove();
continue;
}
}
wayIds.add(way.getAttribute('id'));
}
for (const relation of relations) {
let totalMembers = 0;
let membersRemoved = 0;
for (const child of [...relation.children]) {
if (child.tagName === 'member') {
totalMembers++;
if (!wayIds.has(child.getAttribute('ref'))) {
child.remove();
membersRemoved++;
}
}
}
if (membersRemoved > 0) {
orphanCounter += membersRemoved;
if (totalMembers - membersRemoved === 0) {
relation.remove();
}
}
}
if (orphanCounter > 0) {
console.warn(`Removed ${orphanCounter} orphan references!`);
}
}
export function osm2osc(changesetId: number, osmXml: string): string {
const osmDoc = xml.parse(osmXml);
const features = [];
// Filter features and build an intermediate collection. Appending
// nodes to another document will break this iterator:
for (const feature of osmDoc.firstChild.children) {
if (feature.nodeType === Node.TEXT_NODE) {
continue;
}
features.push(feature);
}
const oscDoc = xml.parse(
'<osmChange version="0.6"><create /><modify /><delete /></osmChange>',
'application/xml'
);
const createNode = oscDoc.firstChild.firstChild;
for (const feature of features) {
feature.setAttribute('changeset', changesetId);
formatFeatureIdPlaceholders(feature);
createNode.appendChild(feature);
}
// TODO: we should show an error for incomplete graphs:
cleanOscForDemo(features);
return xml.serialize(oscDoc);
}
export class OsmApiClientError extends Error {
response: Response;
constructor(response: Response) {
super(`OSM API request failed: ${response.statusText} (${response.url})`);
this.response = response;
}
}
export class OsmApiClient extends BaseHttpClient implements ICancelableClient {
#webUrl: string;
#tdeiClient: TdeiClient;
constructor(
webUrl: string,
apiUrl: string,
tdeiClient: TdeiClient,
signal?: AbortSignal
) {
super(apiUrl, signal);
this.#webUrl = webUrl;
this.#tdeiClient = tdeiClient;
this.#setAuthHeader();
// OSM API can return XML or JSON based on the header or file extension:
this._requestHeaders['Accept'] = '*/*';
this._requestHeaders['Content-Type'] = 'text/plain';
}
get auth() {
return this.#tdeiClient.auth;
}
clone(signal?: AbortSignal) {
return new OsmApiClient(
this.#webUrl,
this._baseUrl,
this.#tdeiClient,
signal ?? this._abortSignal
);
}
webUrl(rest: string) {
return this.#webUrl + rest
}
async provisionUser() {
const body = {
email: this.auth.email,
display_name: this.auth.displayName
};
await this._put(`user/${this.auth.subject}`, body, {
headers: { 'Content-Type': 'application/json' }
});
}
async createWorkspace(workspaceId: WorkspaceId) {
await this._put(`workspaces/${workspaceId}`);
}
async deleteWorkspace(workspaceId: WorkspaceId) {
await this._delete(`workspaces/${workspaceId}`);
}
async getWorkspaceBbox(workspaceId: WorkspaceId) {
const response = await this._get(`workspaces/${workspaceId}/bbox.json`);
if (response.status === 204) {
return undefined
}
return await response.json();
}
async getExportBbox(id: number) {
const bbox = await this.getWorkspaceBbox(id);
if (bbox === undefined) {
return undefined
}
// Passing the exact bounding box to the OSM map call may lose nodes on the
// bounds. We grow the bounding box here to ensure that we export the whole
// workspace. A bounding box of "-180,-90,180,90" covering the entire Earth
// would be ideal, but this crashes CGImap's "map" endpoint as it allocates
// memory for every tile in the coordinate space.
//
// TODO: consider implementing a dedicated endpoint for exporting the whole
// workspace instead of reusing the existing "map" API.
//
const pad = 0.0000001;
return `${bbox.min_lon},${bbox.min_lat},${bbox.max_lon + pad},${bbox.max_lat + pad}`;
}
async getElement(
workspaceId: WorkspaceId,
type: string,
id: number,
version: number,
): Promise<OsmElement> {
const response = await this._get(`${type}/${id}/${version}`, {
headers: {
'Accept': 'application/json',
'X-Workspace': workspaceId,
},
});
const element = (await response.json()).elements[0];
element.tags = element.tags ?? { };
return element;
}
async getNodes(workspaceId: WorkspaceId, nodeIds: (number | string)[]): Promise<OsmNode[]> {
const response = await this._get(`nodes?nodes=${nodeIds.join(',')}`, {
headers: {
'Accept': 'application/json',
'X-Workspace': workspaceId,
},
});
const nodes = (await response.json()).elements;
for (const node of nodes) {
node.timestamp = new Date(node.timestamp);
}
return nodes;
}
async getWays(workspaceId: WorkspaceId, wayIds: (number | string)[]): Promise<OsmWay[]> {
const response = await this._get(`ways?ways=${wayIds.join(',')}`, {
headers: {
'Accept': 'application/json',
'X-Workspace': workspaceId,
},
});
const ways = (await response.json()).elements;
for (const way of ways) {
way.timestamp = new Date(way.timestamp);
}
return ways;
}
async getWaysForNode(workspaceId: WorkspaceId, nodeId: number): Promise<OsmElement[]> {
const response = await this._get(`node/${nodeId}/ways`, {
headers: {
'Accept': 'application/json',
'X-Workspace': workspaceId,
},
});
return (await response.json()).elements;
}
async listChangesets(workspaceId: WorkspaceId): Promise<OsmChangeset[]> {
const response = await this._get(`changesets.json`, {
headers: { 'X-Workspace': workspaceId },
});
const changesets = (await response.json())?.changesets ?? [];
for (const changeset of changesets) {
changeset.created_at = new Date(changeset.created_at);
changeset.closed_at = new Date(changeset.closed_at);
}
return changesets;
}
async getChangeset(
workspaceId: WorkspaceId,
changesetId: number,
includeDiscussion: boolean = false,
): Promise<OsmChangeset | undefined> {
let url = `changeset/${changesetId}.json`;
if (includeDiscussion) {
url += '?include_discussion=true';
}
const response = await this._get(url, {
headers: {
'Accept': 'application/json',
'X-Workspace': workspaceId,
},
});
const changeset = (await response.json())?.changeset;
if (!changeset) {
return;
}
changeset.created_at = new Date(changeset.created_at);
changeset.closed_at = new Date(changeset.closed_at);
for (const comment of changeset.comments ?? []) {
comment.date = new Date(comment.date);
}
return changeset;
}
async getOsmChange(workspaceId: WorkspaceId, changesetId: number)
: Promise<OsmChange>
{
const response = await this._get(`changeset/${changesetId}/download`, {
headers: {
'Accept': 'application/xml',
'X-Workspace': workspaceId,
},
});
const osmChange = parseOsmChangeXml(await response.text());
for (const type of OSMCHANGE_ACTION_TYPES) {
for (const element of osmChange[type] ?? []) {
element.timestamp = new Date(element.timestamp);
}
}
return osmChange;
}
async createChangeset(workspaceId: WorkspaceId): Promise<number> {
const doc = xml.parse('<osm><changeset></changeset></osm>');
const changesetNode = doc.firstChild.firstChild;
changesetNode.appendChild(xml.makeNode(doc, "tag", { k: 'workspace', v: workspaceId }));
changesetNode.appendChild(xml.makeNode(doc, "tag", { k: 'comment', v: 'Import workspace' }));
changesetNode.appendChild(xml.makeNode(doc, "tag", { k: 'created_by', v: 'TDEI Workspaces' }));
const body = xml.serialize(doc);
const response = await this._put('changeset/create', body, {
headers: { 'X-Workspace': workspaceId },
});
return Number(await response.text());
}
async uploadChangeset(
workspaceId: WorkspaceId,
changesetId: number,
changesetXml: string
) {
await this._post(`changeset/${changesetId}/upload`, changesetXml, {
headers: {
'Content-Type': 'application/xml',
'X-Workspace': workspaceId,
},
});
}
async getChangesetComments(
workspaceId: WorkspaceId,
changesetId: number,
): Promise<OsmChangesetComment[]> {
// There is no OSM API that returns comments directly. We must request the
// comments with the changeset:
//
const changeset = await this.getChangeset(workspaceId, changesetId, true);
return changeset?.comments ?? [];
}
async postChangesetComment(
workspaceId: WorkspaceId,
changesetId: number,
message: string,
): Promise<void> {
const body = new FormData();
body.append('text', message);
await this._post(`changeset/${changesetId}/comment`, body, {
headers: { 'X-Workspace': workspaceId },
});
}
async getNotes(workspaceId: WorkspaceId, includeClosed: boolean): Promise<OsmNote[]> {
const params = new URLSearchParams();
// Fetch the maximum number of notes:
params.append('limit', '10000');
// -1: all, 0: open only, > 0: days closed:
params.append('closed', includeClosed ? '-1' : '0');
const response = await this._get(`notes/search.json?${params}`, {
headers: {
'Accept': 'application/json',
'X-Workspace': workspaceId,
},
});
return notesGeoJsonToEntities(await response.json());
}
async getWorkspaceData(workspaceId: WorkspaceId): Promise<Array> {
const bboxParam = await this.getExportBbox(workspaceId);
const response = await this._get(`map.json?bbox=${bboxParam}`, {
headers: {
'Accept': 'application/json',
'X-Workspace': workspaceId
}
});
return (await response.json()).elements;
}
async exportWorkspaceXml(workspaceId: WorkspaceId): Promise<Blob> {
const bboxParam = await this.getExportBbox(workspaceId);
const response = await this._get(`map?bbox=${bboxParam}`, {
headers: {
'Accept': 'application/xml',
'X-Workspace': workspaceId
}
});
return await response.blob();
}
#setAuthHeader() {
if (this.#tdeiClient.auth.complete) {
this._requestHeaders.Authorization = 'Bearer ' + this.#tdeiClient.auth.accessToken;
}
}
override async _send(
url: string,
method: string,
body?: HttpBody,
config?: FetchConfig,
): Promise<Response> {
try {
await this.#tdeiClient.tryRefreshAuth();
this.#setAuthHeader();
const requestOptions: FetchConfig = {
credentials: 'include',
};
return await super._send(url, method, body, { ...requestOptions, ...config });
}
catch (e: unknown) {
if (e instanceof BaseHttpClientError) {
throw new OsmApiClientError(e.response);
}
throw e;
}
}
}