comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java @ 423:2402173a1490

Moved some methods back to old place. gnv-artifacts/trunk@471 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 21 Dec 2009 17:03:10 +0000
parents c6a287398379
children 15b8e95fa8da
comparison
equal deleted inserted replaced
422:f426f55d4f7a 423:2402173a1490
1 /** 1 /**
2 * 2 *
3 */ 3 */
4 package de.intevation.gnv.state.profile.horizontal; 4 package de.intevation.gnv.state.profile.horizontal;
5 5
6 import java.util.Arrays;
7 import java.util.ArrayList;
6 import java.util.Collection; 8 import java.util.Collection;
9 import java.util.List;
7 import java.util.Locale; 10 import java.util.Locale;
8 11
9 import org.apache.log4j.Logger; 12 import org.apache.log4j.Logger;
10 import org.w3c.dom.Node; 13 import org.w3c.dom.Node;
11 14
13 16
14 import de.intevation.artifactdatabase.Config; 17 import de.intevation.artifactdatabase.Config;
15 18
16 import de.intevation.gnv.artifacts.cache.CacheFactory; 19 import de.intevation.gnv.artifacts.cache.CacheFactory;
17 20
21 import de.intevation.gnv.geobackend.base.DefaultResult;
22 import de.intevation.gnv.geobackend.base.DefaultResultDescriptor;
23 import de.intevation.gnv.geobackend.base.Result;
24 import de.intevation.gnv.geobackend.base.ResultDescriptor;
25 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
26 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
27 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
28 import de.intevation.gnv.math.Interpolation2D;
29 import de.intevation.gnv.math.LinearMetrics;
30 import de.intevation.gnv.math.Point2d;
18 import de.intevation.gnv.chart.Chart; 31 import de.intevation.gnv.chart.Chart;
19 import de.intevation.gnv.chart.ChartLabels; 32 import de.intevation.gnv.chart.ChartLabels;
20 import de.intevation.gnv.chart.HorizontalCrossProfileChart; 33 import de.intevation.gnv.chart.HorizontalCrossProfileChart;
21 34
22 import de.intevation.gnv.geobackend.base.Result; 35 import de.intevation.gnv.utils.DistanceCalculator;
23
24 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
25
26 import de.intevation.gnv.utils.WKTUtils; 36 import de.intevation.gnv.utils.WKTUtils;
27 37
28 import org.jfree.chart.ChartTheme; 38 import org.jfree.chart.ChartTheme;
39
40 import com.vividsolutions.jts.geom.Coordinate;
41 import com.vividsolutions.jts.geom.LineString;
42 import com.vividsolutions.jts.io.WKTReader;
43
29 44
30 /** 45 /**
31 * @author Tim Englich <tim.englich@intevation.de> 46 * @author Tim Englich <tim.englich@intevation.de>
32 * 47 *
33 */ 48 */
120 log.debug("Hash for Queryelements: " + key); 135 log.debug("Hash for Queryelements: " + key);
121 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key); 136 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
122 if (value != null) { 137 if (value != null) {
123 result = (Collection<Result>) (value.getObjectValue()); 138 result = (Collection<Result>) (value.getObjectValue());
124 }else{ 139 }else{
125 140
126 if (this.inputData.containsKey("mesh_linestring")){ 141 if (this.inputData.containsKey("mesh_linestring")){
127 142
128 try { 143 try {
129 result = WKTUtils.worldCoordinatesToIndex( 144 LineString ls = (LineString)new WKTReader().read(
130 result, 145 inputData.get("mesh_linestring").getValue());
131 inputData, 146
132 ijkQueryID, 147 Coordinate[] coords = ls.getCoordinates();
148
149 String additionWhere =
150 WKTUtils.worldCoordinatesToIndex(
151 result,
152 inputData,
153 ijkQueryID
154 );
155
156 String[] filterValues =
157 generateFilterValuesFromInputData();
158 String[] addedFilterValues =
159 new String[filterValues.length+1];
160 System.arraycopy(
161 filterValues, 0,
162 addedFilterValues, 0,
163 filterValues.length);
164 addedFilterValues[filterValues.length] = additionWhere;
165
166 QueryExecutor queryExecutor = QueryExecutorFactory
167 .getInstance()
168 .getQueryExecutor();
169
170 result = process(
171 Arrays.asList(coords),
172 queryExecutor.executeQuery(
133 queryID, 173 queryID,
134 generateFilterValuesFromInputData() 174 addedFilterValues)
135 ); 175 );
136 } catch (ParseException e) { 176 } catch (ParseException e) {
137 log.error(e,e); 177 log.error(e,e);
138 }catch (QueryException e) { 178 }catch (QueryException e) {
139 log.error(e,e); 179 log.error(e,e);
140 } 180 }
141 }else{ 181 }else{
142 // TODO: definieren was passiert wenn kein linestring vorhanden ist. 182 // TODO: definieren was passiert wenn kein linestring vorhanden ist.
143 } 183 }
144 184
145 if (CacheFactory.getInstance().isInitialized()) { 185 if (CacheFactory.getInstance().isInitialized()) {
146 CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result)); 186 CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result));
147 } 187 }
148 188
149 } 189 }
150 } 190 }
151 return result; 191 return result;
152 } 192 }
153 193
157 log.debug("create chart subtitle for horizontal crossprofile charts."); 197 log.debug("create chart subtitle for horizontal crossprofile charts.");
158 String subtitle = createTimePeriod(locale, uuid); 198 String subtitle = createTimePeriod(locale, uuid);
159 199
160 return subtitle; 200 return subtitle;
161 } 201 }
202
203
204 public static Collection<Result> process(
205 List<Coordinate> path,
206 Collection<Result> input
207 ) {
208 log.debug("------ number of points before processing: " + input.size());
209 ArrayList<Result> output = new ArrayList<Result>();
210
211 Result last = null;
212
213 int [] diffColums = null;
214
215 SectionHandler sectionHandler = null;
216
217 for (Result result: input) {
218
219 if (sectionHandler == null) {
220
221 ResultDescriptor rd = result.getResultDescriptor();
222 diffColums = rd.getColumnIndices(DIFF_COLUMS);
223 int columns = rd.getColumnCount();
224
225 DefaultResultDescriptor resultDescriptor =
226 new DefaultResultDescriptor();
227
228 for (int j = 0; j < columns; ++j) {
229 String columnName = rd.getColumnName(j);
230 if (!blacklisted(columnName)) {
231 resultDescriptor.addColumn(
232 columnName,
233 rd.getColumnClassName(j));
234 }
235 }
236
237 sectionHandler = new SectionHandler(
238 path,
239 output,
240 resultDescriptor);
241
242 sectionHandler.setPrototyp(result);
243 }
244
245 if (last != null && WKTUtils.different(last, result, diffColums)) {
246 sectionHandler.finish();
247 sectionHandler.setPrototyp(result);
248 }
249
250 sectionHandler.handle(result);
251
252 last = result;
253 }
254
255 if (sectionHandler != null) {
256 sectionHandler.finish();
257 }
258
259 log.debug("------ number of points after processing: " + output.size());
260
261 return output;
262 }
263
264
265 private static final boolean blacklisted(String column) {
266 for (int i = 0; i < COLUMN_BLACKLIST.length; ++i) {
267 if (COLUMN_BLACKLIST.equals(column)) {
268 return true;
269 }
270 }
271 return false;
272 }
273
274 private static final String [] DIFF_COLUMS = {
275 "GROUP1",
276 "GROUP2",
277 "GROUP3"
278 };
279
280 private static final String [] COLUMN_BLACKLIST = {
281 "MEDIAN.MESHPOINT.JPOSITION",
282 "MEDIAN.MESHPOINT.IPOSITION"
283 };
284
285 public static final double EPSILON = 1e-5d;
286
287 public static final int INTERPOLATION_STEPS =
288 Integer.getInteger("interpolation.steps", 500).intValue();
289
290 public static final class SectionHandler
291 implements Interpolation2D.Consumer
292 {
293 private ArrayList<Point2d> points;
294 private List<Coordinate> path;
295 private Collection<Result> output;
296 private Result prototyp;
297 private ResultDescriptor descriptor;
298 private boolean lastWasSuccess;
299
300 public SectionHandler() {
301 }
302
303 public SectionHandler(
304 List<Coordinate> path,
305 Collection<Result> output,
306 ResultDescriptor descriptor
307 ) {
308 this.path = path;
309 this.output = output;
310 this.descriptor = descriptor;
311 points = new ArrayList<Point2d>();
312 lastWasSuccess = true;
313 }
314
315 public void finish() {
316 if (!points.isEmpty()) {
317 double distance = WKTUtils.toKM(
318 DistanceCalculator.calculateDistance(path));
319
320 if (distance > EPSILON) {
321
322 Interpolation2D.interpolate(
323 path,
324 points,
325 0d,
326 distance,
327 INTERPOLATION_STEPS,
328 LinearMetrics.INSTANCE,
329 this);
330 }
331
332 points.clear();
333 }
334 lastWasSuccess = true;
335 }
336
337 public void setPrototyp(Result prototyp) {
338 this.prototyp = prototyp;
339 }
340
341 public void handle(Result result) {
342 Coordinate coordinate =
343 WKTUtils.toCoordinate(result.getString("SHAPE"));
344 double value = result.getDouble("YORDINATE");
345 int iPos = result.getInteger("MEDIAN.MESHPOINT.JPOSITION");
346 int jPos = result.getInteger("MEDIAN.MESHPOINT.JPOSITION");
347 Point2d p = new Point2d(
348 coordinate.x,
349 coordinate.y,
350 value,
351 iPos, jPos);
352 points.add(p);
353 }
354
355 public void interpolated(Coordinate coordinate, boolean success) {
356 DefaultResult result = new DefaultResult(descriptor);
357 ResultDescriptor pd = prototyp.getResultDescriptor();
358
359 int pcolums = pd.getColumnCount();
360 for (int i = 0, j = 0; i < pcolums; ++i) {
361 String colname = pd.getColumnName(i);
362 if (blacklisted(colname)) {
363 continue;
364 }
365 if (colname.equals("SHAPE")) {
366 result.addColumnValue(j, WKTUtils.toWKT(coordinate));
367 }
368 else if (colname.equals("YORDINATE")) {
369 if (success) {
370 result.addColumnValue(j, Double.valueOf(coordinate.z));
371 }
372 else if (lastWasSuccess) {
373 // only insert null if last was valid.
374 // This prevents flooding the result set with nulls
375 // if interpolating over a large gap.
376 result.addColumnValue(j, null);
377 }
378 }
379 else {
380 result.addColumnValue(j, prototyp.getObject(i));
381 }
382 ++j;
383 }
384 output.add(result);
385 lastWasSuccess = success;
386 }
387 }
162 } 388 }

http://dive4elements.wald.intevation.org