From e653a76b6958754108c21a5227281e62d02975c8 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Sat, 3 Sep 2022 14:48:38 +0200 Subject: [PATCH] Improve typing --- Logic/Osm/OsmObject.ts | 14 ++++++++++---- Models/OsmFeature.ts | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Logic/Osm/OsmObject.ts b/Logic/Osm/OsmObject.ts index 5036803cb..25f0e3f09 100644 --- a/Logic/Osm/OsmObject.ts +++ b/Logic/Osm/OsmObject.ts @@ -3,6 +3,7 @@ import * as polygon_features from "../../assets/polygon-features.json"; import {Store, UIEventSource} from "../UIEventSource"; import {BBox} from "../BBox"; import * as OsmToGeoJson from "osmtogeojson"; +import {NodeId, OsmFeature, OsmId, OsmTags, RelationId, WayId} from "../../Models/OsmFeature"; export abstract class OsmObject { @@ -16,7 +17,7 @@ export abstract class OsmObject { /** * The OSM tags as simple object */ - tags: {} = {}; + tags: OsmTags ; version: number; public changed: boolean = false; timestamp: Date; @@ -69,7 +70,12 @@ export abstract class OsmObject { return rawData.elements[0].tags } - static async DownloadObjectAsync(id: string): Promise { + static async DownloadObjectAsync(id: NodeId): Promise; + static async DownloadObjectAsync(id: WayId): Promise; + static async DownloadObjectAsync(id: RelationId): Promise; + static async DownloadObjectAsync(id: OsmId): Promise; + static async DownloadObjectAsync(id: string): Promise; + static async DownloadObjectAsync(id: string): Promise{ const splitted = id.split("/"); const type = splitted[0]; const idN = Number(splitted[1]); @@ -315,7 +321,7 @@ export abstract class OsmObject { tgs["_last_edit:changeset"] = element.changeset tgs["_last_edit:timestamp"] = element.timestamp tgs["_version_number"] = element.version - tgs["id"] = this.type + "/" + this.id; + tgs["id"] = ( this.type + "/" + this.id); } } @@ -347,7 +353,7 @@ export class OsmNode extends OsmObject { return [this.lat, this.lon]; } - asGeoJson() { + asGeoJson() : OsmFeature{ return { "type": "Feature", "properties": this.tags, diff --git a/Models/OsmFeature.ts b/Models/OsmFeature.ts index 4753287ca..e8ad80488 100644 --- a/Models/OsmFeature.ts +++ b/Models/OsmFeature.ts @@ -1,4 +1,9 @@ import {Feature, Geometry} from "@turf/turf"; +export type RelationId = `relation/${number}` +export type WayId = `way/${number}` +export type NodeId = `node/${number}` +export type OsmId = NodeId | WayId | RelationId + export type OsmTags = Record & {id: string} export type OsmFeature = Feature \ No newline at end of file