diff --git a/src/geodata.cpp b/src/geodata.cpp index 0941345..d98ecbd 100644 --- a/src/geodata.cpp +++ b/src/geodata.cpp @@ -145,7 +145,15 @@ Vector3 GeoFeatureLayer::get_center() { } // Utility function for converting a Processing Library Feature to the appropriate GeoFeature -Ref get_specialized_feature(std::shared_ptr raw_feature) { +Ref GeoFeatureLayer::get_specialized_feature(std::shared_ptr raw_feature) { + if (feature_cache.count(raw_feature)) { + return feature_cache[raw_feature]; + } + + // Not cached -> instantiate and cache + + Ref new_feature; + // Check which geometry this feature has, and cast it to the according // specialized class if (raw_feature->geometry_type == raw_feature->POINT) { @@ -156,7 +164,7 @@ Ref get_specialized_feature(std::shared_ptr raw_feature) { point->set_gdal_feature(point_feature); - return point; + new_feature = point; } else if (raw_feature->geometry_type == raw_feature->LINE) { Ref line; line.instantiate(); @@ -165,7 +173,7 @@ Ref get_specialized_feature(std::shared_ptr raw_feature) { line->set_gdal_feature(line_feature); - return line; + new_feature = line; } else if (raw_feature->geometry_type == raw_feature->POLYGON) { Ref polygon; polygon.instantiate(); @@ -174,7 +182,7 @@ Ref get_specialized_feature(std::shared_ptr raw_feature) { polygon->set_gdal_feature(polygon_feature); - return polygon; + new_feature = polygon; } else { // Geometry type is NONE or unknown Ref feature; @@ -182,8 +190,12 @@ Ref get_specialized_feature(std::shared_ptr raw_feature) { feature->set_gdal_feature(raw_feature); - return feature; + new_feature = feature; } + + feature_cache[raw_feature] = new_feature; + + return new_feature; } Ref GeoFeatureLayer::get_feature_by_id(int id) { diff --git a/src/geodata.h b/src/geodata.h index 2c7acda..d479072 100644 --- a/src/geodata.h +++ b/src/geodata.h @@ -83,6 +83,9 @@ class EXPORT GeoFeatureLayer : public RefCounted { /// Note that syntax errors are printed to the console by GDAL. Array get_features_by_attribute_filter(String filter); + /// Returns the (cached) specialized feature of the given raw feature + Ref get_specialized_feature(std::shared_ptr raw_feature); + /// Set the OGRLayer object directly. /// Not exposed to Godot since Godot doesn't know about GDALDatasets - this /// is only for internal use. @@ -96,6 +99,7 @@ class EXPORT GeoFeatureLayer : public RefCounted { private: std::shared_ptr layer; + std::map, Ref> feature_cache; Ref origin_dataset; ExtentData extent_data; };