comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java @ 468:7ba4c7222265

Added ij-Index determination for horizontal-cross-sections. gnv-artifacts/trunk@531 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 12 Jan 2010 11:34:25 +0000
parents eb2ac62e853a
children 06887e2e3f7a
comparison
equal deleted inserted replaced
467:b2d2b36b20a0 468:7ba4c7222265
79 .append(coordinate.y) 79 .append(coordinate.y)
80 .append(')'); 80 .append(')');
81 return sb.toString(); 81 return sb.toString();
82 } 82 }
83 83
84 84 public static String worldEnvelopeCoordinatesToIndex(
85 Coordinate [] coords,
86 Collection<Result> result,
87 String meshid,
88 String ijkQueryID
89 )
90 throws QueryException
91 {
92 List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length);
93 ArrayList<Object []> missingPoints = new ArrayList<Object []>();
94 createIJKPoints(coords, meshid, ijkQueryID, points, missingPoints);
95
96 String additionWhere = "FEATUREID=FEATUREID";
97 if (missingPoints.size() == coords.length) {
98 log.debug("cannot create index buffer");
99 }else {
100 if (points.get(0) != null && points.get(2) != null){
101 additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(0).x+" AND " +
102 " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(0).y+" AND " +
103 " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(2).x+" AND " +
104 " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(2).y+")";
105 }else if (points.get(1) != null && points.get(3) != null){
106 additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(1).x+" AND " +
107 " MEDIAN.MESHPOINT.JPOSITION <= "+points.get(1).y+" AND " +
108 " MEDIAN.MESHPOINT.IPOSITION <= "+points.get(3).x+" AND " +
109 " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(3).y+")";
110 }else{
111 // 3 Points are Missing or the Points one one Side of the Envelopes are Missing
112
113 boolean remainsMissingPoints = calculateIJ4MissingPoints(coords,
114 points, missingPoints);
115
116 if (!remainsMissingPoints || (points.get(0) != null && points.get(2) != null)){
117 additionWhere = "(MEDIAN.MESHPOINT.IPOSITION >= "+points.get(0).x+" AND " +
118 " MEDIAN.MESHPOINT.JPOSITION >= "+points.get(0).y+" AND " +
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 }
85 public static String worldCoordinatesToIndex( 131 public static String worldCoordinatesToIndex(
86 Coordinate [] coords, 132 Coordinate [] coords,
87 Collection<Result> result, 133 Collection<Result> result,
88 String meshid, 134 String meshid,
89 String ijkQueryID 135 String ijkQueryID
92 { 138 {
93 // 1. IJK Anfragen für Stuetzpunkte im Netz ausführen. 139 // 1. IJK Anfragen für Stuetzpunkte im Netz ausführen.
94 140
95 List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length); 141 List<java.awt.Point> points = new ArrayList<java.awt.Point>(coords.length);
96 142
143 ArrayList<Object []> missingPoints = new ArrayList<Object []>();
144
145 createIJKPoints(coords, meshid, ijkQueryID, points, missingPoints);
146
147 String additionWhere = "FEATUREID=FEATUREID";
148 if (missingPoints.size() == coords.length) {
149 log.debug("cannot create index buffer");
150 }
151 else { // generate index filter
152 boolean remainsMissingPoints = calculateIJ4MissingPoints(coords,
153 points, missingPoints);
154
155 if (!remainsMissingPoints) {
156 // TODO: Make Tablenames and Columns Configurable
157 IndexBuffer ib = new IndexBuffer(
158 points,
159 "MEDIAN.MESHPOINT.IPOSITION",
160 "MEDIAN.MESHPOINT.JPOSITION" );
161 additionWhere = ib.toWhereClause();
162 log.debug("Additional Where Clause = "+additionWhere);
163 // 2. Aus diesen Stuetzpunkten den Resultset generieren.
164 }
165 } // if generate index filter
166
167 return additionWhere;
168 }
169
170
171 /**
172 * @param coords
173 * @param points
174 * @param missingPoints
175 * @return
176 */
177 private static boolean calculateIJ4MissingPoints(
178 Coordinate[] coords,
179 List<java.awt.Point> points,
180 ArrayList<Object[]> missingPoints) {
181 boolean remainsMissingPoints = !missingPoints.isEmpty();
182
183 if (remainsMissingPoints) {
184 // try to guess the missing (i, j)
185 CurveFitter iFitter = new CurveFitter(new GaussNewtonOptimizer(true));
186 CurveFitter jFitter = new CurveFitter(new GaussNewtonOptimizer(true));
187
188 for (int i = 0, N = points.size(); i < N; ++i) {
189 java.awt.Point p = (java.awt.Point)points.get(i);
190 if (p != null) {
191 Coordinate coord = coords[i];
192 iFitter.addObservedPoint(coord.x, p.x);
193 jFitter.addObservedPoint(coord.y, p.y);
194 }
195 }
196 try {
197 // XXX: Assumption: (i, j) are created by componentwise linear function.
198 // This is surely not correct because (x, y) are in a ellipsoid projection.
199 // TODO: use ellipsoid functions and fit with Levenberg Marquardt.
200 double [] iParams = iFitter.fit(
201 LinearFunction.INSTANCE, new double [] { 1d, 1d });
202
203 double [] jParams = jFitter.fit(
204 LinearFunction.INSTANCE, new double [] { 1d, 1d });
205
206 for (int i = missingPoints.size()-1; i >= 0; --i) {
207 Object [] a = (Object [])missingPoints.get(i);
208 Coordinate coord = (Coordinate)a[1];
209 int pi = (int)Math.round(iParams[0]*coord.x + iParams[1]);
210 int pj = (int)Math.round(jParams[0]*coord.y + jParams[1]);
211 points.set(
212 ((Integer)a[0]).intValue(),
213 new java.awt.Point(pi, pj));
214 }
215
216 remainsMissingPoints = false; // we filled the gaps
217 }
218 catch (FunctionEvaluationException fee) {
219 log.error(fee);
220 }
221 catch (OptimizationException oe) {
222 log.error(oe);
223 }
224 }
225 return remainsMissingPoints;
226 }
227
228
229 /**
230 * @param coords
231 * @param meshid
232 * @param ijkQueryID
233 * @param points
234 * @param missingPoints
235 * @throws QueryException
236 */
237 private static void createIJKPoints(Coordinate[] coords, String meshid,
238 String ijkQueryID,
239 List<java.awt.Point> points,
240 ArrayList<Object []> missingPoints)
241 throws QueryException {
242 Collection<Result> result;
97 QueryExecutor queryExecutor = QueryExecutorFactory 243 QueryExecutor queryExecutor = QueryExecutorFactory
98 .getInstance() 244 .getInstance()
99 .getQueryExecutor(); 245 .getQueryExecutor();
100 246
101 ArrayList missingPoints = new ArrayList();
102
103 String additionWhere = "FEATUREID=FEATUREID";
104
105 for (int i = 0; i < coords.length; i++) { 247 for (int i = 0; i < coords.length; i++) {
106 248
107 String wkt = toWKT(coords[i]); 249 String wkt = toWKT(coords[i]);
108 250
109 result = queryExecutor.executeQuery(ijkQueryID, 251 result = queryExecutor.executeQuery(ijkQueryID,
119 missingPoints.add(new Object [] { Integer.valueOf(i), coords[i] }); 261 missingPoints.add(new Object [] { Integer.valueOf(i), coords[i] });
120 points.add(i, null); 262 points.add(i, null);
121 // Special Case no i,j found for Coordinate 263 // Special Case no i,j found for Coordinate
122 } 264 }
123 } 265 }
124
125 if (missingPoints.size() == coords.length) {
126 log.debug("cannot create index buffer");
127 }
128 else { // generate index filter
129 boolean remainsMissingPoints = !missingPoints.isEmpty();
130
131 if (remainsMissingPoints) {
132 // try to guess the missing (i, j)
133 CurveFitter iFitter = new CurveFitter(new GaussNewtonOptimizer(true));
134 CurveFitter jFitter = new CurveFitter(new GaussNewtonOptimizer(true));
135
136 for (int i = 0, N = points.size(); i < N; ++i) {
137 java.awt.Point p = (java.awt.Point)points.get(i);
138 if (p != null) {
139 Coordinate coord = coords[i];
140 iFitter.addObservedPoint(coord.x, p.x);
141 jFitter.addObservedPoint(coord.y, p.y);
142 }
143 }
144 try {
145 // XXX: Assumption: (i, j) are created by componentwise linear function.
146 // This is surely not correct because (x, y) are in a ellipsoid projection.
147 // TODO: use ellipsoid functions and fit with Levenberg Marquardt.
148 double [] iParams = iFitter.fit(
149 LinearFunction.INSTANCE, new double [] { 1d, 1d });
150
151 double [] jParams = jFitter.fit(
152 LinearFunction.INSTANCE, new double [] { 1d, 1d });
153
154 for (int i = missingPoints.size()-1; i >= 0; --i) {
155 Object [] a = (Object [])missingPoints.get(i);
156 Coordinate coord = (Coordinate)a[1];
157 int pi = (int)Math.round(iParams[0]*coord.x + iParams[1]);
158 int pj = (int)Math.round(jParams[0]*coord.y + jParams[1]);
159 points.set(
160 ((Integer)a[0]).intValue(),
161 new java.awt.Point(pi, pj));
162 }
163
164 remainsMissingPoints = false; // we filled the gaps
165 }
166 catch (FunctionEvaluationException fee) {
167 log.error(fee);
168 }
169 catch (OptimizationException oe) {
170 log.error(oe);
171 }
172 }
173
174 if (!remainsMissingPoints) {
175 // TODO: Make Tablenames and Columns Configurable
176 IndexBuffer ib = new IndexBuffer(
177 points,
178 "MEDIAN.MESHPOINT.IPOSITION",
179 "MEDIAN.MESHPOINT.JPOSITION" );
180 additionWhere = ib.toWhereClause();
181 log.debug("Additional Where Clause = "+additionWhere);
182 // 2. Aus diesen Stuetzpunkten den Resultset generieren.
183 }
184 } // if generate index filter
185
186 return additionWhere;
187 } 266 }
188 267
189 public static Coordinate [] toCoordinates(String wkt) { 268 public static Coordinate [] toCoordinates(String wkt) {
190 try { 269 try {
191 LineString ls = (LineString)new WKTReader().read(wkt); 270 LineString ls = (LineString)new WKTReader().read(wkt);

http://dive4elements.wald.intevation.org