Improve open layers
[gpl/argeo-suite.git] / org.argeo.app.geo / src / org / argeo / app / geo / acr / GeoEntityUtils.java
index 6019ee9ae46ab4b20c4d1a55035f49033a56f2cd..62b432e7f42e59155d3f0a5f383b9057b087ef7d 100644 (file)
@@ -35,7 +35,6 @@ import jakarta.json.stream.JsonGenerator;
 
 /** Utilities around entity types related to geography. */
 public class GeoEntityUtils {
-       public static final String PLACE_GEOM_JSON = "place.geom.json";
        public static final String _GEOM_JSON = ".geom.json";
 
        public static void putGeometry(Content c, QNamed name, Geometry geometry) {
@@ -43,7 +42,7 @@ public class GeoEntityUtils {
        }
 
        public static void putGeometry(Content c, QName name, Geometry geometry) {
-               QName jsonFileName = new ContentName(name.getNamespaceURI(), name.getLocalPart() + _GEOM_JSON);
+               QName jsonFileName = getJsonFileName(name);
                Content geom = c.soleChild(jsonFileName).orElseGet(
                                () -> c.add(jsonFileName, Collections.singletonMap(DName.getcontenttype.qName(), "application/json")));
                try (OutputStream out = geom.open(OutputStream.class)) {
@@ -58,12 +57,21 @@ public class GeoEntityUtils {
                updateBoundingBox(c);
        }
 
+       public static boolean hasGeometry(Content c, QNamed name) {
+               return hasGeometry(c, name.qName());
+       }
+
+       public static boolean hasGeometry(Content c, QName name) {
+               QName jsonFileName = getJsonFileName(name);
+               return c.hasChild(jsonFileName);
+       }
+
        public static <T extends Geometry> T getGeometry(Content c, QNamed name, Class<T> clss) {
                return getGeometry(c, name.qName(), clss);
        }
 
        public static <T extends Geometry> T getGeometry(Content c, QName name, Class<T> clss) {
-               QName jsonFileName = new ContentName(name.getNamespaceURI(), name.getLocalPart() + _GEOM_JSON);
+               QName jsonFileName = getJsonFileName(name);
                Content geom = c.soleChild(jsonFileName).orElse(null);
                if (geom == null)
                        return null;
@@ -77,13 +85,19 @@ public class GeoEntityUtils {
                }
        }
 
+       private static QName getJsonFileName(QName name) {
+               QName jsonFileName = new ContentName(name.getNamespaceURI(), name.getLocalPart() + _GEOM_JSON);
+               return jsonFileName;
+       }
+
        public static Point toPoint(Content c) {
                if (c.hasContentClass(EntityType.geopoint)) {
                        Double lat = c.get(WGS84PosName.lat, Double.class).orElseThrow();
                        Double lon = c.get(WGS84PosName.lon, Double.class).orElseThrow();
-                       Double alt = c.get(WGS84PosName.alt, Double.class).orElse(null);
-                       return JTS.GEOMETRY_FACTORY_WGS84
-                                       .createPoint(alt != null ? new Coordinate(lat, lon, alt) : new Coordinate(lat, lon));
+                       return JTS.GEOMETRY_FACTORY_WGS84.createPoint(new Coordinate(lat, lon));
+//                     Double alt = c.get(WGS84PosName.alt, Double.class).orElse(null);
+//                     return JTS.GEOMETRY_FACTORY_WGS84
+//                                     .createPoint(alt != null ? new Coordinate(lat, lon, alt) : new Coordinate(lat, lon));
                }
                return null;
        }
@@ -118,6 +132,19 @@ public class GeoEntityUtils {
                entity.put(EntityName.maxLon, bbox.getMaxY());
        }
 
+       public static void updateBoundingBox(Content entity, QName prop) {
+               Geometry geom = getGeometry(entity, prop, Geometry.class);
+               if (geom == null)
+                       return;
+               entity.addContentClasses(EntityType.geobounded.qName());
+
+               Envelope bbox = geom.getEnvelopeInternal();
+               entity.put(EntityName.minLat, bbox.getMinX());
+               entity.put(EntityName.minLon, bbox.getMinY());
+               entity.put(EntityName.maxLat, bbox.getMaxX());
+               entity.put(EntityName.maxLon, bbox.getMaxY());
+       }
+
        /** singleton */
        private GeoEntityUtils() {
        }