diff --git a/Logic/Osm/Actions/CreateNewNodeAction.ts b/Logic/Osm/Actions/CreateNewNodeAction.ts index a638bb554..afec56403 100644 --- a/Logic/Osm/Actions/CreateNewNodeAction.ts +++ b/Logic/Osm/Actions/CreateNewNodeAction.ts @@ -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( diff --git a/Logic/Osm/OsmObject.ts b/Logic/Osm/OsmObject.ts index a74bb3f3e..19aa92423 100644 --- a/Logic/Osm/OsmObject.ts +++ b/Logic/Osm/OsmObject.ts @@ -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 ; + public static DownloadObject(id: RelationId, forceRefresh?: boolean): Store ; + public static DownloadObject(id: WayId, forceRefresh?: boolean): Store ; public static DownloadObject(id: string, forceRefresh: boolean = false): Store { let src: UIEventSource if (OsmObject.objectCache.has(id)) { @@ -69,12 +73,12 @@ export abstract class OsmObject { return rawData.elements[0].tags } - 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 { + static async DownloadObjectAsync(id: NodeId, maxCacheAgeInSecs?: number): Promise + static async DownloadObjectAsync(id: WayId, maxCacheAgeInSecs?: number): Promise + static async DownloadObjectAsync(id: RelationId, maxCacheAgeInSecs?: number): Promise + static async DownloadObjectAsync(id: OsmId, maxCacheAgeInSecs?: number): Promise + static async DownloadObjectAsync(id: string, maxCacheAgeInSecs?: number): Promise + static async DownloadObjectAsync(id: string, maxCacheAgeInSecs?: number): Promise { 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; blacklist: boolean } - > { + private static constructPolygonFeatures(): Map; blacklist: boolean }> { const result = new Map; 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 & { 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: this.tags, + geometry, } }