[geos-commits] [SCM] GEOS branch main updated. 145b8d1f1517736e150a0828e5bd9078cb8a58e2

git at osgeo.org git at osgeo.org
Sun Jan 12 10:09:18 PST 2025


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GEOS".

The branch, main has been updated
       via  145b8d1f1517736e150a0828e5bd9078cb8a58e2 (commit)
      from  a1e35c13b207c239ba34e13909a8f26d6adb47e2 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 145b8d1f1517736e150a0828e5bd9078cb8a58e2
Author: Kunlin Yu <yukunlin at syriusrobotics.com>
Date:   Mon Jan 13 02:08:53 2025 +0800

    Fix bug of GeoJSONWriter when there are sub-objects or sub-array in an array (#1216)
    
    * Add a unit test to trigger the bug "cannot use operator[] with a string argument with array"
    
    Signed-off-by: Kunlin Yu <yukunlin at syriusrobotics.com>
    
    * Add another unit test to trigger the bug "cannot use operator[] with a string argument with array"
    
    Signed-off-by: Kunlin Yu <yukunlin at syriusrobotics.com>
    
    * Fix bug of GeoJSONWriter related array in properties
    
    Signed-off-by: Kunlin Yu <yukunlin at syriusrobotics.com>
    
    ---------
    
    Signed-off-by: Kunlin Yu <yukunlin at syriusrobotics.com>

diff --git a/src/io/GeoJSONWriter.cpp b/src/io/GeoJSONWriter.cpp
index bf3826d49..8c9db9d7b 100644
--- a/src/io/GeoJSONWriter.cpp
+++ b/src/io/GeoJSONWriter.cpp
@@ -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);
         }
     }
 }
diff --git a/tests/unit/io/GeoJSONWriterTest.cpp b/tests/unit/io/GeoJSONWriterTest.cpp
index 53d6e609a..7db14f5cf 100644
--- a/tests/unit/io/GeoJSONWriterTest.cpp
+++ b/tests/unit/io/GeoJSONWriterTest.cpp
@@ -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<>
@@ -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\"}]}}");
+}
+
 }

-----------------------------------------------------------------------

Summary of changes:
 src/io/GeoJSONWriter.cpp            | 30 ++++++++++++++++++++++++------
 tests/unit/io/GeoJSONWriterTest.cpp | 35 ++++++++++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 7 deletions(-)


hooks/post-receive
-- 
GEOS


More information about the geos-commits mailing list