comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.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 eb2ac62e853a
comparison
equal deleted inserted replaced
422:f426f55d4f7a 423:2402173a1490
4 import com.vividsolutions.jts.geom.Point; 4 import com.vividsolutions.jts.geom.Point;
5 import com.vividsolutions.jts.geom.LineString; 5 import com.vividsolutions.jts.geom.LineString;
6 import com.vividsolutions.jts.io.ParseException; 6 import com.vividsolutions.jts.io.ParseException;
7 import com.vividsolutions.jts.io.WKTReader; 7 import com.vividsolutions.jts.io.WKTReader;
8 8
9 import de.intevation.gnv.geobackend.base.DefaultResult;
10 import de.intevation.gnv.geobackend.base.DefaultResultDescriptor;
11 import de.intevation.gnv.geobackend.base.Result; 9 import de.intevation.gnv.geobackend.base.Result;
12 import de.intevation.gnv.geobackend.base.ResultDescriptor;
13 import de.intevation.gnv.geobackend.base.query.QueryExecutor; 10 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
14 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; 11 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
15 import de.intevation.gnv.geobackend.base.query.exception.QueryException; 12 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
16 import de.intevation.gnv.math.Interpolation2D;
17 import de.intevation.gnv.math.LinearFunction; 13 import de.intevation.gnv.math.LinearFunction;
18 import de.intevation.gnv.math.LinearMetrics;
19 import de.intevation.gnv.math.Point2d;
20 import de.intevation.gnv.state.InputData; 14 import de.intevation.gnv.state.InputData;
21 15
22 import java.util.Arrays;
23 import java.util.ArrayList; 16 import java.util.ArrayList;
24 import java.util.Collection; 17 import java.util.Collection;
25 import java.util.List; 18 import java.util.List;
26 import java.util.Map; 19 import java.util.Map;
27 20
34 27
35 public abstract class WKTUtils { 28 public abstract class WKTUtils {
36 29
37 private static Logger log = Logger.getLogger(WKTUtils.class); 30 private static Logger log = Logger.getLogger(WKTUtils.class);
38 31
39 private static final String [] DIFF_COLUMS = {
40 "GROUP1",
41 "GROUP2",
42 "GROUP3"
43 };
44
45 private static final String [] COLUMN_BLACKLIST = {
46 "MEDIAN.MESHPOINT.JPOSITION",
47 "MEDIAN.MESHPOINT.IPOSITION"
48 };
49
50 public static final double NAUTICAL_MILE = 1852.216d; 32 public static final double NAUTICAL_MILE = 1852.216d;
51 public static final double KILOMETER = 1000d; 33 public static final double KILOMETER = 1000d;
52 34
53 public static final double EPSILON = 1e-5d; 35 public static boolean different(Result a, Result b, int [] indices) {
54 public static final int INTERPOLATION_STEPS =
55 Integer.getInteger("interpolation.steps", 500).intValue();
56
57
58 public static final class SectionHandler
59 implements Interpolation2D.Consumer
60 {
61 private ArrayList<Point2d> points;
62 private List<Coordinate> path;
63 private Collection<Result> output;
64 private Result prototyp;
65 private ResultDescriptor descriptor;
66 private boolean lastWasSuccess;
67
68 public SectionHandler() {
69 }
70
71 public SectionHandler(
72 List<Coordinate> path,
73 Collection<Result> output,
74 ResultDescriptor descriptor
75 ) {
76 this.path = path;
77 this.output = output;
78 this.descriptor = descriptor;
79 points = new ArrayList<Point2d>();
80 lastWasSuccess = true;
81 }
82
83 public void finish() {
84 if (!points.isEmpty()) {
85 double distance = toKM(
86 DistanceCalculator.calculateDistance(path));
87
88 if (distance > EPSILON) {
89
90 Interpolation2D.interpolate(
91 path,
92 points,
93 0d,
94 distance,
95 INTERPOLATION_STEPS,
96 LinearMetrics.INSTANCE,
97 this);
98 }
99
100 points.clear();
101 }
102 lastWasSuccess = true;
103 }
104
105 public void setPrototyp(Result prototyp) {
106 this.prototyp = prototyp;
107 }
108
109 public void handle(Result result) {
110 Coordinate coordinate =
111 toCoordinate(result.getString("SHAPE"));
112 double value = result.getDouble("YORDINATE");
113 int iPos = result.getInteger("MEDIAN.MESHPOINT.JPOSITION");
114 int jPos = result.getInteger("MEDIAN.MESHPOINT.JPOSITION");
115 Point2d p = new Point2d(
116 coordinate.x,
117 coordinate.y,
118 value,
119 iPos, jPos);
120 points.add(p);
121 }
122
123 public void interpolated(Coordinate coordinate, boolean success) {
124 DefaultResult result = new DefaultResult(descriptor);
125 ResultDescriptor pd = prototyp.getResultDescriptor();
126
127 int pcolums = pd.getColumnCount();
128 for (int i = 0, j = 0; i < pcolums; ++i) {
129 String colname = pd.getColumnName(i);
130 if (blacklisted(colname)) {
131 continue;
132 }
133 if (colname.equals("SHAPE")) {
134 result.addColumnValue(j, OutputUtils.toWKT(coordinate));
135 }
136 else if (colname.equals("YORDINATE")) {
137 if (success) {
138 result.addColumnValue(j, Double.valueOf(coordinate.z));
139 }
140 else if (lastWasSuccess) {
141 // only insert null if last was valid.
142 // This prevents flooding the result set with nulls
143 // if interpolating over a large gap.
144 result.addColumnValue(j, null);
145 }
146 }
147 else {
148 result.addColumnValue(j, prototyp.getObject(i));
149 }
150 ++j;
151 }
152 output.add(result);
153 lastWasSuccess = success;
154 }
155 }
156
157
158 private static final boolean blacklisted(String column) {
159 for (int i = 0; i < COLUMN_BLACKLIST.length; ++i) {
160 if (COLUMN_BLACKLIST.equals(column)) {
161 return true;
162 }
163 }
164 return false;
165 }
166
167 private static boolean different(Result a, Result b, int [] indices) {
168 for (int i = 0; i < indices.length; ++i) { 36 for (int i = 0; i < indices.length; ++i) {
169 String oa = a.getString(indices[i]); 37 String oa = a.getString(indices[i]);
170 String ob = b.getString(indices[i]); 38 String ob = b.getString(indices[i]);
171 39
172 if (oa == null && ob == null) { 40 if (oa == null && ob == null) {
184 } 52 }
185 return true; 53 return true;
186 } 54 }
187 } 55 }
188 return false; 56 return false;
189 }
190
191 public static Collection<Result> process(
192 List<Coordinate> path,
193 Collection<Result> input
194 ) {
195 log.debug("------ number of points before processing: " + input.size());
196 ArrayList<Result> output = new ArrayList<Result>();
197
198 Result last = null;
199
200 int [] diffColums = null;
201
202 SectionHandler sectionHandler = null;
203
204 for (Result result: input) {
205
206 if (sectionHandler == null) {
207
208 ResultDescriptor rd = result.getResultDescriptor();
209 diffColums = rd.getColumnIndices(DIFF_COLUMS);
210 int columns = rd.getColumnCount();
211
212 DefaultResultDescriptor resultDescriptor =
213 new DefaultResultDescriptor();
214
215 for (int j = 0; j < columns; ++j) {
216 String columnName = rd.getColumnName(j);
217 if (!blacklisted(columnName)) {
218 resultDescriptor.addColumn(
219 columnName,
220 rd.getColumnClassName(j));
221 }
222 }
223
224 sectionHandler = new SectionHandler(
225 path,
226 output,
227 resultDescriptor);
228
229 sectionHandler.setPrototyp(result);
230 }
231
232 if (last != null && different(last, result, diffColums)) {
233 sectionHandler.finish();
234 sectionHandler.setPrototyp(result);
235 }
236
237 sectionHandler.handle(result);
238
239 last = result;
240 }
241
242 if (sectionHandler != null) {
243 sectionHandler.finish();
244 }
245
246 log.debug("------ number of points after processing: " + output.size());
247
248 return output;
249 } 57 }
250 58
251 59
252 public static Coordinate toCoordinate(String shape) { 60 public static Coordinate toCoordinate(String shape) {
253 try { 61 try {
273 .append(')'); 81 .append(')');
274 return sb.toString(); 82 return sb.toString();
275 } 83 }
276 84
277 85
278 public static Collection<Result> worldCoordinatesToIndex( 86 public static String worldCoordinatesToIndex(
279 Collection<Result> result, 87 Collection<Result> result,
280 Map<String, InputData> inputData, 88 Map<String, InputData> inputData,
281 String ijkQueryID, 89 String ijkQueryID
282 String queryID,
283 String[] filterValues
284 ) throws ParseException, QueryException 90 ) throws ParseException, QueryException
285 { 91 {
286 // 1. IJK Anfragen für Stuetzpunkte im Netz ausführen. 92 // 1. IJK Anfragen für Stuetzpunkte im Netz ausführen.
287 LineString ls = (LineString)new WKTReader().read( 93 LineString ls = (LineString)new WKTReader().read(
288 inputData.get("mesh_linestring").getValue()); 94 inputData.get("mesh_linestring").getValue());
379 log.debug("Additional Where Clause = "+additionWhere); 185 log.debug("Additional Where Clause = "+additionWhere);
380 // 2. Aus diesen Stuetzpunkten den Resultset generieren. 186 // 2. Aus diesen Stuetzpunkten den Resultset generieren.
381 } 187 }
382 } // if generate index filter 188 } // if generate index filter
383 189
384 String[] addedFilterValues = new String[filterValues.length+1]; 190 return additionWhere;
385 System.arraycopy(filterValues, 0, addedFilterValues, 0, filterValues.length);
386 addedFilterValues[filterValues.length] = additionWhere;
387
388 result = process(
389 Arrays.asList(coords),
390 queryExecutor.executeQuery(
391 queryID,
392 addedFilterValues));
393
394 return result;
395 } 191 }
396 } 192 }

http://dive4elements.wald.intevation.org