Improve typing, make DownloadAsync cache timing flexible

This commit is contained in:
pietervdvn 2022-09-21 02:11:10 +02:00
parent bdb5833a1c
commit 728f526b61
2 changed files with 35 additions and 26 deletions

View file

@ -19,7 +19,7 @@ export default class CreateNewNodeAction extends OsmCreateAction {
private readonly _lon: number
private readonly _snapOnto: OsmWay
private readonly _reusePointDistance: number
private meta: { changeType: "create" | "import"; theme: string; specialMotivation?: string }
private readonly meta: { changeType: "create" | "import"; theme: string; specialMotivation?: string }
private readonly _reusePreviouslyCreatedPoint: boolean
constructor(

View file

@ -1,9 +1,10 @@
import { Utils } from "../../Utils"
import {Utils} from "../../Utils"
import * as polygon_features from "../../assets/polygon-features.json"
import { Store, UIEventSource } from "../UIEventSource"
import { BBox } from "../BBox"
import {Store, UIEventSource} from "../UIEventSource"
import {BBox} from "../BBox"
import * as OsmToGeoJson from "osmtogeojson"
import { NodeId, OsmFeature, OsmId, OsmTags, RelationId, WayId } from "../../Models/OsmFeature"
import {NodeId, OsmFeature, OsmId, OsmTags, RelationId, WayId} from "../../Models/OsmFeature"
import {Feature, LineString, Polygon} from "geojson";
export abstract class OsmObject {
private static defaultBackend = "https://www.openstreetmap.org/"
@ -16,7 +17,7 @@ export abstract class OsmObject {
/**
* The OSM tags as simple object
*/
tags: OsmTags
tags: OsmTags & {id: OsmId}
version: number
public changed: boolean = false
timestamp: Date
@ -40,6 +41,9 @@ export abstract class OsmObject {
this.backendURL = url
}
public static DownloadObject(id: NodeId, forceRefresh?: boolean): Store<OsmNode> ;
public static DownloadObject(id: RelationId, forceRefresh?: boolean): Store<OsmRelation> ;
public static DownloadObject(id: WayId, forceRefresh?: boolean): Store<OsmWay> ;
public static DownloadObject(id: string, forceRefresh: boolean = false): Store<OsmObject> {
let src: UIEventSource<OsmObject>
if (OsmObject.objectCache.has(id)) {
@ -69,12 +73,12 @@ export abstract class OsmObject {
return rawData.elements[0].tags
}
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>
static async DownloadObjectAsync(id: string): Promise<OsmObject | undefined>
static async DownloadObjectAsync(id: string): Promise<OsmObject | undefined> {
static async DownloadObjectAsync(id: NodeId, maxCacheAgeInSecs?: number): Promise<OsmNode | undefined>
static async DownloadObjectAsync(id: WayId, maxCacheAgeInSecs?: number): Promise<OsmWay | undefined>
static async DownloadObjectAsync(id: RelationId, maxCacheAgeInSecs?: number): Promise<OsmRelation | undefined>
static async DownloadObjectAsync(id: OsmId, maxCacheAgeInSecs?: number): Promise<OsmObject | undefined>
static async DownloadObjectAsync(id: string, maxCacheAgeInSecs?: number): Promise<OsmObject | undefined>
static async DownloadObjectAsync(id: string, maxCacheAgeInSecs?: number): Promise<OsmObject | undefined> {
const splitted = id.split("/")
const type = splitted[0]
const idN = Number(splitted[1])
@ -84,7 +88,7 @@ export abstract class OsmObject {
const full = !id.startsWith("node") ? "/full" : ""
const url = `${OsmObject.backendURL}api/0.6/${id}${full}`
const rawData = await Utils.downloadJsonCached(url, 10000)
const rawData = await Utils.downloadJsonCached(url, (maxCacheAgeInSecs ?? 10) * 1000)
if (rawData === undefined) {
return undefined
}
@ -206,7 +210,7 @@ export abstract class OsmObject {
case "relation":
osmObject = new OsmRelation(idN)
const allGeojsons = OsmToGeoJson.default(
{ elements },
{elements},
// @ts-ignore
{
flatProperties: true,
@ -260,16 +264,14 @@ export abstract class OsmObject {
return false
}
private static constructPolygonFeatures(): Map<
string,
{ values: Set<string>; blacklist: boolean }
> {
private static constructPolygonFeatures(): Map<string,
{ values: Set<string>; blacklist: boolean }> {
const result = new Map<string, { values: Set<string>; blacklist: boolean }>()
for (const polygonFeature of polygon_features["default"] ?? polygon_features) {
const key = polygonFeature.key
if (polygonFeature.polygon === "all") {
result.set(key, { values: null, blacklist: false })
result.set(key, {values: null, blacklist: false})
continue
}
@ -458,20 +460,27 @@ export class OsmWay extends OsmObject {
this.nodes = element.nodes
}
public asGeoJson() {
public asGeoJson(): Feature<Polygon | LineString> & { properties: { id: WayId } } {
let coordinates: [number, number][] | [number, number][][] = this.coordinates.map(
([lat, lon]) => [lon, lat]
)
let geometry: LineString | Polygon
if (this.isPolygon()) {
coordinates = [coordinates]
geometry = {
type: "Polygon",
coordinates: [coordinates],
}
} else {
geometry = {
type: "LineString",
coordinates: coordinates,
}
}
return {
type: "Feature",
properties: this.tags,
geometry: {
type: this.isPolygon() ? "Polygon" : "LineString",
coordinates: coordinates,
},
properties: <any>this.tags,
geometry,
}
}