comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MapInfoService.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents eb9f7fd77edf
children 8e66293c5369
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.services;
2
3 import org.apache.log4j.Logger;
4
5 import java.util.Map;
6 import java.util.HashMap;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Node;
10 import org.w3c.dom.Element;
11
12 import javax.xml.xpath.XPathConstants;
13
14 import com.vividsolutions.jts.geom.Envelope;
15
16 import de.intevation.artifacts.CallMeta;
17 import de.intevation.artifacts.GlobalContext;
18
19 import de.intevation.artifacts.common.ArtifactNamespaceContext;
20 import de.intevation.artifacts.common.utils.Config;
21 import de.intevation.artifacts.common.utils.XMLUtils;
22 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
23
24 import de.intevation.artifactdatabase.XMLService;
25
26 import de.intevation.flys.model.River;
27
28 import de.intevation.flys.utils.GeometryUtils;
29
30 /**
31 * This service provides information about the supported rivers by this
32 * application.
33 *
34 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
35 */
36 public class MapInfoService extends XMLService {
37
38 /** XPath that points to the river.*/
39 public static final String XPATH_RIVER = "/mapinfo/river/text()";
40
41 public static final String XPATH_RIVER_PROJECTION =
42 "/artifact-database/floodmap/river[@name=$river]/srid/@value";
43
44 public static final String XPATH_RIVER_BACKGROUND =
45 "/artifact-database/floodmap/river[@name=$river]/background-wms";
46
47 public static final String XPATH_RIVER_WMS =
48 "/artifact-database/floodmap/river[@name=$river]/river-wms/@url";
49
50
51 /** The logger used in this service.*/
52 private static Logger logger = Logger.getLogger(MapInfoService.class);
53
54
55 /**
56 * The default constructor.
57 */
58 public MapInfoService() {
59 }
60
61 protected static String getStringXPath(
62 String query,
63 Map<String, String> variables
64 ) {
65 return (String)XMLUtils.xpath(
66 Config.getConfig(), query, XPathConstants.STRING,
67 null, variables);
68 }
69
70 protected static Node getNodeXPath(
71 String query,
72 Map<String, String> variables
73 ) {
74 return (Node)XMLUtils.xpath(
75 Config.getConfig(), query, XPathConstants.NODE,
76 null, variables);
77 }
78
79 @Override
80 public Document processXML(
81 Document data,
82 GlobalContext globalContext,
83 CallMeta callMeta
84 ) {
85 logger.debug("MapInfoService.process");
86
87 Document result = XMLUtils.newDocument();
88 ElementCreator cr = new ElementCreator(result, null, null);
89
90 Element mapinfo = cr.create("mapinfo");
91 result.appendChild(mapinfo);
92
93 String river = extractRiver(data);
94 if (river == null || river.length() == 0) {
95 logger.warn("Cannot generate information: river is empty!");
96 return result;
97 }
98
99 Element root = cr.create("river");
100 cr.addAttr(root, "name", river);
101 mapinfo.appendChild(root);
102
103 Envelope env = GeometryUtils.getRiverBoundary(river);
104 if (env != null) {
105 String bounds = GeometryUtils.jtsBoundsToOLBounds(env);
106 logger.debug("River '" + river + "' bounds: " + bounds);
107
108 Element bbox = cr.create("bbox");
109 cr.addAttr(bbox, "value", bounds);
110 root.appendChild(bbox);
111 }
112
113 Map<String, String> vars = new HashMap<String, String>();
114 vars.put("river", river);
115
116 String sridStr = getStringXPath(XPATH_RIVER_PROJECTION, vars);
117
118 if (sridStr != null && sridStr.length() > 0) {
119 Element srid = cr.create("srid");
120 cr.addAttr(srid, "value", sridStr);
121 root.appendChild(srid);
122 }
123
124 Element back = (Element)getNodeXPath(XPATH_RIVER_BACKGROUND, vars);
125 if (back != null) {
126 Element background = cr.create("background-wms");
127 cr.addAttr(background, "url", back.getAttribute("url"));
128 cr.addAttr(background, "layers", back.getAttribute("layers"));
129 root.appendChild(background);
130 }
131
132 String wmsStr = getStringXPath(XPATH_RIVER_WMS, vars);
133 if (wmsStr != null && wmsStr.length() > 0) {
134 Element wms = cr.create("river-wms");
135 cr.addAttr(wms, "url", wmsStr);
136 root.appendChild(wms);
137 }
138
139 return result;
140 }
141
142
143 protected String extractRiver(Document data) {
144 return XMLUtils.xpathString(
145 data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE);
146 }
147 }
148 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org