Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/QueriedXYDepth.java @ 1119:7c4f81f74c47
merged gnv-artifacts
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:00 +0200 |
parents | f953c9a559d8 |
children |
comparison
equal
deleted
inserted
replaced
1027:fca4b5eb8d2f | 1119:7c4f81f74c47 |
---|---|
1 /* | |
2 * Copyright (c) 2010 by Intevation GmbH | |
3 * | |
4 * This program is free software under the LGPL (>=v2.1) | |
5 * Read the file LGPL.txt coming with the software for details | |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | |
7 */ | |
8 | |
9 package de.intevation.gnv.math; | |
10 | |
11 import com.vividsolutions.jts.geom.Coordinate; | |
12 | |
13 import de.intevation.gnv.geobackend.base.Result; | |
14 | |
15 import de.intevation.gnv.geobackend.base.query.QueryExecutor; | |
16 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; | |
17 | |
18 import de.intevation.gnv.geobackend.base.query.exception.QueryException; | |
19 | |
20 import de.intevation.gnv.geobackend.sde.datasources.RasterObject; | |
21 | |
22 import de.intevation.gnv.utils.WKTUtils; | |
23 | |
24 import java.lang.ref.SoftReference; | |
25 | |
26 import java.util.ArrayList; | |
27 import java.util.Collection; | |
28 | |
29 import org.apache.log4j.Logger; | |
30 | |
31 /** | |
32 * This implementation uses the geo backend to query a depth via | |
33 * a raster layer stored in the database. | |
34 * | |
35 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a> | |
36 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | |
37 */ | |
38 public class QueriedXYDepth | |
39 implements XYDepth | |
40 { | |
41 private static Logger log = Logger.getLogger(QueriedXYDepth.class); | |
42 | |
43 private static final String queryID = "rasterQuery"; | |
44 | |
45 private QueryExecutor queryExecutor; | |
46 | |
47 private ArrayList<SoftReference<RasterObject>> rasterData; | |
48 | |
49 private RasterObject last; | |
50 | |
51 private int interpolation; | |
52 | |
53 /** | |
54 * Default construtor. Interpolation method is bilinear. | |
55 */ | |
56 public QueriedXYDepth() { | |
57 this(RasterObject.BILINEAR); | |
58 } | |
59 | |
60 /** | |
61 * Constructor to create a QueriedXYDepth with a given interpolation | |
62 * method. | |
63 * @param interpolation The interpolation method. | |
64 */ | |
65 public QueriedXYDepth(int interpolation) { | |
66 this.interpolation = interpolation; | |
67 rasterData = new ArrayList<SoftReference<RasterObject>>(); | |
68 queryExecutor = QueryExecutorFactory.getInstance().getQueryExecutor(); | |
69 } | |
70 | |
71 public double depth(Coordinate coordinate) { | |
72 double resultValue = Double.NaN; | |
73 | |
74 RasterObject ro = getRasterObject(coordinate); | |
75 | |
76 if (ro == null) { | |
77 try { | |
78 String[] filterValues = | |
79 new String[] { WKTUtils.toWKT(coordinate) }; | |
80 Collection<Result> result = queryExecutor.executeQuery( | |
81 queryID, filterValues); | |
82 for (Result row: result) { | |
83 if ((ro = (RasterObject)row.getObject(0)) != null) { | |
84 rasterData.add( | |
85 new SoftReference<RasterObject>(last = ro)); | |
86 } | |
87 break; | |
88 } | |
89 } catch (QueryException e) { | |
90 log.error(e, e); | |
91 } | |
92 } | |
93 return ro != null | |
94 ? ro.getValue(coordinate, interpolation) | |
95 : Double.NaN; | |
96 } | |
97 | |
98 private RasterObject getRasterObject(Coordinate coordinate) { | |
99 if (last != null && last.contains(coordinate)) { | |
100 return last; | |
101 } | |
102 for (int i = rasterData.size()-1; i >= 0; --i) { | |
103 SoftReference<RasterObject> ref = rasterData.get(i); | |
104 RasterObject ro = ref.get(); | |
105 if (ro != null && ro.contains(coordinate)) { | |
106 return last = ro; | |
107 } | |
108 } | |
109 return null; | |
110 } | |
111 } |