Skip to content

Commit

Permalink
Fix bug of GeoJSONWriter when there are sub-objects or sub-array in a…
Browse files Browse the repository at this point in the history
…n array (#1216)

* Add a unit test to trigger the bug "cannot use operator[] with a string argument with array"

Signed-off-by: Kunlin Yu <[email protected]>

* Add another unit test to trigger the bug "cannot use operator[] with a string argument with array"

Signed-off-by: Kunlin Yu <[email protected]>

* Fix bug of GeoJSONWriter related array in properties

Signed-off-by: Kunlin Yu <[email protected]>

---------

Signed-off-by: Kunlin Yu <[email protected]>
  • Loading branch information
kunlinyu authored Jan 12, 2025
1 parent a1e35c1 commit 145b8d1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 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
35 changes: 34 additions & 1 deletion tests/unit/io/GeoJSONWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ void object::test<25>
std::string result = geojsonwriter.write(geom.get());
ensure_equals(result, "{\"type\":\"LineString\",\"coordinates\":[[102.0,0.0,2.0],[103.0,1.0,4.0],[104.0,0.0,8.0],[105.0,1.0,16.0]]}");
}

// Write a LineString Z with some NaN Z to GeoJSON
template<>
template<>
Expand Down Expand Up @@ -420,4 +419,38 @@ void object::test<28>
ensure_equals(result, "{\"type\":\"Point\",\"coordinates\":[-117.0,33.0]}");
}

// GeoJSONWriter Write a feature with properties "matrix": [ [1, 2, 3], [4, 5, 6] ]
template<>
template<>
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::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"}]
template<>
template<>
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::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 145b8d1

Please sign in to comment.