import type { Feature, Geometry } from "geojson"; import type { GeoJsonExport, LineFeature, MarkerFeature, SearchResult } from "facilmap-types"; import { flattenObject } from "facilmap-utils"; import { getI18n } from "./i18n"; type FmFeatureProperties = Partial | Partial; type FeatureProperties = FmFeatureProperties & { tags?: Record; // Tags for OSM objects type?: string; id?: string; } export type FileResult = SearchResult & { isFileResult: true; fmTypeId?: number; fmProperties?: FmFeatureProperties; } export interface FileResultObject { features: FileResult[]; views: GeoJsonExport["facilmap"]["views"]; types: GeoJsonExport["facilmap"]["types"]; errors: boolean; } async function fileToGeoJSON(file: string): Promise { if(file.match(/^\s* { const filesGeoJSON = await Promise.all(files.map(fileToGeoJSON)); const ret: FileResultObject = { features: [ ], views: [ ], types: { }, errors: false }; let nextTypeIdx = 1; for (const geojson of filesGeoJSON) { if(geojson == null) { ret.errors = true; continue; } const typeMapping: Record = {}; if (geojson.facilmap) { if (geojson.facilmap.types) { for (const i of Object.keys(geojson.facilmap.types)) { typeMapping[i] = nextTypeIdx++; ret.types[typeMapping[i]] = geojson.facilmap.types[i]; } } if(geojson.facilmap.views) ret.views.push(...geojson.facilmap.views); } let features: Feature[]; if(geojson.type == "FeatureCollection") features = geojson.features || [ ]; else if(geojson.type == "Feature") features = [ geojson ]; else features = [ { type: "Feature", geometry: geojson, properties: { } } ]; for (const feature of features) { let name; if(typeof feature.properties != "object") feature.properties = { }; if(feature.properties.name) name = feature.properties.name; else if(feature.properties.tags && feature.properties.tags.name) name = feature.properties.tags.name; else if(feature.properties.type) name = feature.properties.type + " " + feature.properties.id; else if([ "Polygon", "MultiPolygon" ].indexOf(feature.geometry.type) != -1) name = "Polygon"; else if([ "LineString", "MultiLineString" ].indexOf(feature.geometry.type) != -1) name = "Line"; else if([ "Point", "MultiPoint" ].indexOf(feature.geometry.type) != -1) name = "Point"; else name = feature.geometry.type || "Object"; const extraTags = feature.properties.data || feature.properties.tags || flattenObject(Object.assign({}, feature.properties, {coordTimes: null})); let f: FileResult = { isFileResult: true, short_name: name, display_name: name, extratags: Object.fromEntries(Object.entries(extraTags).map(([k, v]) => [k, `${v}`])), geojson: feature.geometry, type: feature.properties.type || feature.geometry.type }; if(geojson.facilmap) { if(feature.properties.typeId && typeMapping[feature.properties.typeId]) f.fmTypeId = typeMapping[feature.properties.typeId]; f.fmProperties = feature.properties; } ret.features.push(f); } } return ret; }