| 
									
										
										
										
											2021-07-16 01:42:09 +02:00
										 |  |  | import {SubtleButton} from "../Base/SubtleButton"; | 
					
						
							|  |  |  | import Svg from "../../Svg"; | 
					
						
							|  |  |  | import Translations from "../i18n/Translations"; | 
					
						
							|  |  |  | import State from "../../State"; | 
					
						
							|  |  |  | import {Utils} from "../../Utils"; | 
					
						
							|  |  |  | import Combine from "../Base/Combine"; | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  | import CheckBoxes from "../Input/Checkboxes"; | 
					
						
							| 
									
										
										
										
											2021-09-28 17:30:48 +02:00
										 |  |  | import {GeoOperations} from "../../Logic/GeoOperations"; | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  | import Toggle from "../Input/Toggle"; | 
					
						
							|  |  |  | import Title from "../Base/Title"; | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | import FeaturePipeline from "../../Logic/FeatureSource/FeaturePipeline"; | 
					
						
							|  |  |  | import {UIEventSource} from "../../Logic/UIEventSource"; | 
					
						
							|  |  |  | import SimpleMetaTagger from "../../Logic/SimpleMetaTagger"; | 
					
						
							|  |  |  | import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"; | 
					
						
							| 
									
										
										
										
											2021-09-28 17:30:48 +02:00
										 |  |  | import {BBox} from "../../Logic/BBox"; | 
					
						
							| 
									
										
										
										
											2022-02-16 01:34:28 +01:00
										 |  |  | import FilteredLayer, {FilterState} from "../../Models/FilteredLayer"; | 
					
						
							| 
									
										
										
										
											2021-07-16 01:42:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-26 21:03:27 +02:00
										 |  |  | export class DownloadPanel extends Toggle { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-16 01:34:28 +01:00
										 |  |  |     constructor(state: { | 
					
						
							|  |  |  |         filteredLayers: UIEventSource<FilteredLayer[]> | 
					
						
							|  |  |  |         featurePipeline: FeaturePipeline, | 
					
						
							|  |  |  |         layoutToUse: LayoutConfig, | 
					
						
							|  |  |  |         currentBounds: UIEventSource<BBox> | 
					
						
							|  |  |  |     }) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-16 01:42:09 +02:00
										 |  |  |         const t = Translations.t.general.download | 
					
						
							| 
									
										
										
										
											2021-09-28 18:00:44 +02:00
										 |  |  |         const name = State.state.layoutToUse.id; | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  |         const includeMetaToggle = new CheckBoxes([t.includeMetaData.Clone()]) | 
					
						
							|  |  |  |         const metaisIncluded = includeMetaToggle.GetValue().map(selected => selected.length > 0) | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-26 21:03:27 +02:00
										 |  |  |         const buttonGeoJson = new SubtleButton(Svg.floppy_ui(), | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  |             new Combine([t.downloadGeojson.Clone().SetClass("font-bold"), | 
					
						
							| 
									
										
										
										
											2021-07-26 21:03:27 +02:00
										 |  |  |                 t.downloadGeoJsonHelper.Clone()]).SetClass("flex flex-col")) | 
					
						
							| 
									
										
										
										
											2022-02-16 01:34:28 +01:00
										 |  |  |             .OnClickWithLoading(t.exporting,async () => { | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 const geojson = DownloadPanel.getCleanGeoJson(state, metaisIncluded.data) | 
					
						
							|  |  |  |                 Utils.offerContentsAsDownloadableFile(JSON.stringify(geojson, null, "  "), | 
					
						
							| 
									
										
										
										
											2021-07-26 21:03:27 +02:00
										 |  |  |                     `MapComplete_${name}_export_${new Date().toISOString().substr(0, 19)}.geojson`, { | 
					
						
							|  |  |  |                         mimetype: "application/vnd.geo+json" | 
					
						
							|  |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2021-07-16 01:42:09 +02:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-26 21:03:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const buttonCSV = new SubtleButton(Svg.floppy_ui(), new Combine( | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  |             [t.downloadCSV.Clone().SetClass("font-bold"), | 
					
						
							| 
									
										
										
										
											2021-07-26 21:03:27 +02:00
										 |  |  |                 t.downloadCSVHelper.Clone()]).SetClass("flex flex-col")) | 
					
						
							| 
									
										
										
										
											2022-02-16 01:34:28 +01:00
										 |  |  |             .OnClickWithLoading(t.exporting, async () => { | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 const geojson = DownloadPanel.getCleanGeoJson(state, metaisIncluded.data) | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  |                 const csv = GeoOperations.toCSV(geojson.features) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 Utils.offerContentsAsDownloadableFile(csv, | 
					
						
							| 
									
										
										
										
											2021-07-26 21:03:27 +02:00
										 |  |  |                     `MapComplete_${name}_export_${new Date().toISOString().substr(0, 19)}.csv`, { | 
					
						
							|  |  |  |                         mimetype: "text/csv" | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  |                     }); | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  |         const downloadButtons = new Combine( | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |             [new Title(t.title), | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 buttonGeoJson, | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 buttonCSV, | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 includeMetaToggle, | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 t.licenseInfo.Clone().SetClass("link-underline")]) | 
					
						
							| 
									
										
										
										
											2021-07-26 21:03:27 +02:00
										 |  |  |             .SetClass("w-full flex flex-col border-4 border-gray-300 rounded-3xl p-4") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-26 20:59:55 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             downloadButtons, | 
					
						
							|  |  |  |             t.noDataLoaded.Clone(), | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |             state.featurePipeline.somethingLoaded) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static getCleanGeoJson(state: { | 
					
						
							|  |  |  |         featurePipeline: FeaturePipeline, | 
					
						
							| 
									
										
										
										
											2022-02-16 01:34:28 +01:00
										 |  |  |         currentBounds: UIEventSource<BBox>, | 
					
						
							|  |  |  |         filteredLayers: UIEventSource<FilteredLayer[]> | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |     }, includeMetaData: boolean) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const resultFeatures = [] | 
					
						
							| 
									
										
										
										
											2022-02-16 01:34:28 +01:00
										 |  |  |         const neededLayers = state.filteredLayers.data.map(l => l.layerDef.id) | 
					
						
							|  |  |  |         const bbox = state.currentBounds.data | 
					
						
							|  |  |  |         const featureList = state.featurePipeline.GetAllFeaturesAndMetaWithin(bbox, new Set(neededLayers)); | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         for (const tile of featureList) { | 
					
						
							| 
									
										
										
										
											2022-02-16 01:34:28 +01:00
										 |  |  |             const layer = state.filteredLayers.data.find(fl => fl.layerDef.id === tile.layer) | 
					
						
							|  |  |  |             const filters = layer.appliedFilters.data | 
					
						
							|  |  |  |             for (const feature of tile.features) { | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  |                 if(!bbox.overlapsWith(BBox.get(feature))){ | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (filters !== undefined) { | 
					
						
							|  |  |  |                     let featureDoesMatchAllFilters = true; | 
					
						
							|  |  |  |                     for (let key of Array.from(filters.keys())) { | 
					
						
							|  |  |  |                         const filter: FilterState = filters.get(key) | 
					
						
							|  |  |  |                         if(filter?.currentFilter === undefined){ | 
					
						
							|  |  |  |                             continue | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         if (!filter.currentFilter.matchesProperties(feature.properties)) { | 
					
						
							|  |  |  |                             featureDoesMatchAllFilters = false; | 
					
						
							|  |  |  |                             break | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     if(!featureDoesMatchAllFilters){ | 
					
						
							|  |  |  |                         continue; // the outer loop
 | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |                 const cleaned = { | 
					
						
							|  |  |  |                     type: feature.type, | 
					
						
							|  |  |  |                     geometry: feature.geometry, | 
					
						
							|  |  |  |                     properties: {...feature.properties} | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (!includeMetaData) { | 
					
						
							|  |  |  |                     for (const key in cleaned.properties) { | 
					
						
							|  |  |  |                         if (key === "_lon" || key === "_lat") { | 
					
						
							|  |  |  |                             continue; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         if (key.startsWith("_")) { | 
					
						
							|  |  |  |                             delete feature.properties[key] | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const datedKeys = [].concat(SimpleMetaTagger.metatags.filter(tagging => tagging.includesDates).map(tagging => tagging.keys)) | 
					
						
							|  |  |  |                 for (const key of datedKeys) { | 
					
						
							|  |  |  |                     delete feature.properties[key] | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 resultFeatures.push(feature) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             type: "FeatureCollection", | 
					
						
							| 
									
										
										
										
											2021-09-21 02:19:31 +02:00
										 |  |  |             features: resultFeatures | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-16 01:42:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |