comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java @ 365:f66088a43ecc

Added horizontal crossprofile charts to chart pallet. Fixed some bugs before interpolation. gnv-artifacts/trunk@440 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 16 Dec 2009 19:29:05 +0000
parents 1ab23cd66870
children 04a242c67fe6
comparison
equal deleted inserted replaced
364:2413273f1c13 365:f66088a43ecc
5 5
6 import java.util.ArrayList; 6 import java.util.ArrayList;
7 import java.util.Arrays; 7 import java.util.Arrays;
8 import java.util.Collection; 8 import java.util.Collection;
9 import java.util.List; 9 import java.util.List;
10 import java.util.Locale;
10 11
11 import org.apache.log4j.Logger; 12 import org.apache.log4j.Logger;
12 import org.w3c.dom.Node; 13 import org.w3c.dom.Node;
13 14
14 import com.vividsolutions.jts.geom.Coordinate; 15 import com.vividsolutions.jts.geom.Coordinate;
19 import com.vividsolutions.jts.io.WKTReader; 20 import com.vividsolutions.jts.io.WKTReader;
20 21
21 import de.intevation.artifactdatabase.Config; 22 import de.intevation.artifactdatabase.Config;
22 23
23 import de.intevation.gnv.artifacts.cache.CacheFactory; 24 import de.intevation.gnv.artifacts.cache.CacheFactory;
25
26 import de.intevation.gnv.chart.Chart;
27 import de.intevation.gnv.chart.ChartLabels;
28 import de.intevation.gnv.chart.HorizontalCrossProfileChart;
24 29
25 import de.intevation.gnv.geobackend.base.DefaultResultDescriptor; 30 import de.intevation.gnv.geobackend.base.DefaultResultDescriptor;
26 import de.intevation.gnv.geobackend.base.ResultDescriptor; 31 import de.intevation.gnv.geobackend.base.ResultDescriptor;
27 import de.intevation.gnv.geobackend.base.DefaultResult; 32 import de.intevation.gnv.geobackend.base.DefaultResult;
28 import de.intevation.gnv.geobackend.base.Result; 33 import de.intevation.gnv.geobackend.base.Result;
46 51
47 import org.apache.commons.math.optimization.general.GaussNewtonOptimizer; 52 import org.apache.commons.math.optimization.general.GaussNewtonOptimizer;
48 import org.apache.commons.math.optimization.OptimizationException; 53 import org.apache.commons.math.optimization.OptimizationException;
49 54
50 import org.apache.commons.math.FunctionEvaluationException; 55 import org.apache.commons.math.FunctionEvaluationException;
56
57 import org.jfree.chart.ChartTheme;
51 58
52 /** 59 /**
53 * @author Tim Englich <tim.englich@intevation.de> 60 * @author Tim Englich <tim.englich@intevation.de>
54 * 61 *
55 */ 62 */
85 super.setup(configuration); 92 super.setup(configuration);
86 this.ijkQueryID = Config.getStringXPath(configuration,"queryID-ijk"); 93 this.ijkQueryID = Config.getStringXPath(configuration,"queryID-ijk");
87 94
88 } 95 }
89 96
97
98 @Override
99 protected Chart getChart(
100 ChartLabels chartLables,
101 ChartTheme theme,
102 Collection parameters,
103 Collection measurements,
104 Collection dates,
105 Collection result,
106 Locale locale,
107 String uuid,
108 boolean linesVisible,
109 boolean shapesVisible
110 ) {
111 Chart chart = null;
112 if (CACHE_CHART) {
113 log.info("Try to get horizontalprofilemeshcross chart from cache.");
114 chart = (Chart) getChartFromCache(uuid);
115 }
116
117 if (chart != null)
118 return chart;
119
120 log.info("Chart not in cache yet.");
121 chart = new HorizontalCrossProfileChart(
122 chartLables,
123 theme,
124 parameters,
125 measurements,
126 dates,
127 result,
128 null,
129 locale,
130 linesVisible,
131 shapesVisible
132 );
133 chart.generateChart();
134
135 if (CACHE_CHART) {
136 log.info("Put chart into cache.");
137 purifyChart(chart, uuid);
138 }
139
140 return chart;
141 }
142
90 @Override 143 @Override
91 protected Collection<Result> getChartResult(String uuid) { 144 protected Collection<Result> getChartResult(String uuid) {
92 log.debug("OutputStateBase.getChartResult"); 145 log.debug("HorizontalProfileMeshCrossOutputState.getChartResult");
93 Collection<Result> result = null; 146 Collection<Result> result = null;
94 if (CacheFactory.getInstance().isInitialized()) { 147 if (CacheFactory.getInstance().isInitialized()) {
95 String key = uuid + super.getID(); 148 String key = uuid + super.getID();
96 log.debug("Hash for Queryelements: " + key); 149 log.debug("Hash for Queryelements: " + key);
97 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key); 150 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
115 .getInstance() 168 .getInstance()
116 .getQueryExecutor(); 169 .getQueryExecutor();
117 170
118 ArrayList missingPoints = new ArrayList(); 171 ArrayList missingPoints = new ArrayList();
119 172
120 String additionWhere = "TRUE"; 173 String additionWhere = "FEATUREID=FEATUREID";
121 174
122 for (int i = 0; i < coords.length; i++) { 175 for (int i = 0; i < coords.length; i++) {
123 176
124 String wkt = "POINT( "+coords[i].x+" "+coords[i].y+" )"; 177 String wkt = toWKT(coords[i]);
125 178
126 result = queryExecutor.executeQuery(this.ijkQueryID, 179 result = queryExecutor.executeQuery(this.ijkQueryID,
127 new String[]{meshid,wkt}); 180 new String[]{meshid,wkt});
128 if (!result.isEmpty()){ 181 if (!result.isEmpty()){
129 Result resultValue = result.iterator().next(); 182 Result resultValue = result.iterator().next();
227 } 280 }
228 } 281 }
229 return result; 282 return result;
230 } 283 }
231 284
285
286 @Override
287 protected String createChartSubtitle(Locale locale, String uuid) {
288 log.debug("create chart subtitle for horizontal crossprofile charts.");
289 String subtitle = createTimePeriod(locale, uuid);
290
291 return subtitle;
292 }
293
232 private static final String [] COLUMN_BLACKLIST = { 294 private static final String [] COLUMN_BLACKLIST = {
233 "MEDIAN.MESHPOINT.JPOSITION", 295 "MEDIAN.MESHPOINT.JPOSITION",
234 "MEDIAN.MESHPOINT.IPOSITION" 296 "MEDIAN.MESHPOINT.IPOSITION"
235 }; 297 };
236 298
243 return false; 305 return false;
244 } 306 }
245 307
246 private static boolean different(Result a, Result b, int [] indices) { 308 private static boolean different(Result a, Result b, int [] indices) {
247 for (int i = 0; i < indices.length; ++i) { 309 for (int i = 0; i < indices.length; ++i) {
248 Object oa = a.getObject(indices[i]); 310 String oa = a.getString(indices[i]);
249 Object ob = b.getObject(indices[i]); 311 String ob = b.getString(indices[i]);
250 if ((oa == null && ob != null) 312
251 || (oa != null && ob == null) 313 if (oa == null && ob == null) {
252 || (oa != null && !oa.equals(ob))) { 314 continue;
315 }
316
317 if (oa == null || ob == null) {
318 return true;
319 }
320
321 if (!oa.equals(ob)) {
322 log.debug("+++++++++++++++ differs ++++++++++++++");
323 log.debug(" " + oa + " != " + ob);
253 return true; 324 return true;
254 } 325 }
255 } 326 }
256 return false; 327 return false;
257 } 328 }
280 ResultDescriptor descriptor 351 ResultDescriptor descriptor
281 ) { 352 ) {
282 this.path = path; 353 this.path = path;
283 this.output = output; 354 this.output = output;
284 this.descriptor = descriptor; 355 this.descriptor = descriptor;
356 points = new ArrayList<Point2d>();
285 } 357 }
286 358
287 public void finish() { 359 public void finish() {
288 if (!points.isEmpty()) { 360 if (!points.isEmpty()) {
289 double distance = toKM( 361 double distance = toKM(
290 DistanceCalculator.calculateDistance(path)); 362 DistanceCalculator.calculateDistance(path));
291 363
292 Interpolation2D.interpolate( 364 if (distance > EPSILON) {
293 path, 365
294 points, 366 Interpolation2D.interpolate(
295 0d, 367 path,
296 distance, 368 points,
297 steps(distance), 369 0d,
298 LinearMetrics.INSTANCE, // XXX: This wrong!!! 370 distance,
299 this); 371 INTERPOLATION_STEPS,
372 LinearMetrics.INSTANCE,
373 this);
374 }
300 375
301 points.clear(); 376 points.clear();
302 } 377 }
303 } 378 }
304 379
334 result.addColumnValue(j, toWKT(coordinate)); 409 result.addColumnValue(j, toWKT(coordinate));
335 } 410 }
336 else if (colname.equals("YORDINATE")) { 411 else if (colname.equals("YORDINATE")) {
337 result.addColumnValue(j, Double.valueOf(coordinate.z)); 412 result.addColumnValue(j, Double.valueOf(coordinate.z));
338 } 413 }
414 else {
415 result.addColumnValue(j, prototyp.getObject(i));
416 }
339 ++j; 417 ++j;
340 } 418 }
341 output.add(result); 419 output.add(result);
342 } 420 }
343 } // class SectionHandler 421 } // class SectionHandler
347 425
348 public static final double toKM(double distance) { 426 public static final double toKM(double distance) {
349 return (distance * NAUTICAL_MILE) / KILOMETER; 427 return (distance * NAUTICAL_MILE) / KILOMETER;
350 } 428 }
351 429
352 public static final double INTERPOLATION_STEP_WIDTH = 430 public static final double EPSILON = 1e-5d;
353 Double.parseDouble(System.getProperty( 431 public static final int INTERPOLATION_STEPS =
354 "interpolation.step.width", "100")); 432 Integer.getInteger("interpolation.steps", 500).intValue();
355
356 public static int steps(double km) {
357 return (int)Math.ceil(
358 Math.max(1d, (km * KILOMETER)/INTERPOLATION_STEP_WIDTH));
359 }
360 433
361 public static Coordinate toCoordinate(String shape) { 434 public static Coordinate toCoordinate(String shape) {
362 try { 435 try {
363 return ((Point)(new WKTReader().read(shape))).getCoordinate(); 436 return ((Point)(new WKTReader().read(shape))).getCoordinate();
364 } 437 }
379 452
380 protected Collection<Result> process( 453 protected Collection<Result> process(
381 List<Coordinate> path, 454 List<Coordinate> path,
382 Collection<Result> input 455 Collection<Result> input
383 ) { 456 ) {
457 log.debug("------ number of points before processing: " + input.size());
384 ArrayList<Result> output = new ArrayList<Result>(); 458 ArrayList<Result> output = new ArrayList<Result>();
385 459
386 460
387 Result last = null; 461 Result last = null;
388 462
430 504
431 if (sectionHandler != null) { 505 if (sectionHandler != null) {
432 sectionHandler.finish(); 506 sectionHandler.finish();
433 } 507 }
434 508
509 log.debug("------ number of points after processing: " + output.size());
510
435 return output; 511 return output;
436 } 512 }
437 } 513 }

http://dive4elements.wald.intevation.org