mapcomplete/Logic/Osm/Changes.ts

229 lines
7.7 KiB
TypeScript
Raw Normal View History

2020-06-23 22:35:19 +00:00
/**
* Handles all changes made to OSM.
* Needs an authenticator via OsmConnection
*/
import {UIEventSource} from "../UIEventSource";
2020-06-23 22:35:19 +00:00
import {OsmConnection} from "./OsmConnection";
import {OsmNode, OsmObject} from "./OsmObject";
2020-07-29 22:59:08 +00:00
import {And, Tag, TagsFilter} from "../TagsFilter";
import {ElementStorage} from "../ElementStorage";
import {State} from "../../State";
2020-07-31 02:58:58 +00:00
import {Utils} from "../../Utils";
2020-06-23 22:35:19 +00:00
export class Changes {
private static _nextId = -1; // New assined ID's are negative
2020-07-05 16:59:47 +00:00
addTag(elementId: string, tagsFilter : TagsFilter){
if(tagsFilter instanceof Tag){
const tag = tagsFilter as Tag;
2020-08-22 23:49:19 +00:00
if(typeof tag.value !== "string"){
throw "Invalid value"
}
2020-07-05 16:59:47 +00:00
this.addChange(elementId, tag.key, tag.value);
return;
}
if(tagsFilter instanceof And){
const and = tagsFilter as And;
for (const tag of and.and) {
this.addTag(elementId, tag);
}
return;
}
console.log("Unsupported tagsfilter element to addTag", tagsFilter);
throw "Unsupported tagsFilter element";
}
2020-06-23 22:35:19 +00:00
/**
* Adds a change to the pending changes
* @param elementId
* @param key
* @param value
*/
addChange(elementId: string, key: string, value: string) {
if (key === undefined || key === null) {
console.log("Invalid key");
return;
}
if (value === undefined || value === null) {
console.log("Invalid value for ",key);
return;
}
2020-08-06 17:42:10 +00:00
if(key.startsWith(" ") || value.startsWith(" ") || value.endsWith(" ") || key.endsWith(" ")){
console.warn("Tag starts with or ends with a space - trimming anyway")
}
2020-08-06 17:42:10 +00:00
key = key.trim();
value = value.trim();
2020-06-23 22:35:19 +00:00
const eventSource = State.state.allElements.getElement(elementId);
2020-06-23 22:35:19 +00:00
eventSource.data[key] = value;
eventSource.ping();
this.uploadAll([], [{elementId: eventSource.data.id, key: key, value: value}]);
2020-06-23 22:35:19 +00:00
}
/**
* Create a new node element at the given lat/long.
* An internal OsmObject is created to upload later on, a geojson represention is returned.
* Note that the geojson version shares the tags (properties) by pointer, but has _no_ id in properties
*/
createElement(basicTags:Tag[], lat: number, lon: number) {
console.log("Creating a new element with ", basicTags)
2020-06-23 22:35:19 +00:00
const osmNode = new OsmNode(Changes._nextId);
Changes._nextId--;
const id = "node/" + osmNode.id;
osmNode.lat = lat;
osmNode.lon = lon;
const properties = {id: id};
const geojson = {
"type": "Feature",
"properties": properties,
"id": id,
"geometry": {
"type": "Point",
"coordinates": [
lon,
lat
]
}
}
// The basictags are COPIED, the id is included in the properties
// The tags are not yet written into the OsmObject, but this is applied onto a
const changes = [];
2020-06-23 22:35:19 +00:00
for (const kv of basicTags) {
properties[kv.key] = kv.value;
if (typeof kv.value !== "string") {
throw "Invalid value: don't use a regex in a preset"
2020-08-22 23:49:19 +00:00
}
changes.push({elementId: id, key: kv.key, value: kv.value})
2020-06-23 22:35:19 +00:00
}
State.state.allElements.addOrGetElement(geojson).ping();
this.uploadAll([osmNode], changes);
2020-06-23 22:35:19 +00:00
return geojson;
}
private uploadAll(
newElements: OsmObject[],
pending: { elementId: string; key: string; value: string }[]
) {
2020-06-23 22:35:19 +00:00
const self = this;
const knownElements = {}; // maps string --> OsmObject
function DownloadAndContinue(neededIds, continuation: (() => void)) {
// local function which downloads all the objects one by one
// this is one big loop, running one download, then rerunning the entire function
if (neededIds.length == 0) {
continuation();
return;
}
const neededId = neededIds.pop();
if (neededId in knownElements) {
DownloadAndContinue(neededIds, continuation);
return;
}
console.log("Downloading ", neededId);
OsmObject.DownloadObject(neededId,
function (element) {
knownElements[neededId] = element; // assign the element for later, continue downloading the next element
DownloadAndContinue(neededIds, continuation);
}
);
}
const neededIds = [];
for (const change of pending) {
const id = change.elementId;
if (parseFloat(id.split("/")[1]) < 0) {
// New element - we don't have to download this
2020-06-23 22:35:19 +00:00
} else {
neededIds.push(id);
}
}
DownloadAndContinue(neededIds, function () {
// Here, inside the continuation, we know that all 'neededIds' are loaded in 'knownElements'
// We apply the changes on them
for (const change of pending) {
if (parseInt(change.elementId.split("/")[1]) < 0) {
// This is a new element - we should apply this on one of the new elements
for (const newElement of newElements) {
if (newElement.type + "/" + newElement.id === change.elementId) {
newElement.addTag(change.key, change.value);
}
}
} else {
knownElements[change.elementId].addTag(change.key, change.value);
}
}
// Small sanity check for duplicate information
let changedElements = [];
for (const elementId in knownElements) {
const element = knownElements[elementId];
if (element.changed) {
changedElements.push(element);
}
}
if (changedElements.length == 0 && newElements.length == 0) {
console.log("No changes in any object");
return;
}
console.log("Beginning upload...");
// At last, we build the changeset and upload
State.state.osmConnection.UploadChangeset(
2020-06-23 22:35:19 +00:00
function (csId) {
let modifications = "";
for (const element of changedElements) {
if (!element.changed) {
continue;
}
modifications += element.ChangesetXML(csId) + "\n";
}
let creations = "";
for (const newElement of newElements) {
creations += newElement.ChangesetXML(csId);
}
2020-08-06 22:45:33 +00:00
let changes = `<osmChange version='0.6' generator='Mapcomplete ${State.vNumber}'>`;
2020-06-23 22:35:19 +00:00
if (creations.length > 0) {
changes +=
"<create>" +
creations +
"</create>";
}
if (modifications.length > 0) {
changes +=
"<modify>" +
modifications +
"</modify>";
}
changes += "</osmChange>";
return changes;
},
() => {
});
2020-07-31 02:58:58 +00:00
});
}
2020-06-23 22:35:19 +00:00
}