Make reading geometries more robust
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 13 Dec 2023 08:17:43 +0000 (09:17 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 13 Dec 2023 08:17:43 +0000 (09:17 +0100)
org.argeo.app.geo/src/org/argeo/app/geo/acr/GeoEntityUtils.java

index 48c3c1b438113f263f36296fff809f5fe8cb8d1e..844e9d3da18a13c4c55d1d19c17222c57f546df9 100644 (file)
@@ -1,5 +1,6 @@
 package org.argeo.app.geo.acr;
 
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -17,11 +18,13 @@ import org.argeo.api.acr.Content;
 import org.argeo.api.acr.ContentName;
 import org.argeo.api.acr.DName;
 import org.argeo.api.acr.QNamed;
+import org.argeo.api.cms.CmsLog;
 import org.argeo.app.api.EntityName;
 import org.argeo.app.api.EntityType;
 import org.argeo.app.api.WGS84PosName;
 import org.argeo.app.geo.GeoJson;
 import org.argeo.app.geo.JTS;
+import org.argeo.cms.util.StreamUtils;
 import org.locationtech.jts.geom.Coordinate;
 import org.locationtech.jts.geom.Envelope;
 import org.locationtech.jts.geom.Geometry;
@@ -32,9 +35,12 @@ import jakarta.json.Json;
 import jakarta.json.JsonObject;
 import jakarta.json.JsonReader;
 import jakarta.json.stream.JsonGenerator;
+import jakarta.json.stream.JsonParsingException;
 
 /** Utilities around entity types related to geography. */
 public class GeoEntityUtils {
+       private final static CmsLog log = CmsLog.getLog(GeoEntityUtils.class);
+
        public static final String _GEOM_JSON = ".geom.json";
 
        public static void putGeometry(Content c, QNamed name, Geometry geometry) {
@@ -55,12 +61,12 @@ public class GeoEntityUtils {
                        throw new UncheckedIOException("Cannot add geometry " + name + " to " + c, e);
                }
 
-//             try (BufferedReader in = new BufferedReader(
-//                             new InputStreamReader(geom.open(InputStream.class), StandardCharsets.UTF_8))) {
-//                     System.out.println(in.readLine());
-//             } catch (IOException e) {
-//                     throw new UncheckedIOException("Cannot parse " + c, e);
-//             }
+               try (BufferedReader in = new BufferedReader(
+                               new InputStreamReader(geom.open(InputStream.class), StandardCharsets.UTF_8))) {
+                       System.out.println(in.readLine());
+               } catch (IOException e) {
+                       throw new UncheckedIOException("Cannot parse " + c, e);
+               }
                updateBoundingBox(c);
        }
 
@@ -82,6 +88,13 @@ public class GeoEntityUtils {
                Content geom = c.soleChild(jsonFileName).orElse(null);
                if (geom == null)
                        return null;
+               try (Reader in = new InputStreamReader(geom.open(InputStream.class), StandardCharsets.UTF_8)) {
+                       String json = StreamUtils.toString(new BufferedReader(in));
+                       System.out.println("JSON:\n" + json);
+               } catch (IOException e1) {
+                       // TODO Auto-generated catch block
+                       e1.printStackTrace();
+               }
                try (Reader in = new InputStreamReader(geom.open(InputStream.class), StandardCharsets.UTF_8)) {
                        JsonReader jsonReader = Json.createReader(in);
                        JsonObject jsonObject = jsonReader.readObject();
@@ -89,6 +102,10 @@ public class GeoEntityUtils {
                        return readGeom;
                } catch (IOException e) {
                        throw new UncheckedIOException("Cannot parse " + c, e);
+               } catch (JsonParsingException e) {
+                       log.warn("Invalid GeoJson for " + geom);
+                       // json is invalid, returning null
+                       return null;
                }
        }