comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java @ 471:06887e2e3f7a

Some minor code cleanup gnv-artifacts/trunk@534 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 12 Jan 2010 17:41:59 +0000
parents 7ba4c7222265
children 211cad2fb5ba
comparison
equal deleted inserted replaced
470:b7bb66440cc8 471:06887e2e3f7a
1 package de.intevation.gnv.utils; 1 package de.intevation.gnv.utils;
2 2
3 import com.vividsolutions.jts.geom.Coordinate; 3 import com.vividsolutions.jts.geom.Coordinate;
4 import com.vividsolutions.jts.geom.Point; 4 import com.vividsolutions.jts.geom.Point;
5 import com.vividsolutions.jts.geom.Polygon;
5 import com.vividsolutions.jts.geom.LineString; 6 import com.vividsolutions.jts.geom.LineString;
6 import com.vividsolutions.jts.io.ParseException; 7 import com.vividsolutions.jts.io.ParseException;
8
7 import com.vividsolutions.jts.io.WKTReader; 9 import com.vividsolutions.jts.io.WKTReader;
8 10
9 import de.intevation.gnv.geobackend.base.Result; 11 import de.intevation.gnv.geobackend.base.Result;
10 import de.intevation.gnv.geobackend.base.query.QueryExecutor; 12 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
11 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; 13 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
29 private static Logger log = Logger.getLogger(WKTUtils.class); 31 private static Logger log = Logger.getLogger(WKTUtils.class);
30 32
31 public static final double NAUTICAL_MILE = 1852.216d; 33 public static final double NAUTICAL_MILE = 1852.216d;
32 public static final double KILOMETER = 1000d; 34 public static final double KILOMETER = 1000d;
33 35
36 public static final String I_NAME = "MEDIAN.MESHPOINT.IPOSITION";
37 public static final String J_NAME = "MEDIAN.MESHPOINT.JPOSITION";
38
39 public static final String TRUE_EXPRESSION = "FEATUREID=FEATUREID";
40
34 public static boolean different(Result a, Result b, int [] indices) { 41 public static boolean different(Result a, Result b, int [] indices) {
35 for (int i = 0; i < indices.length; ++i) { 42 for (int i = 0; i < indices.length; ++i) {
36 String oa = a.getString(indices[i]); 43 String oa = a.getString(indices[i]);
37 String ob = b.getString(indices[i]); 44 String ob = b.getString(indices[i]);
38 45
43 if (oa == null || ob == null) { 50 if (oa == null || ob == null) {
44 return true; 51 return true;
45 } 52 }
46 53
47 if (!oa.equals(ob)) { 54 if (!oa.equals(ob)) {
48 if (log.isDebugEnabled()) {
49 log.debug("+++++++++++++++ differs ++++++++++++++");
50 log.debug(" " + oa + " != " + ob);
51 }
52 return true; 55 return true;
53 } 56 }
54 } 57 }
55 return false; 58 return false;
56 } 59 }
57
58 60
59 public static Coordinate toCoordinate(String shape) { 61 public static Coordinate toCoordinate(String shape) {
60 try { 62 try {
61 return ((Point)(new WKTReader().read(shape))).getCoordinate(); 63 return ((Point)(new WKTReader().read(shape))).getCoordinate();
62 } 64 }
63 catch (ParseException pe) { 65 catch (ParseException pe) {
64 log.error(pe); 66 log.error(pe);
65 } 67 }
68 catch (ClassCastException cce) {
69 log.error("cannot read WKT point", cce);
70 }
66 return null; 71 return null;
67 } 72 }
68 73
74 public static Polygon toPolygon(String shape) {
75 try {
76 return (Polygon)new WKTReader().read(shape);
77 }
78 catch (ParseException pe) {
79 log.error(pe);
80 }
81 catch (ClassCastException cce) {
82 log.error("cannot read WKT polygon", cce);
83 }
84 return null;
85 }
69 86
70 public static final double toKM(double distance) { 87 public static final double toKM(double distance) {
71 return (distance * NAUTICAL_MILE) / KILOMETER; 88 return (distance * NAUTICAL_MILE) / KILOMETER;
72 } 89 }
73 90
77 sb.append(coordinate.x) 94 sb.append(coordinate.x)
78 .append(' ') 95 .append(' ')
79 .append(coordinate.y) 96 .append(coordinate.y)
80 .append(')'); 97 .append(')');
81 return sb.toString(); 98 return sb.toString();
99 }
100
101 public static final String indexBox(
102 java.awt.Point a,
103 java.awt.Point b,
104 String iName,
105 String jName
106 ) {
107 int minI = Math.min(a.x, b.x) - 1;
108 int maxI = Math.max(a.x, b.x) + 1;
109 int minJ = Math.min(a.y, b.y) - 1;
110 int maxJ = Math.max(a.y, b.y) + 1;
111 StringBuilder sb = new StringBuilder("(")
112 .append(iName).append(" >= ").append(minI)
113 .append(" AND ").append(iName).append(" <= ").append(maxI)
114 .append(" AND ").append(jName).append(" >= ").append(minJ)
115 .append(" AND ").append(jName).append(" <= ").append(maxJ)
116 .append(')');
117 return sb.toString();
118 }
119
120 public static final String diagonalBox(List<java.awt.Point> points) {
121
122 if (points.get(0) != null && points.get(2) != null) {
123 return indexBox(
124 points.get(0), points.get(2),
125 I_NAME,
126 J_NAME);
127 }
128
129 if (points.get(1) != null && points.get(3) != null) {
130 return indexBox(
131 points.get(1), points.get(3),
132 I_NAME,
133 J_NAME);
134 }
135
136 return null;
82 } 137 }
83 138
84 public static String worldEnvelopeCoordinatesToIndex( 139 public static String worldEnvelopeCoordinatesToIndex(
85 Coordinate [] coords, 140 Coordinate [] coords,
86 Collection<Result> result, 141 Collection<Result> result,
87 String meshid, 142 String meshid,
88 String ijkQueryID 143 String ijkQueryID
89 ) 144 )
90 throws QueryException 145 throws QueryException
91 { 146 {
92 List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length); 147 List<java.awt.Point> points =
93 ArrayList<Object []> missingPoints = new ArrayList<Object []>(); 148 new ArrayList<java.awt.Point>(coords.length);
149
150 ArrayList<Object []> missingPoints =
151 new ArrayList<Object []>();
152
94 createIJKPoints(coords, meshid, ijkQueryID, points, missingPoints); 153 createIJKPoints(coords, meshid, ijkQueryID, points, missingPoints);
95 154
96 String additionWhere = "FEATUREID=FEATUREID"; 155 String additionWhere = null;
156
97 if (missingPoints.size() == coords.length) { 157 if (missingPoints.size() == coords.length) {
98 log.debug("cannot create index buffer"); 158 log.debug("cannot create index buffer");
99 }else { 159 }
100 if (points.get(0) != null && points.get(2) != null){ 160 else {
101 additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(0).x+" AND " + 161 additionWhere = diagonalBox(points);
102 " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(0).y+" AND " + 162
103 " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(2).x+" AND " + 163 if (additionWhere == null) {
104 " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(2).y+")"; 164 // 3 Points are missing or are on one side of the envelope
105 }else if (points.get(1) != null && points.get(3) != null){ 165 boolean remainsMissingPoints = calculateIJ4MissingPoints(
106 additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(1).x+" AND " + 166 coords, points, missingPoints);
107 " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(1).y+" AND " + 167
108 " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(3).x+" AND " + 168 if (!remainsMissingPoints) {
109 " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(3).y+")"; 169 additionWhere = diagonalBox(points);
110 }else{ 170 }
111 // 3 Points are Missing or the Points one one Side of the Envelopes are Missing 171 }
112 172 }
113 boolean remainsMissingPoints = calculateIJ4MissingPoints(coords, 173
114 points, missingPoints); 174 return additionWhere != null
115 175 ? additionWhere
116 if (!remainsMissingPoints || (points.get(0) != null && points.get(2) != null)){ 176 : TRUE_EXPRESSION;
117 additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(0).x+" AND " + 177 }
118 " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(0).y+" AND " + 178
119 " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(2).x+" AND " +
120 " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(2).y+")";
121 }else if (points.get(1) != null && points.get(3) != null){
122 additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(1).x+" AND " +
123 " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(1).y+" AND " +
124 " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(3).x+" AND " +
125 " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(3).y+")";
126 }
127 }
128 }
129 return additionWhere;
130 }
131 public static String worldCoordinatesToIndex( 179 public static String worldCoordinatesToIndex(
132 Coordinate [] coords, 180 Coordinate [] coords,
133 Collection<Result> result, 181 Collection<Result> result,
134 String meshid, 182 String meshid,
135 String ijkQueryID 183 String ijkQueryID
136 ) 184 )
137 throws QueryException 185 throws QueryException
138 { 186 {
139 // 1. IJK Anfragen für Stuetzpunkte im Netz ausführen.
140
141 List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length); 187 List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length);
142 188
143 ArrayList<Object []> missingPoints = new ArrayList<Object []>(); 189 ArrayList<Object []> missingPoints = new ArrayList<Object []>();
144 190
145 createIJKPoints(coords, meshid, ijkQueryID, points, missingPoints); 191 createIJKPoints(coords, meshid, ijkQueryID, points, missingPoints);
146 192
147 String additionWhere = "FEATUREID=FEATUREID"; 193 String additionWhere = TRUE_EXPRESSION;
194
148 if (missingPoints.size() == coords.length) { 195 if (missingPoints.size() == coords.length) {
149 log.debug("cannot create index buffer"); 196 log.debug("cannot create index buffer");
150 } 197 }
151 else { // generate index filter 198 else { // generate index filter
152 boolean remainsMissingPoints = calculateIJ4MissingPoints(coords, 199 boolean remainsMissingPoints = calculateIJ4MissingPoints(
153 points, missingPoints); 200 coords, points, missingPoints);
154 201
155 if (!remainsMissingPoints) { 202 if (!remainsMissingPoints) {
156 // TODO: Make Tablenames and Columns Configurable 203 // TODO: Make Tablenames and Columns Configurable
157 IndexBuffer ib = new IndexBuffer( 204 IndexBuffer ib = new IndexBuffer(
158 points, 205 points,
159 "MEDIAN.MESHPOINT.IPOSITION", 206 I_NAME,
160 "MEDIAN.MESHPOINT.JPOSITION" ); 207 J_NAME );
161 additionWhere = ib.toWhereClause(); 208 additionWhere = ib.toWhereClause();
162 log.debug("Additional Where Clause = "+additionWhere); 209 log.debug("Additional Where Clause = "+additionWhere);
163 // 2. Aus diesen Stuetzpunkten den Resultset generieren.
164 } 210 }
165 } // if generate index filter 211 } // if generate index filter
166 212
167 return additionWhere; 213 return additionWhere;
168 } 214 }
173 * @param points 219 * @param points
174 * @param missingPoints 220 * @param missingPoints
175 * @return 221 * @return
176 */ 222 */
177 private static boolean calculateIJ4MissingPoints( 223 private static boolean calculateIJ4MissingPoints(
178 Coordinate[] coords, 224 Coordinate [] coords,
179 List<java.awt.Point> points, 225 List<java.awt.Point> points,
180 ArrayList<Object[]> missingPoints) { 226 ArrayList<Object[]> missingPoints
227 ) {
181 boolean remainsMissingPoints = !missingPoints.isEmpty(); 228 boolean remainsMissingPoints = !missingPoints.isEmpty();
182 229
183 if (remainsMissingPoints) { 230 if (remainsMissingPoints) {
184 // try to guess the missing (i, j) 231 // try to guess the missing (i, j)
185 CurveFitter iFitter = new CurveFitter(new GaussNewtonOptimizer(true)); 232 CurveFitter iFitter = new CurveFitter(new GaussNewtonOptimizer(true));
232 * @param ijkQueryID 279 * @param ijkQueryID
233 * @param points 280 * @param points
234 * @param missingPoints 281 * @param missingPoints
235 * @throws QueryException 282 * @throws QueryException
236 */ 283 */
237 private static void createIJKPoints(Coordinate[] coords, String meshid, 284 private static void createIJKPoints(
238 String ijkQueryID, 285 Coordinate[] coords,
239 List<java.awt.Point> points, 286 String meshid,
240 ArrayList<Object []> missingPoints) 287 String ijkQueryID,
241 throws QueryException { 288 List<java.awt.Point> points,
242 Collection<Result> result; 289 ArrayList<Object []> missingPoints
290 )
291 throws QueryException
292 {
293 boolean debug = log.isDebugEnabled();
294
243 QueryExecutor queryExecutor = QueryExecutorFactory 295 QueryExecutor queryExecutor = QueryExecutorFactory
244 .getInstance() 296 .getInstance()
245 .getQueryExecutor(); 297 .getQueryExecutor();
246 298
247 for (int i = 0; i < coords.length; i++) { 299 for (int i = 0; i < coords.length; i++) {
248 300
249 String wkt = toWKT(coords[i]); 301 String wkt = toWKT(coords[i]);
250 302
251 result = queryExecutor.executeQuery(ijkQueryID, 303 Collection<Result> result = queryExecutor.executeQuery(
252 new String[]{meshid,wkt}); 304 ijkQueryID,
253 if (!result.isEmpty()){ 305 new String [] {meshid, wkt});
306
307 if (!result.isEmpty()) {
254 Result resultValue = result.iterator().next(); 308 Result resultValue = result.iterator().next();
255 int iPos = resultValue.getInteger(1); 309 int iPos = resultValue.getInteger(1);
256 int jPos = resultValue.getInteger(0); 310 int jPos = resultValue.getInteger(0);
257 log.debug("Found Pos "+iPos+"/"+jPos +" for "+wkt); 311 if (debug) {
312 log.debug("Found Pos "+iPos+"/"+jPos +" for "+wkt);
313 }
258 points.add(i, new java.awt.Point(iPos,jPos)); 314 points.add(i, new java.awt.Point(iPos,jPos));
259 }else{ 315 }
260 log.debug("No i/j Pos found for "+wkt); 316 else {
317 if (debug) {
318 log.debug("No i/j Pos found for "+wkt);
319 }
261 missingPoints.add(new Object [] { Integer.valueOf(i), coords[i] }); 320 missingPoints.add(new Object [] { Integer.valueOf(i), coords[i] });
262 points.add(i, null); 321 points.add(i, null);
263 // Special Case no i,j found for Coordinate 322 // Special Case no i,j found for Coordinate
264 } 323 }
265 } 324 }

http://dive4elements.wald.intevation.org