Merge upload GPX-tracks to OSM; split 'specialVisualisations' into multiple smaller classes

This commit is contained in:
Pieter Vander Vennet 2022-10-28 04:33:05 +02:00
commit 8d304f9a56
37 changed files with 1459 additions and 1057 deletions

View file

@ -1,20 +1,10 @@
import * as turf from "@turf/turf"
import { BBox } from "./BBox"
import togpx from "togpx"
import Constants from "../Models/Constants"
import {BBox} from "./BBox"
import LayerConfig from "../Models/ThemeConfig/LayerConfig"
import {
AllGeoJSON,
booleanWithin,
Coord,
Feature,
Geometry,
Lines,
MultiPolygon,
Polygon,
Properties,
} from "@turf/turf"
import { GeoJSON, LineString, Point } from "geojson"
import * as turf from "@turf/turf"
import {AllGeoJSON, booleanWithin, Coord, Feature, Geometry, MultiPolygon, Polygon,} from "@turf/turf"
import {LineString, Point} from "geojson"
import togpx from "togpx"
import Constants from "../Models/Constants";
export class GeoOperations {
private static readonly _earthRadius = 6378137
@ -393,21 +383,22 @@ export class GeoOperations {
.features.map((p) => <[number, number]>p.geometry.coordinates)
}
public static AsGpx(feature, generatedWithLayer?: LayerConfig) {
const metadata = {}
public static AsGpx(feature: Feature, options?: {layer?: LayerConfig, gpxMetadata?: any }) : string{
const metadata = options?.gpxMetadata ?? {}
metadata["time"] = metadata["time"] ?? new Date().toISOString()
const tags = feature.properties
if (generatedWithLayer !== undefined) {
metadata["name"] = generatedWithLayer.title?.GetRenderValue(tags)?.Subs(tags)?.txt
metadata["desc"] = "Generated with MapComplete layer " + generatedWithLayer.id
if (options?.layer !== undefined) {
metadata["name"] = options?.layer.title?.GetRenderValue(tags)?.Subs(tags)?.txt
metadata["desc"] = "Generated with MapComplete layer " + options?.layer.id
if (tags._backend?.contains("openstreetmap")) {
metadata["copyright"] =
"Data copyrighted by OpenStreetMap-contributors, freely available under ODbL. See https://www.openstreetmap.org/copyright"
metadata["author"] = tags["_last_edit:contributor"]
metadata["link"] = "https://www.openstreetmap.org/" + tags.id
metadata["time"] = tags["_last_edit:timestamp"]
} else {
metadata["time"] = new Date().toISOString()
}
}

View file

@ -27,10 +27,12 @@ export default class UserDetails {
export class OsmConnection {
public static readonly oauth_configs = {
osm: {
oauth_consumer_key: "hivV7ec2o49Two8g9h8Is1VIiVOgxQ1iYexCbvem",
oauth_secret: "wDBRTCem0vxD7txrg1y6p5r8nvmz8tAhET7zDASI",
url: "https://www.openstreetmap.org",
"osm": {
oauth_consumer_key: 'hivV7ec2o49Two8g9h8Is1VIiVOgxQ1iYexCbvem',
oauth_secret: 'wDBRTCem0vxD7txrg1y6p5r8nvmz8tAhET7zDASI',
url: "https://www.openstreetmap.org"
// OAUTH 1.0 application
// https://www.openstreetmap.org/user/Pieter%20Vander%20Vennet/oauth_clients/7404
},
"osm-test": {
oauth_consumer_key: "Zgr7EoKb93uwPv2EOFkIlf3n9NLwj5wbyfjZMhz2",
@ -333,6 +335,80 @@ export class OsmConnection {
})
}
public async uploadGpxTrack(gpx: string, options: {
description: string,
visibility: "private" | "public" | "trackable" | "identifiable",
filename?: string
/**
* Some words to give some properties;
*
* Note: these are called 'tags' on the wiki, but I opted to name them 'labels' instead as they aren't "key=value" tags, but just words.
*/
labels: string[]
}): Promise<{ id: number }> {
if (this._dryRun.data) {
console.warn("Dryrun enabled - not actually uploading GPX ", gpx)
return new Promise<{ id: number }>((ok, error) => {
window.setTimeout(() => ok({id: Math.floor(Math.random() * 1000)}), Math.random() * 5000)
});
}
const contents = {
"file": gpx,
"description": options.description ?? "",
"tags": options.labels?.join(",") ?? "",
"visibility": options.visibility
}
const extras = {
"file": "; filename=\""+(options.filename ?? ("gpx_track_mapcomplete_"+(new Date().toISOString())))+"\"\r\nContent-Type: application/gpx+xml"
}
const auth = this.auth;
const boundary ="987654"
let body = ""
for (const key in contents) {
body += "--" + boundary + "\r\n"
body += "Content-Disposition: form-data; name=\"" + key + "\""
if(extras[key] !== undefined){
body += extras[key]
}
body += "\r\n\r\n"
body += contents[key] + "\r\n"
}
body += "--" + boundary + "--\r\n"
return new Promise((ok, error) => {
auth.xhr({
method: 'POST',
path: `/api/0.6/gpx/create`,
options: {
header:
{
"Content-Type": "multipart/form-data; boundary=" + boundary,
"Content-Length": body.length
}
},
content: body
}, function (
err,
response: string) {
console.log("RESPONSE IS", response)
if (err !== null) {
error(err)
} else {
const parsed = JSON.parse(response)
console.log("Uploaded GPX track", parsed)
ok({id: parsed})
}
})
})
}
public addCommentToNote(id: number | string, text: string): Promise<void> {
if (this._dryRun.data) {
console.warn("Dryrun enabled - not actually adding comment ", text, "to note ", id)

View file

@ -68,7 +68,7 @@ export default class MapState extends UserRelatedState {
public currentUserLocation: SimpleFeatureSource
/**
* All previously visited points
* All previously visited points, with their metadata
*/
public historicalUserLocations: SimpleFeatureSource
/**
@ -79,6 +79,11 @@ export default class MapState extends UserRelatedState {
7 * 24 * 60 * 60,
"gps_location_retention"
)
/**
* A featureSource containing a single linestring which has the GPS-history of the user.
* However, metadata (such as when every single point was visited) is lost here (but is kept in `historicalUserLocations`.
* Note that this featureSource is _derived_ from 'historicalUserLocations'
*/
public historicalUserLocationsTrack: FeatureSourceForLayer & Tiled
/**