1 | /* WRS 2.0 |
---|
2 | * Copyright (C) 2009 Anton Patrushev |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | |
---|
18 | package util.format; |
---|
19 | |
---|
20 | import geometry.Point; |
---|
21 | import handler.ResourceHandler; |
---|
22 | |
---|
23 | import java.util.ArrayList; |
---|
24 | import java.util.HashMap; |
---|
25 | import java.util.Iterator; |
---|
26 | import java.util.ListIterator; |
---|
27 | import java.util.Map.Entry; |
---|
28 | |
---|
29 | import org.antlr.stringtemplate.StringTemplate; |
---|
30 | import org.opengis.geometry.MismatchedDimensionException; |
---|
31 | import org.opengis.referencing.FactoryException; |
---|
32 | import org.opengis.referencing.NoSuchAuthorityCodeException; |
---|
33 | import org.opengis.referencing.operation.TransformException; |
---|
34 | |
---|
35 | import com.vividsolutions.jts.io.ParseException; |
---|
36 | |
---|
37 | import util.IOHelper; |
---|
38 | import util.Log; |
---|
39 | import util.Parameter; |
---|
40 | import util.ServiceRequest; |
---|
41 | |
---|
42 | public abstract class TemplateFiller |
---|
43 | { |
---|
44 | protected HashMap<String, Double> max = new HashMap<String, Double>(); |
---|
45 | protected HashMap<String, Double> min = new HashMap<String, Double>(); |
---|
46 | protected HashMap<String, Double> avg = new HashMap<String, Double>(); |
---|
47 | protected HashMap<String, Double> sum = new HashMap<String, Double>(); |
---|
48 | protected int count; |
---|
49 | |
---|
50 | protected String srid_in; |
---|
51 | protected String srid_out; |
---|
52 | |
---|
53 | protected enum Units |
---|
54 | { |
---|
55 | DD, METERS, MILES; |
---|
56 | } |
---|
57 | |
---|
58 | protected enum DataTypes |
---|
59 | { |
---|
60 | STRING, INTEGER, DOUBLE, GEOMETRY, BOOLEAN; |
---|
61 | } |
---|
62 | |
---|
63 | protected enum AggregateTypes |
---|
64 | { |
---|
65 | MAX, MIN, AVG, SUM, COUNT; |
---|
66 | } |
---|
67 | |
---|
68 | protected abstract String getGeometryString(ArrayList<Point> points); |
---|
69 | |
---|
70 | public String getX(Point point) |
---|
71 | { |
---|
72 | return IOHelper.FORMATTER.format(point.getX()); |
---|
73 | } |
---|
74 | |
---|
75 | public String getY(Point point) |
---|
76 | { |
---|
77 | return IOHelper.FORMATTER.format(point.getY()); |
---|
78 | } |
---|
79 | |
---|
80 | public String fillOutputTemplate(ServiceRequest serviceRequest, |
---|
81 | ArrayList<HashMap<String, Parameter>> output, |
---|
82 | ResourceHandler handler, Log log) |
---|
83 | { |
---|
84 | StringTemplate template = new StringTemplate(serviceRequest |
---|
85 | .getTemplate().getBody()); |
---|
86 | |
---|
87 | ArrayList<HashMap<String, String>> out = new ArrayList<HashMap<String, String>>(); |
---|
88 | Iterator<HashMap<String, Parameter>> it = output.iterator(); |
---|
89 | |
---|
90 | while (it.hasNext()) |
---|
91 | { |
---|
92 | ++count; |
---|
93 | HashMap<String, Parameter> paraMap = it.next(); |
---|
94 | HashMap<String, String> stringMap = new HashMap<String, String>(); |
---|
95 | Iterator<Parameter> params = paraMap.values().iterator(); |
---|
96 | while (params.hasNext()) |
---|
97 | { |
---|
98 | Parameter p = params.next(); |
---|
99 | String value = p.getValue(); |
---|
100 | String name = p.getName(); |
---|
101 | |
---|
102 | // Geometry string needs to be handled in a special way |
---|
103 | // if |
---|
104 | // (p.getType().toLowerCase().equals(ResourceHandler.GEOMETRY)) |
---|
105 | switch (DataTypes.valueOf(p.getType().toUpperCase())) |
---|
106 | { |
---|
107 | case GEOMETRY: |
---|
108 | |
---|
109 | srid_in = serviceRequest.getResource().getParameters().get( |
---|
110 | "srid").getValue(); |
---|
111 | srid_out = srid_in; |
---|
112 | |
---|
113 | ArrayList<Point> points = handler.parseGeometry(value); |
---|
114 | try |
---|
115 | { |
---|
116 | // Check if we need to re-project points |
---|
117 | if (serviceRequest.getParameters().containsKey( |
---|
118 | "srid_out")) |
---|
119 | { |
---|
120 | srid_out = serviceRequest.getParameters().get("srid_out") |
---|
121 | .getValue(); |
---|
122 | } |
---|
123 | |
---|
124 | ListIterator<Point> pi = points.listIterator(); |
---|
125 | while (pi.hasNext()) |
---|
126 | { |
---|
127 | Point point = pi.next(); |
---|
128 | |
---|
129 | // calculating max value for all geometry parameters |
---|
130 | if (max.keySet().contains(name + "_x") |
---|
131 | && max.keySet().contains(name + "_y")) |
---|
132 | { |
---|
133 | if (max.get(name + "_x").doubleValue() < point |
---|
134 | .getX()) |
---|
135 | { |
---|
136 | max.put(name + "_x", new Double(point |
---|
137 | .getX())); |
---|
138 | } |
---|
139 | if (max.get(name + "_y").doubleValue() < point |
---|
140 | .getY()) |
---|
141 | { |
---|
142 | max.put(name + "_y", new Double(point |
---|
143 | .getY())); |
---|
144 | } |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | max.put(name + "_x", new Double(point.getX())); |
---|
149 | max.put(name + "_y", new Double(point.getY())); |
---|
150 | } |
---|
151 | |
---|
152 | // calculating min value for all geometry parameters |
---|
153 | if (min.keySet().contains(name + "_x") |
---|
154 | && min.keySet().contains(name + "_y")) |
---|
155 | { |
---|
156 | if (min.get(name + "_x").doubleValue() > point |
---|
157 | .getX()) |
---|
158 | { |
---|
159 | min.put(name + "_x", new Double(point |
---|
160 | .getX())); |
---|
161 | } |
---|
162 | if (min.get(name + "_y").doubleValue() > point |
---|
163 | .getY()) |
---|
164 | { |
---|
165 | min.put(name + "_y", new Double(point |
---|
166 | .getY())); |
---|
167 | } |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | min.put(name + "_x", new Double(point.getX())); |
---|
172 | min.put(name + "_y", new Double(point.getY())); |
---|
173 | } |
---|
174 | |
---|
175 | if (srid_in != srid_out) |
---|
176 | { |
---|
177 | |
---|
178 | String x = IOHelper.FORMATTER.format(point |
---|
179 | .getX()); |
---|
180 | String y = IOHelper.FORMATTER.format(point |
---|
181 | .getY()); |
---|
182 | String[] transformed = handler.transformPoint( |
---|
183 | x, y, srid_in, srid_out); |
---|
184 | point.setX(Double.parseDouble(transformed[0])); |
---|
185 | point.setY(Double.parseDouble(transformed[1])); |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | catch (NullPointerException e) |
---|
190 | { |
---|
191 | // Can't find srid_in or srid_out |
---|
192 | // Do nothing |
---|
193 | } |
---|
194 | catch (MismatchedDimensionException e) |
---|
195 | { |
---|
196 | // TODO Auto-generated catch block |
---|
197 | e.printStackTrace(); |
---|
198 | } |
---|
199 | catch (NoSuchAuthorityCodeException e) |
---|
200 | { |
---|
201 | // TODO Auto-generated catch block |
---|
202 | e.printStackTrace(); |
---|
203 | } |
---|
204 | catch (FactoryException e) |
---|
205 | { |
---|
206 | // TODO Auto-generated catch block |
---|
207 | e.printStackTrace(); |
---|
208 | } |
---|
209 | catch (ParseException e) |
---|
210 | { |
---|
211 | // TODO Auto-generated catch block |
---|
212 | e.printStackTrace(); |
---|
213 | } |
---|
214 | catch (TransformException e) |
---|
215 | { |
---|
216 | // TODO Auto-generated catch block |
---|
217 | e.printStackTrace(); |
---|
218 | } |
---|
219 | |
---|
220 | value = getGeometryString(points); |
---|
221 | break; |
---|
222 | case INTEGER: |
---|
223 | case DOUBLE: |
---|
224 | double doubleValue = Double.parseDouble(value); |
---|
225 | |
---|
226 | // calculating max value for all numeric parameters |
---|
227 | if (max.keySet().contains(name)) |
---|
228 | { |
---|
229 | if (max.get(name).doubleValue() < doubleValue) |
---|
230 | { |
---|
231 | max.put(name, new Double(doubleValue)); |
---|
232 | } |
---|
233 | } |
---|
234 | else |
---|
235 | { |
---|
236 | max.put(name, new Double(doubleValue)); |
---|
237 | } |
---|
238 | |
---|
239 | // calculating min value for all numeric parameters |
---|
240 | if (min.keySet().contains(name)) |
---|
241 | { |
---|
242 | if (min.get(name).doubleValue() > doubleValue) |
---|
243 | { |
---|
244 | min.put(name, new Double(doubleValue)); |
---|
245 | } |
---|
246 | } |
---|
247 | else |
---|
248 | { |
---|
249 | min.put(name, new Double(doubleValue)); |
---|
250 | } |
---|
251 | |
---|
252 | // calculating sum value for all numeric parameters |
---|
253 | if (sum.keySet().contains(name)) |
---|
254 | { |
---|
255 | double prevValue = sum.get(name).doubleValue(); |
---|
256 | sum.put(name, new Double(prevValue + doubleValue)); |
---|
257 | |
---|
258 | } |
---|
259 | else |
---|
260 | { |
---|
261 | sum.put(name, new Double(doubleValue)); |
---|
262 | } |
---|
263 | |
---|
264 | break; |
---|
265 | } |
---|
266 | |
---|
267 | stringMap.put(p.getName(), value); |
---|
268 | } |
---|
269 | out.add(stringMap); |
---|
270 | } |
---|
271 | |
---|
272 | // calculating avg values for all numeric parameters |
---|
273 | Iterator<Entry<String, Double>> sumIt = sum.entrySet().iterator(); |
---|
274 | while (sumIt.hasNext()) |
---|
275 | { |
---|
276 | Entry<String, Double> entry = sumIt.next(); |
---|
277 | double avgValue = sum.get(entry.getKey()).doubleValue() / count; |
---|
278 | avg.put(entry.getKey(), new Double(avgValue)); |
---|
279 | } |
---|
280 | |
---|
281 | template.setAttribute("parameters", out); |
---|
282 | |
---|
283 | template.setAttribute("min", min); |
---|
284 | template.setAttribute("max", max); |
---|
285 | template.setAttribute("avg", avg); |
---|
286 | template.setAttribute("sum", sum); |
---|
287 | template.setAttribute("count", count); |
---|
288 | template.setAttribute("srid_out", srid_out); |
---|
289 | /* |
---|
290 | * template.setAttribute("srid_out", serviceRequest.getParameters().get( |
---|
291 | * "srid_out").getValue()); |
---|
292 | */ |
---|
293 | return template.toString(); |
---|
294 | } |
---|
295 | } |
---|