Skip to content

Commit

Permalink
Fix bug of GeoJSONWriter related array in properties
Browse files Browse the repository at this point in the history
Signed-off-by: Kunlin Yu <[email protected]>
  • Loading branch information
kunlinyu committed Jan 2, 2025
1 parent 9702340 commit adb4af8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
30 changes: 24 additions & 6 deletions src/io/GeoJSONWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,33 @@ void GeoJSONWriter::encodeGeoJSONValue(const std::string& key, const GeoJSONValu
}
}
else if (value.isArray()) {
j[key] = json::array();
for (const GeoJSONValue& v : value.getArray()) {
encodeGeoJSONValue("", v, j[key]);
if (j.is_object()) {
j[key] = json::array();
for (const GeoJSONValue& v : value.getArray()) {
encodeGeoJSONValue("", v, j[key]);
}
}
else {
json sub_array = json::array();
for (const GeoJSONValue& v : value.getArray()) {
encodeGeoJSONValue("", v, sub_array);
}
j.push_back(sub_array);
}
}
else if (value.isObject()) {
j[key] = json::object();
for (const auto& entry : value.getObject()) {
encodeGeoJSONValue(entry.first, entry.second, j[key]);
if (j.is_object()) {
j[key] = json::object();
for (const auto& entry : value.getObject()) {
encodeGeoJSONValue(entry.first, entry.second, j[key]);
}
}
else {
json sub_obj = json::object();
for (const auto& entry : value.getObject()) {
encodeGeoJSONValue(entry.first, entry.second, sub_obj);
}
j.push_back(sub_obj);
}
}
}
Expand Down
30 changes: 12 additions & 18 deletions tests/unit/io/GeoJSONWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,12 @@ void object::test<29>
geos::io::GeoJSONValue row1(std::vector<geos::io::GeoJSONValue>({1.0, 2.0, 3.0}));
geos::io::GeoJSONValue row2(std::vector<geos::io::GeoJSONValue>({4.0, 5.0, 6.0}));
std::vector<geos::io::GeoJSONValue> obj_array = {row1, row2};
geos::io::GeoJSONFeatureCollection features {{
geos::io::GeoJSONFeature { wktreader.read("POINT(0 0)"), std::map<std::string, geos::io::GeoJSONValue> {
{"id", geos::io::GeoJSONValue("id_123")},
{"name", geos::io::GeoJSONValue(std::string{"Kunlin Yu"})},
{"matrix", geos::io::GeoJSONValue(obj_array)}
}}
}};
std::string result = geojsonwriter.write(features);
ensure_equals(result, "{}");
geos::io::GeoJSONFeature feature = {
wktreader.read("POINT(0 0)"),
std::map<std::string, geos::io::GeoJSONValue> {{"matrix", geos::io::GeoJSONValue(obj_array)}}
};
std::string result = geojsonwriter.write(feature);
ensure_equals(result, "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]},\"properties\":{\"matrix\":[[1.0,2.0,3.0],[4.0,5.0,6.0]]}}");
}

// GeoJSONWriter Write a feature with properties "array": [{"key": "value_1"}, {"key": "value_2"}]
Expand All @@ -448,15 +445,12 @@ void object::test<30>
geos::io::GeoJSONValue obj1(std::map<std::string, geos::io::GeoJSONValue>({{"key", std::string("value_1")}}));
geos::io::GeoJSONValue obj2(std::map<std::string, geos::io::GeoJSONValue>({{"key", std::string("value_2")}}));
std::vector<geos::io::GeoJSONValue> obj_array = {obj1, obj2};
geos::io::GeoJSONFeatureCollection features {{
geos::io::GeoJSONFeature { wktreader.read("POINT(0 0)"), std::map<std::string, geos::io::GeoJSONValue> {
{"id", geos::io::GeoJSONValue("id_123")},
{"name", geos::io::GeoJSONValue(std::string{"Kunlin Yu"})},
{"array", geos::io::GeoJSONValue(obj_array)}
}}
}};
std::string result = geojsonwriter.write(features);
ensure_equals(result, "{}");
geos::io::GeoJSONFeature feature = {
wktreader.read("POINT(0 0)"),
std::map<std::string, geos::io::GeoJSONValue> {{"array", geos::io::GeoJSONValue(obj_array)}}
};
std::string result = geojsonwriter.write(feature);
ensure_equals(result, "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[0.0,0.0]},\"properties\":{\"array\":[{\"key\":\"value_1\"},{\"key\":\"value_2\"}]}}");
}

}

0 comments on commit adb4af8

Please sign in to comment.