Customisable map styling
[gpl/argeo-suite.git] / org.argeo.app.geo / src / org / argeo / app / internal / geo / http / GeoJsonHttpHandler.java
index 8840a4ef4dae07c7f165d23dc0dcfc7735fc1f73..ca812581d86ba916c53145d5f1ec351017c9b087 100644 (file)
@@ -3,19 +3,24 @@ package org.argeo.app.internal.geo.http;
 import static org.argeo.app.geo.CqlUtils.CQL_FILTER;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.UncheckedIOException;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Stream;
 
+import javax.xml.namespace.QName;
+
 import org.argeo.api.acr.Content;
 import org.argeo.api.acr.ContentSession;
 import org.argeo.api.acr.NamespaceUtils;
 import org.argeo.api.acr.ldap.LdapAttr;
 import org.argeo.api.acr.spi.ProvidedRepository;
+import org.argeo.app.api.EntityName;
 import org.argeo.app.api.EntityType;
 import org.argeo.app.api.WGS84PosName;
 import org.argeo.app.geo.CqlUtils;
+import org.argeo.app.geo.GpxUtils;
 import org.argeo.cms.http.HttpHeader;
 import org.argeo.cms.http.server.HttpServerUtils;
 import org.geotools.data.DataUtilities;
@@ -25,8 +30,8 @@ import org.geotools.feature.simple.SimpleFeatureBuilder;
 import org.geotools.filter.text.cql2.CQL;
 import org.geotools.geometry.jts.JTSFactoryFinder;
 import org.locationtech.jts.geom.Coordinate;
+import org.locationtech.jts.geom.Geometry;
 import org.locationtech.jts.geom.GeometryFactory;
-import org.locationtech.jts.geom.Point;
 import org.opengis.feature.simple.SimpleFeature;
 import org.opengis.feature.simple.SimpleFeatureType;
 import org.opengis.filter.Filter;
@@ -43,7 +48,7 @@ public class GeoJsonHttpHandler implements HttpHandler {
                ContentSession session = HttpServerUtils.getContentSession(contentRepository, exchange);
                // Content content = session.get(path);
 
-               exchange.getResponseHeaders().set(HttpHeader.CONTENT_TYPE.getHeaderName(), "application/json; charset=utf-8");
+               exchange.getResponseHeaders().set(HttpHeader.CONTENT_TYPE.getHeaderName(), "application/json");
 
                Map<String, List<String>> parameters = HttpServerUtils.parseParameters(exchange);
                String cql = parameters.containsKey(CQL_FILTER) ? parameters.get(CQL_FILTER).get(0) : null;
@@ -54,6 +59,9 @@ public class GeoJsonHttpHandler implements HttpHandler {
                                search.getWhere().isContentClass(EntityType.local);
                        });
 
+                       exchange.sendResponseHeaders(200, 0);
+
+                       // BODY PROCESSING
                        GeoJSONWriter geoJSONWriter = new GeoJSONWriter(exchange.getResponseBody());
                        geoJSONWriter.setPrettyPrinting(true);
 
@@ -64,9 +72,13 @@ public class GeoJsonHttpHandler implements HttpHandler {
 //                                       "features": [
 //                                     """);
 
+                       boolean gpx = false;
                        SimpleFeatureType TYPE;
                        try {
-                               TYPE = DataUtilities.createType("Content", "the_geom:Point:srid=4326,path:String,name:String");
+                               if (gpx)
+                                       TYPE = DataUtilities.createType("Content", "the_geom:Polygon:srid=4326,path:String,type:String");
+                               else
+                                       TYPE = DataUtilities.createType("Content", "the_geom:Point:srid=4326,path:String,type:String");
                        } catch (SchemaException e) {
                                throw new RuntimeException(e);
                        }
@@ -74,19 +86,41 @@ public class GeoJsonHttpHandler implements HttpHandler {
                        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
 
                        res.forEach((c) -> {
-                               if (!c.hasContentClass(EntityType.geopoint))
-                                       return;
-
-                               double latitude = c.get(WGS84PosName.lat, Double.class).get();
-                               double longitude = c.get(WGS84PosName.lng, Double.class).get();
+                               Geometry the_geom;
+                               if (gpx) {// experimental
+                                       Content area = c.getContent("gpx/area.gpx").orElse(null);
+                                       if (area == null)
+                                               return;
+                                       try (InputStream in = area.open(InputStream.class)) {
+                                               SimpleFeature feature = GpxUtils.parseGpxToPolygon(in);
+                                               the_geom = (Geometry) feature.getDefaultGeometry();
+                                       } catch (IOException e) {
+                                               throw new UncheckedIOException("Cannot parse " + c, e);
+                                       }
+                               } else {
+                                       if (!c.hasContentClass(EntityType.geopoint))
+                                               return;
+
+                                       double latitude = c.get(WGS84PosName.lat, Double.class).get();
+                                       double longitude = c.get(WGS84PosName.lng, Double.class).get();
+
+                                       Coordinate coordinate = new Coordinate(longitude, latitude);
+                                       the_geom = geometryFactory.createPoint(coordinate);
 
-                               Coordinate coordinate = new Coordinate(longitude, latitude);
-                               Point point = geometryFactory.createPoint(coordinate);
+                               }
 
-                               featureBuilder.add(point);
+                               featureBuilder.add(the_geom);
                                String pth = c.getPath();
                                featureBuilder.add(pth);
-                               featureBuilder.add(NamespaceUtils.toPrefixedName(c.getName()));
+                               if (c.hasContentClass(EntityType.local)) {
+                                       String type = c.attr(EntityName.type);
+                                       featureBuilder.add(type);
+                               } else {
+                                       List<QName> contentClasses = c.getContentClasses();
+                                       if (!contentClasses.isEmpty()) {
+                                               featureBuilder.add(NamespaceUtils.toPrefixedName(contentClasses.get(0)));
+                                       }
+                               }
 
                                String uuid = c.attr(LdapAttr.entryUUID);