From f223ed5e7aeff6f31427aebe03f507f3426b0b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Dukai?= Date: Fri, 18 Aug 2023 11:03:04 +0200 Subject: [PATCH] Fix extent initialization when there are only files, no dirs --- src/parser.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 5410f56..a810998 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -104,7 +104,14 @@ impl World { .collect(); let mut nr_features = 0; let mut nr_features_ignored = 0; - let mut extent_qc: BboxQc = BboxQc::default(); + let mut extent_qc = Self::extent_qc_init(&path_features_root, cityobject_types.as_ref()) + .unwrap_or_else(|| { + panic!( + "Did not find any CityJSONFeature of type {:?} in {}", + cityobject_types, + path_features_root.display() + ) + }); let mut cityobject_types_ignored: Vec = Vec::new(); for (i, extent) in extents.iter().enumerate() { nr_features += extent.nr_features; @@ -212,7 +219,10 @@ impl World { let mut features_enum_iter = WalkDir::new(&path_features) .into_iter() .filter_map(Self::jsonl_path); - // Init the extent with from the first feature of the requested types + // Init the extent with from the first feature of the requested types. + // We do not use extent_qc_init() here, because we need to collect the CityObject types + // and counts accurately, and we want to retain the position of the features_enum_iter + // for the full iteration after the first feature has been found. let mut extent_qc = BboxQc([0, 0, 0, 0, 0, 0]); let mut found_feature_type = false; let mut nr_features = 0; @@ -260,6 +270,29 @@ impl World { }) } + /// Initialize a [BboxQc] from the first feature of the correct type that is found in the + /// `path_features`. + fn extent_qc_init + std::fmt::Debug>( + path_features: P, + cityobject_types: Option<&Vec>, + ) -> Option { + let features_enum_iter = WalkDir::new(&path_features) + .into_iter() + .filter_map(Self::jsonl_path); + // Iterate only until the first feature is found + for feature_path in features_enum_iter { + if let Ok(cf) = CityJSONFeatureVertices::from_file(&feature_path) { + let extent_qc_op = cf.bbox_of_types(cityobject_types); + if extent_qc_op.is_some() { + return extent_qc_op; + } + } else { + warn!("Failed to parse {:?}", &feature_path) + } + } + None + } + fn extent_qc_file( cityobject_types: Option<&Vec>, extent_qc: &mut BboxQc,