Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geojson optimization #444

Merged
merged 16 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions include/geojson/FeatureBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ namespace geojson {
for (auto &coordinate : group.second) {
latLon.push_back(std::stod(coordinate.second.data()));
}
shape_coordinates.push_back(latLon);
shape_coordinates.push_back(std::move(latLon));
}
total_coordinates.push_back(shape_coordinates);

total_coordinates.push_back(std::move(shape_coordinates));
}

polygon_t polygon;
Expand Down Expand Up @@ -172,14 +171,12 @@ namespace geojson {
for(auto &coordinate : point.second) {
latLon.push_back(std::stod(coordinate.second.data()));
}

coordinate_group.push_back(latLon);
coordinate_group.push_back(std::move(latLon));
}

polygon_coordinates.push_back(coordinate_group);
polygon_coordinates.push_back(std::move(coordinate_group));
}

coordinates_groups.push_back(polygon_coordinates);
coordinates_groups.push_back(std::move(polygon_coordinates));
}

bg::model::multi_polygon<polygon_t> multi_polygon;
Expand All @@ -190,15 +187,15 @@ namespace geojson {
for (auto &point : polygon_points) {
bg::append(polygon, coordinate_t(point[0], point[1]));
}
multi_polygon.push_back(polygon);
multi_polygon.push_back(std::move(polygon));
}
}

return multi_polygon;
}

static geometry build_geometry(boost::property_tree::ptree &tree) {
std::string type = tree.get<std::string>("type");
std::string type = std::move(tree.get<std::string>("type"));

if (type == "Point") {

Expand Down Expand Up @@ -241,7 +238,7 @@ namespace geojson {

for (auto& child : tree) {
if (child.first == "geometry") {
std::string geometry_type = child.second.get<std::string>("type");
const std::string& geometry_type = child.second.get<std::string>("type");
geometry_object = build_geometry(child.second);
has_geometry = true;

Expand Down Expand Up @@ -277,7 +274,7 @@ namespace geojson {
}
}
else if (child.first == "id") {
id = child.second.data();
id = std::move(child.second.data());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that std::move might end up ripping stuff off off child here. It's hard to tell here without having example docs in front of me, but it looks like there's a chance that these std::move operations might modify the tree and bubble up the updates, harming reuse. There might need to be some comments highlighting this so it's easier to identify while skimming.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once processed and recorded does the state of that node in the ptree really matter? The reference in the tree should still be valid if I understand the semantics of move correctly, but even if it isn't does it matter in this context?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I'm not sure - if the ptree isn't used afterwards, no one cares, but std::move stands to "replace" values (I'm assuming arrays?) when moving. Strings, for example, stand to be converted to empty strings after movement (new location still gets the correct value, though). This specific line isn't a good example; I just picked one of the moves to put a comment about moves. I think the biggest risk is when moving stuff into vectors. I'm not the expert, so I can easily be missing 90% of what's going on. That's sort of the reason I suggested adding comments instead of actually changing code. If someone comes along further down the line (most likely a goober like me) and expects the state of the tree to remain the same they might be in for a rude awakening.

std::move was a really good idea, so I'm not disputing its use.

}
else if (child.first == "bbox") {
for (auto &value : tree.get_child("bbox")) {
Expand All @@ -286,11 +283,11 @@ namespace geojson {
}
else if (child.first == "properties") {
for (auto& property : child.second) {
properties.emplace(property.first, JSONProperty(property.first, property.second));
properties.emplace(property.first, std::move(JSONProperty(property.first, property.second)));
}
}
else {
foreign_members.emplace(child.first, JSONProperty(child.first, child.second));
foreign_members.emplace(child.first, std::move(JSONProperty(child.first, child.second)));
}
}

Expand Down Expand Up @@ -374,7 +371,7 @@ namespace geojson {
* @param tree boost::property_tree::ptree holding the parsed GeoJSON
* @param ids optional subset of string feature ids, only features in tree with these ids will be in the collection
*/
static GeoJSON build_collection(const boost::property_tree::ptree tree, const std::vector<std::string> ids={}) {
static GeoJSON build_collection(const boost::property_tree::ptree& tree, const std::vector<std::string>& ids={}) {
std::vector<double> bbox_values;
std::vector<Feature> features;
PropertyMap foreign_members;
Expand All @@ -384,7 +381,7 @@ namespace geojson {
if (child.first == "bbox") {
std::vector<std::string> bounding_box;
for (auto &feature : tree.get_child("bbox")) {
bounding_box.push_back(feature.second.data());
bounding_box.push_back(std::move(feature.second.data()));
}

for (int point_index = 0; point_index < bounding_box.size(); point_index++) {
Expand All @@ -402,7 +399,7 @@ namespace geojson {
//optional id, but the input files set id under the 'property' key, so when a feature is constructed
//feature->get_id() returns '' because the feature itself doesn't have an id, so we hae to read
//from the associated properties to find the id. If the inputs change, we will need to adjust
//this line to read feature->get_id()
//this line to read feature->get_id()
if( tmp_id == "" ) {
try {
tmp_id = feature->get_property("id").as_string();
Expand All @@ -418,20 +415,22 @@ namespace geojson {
//or feature id was found in the provided ids vector
//so hold the feature to add to collection later

features.push_back(feature);
features.push_back(std::move(feature));
}
feature_tree.second.erase("child"); //we are done with this feature, drop it from the ptree. Not convinced this releases any resources, though =(
}
}
else {
std::cout << "No features were found" << std::endl;
}
}
else {
foreign_members.emplace(child.first, JSONProperty(child.first, child.second));
foreign_members.emplace(child.first, std::move(JSONProperty(child.first, child.second)));

}

}

GeoJSON collection = std::make_shared<FeatureCollection>(FeatureCollection(features, bbox_values));
GeoJSON collection = std::make_shared<FeatureCollection>(FeatureCollection(std::move(features), std::move(bbox_values)));

for (Feature feature : features) {
if (feature->get_id() != "") {
Expand Down
14 changes: 12 additions & 2 deletions include/geojson/FeatureCollection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ namespace geojson {
}
}

/**
* Move Constructor
*
* @param new_features An overall list of features
* @param bounding_box A set of bounds for all features
*/
FeatureCollection(FeatureList &&new_features, std::vector<double> &&bounding_box) :features(new_features), bounding_box(bounding_box)
{
}

FeatureCollection() {}

/**
Expand Down Expand Up @@ -60,10 +70,10 @@ namespace geojson {
{
idset.insert(*it);
}
for (Feature feature : feature_collection) {
for (const Feature& feature : feature_collection) {
auto id = feature->get_id();
if (idset.count(id)) {
features.push_back(feature);
features.push_back(std::move(feature));
}
}
this->update_ids();
Expand Down
Loading