Improve typing

This commit is contained in:
pietervdvn 2022-08-24 02:22:06 +02:00
parent 1fbe033456
commit 4d783aae45
4 changed files with 19 additions and 8 deletions

View file

@ -3,6 +3,7 @@ import * as polygon_features from "../../assets/polygon-features.json";
import {Store, UIEventSource} from "../UIEventSource"; import {Store, UIEventSource} from "../UIEventSource";
import {BBox} from "../BBox"; import {BBox} from "../BBox";
import * as OsmToGeoJson from "osmtogeojson"; import * as OsmToGeoJson from "osmtogeojson";
import {NodeId, OsmId, OsmTags, RelationId, WayId} from "../../Models/OsmFeature";
export abstract class OsmObject { export abstract class OsmObject {
@ -38,10 +39,12 @@ export abstract class OsmObject {
throw "Backend URL must begin with http" throw "Backend URL must begin with http"
} }
this.backendURL = url; this.backendURL = url;
this.DownloadObject("id/5")
} }
public static DownloadObject(id: string, forceRefresh: boolean = false): Store<OsmObject> { public static DownloadObject(id: NodeId, forceRefresh?: boolean ): Store<OsmNode> ;
public static DownloadObject(id: WayId, forceRefresh?: boolean ): Store<OsmWay> ;
public static DownloadObject(id: RelationId, forceRefresh?: boolean ): Store<OsmRelation> ;
public static DownloadObject(id: OsmId, forceRefresh: boolean = false): Store<OsmObject> {
let src: UIEventSource<OsmObject>; let src: UIEventSource<OsmObject>;
if (OsmObject.objectCache.has(id)) { if (OsmObject.objectCache.has(id)) {
src = OsmObject.objectCache.get(id) src = OsmObject.objectCache.get(id)
@ -51,14 +54,14 @@ export abstract class OsmObject {
return src; return src;
} }
} else { } else {
src = UIEventSource.FromPromise(OsmObject.DownloadObjectAsync(id)) src = UIEventSource.FromPromise(OsmObject.DownloadObjectAsync(<any> id))
} }
OsmObject.objectCache.set(id, src); OsmObject.objectCache.set(id, src);
return src; return src;
} }
static async DownloadPropertiesOf(id: string): Promise<any> { static async DownloadPropertiesOf(id: OsmId): Promise<OsmTags> {
const splitted = id.split("/"); const splitted = id.split("/");
const idN = Number(splitted[1]); const idN = Number(splitted[1]);
if (idN < 0) { if (idN < 0) {
@ -70,7 +73,10 @@ export abstract class OsmObject {
return rawData.elements[0].tags return rawData.elements[0].tags
} }
static async DownloadObjectAsync(id: string): Promise<OsmObject | undefined> { static async DownloadObjectAsync(id: NodeId): Promise<OsmNode | undefined>;
static async DownloadObjectAsync(id: WayId): Promise<OsmWay | undefined>;
static async DownloadObjectAsync(id: RelationId): Promise<OsmRelation | undefined>;
static async DownloadObjectAsync(id: OsmId): Promise<OsmObject | undefined>{
const splitted = id.split("/"); const splitted = id.split("/");
const type = splitted[0]; const type = splitted[0];
const idN = Number(splitted[1]); const idN = Number(splitted[1]);

View file

@ -1,4 +1,9 @@
import {Feature, Geometry} from "@turf/turf"; import {Feature, Geometry} from "@turf/turf";
export type OsmTags = Record<string, string> & {id: string} 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<string, string> & {id: OsmId}
export type OsmFeature = Feature<Geometry, OsmTags> export type OsmFeature = Feature<Geometry, OsmTags>

View file

@ -41,7 +41,7 @@
"maxZoom": 14, "maxZoom": 14,
"minNeededElements": 100 "minNeededElements": 100
}, },
"credits": "nicolelaine" "credits": "nicolelaine",
"layers": [ "layers": [
"postboxes", "postboxes",
"postoffices", "postoffices",

View file

@ -604,7 +604,7 @@ describe("RelationSplitHandler", () => {
"should split turn restrictions (split of https://www.openstreetmap.org/way/143298912)", "should split turn restrictions (split of https://www.openstreetmap.org/way/143298912)",
async () => { async () => {
const relation: OsmRelation = <OsmRelation>await OsmObject.DownloadObjectAsync("relation/4374576") const relation: OsmRelation = await OsmObject.DownloadObjectAsync("relation/4374576")
const originalNodeIds = const originalNodeIds =
[ [
1407529979, 1407529979,