From b68bf7b95029a3161b5471daebf12997c767349f Mon Sep 17 00:00:00 2001 From: Arno Deceuninck Date: Thu, 15 Jul 2021 09:34:00 +0200 Subject: [PATCH] Download cached features as geojson --- InitUiElements.ts | 2 ++ State.ts | 2 ++ UI/BigComponents/LayerSelection.ts | 5 +++++ UI/GeoJsonExport.ts | 15 +++++++++++++++ Utils.ts | 8 ++++++++ 5 files changed, 32 insertions(+) create mode 100644 UI/GeoJsonExport.ts diff --git a/InitUiElements.ts b/InitUiElements.ts index 0f1143eba7..97dd2b02c4 100644 --- a/InitUiElements.ts +++ b/InitUiElements.ts @@ -427,6 +427,8 @@ export class InitUiElements { state.locationControl, state.selectedElement); + State.state.featurePipeline = source; + new ShowDataLayer(source.features, State.state.leafletMap, State.state.layoutToUse); const selectedFeatureHandler = new SelectedFeatureHandler(Hash.hash, State.state.selectedElement, source, State.state.osmApiFeatureSource); diff --git a/State.ts b/State.ts index 8e4322d654..7793485aa3 100644 --- a/State.ts +++ b/State.ts @@ -19,6 +19,7 @@ import TitleHandler from "./Logic/Actors/TitleHandler"; import PendingChangesUploader from "./Logic/Actors/PendingChangesUploader"; import {Relation} from "./Logic/Osm/ExtractRelations"; import OsmApiFeatureSource from "./Logic/FeatureSource/OsmApiFeatureSource"; +import FeaturePipeline from "./Logic/FeatureSource/FeaturePipeline"; /** * Contains the global state: a bunch of UI-event sources @@ -95,6 +96,7 @@ export default class State { public readonly featureSwitchIsDebugging: UIEventSource; public readonly featureSwitchShowAllQuestions: UIEventSource; public readonly featureSwitchApiURL: UIEventSource; + public readonly featurePipeline: FeaturePipeline; /** diff --git a/UI/BigComponents/LayerSelection.ts b/UI/BigComponents/LayerSelection.ts index e282947096..11237fa329 100644 --- a/UI/BigComponents/LayerSelection.ts +++ b/UI/BigComponents/LayerSelection.ts @@ -7,6 +7,8 @@ import Translations from "../i18n/Translations"; import LayerConfig from "../../Customizations/JSON/LayerConfig"; import BaseUIElement from "../BaseUIElement"; import {Translation} from "../i18n/Translation"; +import {SubtleButton} from "../Base/SubtleButton"; +import {exportAsGeoJson} from "../GeoJsonExport"; /** * Shows the panel with all layers and a toggle for each of them @@ -74,6 +76,9 @@ export default class LayerSelection extends Combine { ); } + const downloadButton = new SubtleButton("./assets/svg/floppy.svg", "Download visible data as geojson") + downloadButton.onClick(() => exportAsGeoJson(State.state.featurePipeline)) // TODO: Define this + checkboxes.push(downloadButton) super(checkboxes) this.SetStyle("display:flex;flex-direction:column;") diff --git a/UI/GeoJsonExport.ts b/UI/GeoJsonExport.ts new file mode 100644 index 0000000000..c18baed17e --- /dev/null +++ b/UI/GeoJsonExport.ts @@ -0,0 +1,15 @@ +import FeaturePipeline from "../Logic/FeatureSource/FeaturePipeline"; +import {Utils} from "../Utils"; + +export function exportAsGeoJson(featurePipeline: FeaturePipeline, options?: {metadata?: boolean}) { + let defaults = { + metadata: false + } + options = Utils.setDefaults(options, defaults); + // Select all features, ignore the freshness and other data + // TODO: Remove mapcomplete metadata (starting with underscore) + let featureList: JSON[] = featurePipeline? featurePipeline.features.data.map((feature) => feature.feature) : ["I'm empty"]; + let geojson = {type: "FeatureCollection", features: featureList} + + Utils.offerContentsAsDownloadableFile(JSON.stringify(geojson), "Geodata.json"); +} diff --git a/Utils.ts b/Utils.ts index 05ebcbaabc..418f1d1111 100644 --- a/Utils.ts +++ b/Utils.ts @@ -447,6 +447,14 @@ export class Utils { b: parseInt(hex.substr(5, 2), 16), } } + + public static setDefaults(options, defaults){ + let result = {}; + for (let key of defaults){ + result[key] = key in options ? options[key] : defaults[key]; + } + return result; + } } export interface TileRange {