comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/AreaInterpolation.java @ 540:80630520e25a

merged gnv-artifacts/0.4
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:49 +0200
parents 44415ae01ddb
children b248531fa20b
comparison
equal deleted inserted replaced
415:9f4a0b990d27 540:80630520e25a
1 package de.intevation.gnv.math;
2
3 import com.vividsolutions.jts.geom.Coordinate;
4 import com.vividsolutions.jts.geom.Envelope;
5
6 import com.vividsolutions.jts.index.strtree.STRtree;
7
8 import java.awt.Dimension;
9
10 import java.io.Serializable;
11
12 import java.util.Arrays;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16
17 /**
18 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
19 */
20 public class AreaInterpolation
21 implements Serializable
22 {
23 private static Logger log = Logger.getLogger(AreaInterpolation.class);
24
25 protected double [] raster;
26
27 protected int width;
28 protected int height;
29
30 public AreaInterpolation() {
31 }
32
33 public int getWidth() {
34 return width;
35 }
36
37 public int getHeight() {
38 return height;
39 }
40
41 public double [] getRaster() {
42 return raster;
43 }
44
45 public static final double EPS = 1e-6d;
46
47
48 public boolean interpolate(
49 List<? extends Point2d> points,
50 Envelope boundingBox,
51 Dimension samples,
52 XYDepth depth
53 ) {
54 boolean debug = log.isDebugEnabled();
55
56 if (points == null || points.isEmpty()) {
57 log.warn("no points to interpolate");
58 return false;
59 }
60
61 List<GridCell> cells = GridCell.pointsToGridCells(
62 points,
63 Interpolation2D.relevantArea(
64 boundingBox,
65 points));
66
67 if (cells.isEmpty()) {
68 log.warn("no cells to interpolate");
69 return false;
70 }
71
72 int W = samples.width;
73 int H = samples.height;
74
75 double cellWidth = boundingBox.getWidth() / W;
76 double cellHeight = boundingBox.getHeight() / H;
77
78 STRtree spatialIndex = new STRtree();
79
80 for (GridCell cell: cells) {
81 spatialIndex.insert(cell.getEnvelope(), cell);
82 }
83
84 if (debug) {
85 log.debug("width: " + boundingBox.getWidth());
86 log.debug("height: " + boundingBox.getHeight());
87 log.debug("sample width: " + W);
88 log.debug("sample height: " + H);
89 log.debug("cell width: " + cellWidth);
90 log.debug("cell height: " + cellHeight);
91 }
92
93 Envelope queryBuffer = new Envelope();
94 Coordinate center = new Coordinate();
95 GridCell.CellFinder finder = new GridCell.CellFinder();
96
97 double [] raster = new double[W*H];
98 Arrays.fill(raster, Double.NaN);
99
100 double minX = boundingBox.getMinX();
101 double minY = boundingBox.getMinY();
102
103 long startTime = System.currentTimeMillis();
104
105 int pos = 0;
106 for (int j = 0; j < H; ++j) {
107
108 double y = j*cellHeight + 0.5d*cellHeight + minY;
109 double x = 0.5d*cellWidth + minX;
110
111 for (int end = pos + W; pos < end; ++pos, x += cellWidth) {
112
113 queryBuffer.init(x - EPS, x + EPS, y - EPS, y + EPS);
114 center.x = x; center.y = y;
115 finder.prepare(center);
116 spatialIndex.query(queryBuffer, finder);
117
118 GridCell found = finder.found;
119
120 if (found == null || depth.depth(center) > 0d) {
121 continue;
122 }
123
124 double z1 = Interpolation2D.interpolate(
125 found.p1.x, found.p1.z,
126 found.p2.x, found.p2.z,
127 center.x);
128 double z2 = Interpolation2D.interpolate(
129 found.p3.x, found.p3.z,
130 found.p4.x, found.p4.z,
131 center.x);
132 double y1 = Interpolation2D.interpolate(
133 found.p1.x, found.p1.y,
134 found.p2.x, found.p2.y,
135 center.x);
136 double y2 = Interpolation2D.interpolate(
137 found.p3.x, found.p3.y,
138 found.p4.x, found.p4.y,
139 center.x);
140 raster[pos] = Interpolation2D.interpolate(
141 y1, z1,
142 y2, z2,
143 center.y);
144 }
145 }
146
147 long stopTime = System.currentTimeMillis();
148
149 if (debug) {
150 log.debug("interpolation took: " + (stopTime - startTime)/1000f + " secs");
151 }
152
153 this.raster = raster;
154 this.width = W;
155 this.height = H;
156
157 return true;
158 }
159 }
160 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org