You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sedona.apache.org by ji...@apache.org on 2023/01/06 06:35:12 UTC

[sedona] branch rdd-save-geojson updated: Can read null properties or properties with a few null values

This is an automated email from the ASF dual-hosted git repository.

jiayu pushed a commit to branch rdd-save-geojson
in repository https://gitbox.apache.org/repos/asf/sedona.git


The following commit(s) were added to refs/heads/rdd-save-geojson by this push:
     new d0fa74e1 Can read null properties or properties with a few null values
d0fa74e1 is described below

commit d0fa74e19657a7062e3ea838a9665ebcf5576438
Author: Jia Yu <ji...@apache.org>
AuthorDate: Thu Jan 5 23:35:06 2023 -0700

    Can read null properties or properties with a few null values
---
 .../sedona/core/formatMapper/FormatUtils.java      | 27 +++++++++++-----------
 .../sedona/core/formatMapper/GeoJsonIOTest.java    | 21 ++++++++++++++++-
 2 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/core/src/main/java/org/apache/sedona/core/formatMapper/FormatUtils.java b/core/src/main/java/org/apache/sedona/core/formatMapper/FormatUtils.java
index 525f30b3..c15c3567 100644
--- a/core/src/main/java/org/apache/sedona/core/formatMapper/FormatUtils.java
+++ b/core/src/main/java/org/apache/sedona/core/formatMapper/FormatUtils.java
@@ -119,23 +119,22 @@ public class FormatUtils<T extends Geometry> implements Serializable {
 
     public static List<String> readGeoJsonPropertyNames(String geoJson)
     {
+        if (geoJson == null) {
+            logger.warn("The given GeoJson record is null and cannot be used to find property names");
+            return null;
+        }
+        List<String> propertyList = new ArrayList<>();
         if (geoJson.contains("Feature") || geoJson.contains("feature") || geoJson.contains("FEATURE")) {
-            if (geoJson.contains("properties")) {
-                Feature feature = (Feature) GeoJSONFactory.create(geoJson);
-                if (Objects.isNull(feature.getId())) {
-                    return new ArrayList(feature.getProperties().keySet());
-                }
-                else {
-                    List<String> propertyList = new ArrayList<>(Arrays.asList("id"));
-                    for (String geoJsonProperty : feature.getProperties().keySet()) {
-                        propertyList.add(geoJsonProperty);
-                    }
-                    return propertyList;
-                }
+            Feature feature = (Feature) GeoJSONFactory.create(geoJson);
+            if (!Objects.isNull(feature.getId())) {
+                propertyList.add("id");
+            }
+            Map<String, Object> properties = feature.getProperties();
+            if (properties != null) {
+                propertyList.addAll(properties.keySet());
             }
         }
-        logger.warn("[Sedona] The GeoJSON file doesn't have feature properties");
-        return null;
+        return propertyList.size() > 0 ? propertyList:null;
     }
 
     private void readObject(ObjectInputStream inputStream)
diff --git a/core/src/test/java/org/apache/sedona/core/formatMapper/GeoJsonIOTest.java b/core/src/test/java/org/apache/sedona/core/formatMapper/GeoJsonIOTest.java
index 7219f30b..cdc3569a 100644
--- a/core/src/test/java/org/apache/sedona/core/formatMapper/GeoJsonIOTest.java
+++ b/core/src/test/java/org/apache/sedona/core/formatMapper/GeoJsonIOTest.java
@@ -73,7 +73,6 @@ public class GeoJsonIOTest
         // load geojson with our tool
         SpatialRDD geojsonRDD = GeoJsonReader.readToGeometryRDD(sc, geoJsonGeomWithFeatureProperty);
         assertEquals(geojsonRDD.rawSpatialRDD.count(), 1001);
-        geojsonRDD.saveAsGeoJSON("target/geojson.tmp");
         geojsonRDD = GeoJsonReader.readToGeometryRDD(sc, geoJsonGeomWithoutFeatureProperty);
         assertEquals(geojsonRDD.rawSpatialRDD.count(), 10);
     }
@@ -105,6 +104,26 @@ public class GeoJsonIOTest
         assertEquals(initRdd.rawSpatialRDD.count(), newRdd.rawSpatialRDD.count());
     }
 
+    @Test
+    public void testReadWriteSpecialGeoJsons()
+            throws IOException
+    {
+        String tmpFilePath = "target/geojson.tmp";
+        SpatialRDD initRdd = GeoJsonReader.readToGeometryRDD(sc, geoJsonGeomWithoutFeatureProperty);
+        deleteFile(tmpFilePath);
+        initRdd.saveAsGeoJSON(tmpFilePath);
+        SpatialRDD newRdd = GeoJsonReader.readToGeometryRDD(sc, tmpFilePath);
+        assertEquals(initRdd.rawSpatialRDD.count(), newRdd.rawSpatialRDD.count());
+
+        initRdd = GeoJsonReader.readToGeometryRDD(sc, geoJsonWithNullProperty);
+        deleteFile(tmpFilePath);
+        initRdd.saveAsGeoJSON(tmpFilePath);
+        newRdd = GeoJsonReader.readToGeometryRDD(sc, tmpFilePath);
+        assertEquals(initRdd.rawSpatialRDD.count(), newRdd.rawSpatialRDD.count());
+
+        deleteFile(tmpFilePath);
+    }
+
     /**
      * Test geojson with null values in the properties
      *