comparison artifacts/src/main/java/org/dive4elements/river/artifacts/services/MapInfoService.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/services/MapInfoService.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.services;
2
3 import com.vividsolutions.jts.geom.Envelope;
4
5 import org.dive4elements.artifactdatabase.XMLService;
6 import org.dive4elements.artifacts.CallMeta;
7 import org.dive4elements.artifacts.GlobalContext;
8 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
9 import org.dive4elements.artifacts.common.utils.Config;
10 import org.dive4elements.artifacts.common.utils.XMLUtils;
11 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
12 import org.dive4elements.river.utils.GeometryUtils;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import javax.xml.xpath.XPathConstants;
18
19 import org.apache.log4j.Logger;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23
24 /**
25 * This service provides information about the supported rivers by this
26 * application.
27 *
28 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
29 */
30 public class MapInfoService extends XMLService {
31
32 /** XPath that points to the river.*/
33 public static final String XPATH_RIVER = "/mapinfo/river/text()";
34
35 public static final String XPATH_MAPTYPE = "/mapinfo/maptype/text()";
36
37 private static final String XPATH_RIVER_PROJECTION =
38 "/artifact-database/*[local-name()=$maptype]/river[@name=$river]/srid/@value";
39
40 private static final String XPATH_RIVER_BACKGROUND =
41 "/artifact-database/*[local-name()=$maptype]/river[@name=$river]/background-wms";
42
43 private static final String XPATH_RIVER_WMS =
44 "/artifact-database/*[local-name()=$maptype]/river[@name=$river]/river-wms";
45
46
47 /** The logger used in this service.*/
48 private static Logger logger = Logger.getLogger(MapInfoService.class);
49
50
51 /**
52 * The default constructor.
53 */
54 public MapInfoService() {
55 }
56
57 protected static String getStringXPath(
58 String query,
59 Map<String, String> variables
60 ) {
61 return (String)XMLUtils.xpath(
62 Config.getConfig(), query, XPathConstants.STRING,
63 null, variables);
64 }
65
66 protected static Node getNodeXPath(
67 String query,
68 Map<String, String> variables
69 ) {
70 return (Node)XMLUtils.xpath(
71 Config.getConfig(), query, XPathConstants.NODE,
72 null, variables);
73 }
74
75 @Override
76 public Document processXML(
77 Document data,
78 GlobalContext globalContext,
79 CallMeta callMeta
80 ) {
81 logger.debug("MapInfoService.process");
82
83 Document result = XMLUtils.newDocument();
84 ElementCreator cr = new ElementCreator(result, null, null);
85
86 Element mapinfo = cr.create("mapinfo");
87 result.appendChild(mapinfo);
88
89 String river = extractRiver(data);
90 if (river == null || river.length() == 0) {
91 logger.warn("Cannot generate information: river is empty!");
92 return result;
93 }
94
95 String mapType = extractMaptype(data);
96 if (mapType == null
97 || !(mapType.equals("floodmap") || mapType.equals("rivermap"))) {
98 mapType = "floodmap";
99 }
100
101 Element root = cr.create("river");
102 cr.addAttr(root, "name", river);
103 mapinfo.appendChild(root);
104
105 Envelope env = GeometryUtils.getRiverBoundary(river);
106 if (env != null) {
107 String bounds = GeometryUtils.jtsBoundsToOLBounds(env);
108 if (logger.isDebugEnabled()) {
109 logger.debug("River '" + river + "' bounds: " + bounds);
110 }
111
112 Element bbox = cr.create("bbox");
113 cr.addAttr(bbox, "value", bounds);
114 root.appendChild(bbox);
115 }
116
117 Map<String, String> vars = new HashMap<String, String>();
118 vars.put("maptype", mapType);
119 vars.put("river", river);
120
121 String sridStr = getStringXPath(XPATH_RIVER_PROJECTION, vars);
122
123 if (sridStr != null && sridStr.length() > 0) {
124 Element srid = cr.create("srid");
125 cr.addAttr(srid, "value", sridStr);
126 root.appendChild(srid);
127 }
128
129 if (logger.isDebugEnabled()) {
130 logger.debug("processXML: " + XMLUtils.toString(root));
131 }
132
133 root.appendChild(
134 createWMSElement("background-wms",
135 XPATH_RIVER_BACKGROUND, vars, cr));
136
137 root.appendChild(
138 createWMSElement("river-wms",
139 XPATH_RIVER_WMS, vars, cr));
140
141 return result;
142 }
143
144
145 protected Element createWMSElement(
146 String elementName,
147 String xpath,
148 Map<String, String> vars,
149 ElementCreator cr)
150 {
151 logger.debug("createWMSElement()");
152
153 Element el = cr.create(elementName);
154 Element wms = (Element)getNodeXPath(xpath, vars);
155
156 if (wms != null) {
157 cr.addAttr(el, "url", wms.getAttribute("url"));
158 cr.addAttr(el, "layers", wms.getAttribute("layers"));
159
160 logger.debug("createWMSElement: " + XMLUtils.toString(el));
161 }
162 else {
163 logger.debug("createWMSElement: wms == null");
164 }
165
166 return el;
167 }
168
169
170 private static String extractRiver(Document data) {
171 return XMLUtils.xpathString(
172 data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE);
173 }
174
175 private static String extractMaptype(Document data) {
176 return XMLUtils.xpathString(
177 data, XPATH_MAPTYPE, ArtifactNamespaceContext.INSTANCE);
178 }
179 }
180 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org