comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MapInfoService.java @ 3818:dc18457b1cef

merged flys-artifacts/pre2.7-2012-03-16
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:59 +0200
parents 8284c8fca840
children 247f3e98a14b
comparison
equal deleted inserted replaced
2456:60ab1054069d 3818:dc18457b1cef
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.DefaultService;
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 DefaultService {
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 public Document process(
80 Document data,
81 GlobalContext globalContext,
82 CallMeta callMeta
83 ) {
84 logger.debug("MapInfoService.process");
85
86 Document result = XMLUtils.newDocument();
87 ElementCreator cr = new ElementCreator(result, null, null);
88
89 Element mapinfo = cr.create("mapinfo");
90 result.appendChild(mapinfo);
91
92 String river = extractRiver(data);
93 if (river == null || river.length() == 0) {
94 logger.warn("Cannot generate information: river is empty!");
95 return result;
96 }
97
98 Element root = cr.create("river");
99 cr.addAttr(root, "name", river);
100 mapinfo.appendChild(root);
101
102 Envelope env = GeometryUtils.getRiverBoundary(river);
103 if (env != null) {
104 String bounds = GeometryUtils.jtsBoundsToOLBounds(env);
105 logger.debug("River '" + river + "' bounds: " + bounds);
106
107 Element bbox = cr.create("bbox");
108 cr.addAttr(bbox, "value", bounds);
109 root.appendChild(bbox);
110 }
111
112 Map<String, String> vars = new HashMap<String, String>();
113 vars.put("river", river);
114
115 String sridStr = getStringXPath(XPATH_RIVER_PROJECTION, vars);
116
117 if (sridStr != null && sridStr.length() > 0) {
118 Element srid = cr.create("srid");
119 cr.addAttr(srid, "value", sridStr);
120 root.appendChild(srid);
121 }
122
123 Element back = (Element)getNodeXPath(XPATH_RIVER_BACKGROUND, vars);
124 if (back != null) {
125 Element background = cr.create("background-wms");
126 cr.addAttr(background, "url", back.getAttribute("url"));
127 cr.addAttr(background, "layers", back.getAttribute("layers"));
128 root.appendChild(background);
129 }
130
131 String wmsStr = getStringXPath(XPATH_RIVER_WMS, vars);
132 if (wmsStr != null && wmsStr.length() > 0) {
133 Element wms = cr.create("river-wms");
134 cr.addAttr(wms, "url", wmsStr);
135 root.appendChild(wms);
136 }
137
138 return result;
139 }
140
141
142 protected String extractRiver(Document data) {
143 return XMLUtils.xpathString(
144 data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE);
145 }
146 }
147 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org