2020-09-30 22:22:58 +02:00
|
|
|
import {Utils} from "../../Utils";
|
2021-05-31 20:47:08 +02:00
|
|
|
import * as polygon_features from "../../assets/polygon-features.json";
|
2021-06-30 18:48:23 +02:00
|
|
|
import {UIEventSource} from "../UIEventSource";
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
export abstract class OsmObject {
|
|
|
|
|
2021-07-10 15:52:52 +02:00
|
|
|
private static defaultBackend = "https://www.openstreetmap.org/"
|
|
|
|
protected static backendURL = OsmObject.defaultBackend;
|
2021-06-30 18:48:23 +02:00
|
|
|
private static polygonFeatures = OsmObject.constructPolygonFeatures()
|
|
|
|
private static objectCache = new Map<string, UIEventSource<OsmObject>>();
|
|
|
|
private static referencingWaysCache = new Map<string, UIEventSource<OsmWay[]>>();
|
|
|
|
private static referencingRelationsCache = new Map<string, UIEventSource<OsmRelation[]>>();
|
|
|
|
private static historyCache = new Map<string, UIEventSource<OsmObject[]>>();
|
2020-06-24 00:35:19 +02:00
|
|
|
type: string;
|
|
|
|
id: number;
|
|
|
|
tags: {} = {};
|
|
|
|
version: number;
|
|
|
|
public changed: boolean = false;
|
2021-05-06 03:03:54 +02:00
|
|
|
timestamp: Date;
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
protected constructor(type: string, id: number) {
|
|
|
|
this.id = id;
|
|
|
|
this.type = type;
|
2021-05-06 01:33:09 +02:00
|
|
|
this.tags = {
|
2021-07-18 14:52:09 +02:00
|
|
|
id: `${this.type}/${id}`
|
2021-05-06 01:33:09 +02:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
public static SetBackendUrl(url: string) {
|
|
|
|
if (!url.endsWith("/")) {
|
|
|
|
throw "Backend URL must end with a '/'"
|
|
|
|
}
|
|
|
|
if (!url.startsWith("http")) {
|
|
|
|
throw "Backend URL must begin with http"
|
|
|
|
}
|
|
|
|
this.backendURL = url;
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:56:00 +02:00
|
|
|
static DownloadObject(id: string, forceRefresh: boolean = false): UIEventSource<OsmObject> {
|
2021-07-10 15:52:52 +02:00
|
|
|
let src: UIEventSource<OsmObject>;
|
2021-06-30 18:48:23 +02:00
|
|
|
if (OsmObject.objectCache.has(id)) {
|
2021-07-09 19:56:00 +02:00
|
|
|
src = OsmObject.objectCache.get(id)
|
2021-07-10 15:52:52 +02:00
|
|
|
if (forceRefresh) {
|
2021-07-09 19:56:00 +02:00
|
|
|
src.setData(undefined)
|
2021-07-10 15:52:52 +02:00
|
|
|
} else {
|
2021-07-09 19:56:00 +02:00
|
|
|
return src;
|
|
|
|
}
|
2021-07-10 15:52:52 +02:00
|
|
|
} else {
|
2021-07-09 19:56:00 +02:00
|
|
|
src = new UIEventSource<OsmObject>(undefined)
|
2021-06-30 18:48:23 +02:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
const splitted = id.split("/");
|
|
|
|
const type = splitted[0];
|
2021-07-15 00:39:11 +02:00
|
|
|
const idN = Number(splitted[1]);
|
2021-09-09 00:05:51 +02:00
|
|
|
if (idN < 0) {
|
2021-07-18 14:52:09 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-04-21 01:23:28 +02:00
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
OsmObject.objectCache.set(id, src);
|
|
|
|
const newContinuation = (element: OsmObject) => {
|
|
|
|
src.setData(element)
|
2020-08-30 01:13:18 +02:00
|
|
|
}
|
2021-04-21 01:23:28 +02:00
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
switch (type) {
|
|
|
|
case("node"):
|
2021-06-30 18:48:23 +02:00
|
|
|
new OsmNode(idN).Download(newContinuation);
|
|
|
|
break;
|
2020-06-24 00:35:19 +02:00
|
|
|
case("way"):
|
2021-06-30 18:48:23 +02:00
|
|
|
new OsmWay(idN).Download(newContinuation);
|
|
|
|
break;
|
2020-06-24 00:35:19 +02:00
|
|
|
case("relation"):
|
2021-06-30 18:48:23 +02:00
|
|
|
new OsmRelation(idN).Download(newContinuation);
|
|
|
|
break;
|
2021-07-15 00:26:25 +02:00
|
|
|
default:
|
2021-07-18 14:52:09 +02:00
|
|
|
throw "Invalid object type:" + type + id;
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
}
|
2021-06-30 18:48:23 +02:00
|
|
|
return src;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 18:06:54 +02:00
|
|
|
/**
|
|
|
|
* Downloads the ways that are using this node.
|
|
|
|
* Beware: their geometry will be incomplete!
|
|
|
|
*/
|
2021-06-30 18:48:23 +02:00
|
|
|
public static DownloadReferencingWays(id: string): UIEventSource<OsmWay[]> {
|
|
|
|
if (OsmObject.referencingWaysCache.has(id)) {
|
|
|
|
return OsmObject.referencingWaysCache.get(id);
|
|
|
|
}
|
|
|
|
const waysSrc = new UIEventSource<OsmWay[]>([])
|
|
|
|
OsmObject.referencingWaysCache.set(id, waysSrc);
|
|
|
|
Utils.downloadJson(`${OsmObject.backendURL}api/0.6/${id}/ways`)
|
2021-06-28 18:06:54 +02:00
|
|
|
.then(data => {
|
2021-06-30 18:48:23 +02:00
|
|
|
const ways = data.elements.map(wayInfo => {
|
2021-06-28 18:06:54 +02:00
|
|
|
const way = new OsmWay(wayInfo.id)
|
|
|
|
way.LoadData(wayInfo)
|
|
|
|
return way
|
|
|
|
})
|
2021-06-30 18:48:23 +02:00
|
|
|
waysSrc.setData(ways)
|
2021-06-28 18:06:54 +02:00
|
|
|
})
|
2021-06-30 18:48:23 +02:00
|
|
|
return waysSrc;
|
2021-06-28 18:06:54 +02:00
|
|
|
}
|
2021-06-30 18:48:23 +02:00
|
|
|
|
2021-06-28 18:06:54 +02:00
|
|
|
/**
|
|
|
|
* Downloads the relations that are using this feature.
|
|
|
|
* Beware: their geometry will be incomplete!
|
|
|
|
*/
|
2021-06-30 18:48:23 +02:00
|
|
|
public static DownloadReferencingRelations(id: string): UIEventSource<OsmRelation[]> {
|
|
|
|
if (OsmObject.referencingRelationsCache.has(id)) {
|
|
|
|
return OsmObject.referencingRelationsCache.get(id);
|
|
|
|
}
|
2021-07-18 14:52:09 +02:00
|
|
|
const relsSrc = new UIEventSource<OsmRelation[]>(undefined)
|
2021-06-30 18:48:23 +02:00
|
|
|
OsmObject.referencingRelationsCache.set(id, relsSrc);
|
|
|
|
Utils.downloadJson(`${OsmObject.backendURL}api/0.6/${id}/relations`)
|
2021-06-28 18:06:54 +02:00
|
|
|
.then(data => {
|
|
|
|
const rels = data.elements.map(wayInfo => {
|
|
|
|
const rel = new OsmRelation(wayInfo.id)
|
|
|
|
rel.LoadData(wayInfo)
|
2021-08-24 12:13:47 +02:00
|
|
|
rel.SaveExtraData(wayInfo)
|
2021-06-28 18:06:54 +02:00
|
|
|
return rel
|
|
|
|
})
|
2021-06-30 18:48:23 +02:00
|
|
|
relsSrc.setData(rels)
|
2021-06-28 18:06:54 +02:00
|
|
|
})
|
2021-06-30 18:48:23 +02:00
|
|
|
return relsSrc;
|
2021-06-28 18:06:54 +02:00
|
|
|
}
|
2021-06-30 18:48:23 +02:00
|
|
|
|
|
|
|
public static DownloadHistory(id: string): UIEventSource<OsmObject []> {
|
|
|
|
if (OsmObject.historyCache.has(id)) {
|
|
|
|
return OsmObject.historyCache.get(id)
|
|
|
|
}
|
2021-05-06 03:03:54 +02:00
|
|
|
const splitted = id.split("/");
|
|
|
|
const type = splitted[0];
|
2021-07-15 20:47:28 +02:00
|
|
|
const idN = Number(splitted[1]);
|
2021-06-30 18:48:23 +02:00
|
|
|
const src = new UIEventSource<OsmObject[]>([]);
|
|
|
|
OsmObject.historyCache.set(id, src);
|
|
|
|
Utils.downloadJson(`${OsmObject.backendURL}api/0.6/${type}/${idN}/history`).then(data => {
|
2021-05-06 03:03:54 +02:00
|
|
|
const elements: any[] = data.elements;
|
|
|
|
const osmObjects: OsmObject[] = []
|
|
|
|
for (const element of elements) {
|
|
|
|
let osmObject: OsmObject = null
|
|
|
|
switch (type) {
|
|
|
|
case("node"):
|
|
|
|
osmObject = new OsmNode(idN);
|
|
|
|
break;
|
|
|
|
case("way"):
|
|
|
|
osmObject = new OsmWay(idN);
|
|
|
|
break;
|
|
|
|
case("relation"):
|
|
|
|
osmObject = new OsmRelation(idN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
osmObject?.LoadData(element);
|
|
|
|
osmObject?.SaveExtraData(element, []);
|
|
|
|
osmObjects.push(osmObject)
|
|
|
|
}
|
2021-06-30 18:48:23 +02:00
|
|
|
src.setData(osmObjects)
|
2021-05-06 03:03:54 +02:00
|
|
|
})
|
2021-06-30 18:48:23 +02:00
|
|
|
return src;
|
2021-05-06 03:03:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
// bounds should be: [[maxlat, minlon], [minlat, maxlon]] (same as Utils.tile_bounds)
|
|
|
|
public static LoadArea(bounds: [[number, number], [number, number]], callback: (objects: OsmObject[]) => void) {
|
|
|
|
const minlon = bounds[0][1]
|
|
|
|
const maxlon = bounds[1][1]
|
|
|
|
const minlat = bounds[1][0]
|
|
|
|
const maxlat = bounds[0][0];
|
|
|
|
const url = `${OsmObject.backendURL}api/0.6/map.json?bbox=${minlon},${minlat},${maxlon},${maxlat}`
|
2021-07-15 00:26:25 +02:00
|
|
|
Utils.downloadJson(url).then(data => {
|
2021-06-30 18:48:23 +02:00
|
|
|
const elements: any[] = data.elements;
|
|
|
|
const objects = OsmObject.ParseObjects(elements)
|
|
|
|
callback(objects);
|
2021-05-31 20:47:08 +02:00
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
})
|
|
|
|
}
|
2021-05-31 20:47:08 +02:00
|
|
|
|
2021-07-09 19:56:00 +02:00
|
|
|
public static DownloadAll(neededIds, forceRefresh = true): UIEventSource<OsmObject[]> {
|
2021-06-30 18:48:23 +02:00
|
|
|
// local function which downloads all the objects one by one
|
|
|
|
// this is one big loop, running one download, then rerunning the entire function
|
2021-05-31 20:47:08 +02:00
|
|
|
|
2021-07-09 19:56:00 +02:00
|
|
|
const allSources: UIEventSource<OsmObject> [] = neededIds.map(id => OsmObject.DownloadObject(id, forceRefresh))
|
2021-06-30 18:48:23 +02:00
|
|
|
const allCompleted = new UIEventSource(undefined).map(_ => {
|
|
|
|
return !allSources.some(uiEventSource => uiEventSource.data === undefined)
|
|
|
|
}, allSources)
|
|
|
|
return allCompleted.map(completed => {
|
|
|
|
if (completed) {
|
|
|
|
return allSources.map(src => src.data)
|
|
|
|
}
|
2021-07-10 13:57:40 +02:00
|
|
|
return undefined
|
2021-06-30 18:48:23 +02:00
|
|
|
});
|
2021-05-31 20:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static isPolygon(tags: any): boolean {
|
|
|
|
for (const tagsKey in tags) {
|
|
|
|
if (!tags.hasOwnProperty(tagsKey)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
const polyGuide = OsmObject.polygonFeatures.get(tagsKey)
|
|
|
|
if (polyGuide === undefined) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ((polyGuide.values === null)) {
|
|
|
|
// We match all
|
|
|
|
return !polyGuide.blacklist
|
|
|
|
}
|
|
|
|
// is the key contained?
|
|
|
|
return polyGuide.values.has(tags[tagsKey])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
private static constructPolygonFeatures(): Map<string, { values: Set<string>, blacklist: boolean }> {
|
|
|
|
const result = new Map<string, { values: Set<string>, blacklist: boolean }>();
|
2021-05-06 03:03:54 +02:00
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
for (const polygonFeature of polygon_features) {
|
|
|
|
const key = polygonFeature.key;
|
2021-05-06 03:03:54 +02:00
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
if (polygonFeature.polygon === "all") {
|
|
|
|
result.set(key, {values: null, blacklist: false})
|
|
|
|
continue
|
|
|
|
}
|
2021-05-10 23:51:03 +02:00
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
const blacklist = polygonFeature.polygon === "blacklist"
|
|
|
|
result.set(key, {values: new Set<string>(polygonFeature.values), blacklist: blacklist})
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2021-04-21 01:23:28 +02:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2021-06-30 18:48:23 +02:00
|
|
|
return result;
|
2021-04-21 01:23:28 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 23:51:03 +02:00
|
|
|
private static ParseObjects(elements: any[]): OsmObject[] {
|
|
|
|
const objects: OsmObject[] = [];
|
|
|
|
const allNodes: Map<number, OsmNode> = new Map<number, OsmNode>()
|
|
|
|
for (const element of elements) {
|
|
|
|
const type = element.type;
|
|
|
|
const idN = element.id;
|
|
|
|
let osmObject: OsmObject = null
|
|
|
|
switch (type) {
|
|
|
|
case("node"):
|
|
|
|
const node = new OsmNode(idN);
|
|
|
|
allNodes.set(idN, node);
|
|
|
|
osmObject = node
|
|
|
|
node.SaveExtraData(element);
|
|
|
|
break;
|
|
|
|
case("way"):
|
|
|
|
osmObject = new OsmWay(idN);
|
|
|
|
const nodes = element.nodes.map(i => allNodes.get(i));
|
|
|
|
osmObject.SaveExtraData(element, nodes)
|
|
|
|
break;
|
|
|
|
case("relation"):
|
|
|
|
osmObject = new OsmRelation(idN);
|
|
|
|
osmObject.SaveExtraData(element, [])
|
|
|
|
break;
|
|
|
|
}
|
2021-05-27 18:55:12 +02:00
|
|
|
osmObject?.LoadData(element)
|
2021-05-10 23:51:03 +02:00
|
|
|
objects.push(osmObject)
|
|
|
|
}
|
|
|
|
return objects;
|
|
|
|
}
|
|
|
|
|
2021-05-06 01:33:09 +02:00
|
|
|
// The centerpoint of the feature, as [lat, lon]
|
|
|
|
public abstract centerpoint(): [number, number];
|
|
|
|
|
|
|
|
public abstract asGeoJson(): any;
|
|
|
|
|
|
|
|
abstract SaveExtraData(element: any, allElements: any[]);
|
2020-07-05 18:59:47 +02:00
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
/**
|
|
|
|
* Generates the changeset-XML for tags
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
TagsXML(): string {
|
|
|
|
let tags = "";
|
|
|
|
for (const key in this.tags) {
|
2021-05-10 23:51:03 +02:00
|
|
|
if (key.startsWith("_")) {
|
2021-05-09 18:56:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-10 23:51:03 +02:00
|
|
|
if (key === "id") {
|
2021-05-10 16:03:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
const v = this.tags[key];
|
|
|
|
if (v !== "") {
|
2020-09-30 22:22:58 +02:00
|
|
|
tags += ' <tag k="' + Utils.EncodeXmlValue(key) + '" v="' + Utils.EncodeXmlValue(this.tags[key]) + '"/>\n'
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
2021-04-21 01:23:28 +02:00
|
|
|
Download(continuation: ((element: OsmObject, meta: OsmObjectMeta) => void)) {
|
2020-06-24 00:35:19 +02:00
|
|
|
const self = this;
|
2021-05-06 01:33:09 +02:00
|
|
|
const full = this.type !== "way" ? "" : "/full";
|
2021-06-30 18:48:23 +02:00
|
|
|
const url = `${OsmObject.backendURL}api/0.6/${this.type}/${this.id}${full}`;
|
2021-07-03 22:24:12 +02:00
|
|
|
Utils.downloadJson(url).then(data => {
|
2021-05-10 23:51:03 +02:00
|
|
|
|
2021-05-07 01:43:32 +02:00
|
|
|
const element = data.elements.pop();
|
2021-05-06 03:03:54 +02:00
|
|
|
|
2021-05-07 01:43:32 +02:00
|
|
|
let nodes = []
|
2021-09-03 17:00:36 +02:00
|
|
|
if (self.type === "way" && data.elements.length >= 0) {
|
2021-05-07 01:43:32 +02:00
|
|
|
nodes = OsmObject.ParseObjects(data.elements)
|
|
|
|
}
|
2021-05-10 23:51:03 +02:00
|
|
|
|
2021-05-06 03:03:54 +02:00
|
|
|
self.LoadData(element)
|
2021-05-07 01:43:32 +02:00
|
|
|
self.SaveExtraData(element, nodes);
|
2021-07-10 15:52:52 +02:00
|
|
|
|
2021-05-10 23:51:03 +02:00
|
|
|
const meta = {
|
2021-04-21 01:23:28 +02:00
|
|
|
"_last_edit:contributor": element.user,
|
|
|
|
"_last_edit:contributor:uid": element.uid,
|
|
|
|
"_last_edit:changeset": element.changeset,
|
|
|
|
"_last_edit:timestamp": new Date(element.timestamp),
|
|
|
|
"_version_number": element.version
|
2021-05-10 23:51:03 +02:00
|
|
|
}
|
|
|
|
|
2021-07-10 15:52:52 +02:00
|
|
|
if (OsmObject.backendURL !== OsmObject.defaultBackend) {
|
|
|
|
self.tags["_backend"] = OsmObject.backendURL
|
|
|
|
meta["_backend"] = OsmObject.backendURL;
|
|
|
|
}
|
|
|
|
|
2021-05-10 23:51:03 +02:00
|
|
|
continuation(self, meta);
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
abstract ChangesetXML(changesetId: string): string;
|
2020-08-27 18:44:16 +02:00
|
|
|
|
2021-04-21 01:23:28 +02:00
|
|
|
protected VersionXML() {
|
|
|
|
if (this.version === undefined) {
|
|
|
|
return "";
|
2020-08-27 18:44:16 +02:00
|
|
|
}
|
2021-04-21 01:23:28 +02:00
|
|
|
return 'version="' + this.version + '"';
|
2020-08-27 18:44:16 +02:00
|
|
|
}
|
2021-05-06 03:03:54 +02:00
|
|
|
|
|
|
|
private LoadData(element: any): void {
|
|
|
|
this.tags = element.tags ?? this.tags;
|
|
|
|
this.version = element.version;
|
|
|
|
this.timestamp = element.timestamp;
|
|
|
|
const tgs = this.tags;
|
2021-05-10 23:51:03 +02:00
|
|
|
if (element.tags === undefined) {
|
2021-05-06 03:03:54 +02:00
|
|
|
// Simple node which is part of a way - not important
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tgs["_last_edit:contributor"] = element.user
|
|
|
|
tgs["_last_edit:contributor:uid"] = element.uid
|
|
|
|
tgs["_last_edit:changeset"] = element.changeset
|
|
|
|
tgs["_last_edit:timestamp"] = element.timestamp
|
|
|
|
tgs["_version_number"] = element.version
|
|
|
|
tgs["id"] = this.type + "/" + this.id;
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class OsmNode extends OsmObject {
|
|
|
|
|
|
|
|
lat: number;
|
|
|
|
lon: number;
|
|
|
|
|
2021-07-15 00:39:11 +02:00
|
|
|
constructor(id: number) {
|
2020-06-24 00:35:19 +02:00
|
|
|
super("node", id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ChangesetXML(changesetId: string): string {
|
|
|
|
let tags = this.TagsXML();
|
|
|
|
|
2021-07-15 00:26:25 +02:00
|
|
|
return ' <node id="' + this.id + '" changeset="' + changesetId + '" ' + this.VersionXML() + ' lat="' + this.lat + '" lon="' + this.lon + '">\n' +
|
2020-06-24 00:35:19 +02:00
|
|
|
tags +
|
2021-07-15 00:26:25 +02:00
|
|
|
' </node>\n';
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SaveExtraData(element) {
|
|
|
|
this.lat = element.lat;
|
|
|
|
this.lon = element.lon;
|
|
|
|
}
|
2021-05-06 01:33:09 +02:00
|
|
|
|
|
|
|
centerpoint(): [number, number] {
|
|
|
|
return [this.lat, this.lon];
|
|
|
|
}
|
|
|
|
|
|
|
|
asGeoJson() {
|
|
|
|
return {
|
|
|
|
"type": "Feature",
|
|
|
|
"properties": this.tags,
|
|
|
|
"geometry": {
|
|
|
|
"type": "Point",
|
|
|
|
"coordinates": [
|
|
|
|
this.lon,
|
|
|
|
this.lat
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 01:33:09 +02:00
|
|
|
export interface OsmObjectMeta {
|
2021-04-21 01:23:28 +02:00
|
|
|
"_last_edit:contributor": string,
|
|
|
|
"_last_edit:contributor:uid": number,
|
|
|
|
"_last_edit:changeset": number,
|
|
|
|
"_last_edit:timestamp": Date,
|
|
|
|
"_version_number": number
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
export class OsmWay extends OsmObject {
|
|
|
|
|
|
|
|
nodes: number[];
|
2021-05-06 01:33:09 +02:00
|
|
|
coordinates: [number, number][] = []
|
|
|
|
lat: number;
|
|
|
|
lon: number;
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2021-07-15 00:39:11 +02:00
|
|
|
constructor(id: number) {
|
2020-06-24 00:35:19 +02:00
|
|
|
super("way", id);
|
|
|
|
}
|
|
|
|
|
2021-05-06 01:33:09 +02:00
|
|
|
centerpoint(): [number, number] {
|
|
|
|
return [this.lat, this.lon];
|
|
|
|
}
|
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
ChangesetXML(changesetId: string): string {
|
|
|
|
let tags = this.TagsXML();
|
|
|
|
let nds = "";
|
|
|
|
for (const node in this.nodes) {
|
|
|
|
nds += ' <nd ref="' + this.nodes[node] + '"/>\n';
|
|
|
|
}
|
|
|
|
|
2021-05-06 01:33:09 +02:00
|
|
|
return ' <way id="' + this.id + '" changeset="' + changesetId + '" ' + this.VersionXML() + '>\n' +
|
2020-06-24 00:35:19 +02:00
|
|
|
nds +
|
|
|
|
tags +
|
2021-07-15 00:26:25 +02:00
|
|
|
' </way>\n';
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 03:03:54 +02:00
|
|
|
SaveExtraData(element, allNodes: OsmNode[]) {
|
|
|
|
|
|
|
|
let latSum = 0
|
|
|
|
let lonSum = 0
|
2021-05-06 01:33:09 +02:00
|
|
|
|
2021-05-27 18:55:12 +02:00
|
|
|
const nodeDict = new Map<number, OsmNode>()
|
2021-05-06 01:33:09 +02:00
|
|
|
for (const node of allNodes) {
|
2021-05-27 18:55:12 +02:00
|
|
|
nodeDict.set(node.id, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const nodeId of element.nodes) {
|
|
|
|
const node = nodeDict.get(nodeId)
|
2021-09-09 00:05:51 +02:00
|
|
|
if (node === undefined) {
|
2021-07-28 15:14:13 +02:00
|
|
|
console.error("Error: node ", nodeId, "not found in ", nodeDict)
|
2021-07-28 15:50:33 +02:00
|
|
|
// This is probably part of a relation which hasn't been fully downloaded
|
|
|
|
continue;
|
2021-07-28 15:14:13 +02:00
|
|
|
}
|
2021-05-06 03:03:54 +02:00
|
|
|
const cp = node.centerpoint();
|
|
|
|
this.coordinates.push(cp);
|
2021-09-03 17:00:36 +02:00
|
|
|
latSum += cp[0]
|
|
|
|
lonSum += cp[1]
|
2021-05-06 01:33:09 +02:00
|
|
|
}
|
|
|
|
let count = this.coordinates.length;
|
2021-05-06 03:03:54 +02:00
|
|
|
this.lat = latSum / count;
|
|
|
|
this.lon = lonSum / count;
|
2020-06-24 00:35:19 +02:00
|
|
|
this.nodes = element.nodes;
|
|
|
|
}
|
2021-05-06 01:33:09 +02:00
|
|
|
|
2021-07-14 15:28:02 +02:00
|
|
|
public asGeoJson() {
|
2021-05-06 01:33:09 +02:00
|
|
|
return {
|
|
|
|
"type": "Feature",
|
|
|
|
"properties": this.tags,
|
|
|
|
"geometry": {
|
2021-05-27 18:55:12 +02:00
|
|
|
"type": this.isPolygon() ? "Polygon" : "LineString",
|
2021-05-06 01:33:09 +02:00
|
|
|
"coordinates": this.coordinates.map(c => [c[1], c[0]])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 18:55:12 +02:00
|
|
|
|
|
|
|
private isPolygon(): boolean {
|
|
|
|
if (this.coordinates[0] !== this.coordinates[this.coordinates.length - 1]) {
|
|
|
|
return false; // Not closed
|
|
|
|
}
|
2021-05-31 20:47:08 +02:00
|
|
|
return OsmObject.isPolygon(this.tags)
|
2021-05-27 18:55:12 +02:00
|
|
|
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class OsmRelation extends OsmObject {
|
|
|
|
|
2021-07-15 20:47:28 +02:00
|
|
|
public members: {
|
|
|
|
type: "node" | "way" | "relation",
|
|
|
|
ref: number,
|
|
|
|
role: string
|
|
|
|
}[];
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2021-07-15 00:39:11 +02:00
|
|
|
constructor(id: number) {
|
2020-06-24 00:35:19 +02:00
|
|
|
super("relation", id);
|
|
|
|
}
|
|
|
|
|
2021-05-06 01:33:09 +02:00
|
|
|
centerpoint(): [number, number] {
|
|
|
|
return [0, 0]; // TODO
|
|
|
|
}
|
|
|
|
|
2020-06-24 00:35:19 +02:00
|
|
|
ChangesetXML(changesetId: string): string {
|
|
|
|
let members = "";
|
2021-05-06 01:33:09 +02:00
|
|
|
for (const member of this.members) {
|
2020-06-24 00:35:19 +02:00
|
|
|
members += ' <member type="' + member.type + '" ref="' + member.ref + '" role="' + member.role + '"/>\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
let tags = this.TagsXML();
|
2021-08-24 12:13:47 +02:00
|
|
|
let cs = ""
|
2021-09-09 00:05:51 +02:00
|
|
|
if (changesetId !== undefined) {
|
2021-08-24 12:13:47 +02:00
|
|
|
cs = `changeset="${changesetId}"`
|
|
|
|
}
|
|
|
|
return ` <relation id="${this.id}" ${cs} ${this.VersionXML()}>
|
|
|
|
${members}${tags} </relation>
|
|
|
|
`;
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveExtraData(element) {
|
|
|
|
this.members = element.members;
|
|
|
|
}
|
2021-05-06 01:33:09 +02:00
|
|
|
|
|
|
|
asGeoJson() {
|
|
|
|
throw "Not Implemented"
|
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|