forked from MapComplete/MapComplete
Huge refactorings of JSON-parsing and Tagsfilter, other cleanups, warning cleanups and lots of small subtle bugfixes
This commit is contained in:
parent
9a5b35b9f3
commit
a57b7d93fa
113 changed files with 1565 additions and 2594 deletions
|
@ -2,11 +2,8 @@
|
|||
* Handles all changes made to OSM.
|
||||
* Needs an authenticator via OsmConnection
|
||||
*/
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {OsmConnection} from "./OsmConnection";
|
||||
import {OsmNode, OsmObject} from "./OsmObject";
|
||||
import {And, Tag, TagsFilter} from "../TagsFilter";
|
||||
import {ElementStorage} from "../ElementStorage";
|
||||
import {And, Tag, TagsFilter} from "../Tags";
|
||||
import {State} from "../../State";
|
||||
import {Utils} from "../../Utils";
|
||||
|
||||
|
@ -163,6 +160,8 @@ export class Changes {
|
|||
console.log("Beginning upload...");
|
||||
// At last, we build the changeset and upload
|
||||
State.state.osmConnection.UploadChangeset(
|
||||
State.state.layoutToUse.data,
|
||||
State.state.allElements,
|
||||
function (csId) {
|
||||
|
||||
let modifications = "";
|
||||
|
@ -190,11 +189,10 @@ export class Changes {
|
|||
}
|
||||
|
||||
if (modifications.length > 0) {
|
||||
|
||||
changes +=
|
||||
"<modify>" +
|
||||
"<modify>\n" +
|
||||
modifications +
|
||||
"</modify>";
|
||||
"\n</modify>";
|
||||
}
|
||||
|
||||
changes += "</osmChange>";
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import {State} from "../../State";
|
||||
import {OsmConnection, UserDetails} from "./OsmConnection";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {ElementStorage} from "../ElementStorage";
|
||||
import {Layout} from "../../Customizations/Layout";
|
||||
import {State} from "../../State";
|
||||
|
||||
export class ChangesetHandler {
|
||||
|
||||
|
@ -22,11 +24,14 @@ export class ChangesetHandler {
|
|||
}
|
||||
|
||||
|
||||
public UploadChangeset(generateChangeXML: (csid: string) => string,
|
||||
continuation: () => void) {
|
||||
public UploadChangeset(
|
||||
layout: Layout,
|
||||
allElements: ElementStorage,
|
||||
generateChangeXML: (csid: string) => string,
|
||||
continuation: () => void) {
|
||||
|
||||
if (this._dryRun) {
|
||||
var changesetXML = generateChangeXML("123456");
|
||||
const changesetXML = generateChangeXML("123456");
|
||||
console.log(changesetXML);
|
||||
continuation();
|
||||
return;
|
||||
|
@ -34,14 +39,14 @@ export class ChangesetHandler {
|
|||
|
||||
const self = this;
|
||||
|
||||
|
||||
if (this.currentChangeset.data === undefined || this.currentChangeset.data === "") {
|
||||
// We have to open a new changeset
|
||||
this.OpenChangeset((csId) => {
|
||||
this.OpenChangeset(layout,(csId) => {
|
||||
this.currentChangeset.setData(csId);
|
||||
const changeset = generateChangeXML(csId);
|
||||
console.log(changeset);
|
||||
self.AddChange(csId, changeset,
|
||||
allElements,
|
||||
() => {
|
||||
},
|
||||
(e) => {
|
||||
|
@ -55,6 +60,7 @@ export class ChangesetHandler {
|
|||
self.AddChange(
|
||||
csId,
|
||||
generateChangeXML(csId),
|
||||
allElements,
|
||||
() => {
|
||||
},
|
||||
(e) => {
|
||||
|
@ -62,7 +68,7 @@ export class ChangesetHandler {
|
|||
// Mark the CS as closed...
|
||||
this.currentChangeset.setData("");
|
||||
// ... and try again. As the cs is closed, no recursive loop can exist
|
||||
self.UploadChangeset(generateChangeXML, continuation);
|
||||
self.UploadChangeset(layout, allElements, generateChangeXML, continuation);
|
||||
|
||||
}
|
||||
)
|
||||
|
@ -71,9 +77,10 @@ export class ChangesetHandler {
|
|||
}
|
||||
|
||||
|
||||
private OpenChangeset(continuation: (changesetId: string) => void) {
|
||||
private OpenChangeset(
|
||||
layout : Layout,
|
||||
continuation: (changesetId: string) => void) {
|
||||
|
||||
const layout = State.state.layoutToUse.data;
|
||||
const commentExtra = layout.changesetMessage !== undefined? " - "+layout.changesetMessage : "";
|
||||
this.auth.xhr({
|
||||
method: 'PUT',
|
||||
|
@ -81,8 +88,8 @@ export class ChangesetHandler {
|
|||
options: {header: {'Content-Type': 'text/xml'}},
|
||||
content: [`<osm><changeset>`,
|
||||
`<tag k="created_by" v="MapComplete ${State.vNumber}" />`,
|
||||
`<tag k="comment" v="Adding data with #MapComplete for theme #${layout.name}${commentExtra}"/>`,
|
||||
`<tag k="theme" v="${layout.name}"/>`,
|
||||
`<tag k="comment" v="Adding data with #MapComplete for theme #${layout.id}${commentExtra}"/>`,
|
||||
`<tag k="theme" v="${layout.id}"/>`,
|
||||
layout.maintainer !== undefined ? `<tag k="theme-creator" v="${layout.maintainer}"/>` : "",
|
||||
`</changeset></osm>`].join("")
|
||||
}, function (err, response) {
|
||||
|
@ -98,6 +105,7 @@ export class ChangesetHandler {
|
|||
|
||||
private AddChange(changesetId: string,
|
||||
changesetXML: string,
|
||||
allElements: ElementStorage,
|
||||
continuation: ((changesetId: string, idMapping: any) => void),
|
||||
onFail: ((changesetId: string) => void) = undefined) {
|
||||
this.auth.xhr({
|
||||
|
@ -113,7 +121,7 @@ export class ChangesetHandler {
|
|||
}
|
||||
return;
|
||||
}
|
||||
const mapping = ChangesetHandler.parseUploadChangesetResponse(response);
|
||||
const mapping = ChangesetHandler.parseUploadChangesetResponse(response, allElements);
|
||||
console.log("Uploaded changeset ", changesetId);
|
||||
continuation(changesetId, mapping);
|
||||
});
|
||||
|
@ -145,7 +153,7 @@ export class ChangesetHandler {
|
|||
});
|
||||
}
|
||||
|
||||
public static parseUploadChangesetResponse(response: XMLDocument) {
|
||||
private static parseUploadChangesetResponse(response: XMLDocument, allElements: ElementStorage) {
|
||||
const nodes = response.getElementsByTagName("node");
|
||||
// @ts-ignore
|
||||
for (const node of nodes) {
|
||||
|
@ -157,9 +165,9 @@ export class ChangesetHandler {
|
|||
continue;
|
||||
}
|
||||
console.log("Rewriting id: ", oldId, "-->", newId);
|
||||
const element = State.state.allElements.getElement("node/" + oldId);
|
||||
const element = allElements.getElement("node/" + oldId);
|
||||
element.data.id = "node/" + newId;
|
||||
State.state.allElements.addElementById("node/" + newId, element);
|
||||
allElements.addElementById("node/" + newId, element);
|
||||
element.ping();
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
// @ts-ignore
|
||||
import osmAuth from "osm-auth";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {State} from "../../State";
|
||||
import {All} from "../../Customizations/Layouts/All";
|
||||
import {OsmPreferences} from "./OsmPreferences";
|
||||
import {ChangesetHandler} from "./ChangesetHandler";
|
||||
import {Layout} from "../../Customizations/Layout";
|
||||
import {ElementStorage} from "../ElementStorage";
|
||||
|
||||
export class UserDetails {
|
||||
|
||||
|
@ -32,8 +32,7 @@ export class OsmConnection {
|
|||
constructor(dryRun: boolean, oauth_token: UIEventSource<string>,
|
||||
// Used to keep multiple changesets open and to write to the correct changeset
|
||||
layoutName: string,
|
||||
singlePage: boolean = true,
|
||||
useDevServer:boolean = false) {
|
||||
singlePage: boolean = true) {
|
||||
|
||||
let pwaStandAloneMode = false;
|
||||
try {
|
||||
|
@ -55,7 +54,6 @@ export class OsmConnection {
|
|||
oauth_secret: 'wDBRTCem0vxD7txrg1y6p5r8nvmz8tAhET7zDASI',
|
||||
singlepage: false,
|
||||
auto: true,
|
||||
url: useDevServer ? "https://master.apis.dev.openstreetmap.org" : undefined
|
||||
});
|
||||
} else {
|
||||
|
||||
|
@ -65,7 +63,6 @@ export class OsmConnection {
|
|||
singlepage: true,
|
||||
landing: window.location.href,
|
||||
auto: true,
|
||||
url: useDevServer ? "https://master.apis.dev.openstreetmap.org" : undefined
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -97,9 +94,12 @@ export class OsmConnection {
|
|||
}
|
||||
|
||||
|
||||
public UploadChangeset(generateChangeXML: (csid: string) => string,
|
||||
public UploadChangeset(
|
||||
layout: Layout,
|
||||
allElements: ElementStorage,
|
||||
generateChangeXML: (csid: string) => string,
|
||||
continuation: () => void = () => {}) {
|
||||
this.changesetHandler.UploadChangeset(generateChangeXML, continuation);
|
||||
this.changesetHandler.UploadChangeset(layout, allElements, generateChangeXML, continuation);
|
||||
}
|
||||
|
||||
public GetPreference(key: string, prefix: string = "mapcomplete-"): UIEventSource<string> {
|
||||
|
@ -168,11 +168,12 @@ export class OsmConnection {
|
|||
data.unreadMessages = parseInt(messages.getAttribute("unread"));
|
||||
data.totalMessages = parseInt(messages.getAttribute("count"));
|
||||
|
||||
self.userDetails.ping();
|
||||
for (const action of self._onLoggedIn) {
|
||||
action(self.userDetails.data);
|
||||
}
|
||||
self._onLoggedIn = [];
|
||||
|
||||
self.userDetails.ping();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,18 +1,16 @@
|
|||
/**
|
||||
* Helps in uplaoding, by generating the rigth title, decription and by adding the tag to the changeset
|
||||
*/
|
||||
import {Changes} from "./Changes";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {ImageUploadFlow} from "../../UI/ImageUploadFlow";
|
||||
import {UserDetails} from "./OsmConnection";
|
||||
import {SlideShow} from "../../UI/SlideShow";
|
||||
import {State} from "../../State";
|
||||
import {Tag} from "../TagsFilter";
|
||||
import {Tag} from "../Tags";
|
||||
|
||||
export class OsmImageUploadHandler {
|
||||
private _tags: UIEventSource<any>;
|
||||
private _slideShow: SlideShow;
|
||||
private _preferedLicense: UIEventSource<string>;
|
||||
private readonly _tags: UIEventSource<any>;
|
||||
private readonly _slideShow: SlideShow;
|
||||
private readonly _preferedLicense: UIEventSource<string>;
|
||||
|
||||
constructor(tags: UIEventSource<any>,
|
||||
preferedLicense: UIEventSource<string>,
|
||||
|
|
|
@ -18,13 +18,21 @@ export abstract class OsmObject {
|
|||
const splitted = id.split("/");
|
||||
const type = splitted[0];
|
||||
const idN = splitted[1];
|
||||
|
||||
const newContinuation = (element: OsmObject) => {
|
||||
|
||||
console.log("Received: ",element);
|
||||
|
||||
continuation(element);
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case("node"):
|
||||
return new OsmNode(idN).Download(continuation);
|
||||
return new OsmNode(idN).Download(newContinuation);
|
||||
case("way"):
|
||||
return new OsmWay(idN).Download(continuation);
|
||||
return new OsmWay(idN).Download(newContinuation);
|
||||
case("relation"):
|
||||
return new OsmRelation(idN).Download(continuation);
|
||||
return new OsmRelation(idN).Download(newContinuation);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -38,11 +46,9 @@ export abstract class OsmObject {
|
|||
* @param string
|
||||
* @constructor
|
||||
*/
|
||||
private Escape(string: string) {
|
||||
while (string.indexOf('"') >= 0) {
|
||||
string = string.replace('"', '"');
|
||||
}
|
||||
return string;
|
||||
private static Escape(string: string) {
|
||||
return string.replace(/"/g, '"')
|
||||
.replace(/&/g, "&");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -54,7 +60,7 @@ export abstract class OsmObject {
|
|||
for (const key in this.tags) {
|
||||
const v = this.tags[key];
|
||||
if (v !== "") {
|
||||
tags += ' <tag k="' + this.Escape(key) + '" v="' + this.Escape(this.tags[key]) + '"/>\n'
|
||||
tags += ' <tag k="' + OsmObject.Escape(key) + '" v="' + OsmObject.Escape(this.tags[key]) + '"/>\n'
|
||||
}
|
||||
}
|
||||
return tags;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import {UIEventSource} from "../UIEventSource";
|
||||
import {OsmConnection, UserDetails} from "./OsmConnection";
|
||||
import {All} from "../../Customizations/Layouts/All";
|
||||
import {Utils} from "../../Utils";
|
||||
|
||||
export class OsmPreferences {
|
||||
|
@ -68,8 +67,6 @@ export class OsmPreferences {
|
|||
}
|
||||
if (l > 25) {
|
||||
throw "Length to long";
|
||||
source.setData(undefined);
|
||||
return;
|
||||
}
|
||||
const prefsCount = Number(l);
|
||||
let str = "";
|
||||
|
@ -154,7 +151,7 @@ export class OsmPreferences {
|
|||
method: 'DELETE',
|
||||
path: '/api/0.6/user/preferences/' + k,
|
||||
options: {header: {'Content-Type': 'text/plain'}},
|
||||
}, function (error, result) {
|
||||
}, function (error) {
|
||||
if (error) {
|
||||
console.log("Could not remove preference", error);
|
||||
return;
|
||||
|
@ -172,7 +169,7 @@ export class OsmPreferences {
|
|||
path: '/api/0.6/user/preferences/' + k,
|
||||
options: {header: {'Content-Type': 'text/plain'}},
|
||||
content: v
|
||||
}, function (error, result) {
|
||||
}, function (error) {
|
||||
if (error) {
|
||||
console.log(`Could not set preference "${k}"'`, error);
|
||||
return;
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
/**
|
||||
* Interfaces overpass to get all the latest data
|
||||
*/
|
||||
import {Bounds} from "../Bounds";
|
||||
import {TagsFilter} from "../TagsFilter";
|
||||
import {TagsFilter} from "../Tags";
|
||||
import $ from "jquery"
|
||||
import * as OsmToGeoJson from "osmtogeojson";
|
||||
|
||||
/**
|
||||
* Interfaces overpass to get all the latest data
|
||||
*/
|
||||
export class Overpass {
|
||||
private _filter: TagsFilter
|
||||
public static testUrl: string = null
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue