forked from MapComplete/MapComplete
Improve typing, make DownloadAsync cache timing flexible
This commit is contained in:
parent
bdb5833a1c
commit
728f526b61
2 changed files with 35 additions and 26 deletions
|
@ -19,7 +19,7 @@ export default class CreateNewNodeAction extends OsmCreateAction {
|
||||||
private readonly _lon: number
|
private readonly _lon: number
|
||||||
private readonly _snapOnto: OsmWay
|
private readonly _snapOnto: OsmWay
|
||||||
private readonly _reusePointDistance: number
|
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
|
private readonly _reusePreviouslyCreatedPoint: boolean
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import { Utils } from "../../Utils"
|
import {Utils} from "../../Utils"
|
||||||
import * as polygon_features from "../../assets/polygon-features.json"
|
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, 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 {
|
export abstract class OsmObject {
|
||||||
private static defaultBackend = "https://www.openstreetmap.org/"
|
private static defaultBackend = "https://www.openstreetmap.org/"
|
||||||
|
@ -16,7 +17,7 @@ export abstract class OsmObject {
|
||||||
/**
|
/**
|
||||||
* The OSM tags as simple object
|
* The OSM tags as simple object
|
||||||
*/
|
*/
|
||||||
tags: OsmTags
|
tags: OsmTags & {id: OsmId}
|
||||||
version: number
|
version: number
|
||||||
public changed: boolean = false
|
public changed: boolean = false
|
||||||
timestamp: Date
|
timestamp: Date
|
||||||
|
@ -40,6 +41,9 @@ export abstract class OsmObject {
|
||||||
this.backendURL = url
|
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> {
|
public static DownloadObject(id: string, forceRefresh: boolean = false): Store<OsmObject> {
|
||||||
let src: UIEventSource<OsmObject>
|
let src: UIEventSource<OsmObject>
|
||||||
if (OsmObject.objectCache.has(id)) {
|
if (OsmObject.objectCache.has(id)) {
|
||||||
|
@ -69,12 +73,12 @@ export abstract class OsmObject {
|
||||||
return rawData.elements[0].tags
|
return rawData.elements[0].tags
|
||||||
}
|
}
|
||||||
|
|
||||||
static async DownloadObjectAsync(id: NodeId): Promise<OsmNode | undefined>
|
static async DownloadObjectAsync(id: NodeId, maxCacheAgeInSecs?: number): Promise<OsmNode | undefined>
|
||||||
static async DownloadObjectAsync(id: WayId): Promise<OsmWay | undefined>
|
static async DownloadObjectAsync(id: WayId, maxCacheAgeInSecs?: number): Promise<OsmWay | undefined>
|
||||||
static async DownloadObjectAsync(id: RelationId): Promise<OsmRelation | undefined>
|
static async DownloadObjectAsync(id: RelationId, maxCacheAgeInSecs?: number): Promise<OsmRelation | undefined>
|
||||||
static async DownloadObjectAsync(id: OsmId): Promise<OsmObject | undefined>
|
static async DownloadObjectAsync(id: OsmId, maxCacheAgeInSecs?: number): Promise<OsmObject | undefined>
|
||||||
static async DownloadObjectAsync(id: string): Promise<OsmObject | undefined>
|
static async DownloadObjectAsync(id: string, maxCacheAgeInSecs?: number): Promise<OsmObject | undefined>
|
||||||
static async DownloadObjectAsync(id: string): Promise<OsmObject | undefined> {
|
static async DownloadObjectAsync(id: string, maxCacheAgeInSecs?: number): 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])
|
||||||
|
@ -84,7 +88,7 @@ export abstract class OsmObject {
|
||||||
|
|
||||||
const full = !id.startsWith("node") ? "/full" : ""
|
const full = !id.startsWith("node") ? "/full" : ""
|
||||||
const url = `${OsmObject.backendURL}api/0.6/${id}${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) {
|
if (rawData === undefined) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
@ -206,7 +210,7 @@ export abstract class OsmObject {
|
||||||
case "relation":
|
case "relation":
|
||||||
osmObject = new OsmRelation(idN)
|
osmObject = new OsmRelation(idN)
|
||||||
const allGeojsons = OsmToGeoJson.default(
|
const allGeojsons = OsmToGeoJson.default(
|
||||||
{ elements },
|
{elements},
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
{
|
{
|
||||||
flatProperties: true,
|
flatProperties: true,
|
||||||
|
@ -260,16 +264,14 @@ export abstract class OsmObject {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private static constructPolygonFeatures(): Map<
|
private static constructPolygonFeatures(): Map<string,
|
||||||
string,
|
{ values: Set<string>; blacklist: boolean }> {
|
||||||
{ values: Set<string>; blacklist: boolean }
|
|
||||||
> {
|
|
||||||
const result = new 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) {
|
for (const polygonFeature of polygon_features["default"] ?? polygon_features) {
|
||||||
const key = polygonFeature.key
|
const key = polygonFeature.key
|
||||||
|
|
||||||
if (polygonFeature.polygon === "all") {
|
if (polygonFeature.polygon === "all") {
|
||||||
result.set(key, { values: null, blacklist: false })
|
result.set(key, {values: null, blacklist: false})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -458,20 +460,27 @@ export class OsmWay extends OsmObject {
|
||||||
this.nodes = element.nodes
|
this.nodes = element.nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
public asGeoJson() {
|
public asGeoJson(): Feature<Polygon | LineString> & { properties: { id: WayId } } {
|
||||||
let coordinates: [number, number][] | [number, number][][] = this.coordinates.map(
|
let coordinates: [number, number][] | [number, number][][] = this.coordinates.map(
|
||||||
([lat, lon]) => [lon, lat]
|
([lat, lon]) => [lon, lat]
|
||||||
)
|
)
|
||||||
|
let geometry: LineString | Polygon
|
||||||
|
|
||||||
if (this.isPolygon()) {
|
if (this.isPolygon()) {
|
||||||
coordinates = [coordinates]
|
geometry = {
|
||||||
|
type: "Polygon",
|
||||||
|
coordinates: [coordinates],
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
geometry = {
|
||||||
|
type: "LineString",
|
||||||
|
coordinates: coordinates,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
type: "Feature",
|
type: "Feature",
|
||||||
properties: this.tags,
|
properties: <any>this.tags,
|
||||||
geometry: {
|
geometry,
|
||||||
type: this.isPolygon() ? "Polygon" : "LineString",
|
|
||||||
coordinates: coordinates,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue