view flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MapInfoService.java @ 3992:a9c93b7c9da1

Simpify the S(Q) fraction sieving stuff.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 30 Sep 2012 21:15:23 +0200
parents eb9f7fd77edf
children 8e66293c5369
line wrap: on
line source
package de.intevation.flys.artifacts.services;

import org.apache.log4j.Logger;

import java.util.Map;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import javax.xml.xpath.XPathConstants;

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.XMLService;

import de.intevation.flys.model.River;

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 XMLService {

    /** 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() {
    }

    protected static String getStringXPath(
        String              query,
        Map<String, String> variables
    ) {
        return (String)XMLUtils.xpath(
            Config.getConfig(), query, XPathConstants.STRING,
            null, variables);
    }

    protected static Node getNodeXPath(
        String              query,
        Map<String, String> variables
    ) {
        return (Node)XMLUtils.xpath(
            Config.getConfig(), query, XPathConstants.NODE,
            null, variables);
    }

    @Override
    public Document processXML(
        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);

        Envelope env = GeometryUtils.getRiverBoundary(river);
        if (env != null) {
            String bounds = GeometryUtils.jtsBoundsToOLBounds(env);
            logger.debug("River '" + river + "' bounds: " + bounds);

            Element bbox = cr.create("bbox");
            cr.addAttr(bbox, "value", bounds);
            root.appendChild(bbox);
        }

        Map<String, String> vars = new HashMap<String, String>();
        vars.put("river", river);

        String sridStr = getStringXPath(XPATH_RIVER_PROJECTION, vars);

        if (sridStr != null && sridStr.length() > 0) {
            Element srid = cr.create("srid");
            cr.addAttr(srid, "value", sridStr);
            root.appendChild(srid);
        }

        Element back = (Element)getNodeXPath(XPATH_RIVER_BACKGROUND, vars);
        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 wmsStr = getStringXPath(XPATH_RIVER_WMS, vars);
        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