X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.app.geo%2Fsrc%2Forg%2Fargeo%2Fapp%2Fgeo%2Facr%2FGeoEntityUtils.java;h=844e9d3da18a13c4c55d1d19c17222c57f546df9;hb=55c1d5969ea023e3cf29d0688f62579a5e5e4d0f;hp=6019ee9ae46ab4b20c4d1a55035f49033a56f2cd;hpb=1eaec4a7690f29973a712a9000c010a37f725ea1;p=gpl%2Fargeo-suite.git diff --git a/org.argeo.app.geo/src/org/argeo/app/geo/acr/GeoEntityUtils.java b/org.argeo.app.geo/src/org/argeo/app/geo/acr/GeoEntityUtils.java index 6019ee9..844e9d3 100644 --- a/org.argeo.app.geo/src/org/argeo/app/geo/acr/GeoEntityUtils.java +++ b/org.argeo.app.geo/src/org/argeo/app/geo/acr/GeoEntityUtils.java @@ -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,10 +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 { - public static final String PLACE_GEOM_JSON = "place.geom.json"; + 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) { @@ -43,7 +48,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)) { @@ -55,18 +60,41 @@ public class GeoEntityUtils { } catch (IOException e) { 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); + } 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 getGeometry(Content c, QNamed name, Class clss) { return getGeometry(c, name.qName(), clss); } public static T getGeometry(Content c, QName name, Class 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; + 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(); @@ -74,16 +102,26 @@ 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; } } + 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)) { + if (c.containsKey(WGS84PosName.lon) && c.containsKey(WGS84PosName.lat)) { 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 +156,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() { }