diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MapInfoService.java @ 3814:8083f6384023

merged flys-artifacts/pre2.6-2012-01-04
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:56 +0200
parents 092e1e5020bc
children cbeeaaad1056
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MapInfoService.java	Fri Sep 28 12:14:56 2012 +0200
@@ -0,0 +1,125 @@
+package de.intevation.flys.artifacts.services;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import com.vividsolutions.jts.geom.Envelope;
+
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.GlobalContext;
+
+import de.intevation.artifacts.common.ArtifactNamespaceContext;
+import de.intevation.artifacts.common.utils.Config;
+import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
+
+import de.intevation.artifactdatabase.DefaultService;
+
+import de.intevation.flys.model.River;
+
+import de.intevation.flys.model.RiverAxis;
+import de.intevation.flys.utils.GeometryUtils;
+
+/**
+ * This service provides information about the supported rivers by this
+ * application.
+ *
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class MapInfoService extends DefaultService {
+
+    /** XPath that points to the river.*/
+    public static final String XPATH_RIVER = "/mapinfo/river/text()";
+
+    public static final String XPATH_RIVER_PROJECTION =
+        "/artifact-database/floodmap/river[@name='%RIVER%']/srid/@value";
+
+    public static final String XPATH_RIVER_BACKGROUND =
+        "/artifact-database/floodmap/river[@name='%RIVER%']/background-wms";
+
+    public static final String XPATH_RIVER_WMS =
+        "/artifact-database/floodmap/river[@name='%RIVER%']/river-wms/@url";
+
+
+    /** The logger used in this service.*/
+    private static Logger logger = Logger.getLogger(MapInfoService.class);
+
+
+    /**
+     * The default constructor.
+     */
+    public MapInfoService() {
+    }
+
+
+    public Document process(
+        Document      data,
+        GlobalContext globalContext,
+        CallMeta      callMeta
+    ) {
+        logger.debug("MapInfoService.process");
+
+        Document result   = XMLUtils.newDocument();
+        ElementCreator cr = new ElementCreator(result, null, null);
+
+        Element mapinfo = cr.create("mapinfo");
+        result.appendChild(mapinfo);
+
+        String river = extractRiver(data);
+        if (river == null || river.length() == 0) {
+            logger.warn("Cannot generate information: river is empty!");
+            return result;
+        }
+
+        Element root = cr.create("river");
+        cr.addAttr(root, "name", river);
+        mapinfo.appendChild(root);
+
+        RiverAxis axis = RiverAxis.getRiverAxis(river);
+        if (axis != null) {
+            Envelope env    = axis.getGeom().getEnvelopeInternal();
+            String   bounds = GeometryUtils.jtsBoundsToOLBounds(env);
+
+            logger.debug("River '" + river + "' bounds: " + bounds);
+            Element bbox = cr.create("bbox");
+            cr.addAttr(bbox, "value", bounds);
+            root.appendChild(bbox);
+        }
+
+        String xpathS  = XPATH_RIVER_PROJECTION.replace("%RIVER%", river);
+        String sridStr = Config.getStringXPath(xpathS);
+        if (sridStr != null && sridStr.length() > 0) {
+            Element srid = cr.create("srid");
+            cr.addAttr(srid, "value", sridStr);
+            root.appendChild(srid);
+        }
+
+        String xpathB = XPATH_RIVER_BACKGROUND.replace("%RIVER%", river);
+        Element back  = (Element) Config.getNodeXPath(xpathB);
+        if (back != null) {
+            Element background = cr.create("background-wms");
+            cr.addAttr(background, "url", back.getAttribute("url"));
+            cr.addAttr(background, "layers", back.getAttribute("layers"));
+            root.appendChild(background);
+        }
+
+        String xpathWMS = XPATH_RIVER_WMS.replace("%RIVER%", river);
+        String wmsStr   = Config.getStringXPath(xpathWMS);
+        if (wmsStr != null && wmsStr.length() > 0) {
+            Element wms = cr.create("river-wms");
+            cr.addAttr(wms, "url", wmsStr);
+            root.appendChild(wms);
+        }
+
+        return result;
+    }
+
+
+    protected String extractRiver(Document data) {
+        return XMLUtils.xpathString(
+            data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org