Merge branch 'develop' into RobinLinde-patch-9

This commit is contained in:
Robin van der Linde 2023-03-28 11:48:05 +02:00
commit 24b7756c8d
Signed by untrusted user: Robin-van-der-Linde
GPG key ID: 53956B3252478F0D
6 changed files with 89 additions and 35 deletions

View file

@ -130,10 +130,14 @@ export default class GeoLocationHandler {
private CopyGeolocationIntoMapstate() { private CopyGeolocationIntoMapstate() {
const state = this._state const state = this._state
// For some weird reason, the 'Object.keys' method doesn't work for the 'location: GeolocationCoordinates'-object and will thus not copy all the properties when using {...location}
// As such, they are copied here
const keysToCopy = ["speed", "accuracy", "altitude", "altitudeAccuracy", "heading"]
this.geolocationState.currentGPSLocation.addCallbackAndRun((location) => { this.geolocationState.currentGPSLocation.addCallbackAndRun((location) => {
if (location === undefined) { if (location === undefined) {
return return
} }
const feature = { const feature = {
type: "Feature", type: "Feature",
properties: <GeoLocationPointProperties>{ properties: <GeoLocationPointProperties>{
@ -147,6 +151,11 @@ export default class GeoLocationHandler {
coordinates: [location.longitude, location.latitude], coordinates: [location.longitude, location.latitude],
}, },
} }
for (const key of keysToCopy) {
if (location[key] !== null) {
feature.properties[key] = location[key]
}
}
state.currentUserLocation?.features?.setData([{ feature, freshness: new Date() }]) state.currentUserLocation?.features?.setData([{ feature, freshness: new Date() }])
}) })

View file

@ -38,7 +38,7 @@ export class ElementStorage {
return es return es
} }
getEventSourceById(elementId): UIEventSource<any> { getEventSourceById(elementId): UIEventSource<any> | undefined {
if (elementId === undefined) { if (elementId === undefined) {
return undefined return undefined
} }

View file

@ -163,6 +163,11 @@ export abstract class OsmObject {
}) })
} }
public static DownloadHistory(id: NodeId): UIEventSource<OsmNode[]>
public static DownloadHistory(id: WayId): UIEventSource<OsmWay[]>
public static DownloadHistory(id: RelationId): UIEventSource<OsmRelation[]>
public static DownloadHistory(id: OsmId): UIEventSource<OsmObject[]>
public static DownloadHistory(id: string): UIEventSource<OsmObject[]> { public static DownloadHistory(id: string): UIEventSource<OsmObject[]> {
if (OsmObject.historyCache.has(id)) { if (OsmObject.historyCache.has(id)) {
return OsmObject.historyCache.get(id) return OsmObject.historyCache.get(id)
@ -180,6 +185,7 @@ export abstract class OsmObject {
const osmObjects: OsmObject[] = [] const osmObjects: OsmObject[] = []
for (const element of elements) { for (const element of elements) {
let osmObject: OsmObject = null let osmObject: OsmObject = null
element.nodes = []
switch (type) { switch (type) {
case "node": case "node":
osmObject = new OsmNode(idN) osmObject = new OsmNode(idN)

View file

@ -80,20 +80,25 @@ export class ReferencingWaysMetaTagger extends SimpleMetaTagger {
if (!id.startsWith("node/")) { if (!id.startsWith("node/")) {
return false return false
} }
console.trace("Downloading referencing ways for", feature.properties.id)
OsmObject.DownloadReferencingWays(id).then((referencingWays) => { const currentTagsSource = state.allElements?.getEventSourceById(id)
const currentTagsSource = state.allElements?.getEventSourceById(id) ?? [] if (currentTagsSource === undefined) {
return
}
Utils.AddLazyPropertyAsync(
currentTagsSource.data,
"_referencing_ways",
async () => {
const referencingWays = await OsmObject.DownloadReferencingWays(id)
const wayIds = referencingWays.map((w) => "way/" + w.id) const wayIds = referencingWays.map((w) => "way/" + w.id)
wayIds.sort() wayIds.sort()
const wayIdsStr = wayIds.join(";") const wayIdsStr = wayIds.join(";")
if ( if (wayIdsStr !== "" && currentTagsSource.data[""] !== wayIdsStr) {
wayIdsStr !== "" &&
currentTagsSource.data["_referencing_ways"] !== wayIdsStr
) {
currentTagsSource.data["_referencing_ways"] = wayIdsStr currentTagsSource.data["_referencing_ways"] = wayIdsStr
currentTagsSource.ping() currentTagsSource.ping()
} }
}) }
)
return true return true
} }
@ -282,16 +287,9 @@ export default class SimpleMetaTaggers {
}, },
}) })
Object.defineProperty(feature.properties, "_surface:ha", { Utils.AddLazyProperty(feature.properties, "_surface:ha", () => {
enumerable: false,
configurable: true,
get: () => {
const sqMeters = GeoOperations.surfaceAreaInSqMeters(feature) const sqMeters = GeoOperations.surfaceAreaInSqMeters(feature)
const sqMetersHa = "" + Math.floor(sqMeters / 1000) / 10 return "" + Math.floor(sqMeters / 1000) / 10
delete feature.properties["_surface:ha"]
feature.properties["_surface:ha"] = sqMetersHa
return sqMetersHa
},
}) })
return true return true
@ -443,8 +441,6 @@ export default class SimpleMetaTaggers {
} }
}, },
}) })
const tagsSource = state.allElements.getEventSourceById(feature.properties.id)
} }
) )
private static directionSimplified = new SimpleMetaTagger( private static directionSimplified = new SimpleMetaTagger(

View file

@ -300,6 +300,49 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
return str.substr(0, l - 3) + "..." return str.substr(0, l - 3) + "..."
} }
/**
* Adds a property to the given object, but the value will _only_ be calculated when it is actually requested
* @param object
* @param name
* @param init
* @constructor
*/
public static AddLazyProperty(object: any, name: string, init: () => any) {
Object.defineProperty(object, name, {
enumerable: false,
configurable: true,
get: () => {
delete object[name]
object[name] = init()
return object[name]
},
})
}
/**
* Adds a property to the given object, but the value will _only_ be calculated when it is actually requested
*/
public static AddLazyPropertyAsync(
object: any,
name: string,
init: () => Promise<any>,
whenDone?: () => void
) {
Object.defineProperty(object, name, {
enumerable: false,
configurable: true,
get: () => {
init().then((r) => {
delete object[name]
object[name] = r
if (whenDone) {
whenDone()
}
})
},
})
}
public static FixedLength(str: string, l: number) { public static FixedLength(str: string, l: number) {
str = Utils.EllipsesAfter(str, l) str = Utils.EllipsesAfter(str, l)
while (str.length < l) { while (str.length < l) {
@ -1281,13 +1324,6 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
return d return d
} }
private static colorDiff(
c0: { r: number; g: number; b: number },
c1: { r: number; g: number; b: number }
) {
return Math.abs(c0.r - c1.r) + Math.abs(c0.g - c1.g) + Math.abs(c0.b - c1.b)
}
static toIdRecord<T extends { id: string }>(ts: T[]): Record<string, T> { static toIdRecord<T extends { id: string }>(ts: T[]): Record<string, T> {
const result: Record<string, T> = {} const result: Record<string, T> = {}
for (const t of ts) { for (const t of ts) {
@ -1317,4 +1353,11 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
// If the element has a parent, repeat the process for the parent element // If the element has a parent, repeat the process for the parent element
return Utils.findParentWithScrolling(element.parentElement) return Utils.findParentWithScrolling(element.parentElement)
} }
private static colorDiff(
c0: { r: number; g: number; b: number },
c1: { r: number; g: number; b: number }
) {
return Math.abs(c0.r - c1.r) + Math.abs(c0.g - c1.g) + Math.abs(c0.b - c1.b)
}
} }

View file

@ -1,6 +1,6 @@
{ {
"id": "gps_location", "id": "gps_location",
"description": "Meta layer showing the current location of the user. Add this to your theme and override the icon to change the appearance of the current location. The object will always have `id=gps` and will have _all_ the properties included in the [`Coordinates`-object](https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates) returned by the browser.", "description": "Meta layer showing the current location of the user. Add this to your theme and override the icon to change the appearance of the current location. The object will always have `id=gps` and will have _all_ the properties included in the [`Coordinates`-object](https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates) (except latitude and longitude) returned by the browser, such as `speed`, `altitude`, `heading`, ....",
"minzoom": 0, "minzoom": 0,
"source": { "source": {
"osmTags": "id=gps", "osmTags": "id=gps",