Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/GFIServiceImpl.java @ 1400:96708d81eaf6
Added an initial GetFeatureInfo tool to get information about points in the map.
flys-client/trunk@3285 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 17 Nov 2011 16:20:55 +0000 |
parents | |
children | 15ef3d3081b7 |
comparison
equal
deleted
inserted
replaced
1399:748e7c828d03 | 1400:96708d81eaf6 |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import java.io.BufferedReader; | |
4 import java.io.InputStream; | |
5 import java.io.InputStreamReader; | |
6 import java.io.IOException; | |
7 import java.io.StringWriter; | |
8 import java.net.URL; | |
9 import java.net.URLConnection; | |
10 import java.util.List; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 | |
14 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
15 | |
16 | |
17 import de.intevation.flys.client.shared.exceptions.ServerException; | |
18 import de.intevation.flys.client.shared.model.AttributedTheme; | |
19 import de.intevation.flys.client.shared.model.Theme; | |
20 | |
21 import de.intevation.flys.client.client.services.GFIService; | |
22 | |
23 | |
24 /** | |
25 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
26 */ | |
27 public class GFIServiceImpl | |
28 extends RemoteServiceServlet | |
29 implements GFIService | |
30 { | |
31 public static final String ERR_NO_VALID_GFI_URL = | |
32 "error_no_valid_gfi_url"; | |
33 | |
34 public static final String ERR_GFI_REQUEST_FAILED = | |
35 "error_gfi_req_failed"; | |
36 | |
37 public static final String ERR_PARSING_RESPONSE_FAILED = | |
38 "error_gfi_parsing_failed"; | |
39 | |
40 | |
41 private static final Logger logger = | |
42 Logger.getLogger(GFIServiceImpl.class); | |
43 | |
44 | |
45 /** | |
46 * @param themes | |
47 * @param format | |
48 * @param bbox | |
49 * @param height | |
50 * @param width | |
51 * @param x | |
52 * @param y | |
53 * | |
54 * @return | |
55 */ | |
56 public String query( | |
57 List<Theme> themes, | |
58 String format, | |
59 String bbox, | |
60 String projection, | |
61 int height, | |
62 int width, | |
63 int x, | |
64 int y | |
65 ) throws ServerException | |
66 { | |
67 logger.info("GFIServiceImpl.query"); | |
68 | |
69 String path = createGetFeautureInfoURL( | |
70 themes, format, bbox, projection, height, width, x, y); | |
71 logger.debug("URL=" + path); | |
72 | |
73 try { | |
74 URL url = new URL(path); | |
75 | |
76 URLConnection conn = url.openConnection(); | |
77 conn.connect(); | |
78 | |
79 InputStream is = conn.getInputStream(); | |
80 | |
81 return getResponseText(is); | |
82 | |
83 } | |
84 catch (IOException ioe) { | |
85 logger.warn(ioe, ioe); | |
86 } | |
87 | |
88 throw new ServerException(ERR_GFI_REQUEST_FAILED); | |
89 } | |
90 | |
91 | |
92 protected String getResponseText(InputStream is) | |
93 throws ServerException { | |
94 BufferedReader reader = null; | |
95 StringWriter writer = new StringWriter(); | |
96 | |
97 try { | |
98 reader = new BufferedReader(new InputStreamReader(is)); | |
99 | |
100 String line = null; | |
101 | |
102 if (reader.ready()) { | |
103 while ((line = reader.readLine()) != null) { | |
104 String test = line.trim(); | |
105 if (test.startsWith("<") && !test.startsWith("</") | |
106 && test.indexOf("_feature") > 0) | |
107 { | |
108 writer.append("<gml:featureMember>"); | |
109 } | |
110 writer.append(line); | |
111 | |
112 if (test.startsWith("</") && test.indexOf("_feature") > 0) { | |
113 writer.append("</gml:featureMember>"); | |
114 } | |
115 } | |
116 } | |
117 } | |
118 catch (IOException ioe) { | |
119 logger.warn(ioe, ioe); | |
120 throw new ServerException(ERR_PARSING_RESPONSE_FAILED); | |
121 } | |
122 finally { | |
123 if (reader != null) { | |
124 try { | |
125 reader.close(); | |
126 } | |
127 catch (IOException ioe) { | |
128 // do nothing here | |
129 } | |
130 } | |
131 } | |
132 | |
133 return writer.toString(); | |
134 } | |
135 | |
136 | |
137 /** | |
138 * @param map | |
139 * @param themes | |
140 * @param format | |
141 * @param x | |
142 * @param y | |
143 * | |
144 * @return | |
145 */ | |
146 protected String createGetFeautureInfoURL( | |
147 List<Theme> themes, | |
148 String infoFormat, | |
149 String bbox, | |
150 String projection, | |
151 int height, | |
152 int width, | |
153 int x, | |
154 int y | |
155 ) throws ServerException | |
156 { | |
157 String url = getUrl(themes); | |
158 | |
159 if (url == null || url.length() == 0) { | |
160 throw new ServerException(ERR_NO_VALID_GFI_URL); | |
161 } | |
162 | |
163 String layers = createLayersString(themes); | |
164 | |
165 StringBuilder sb = new StringBuilder(); | |
166 sb.append(url); | |
167 | |
168 if (url.indexOf("?") < 0) { | |
169 sb.append("?SERVICE=WMS"); | |
170 } | |
171 else { | |
172 sb.append("&SERVICE=WMS"); | |
173 } | |
174 | |
175 sb.append("&VERSION=1.1.1"); | |
176 sb.append("&REQUEST=GetFeatureInfo"); | |
177 sb.append("&LAYERS=" + layers); | |
178 sb.append("&QUERY_LAYERS=" + layers); | |
179 sb.append("&BBOX=" + bbox); | |
180 sb.append("&HEIGHT=" + height); | |
181 sb.append("&WIDTH=" + width); | |
182 sb.append("&FORMAT=image/png"); | |
183 sb.append("&INFO_FORMAT=" + infoFormat); | |
184 sb.append("&SRS=" + projection); | |
185 sb.append("&X=" + String.valueOf(x)); | |
186 sb.append("&Y=" + String.valueOf(y)); | |
187 | |
188 return sb.toString(); | |
189 } | |
190 | |
191 | |
192 protected String getUrl(List<Theme> themes) { | |
193 for (Theme t: themes) { | |
194 AttributedTheme attr = (AttributedTheme) t; | |
195 | |
196 if (attr.getAttrAsBoolean("queryable")) { | |
197 return attr.getAttr("url"); | |
198 } | |
199 } | |
200 | |
201 return null; | |
202 } | |
203 | |
204 | |
205 protected String createLayersString(List<Theme> themes) { | |
206 StringBuilder sb = new StringBuilder(); | |
207 boolean first = true; | |
208 | |
209 for (Theme theme: themes) { | |
210 if (!first) { | |
211 sb.append(","); | |
212 } | |
213 | |
214 AttributedTheme layer = (AttributedTheme) theme; | |
215 if (layer.getAttrAsBoolean("queryable")) { | |
216 sb.append(layer.getAttr("layers")); | |
217 first = false; | |
218 } | |
219 } | |
220 | |
221 return sb.toString(); | |
222 } | |
223 } | |
224 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |