From e4072fdaac0d6e0e43dcaed205fe110ac7c09297 Mon Sep 17 00:00:00 2001 From: Gabriel Bruno Date: Thu, 23 Jan 2025 17:48:29 -0500 Subject: [PATCH] Add option to getGeojsonsFromRawData() Modifies the function getGeojsonsFromRawData() so that it accepts a new option parameter, continueOnMissingGeojson. When generateNodesIfNotFound is false and continueOnMissingGeojson true, the function will just go to the next geojson and print a warning if a feature that is in the osm raw data is not in the geojson data. Previously, the only option was to throw an error and interrupt the process. --- .../OsmDataPreparationNonResidential.ts | 21 +++++++++++++------ .../OsmDataPreparationResidential.ts | 3 ++- .../data/__tests__/osmGeojsonService.test.ts | 2 +- .../dataImport/data/osmGeojsonService.ts | 13 +++++++++--- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/chaire-lib-common/src/tasks/dataImport/OsmDataPreparationNonResidential.ts b/packages/chaire-lib-common/src/tasks/dataImport/OsmDataPreparationNonResidential.ts index 83af42a0a..d1a2b8d89 100644 --- a/packages/chaire-lib-common/src/tasks/dataImport/OsmDataPreparationNonResidential.ts +++ b/packages/chaire-lib-common/src/tasks/dataImport/OsmDataPreparationNonResidential.ts @@ -156,12 +156,15 @@ export default class OsmDataPreparationNonResidential { const allPoIBuildings: PoiBuilding[] = osmGeojsonService.getGeojsonsFromRawData( this._osmGeojsonData, allOsmBuildings, - { generateNodesIfNotFound: true } + { generateNodesIfNotFound: true, continueOnMissingGeojson: false } ); const allBuildingPartsRaw = this._osmRawData.queryOr(queryBuildingPartsFromOsm); const allBuildingParts: SingleGeoFeature[] = osmGeojsonService - .getGeojsonsFromRawData(this._osmGeojsonData, allBuildingPartsRaw, { generateNodesIfNotFound: true }) + .getGeojsonsFromRawData(this._osmGeojsonData, allBuildingPartsRaw, { + generateNodesIfNotFound: true, + continueOnMissingGeojson: false + }) .map((part) => part.geojson); console.log('=== Map shop and main entrances to each building... ==='); @@ -176,7 +179,10 @@ export default class OsmDataPreparationNonResidential { entrances.length === 0 ? undefined : osmGeojsonService - .getGeojsonsFromRawData(this._osmGeojsonData, entrances, { generateNodesIfNotFound: true }) + .getGeojsonsFromRawData(this._osmGeojsonData, entrances, { + generateNodesIfNotFound: true, + continueOnMissingGeojson: true + }) .map((entrance) => entrance.geojson as GeoJSON.Feature); // Get building parts building.parts = findOverlappingFeatures(building.geojson, allBuildingParts); @@ -197,7 +203,8 @@ export default class OsmDataPreparationNonResidential { .filter((poi) => getCategoryFromProperty(poi.tags || {}).length !== 0); const allOsmPoisGeojson = osmGeojsonService .getGeojsonsFromRawData(this._osmGeojsonData, allOsmPoisFeatures, { - generateNodesIfNotFound: true + generateNodesIfNotFound: true, + continueOnMissingGeojson: true }) .map((poi) => poi.geojson); @@ -279,7 +286,8 @@ export default class OsmDataPreparationNonResidential { } return toPoi( osmGeojsonService.getGeojsonsFromRawData(this._osmGeojsonData, [entrances[0]], { - generateNodesIfNotFound: true + generateNodesIfNotFound: true, + continueOnMissingGeojson: false })[0].geojson.geometry as GeoJSON.Point, poi, { @@ -365,7 +373,8 @@ export default class OsmDataPreparationNonResidential { } return toPoi( osmGeojsonService.getGeojsonsFromRawData(this._osmGeojsonData, [entrances[0]], { - generateNodesIfNotFound: true + generateNodesIfNotFound: true, + continueOnMissingGeojson: false })[0].geojson.geometry as GeoJSON.Point, poi, { diff --git a/packages/chaire-lib-common/src/tasks/dataImport/OsmDataPreparationResidential.ts b/packages/chaire-lib-common/src/tasks/dataImport/OsmDataPreparationResidential.ts index f65e97e4f..da5a8b3e6 100644 --- a/packages/chaire-lib-common/src/tasks/dataImport/OsmDataPreparationResidential.ts +++ b/packages/chaire-lib-common/src/tasks/dataImport/OsmDataPreparationResidential.ts @@ -73,7 +73,8 @@ export default class OsmDataPreparationResidential { const allOsmResidentialBuildings = osmRawData.queryOr(queryResidentialBuildingsFromOsm); const residentialBuildings = osmGeojsonService.getGeojsonsFromRawData( osmGeojsonData, - allOsmResidentialBuildings + allOsmResidentialBuildings, + { generateNodesIfNotFound: false, continueOnMissingGeojson: true } ); // For each building, get its entrances diff --git a/packages/chaire-lib-common/src/tasks/dataImport/data/__tests__/osmGeojsonService.test.ts b/packages/chaire-lib-common/src/tasks/dataImport/data/__tests__/osmGeojsonService.test.ts index edf53bb71..be689e63f 100644 --- a/packages/chaire-lib-common/src/tasks/dataImport/data/__tests__/osmGeojsonService.test.ts +++ b/packages/chaire-lib-common/src/tasks/dataImport/data/__tests__/osmGeojsonService.test.ts @@ -83,7 +83,7 @@ describe('getGeojsonsFromRawData', () => { { type: 'node' as const, id: '1234', lon: -73, lat: 45 }, { type: 'node' as const, id: '2345', lon: -73.1, lat: 45.1, tags: { test: ['foo'], abc: ['foo', 'bar'] } } ] - expect(osmGeojsonService.getGeojsonsFromRawData(geojsonData, rawData, { generateNodesIfNotFound: true })).toEqual([ + expect(osmGeojsonService.getGeojsonsFromRawData(geojsonData, rawData, { generateNodesIfNotFound: true, continueOnMissingGeojson: false })).toEqual([ { geojson: { diff --git a/packages/chaire-lib-common/src/tasks/dataImport/data/osmGeojsonService.ts b/packages/chaire-lib-common/src/tasks/dataImport/data/osmGeojsonService.ts index b8480b6ac..97b5841aa 100644 --- a/packages/chaire-lib-common/src/tasks/dataImport/data/osmGeojsonService.ts +++ b/packages/chaire-lib-common/src/tasks/dataImport/data/osmGeojsonService.ts @@ -278,7 +278,10 @@ const unsplitTags = (tags: { [key: string]: string[] | undefined }): { [key: str const getGeojsonsFromRawData = ( geojsonData: DataGeojson, features: OsmRawDataType[], - options: { generateNodesIfNotFound: boolean } = { generateNodesIfNotFound: false } + options: { generateNodesIfNotFound: boolean; continueOnMissingGeojson: boolean } = { + generateNodesIfNotFound: false, + continueOnMissingGeojson: false + } ): { geojson: SingleGeoFeature; raw: OsmRawDataType }[] => { const geojsonFeatures: { geojson: SingleGeoFeature; raw: OsmRawDataType }[] = []; for (let i = 0; i < features.length; i++) { @@ -298,13 +301,17 @@ const getGeojsonsFromRawData = ( features[i].type, features[i].id ); - throw 'Missing OSM geojson'; + if (options.continueOnMissingGeojson) { + continue; + } else { + throw 'Missing OSM geojson'; + } } } else if (geojson.geometry.type === 'GeometryCollection') { console.log('Building ' + geojson.id + ' is of unsupported type GeometryCollection'); throw 'Unsupported geometry type for building'; } - geojsonFeatures[i] = { geojson: geojson as SingleGeoFeature, raw: features[i] }; + geojsonFeatures.push({ geojson: geojson as SingleGeoFeature, raw: features[i] }); } return geojsonFeatures; };