Revision 29972 branches/v2_0_0_prep/libraries/libFMap_geometries/src/org/gvsig/fmap/geom/impl/Geometry2D.java
Geometry2D.java | ||
---|---|---|
36 | 36 |
|
37 | 37 |
import org.cresques.cts.ICoordTrans; |
38 | 38 |
import org.gvsig.fmap.geom.Geometry; |
39 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
40 |
import org.gvsig.fmap.geom.GeometryManager; |
|
41 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
39 | 42 |
import org.gvsig.fmap.geom.handler.Handler; |
43 |
import org.gvsig.fmap.geom.operation.GeometryOperation; |
|
40 | 44 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
41 | 45 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
42 | 46 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
47 |
import org.gvsig.fmap.geom.operation.relationship.DefaultRelationshipGeometryOperationContext; |
|
43 | 48 |
import org.gvsig.fmap.geom.primitive.Envelope; |
44 |
import org.gvsig.fmap.geom.primitive.FShape; |
|
45 | 49 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
46 |
import org.gvsig.fmap.geom.primitive.Primitive; |
|
47 | 50 |
import org.gvsig.fmap.geom.type.GeometryType; |
51 |
import org.slf4j.Logger; |
|
52 |
import org.slf4j.LoggerFactory; |
|
48 | 53 |
|
49 | 54 |
/** |
50 | 55 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a> |
51 | 56 |
*/ |
52 | 57 |
public class Geometry2D implements Geometry{ |
58 |
|
|
59 |
private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class); |
|
60 |
|
|
53 | 61 |
private Geometry geometry = null; |
54 | 62 |
|
55 | 63 |
/** |
... | ... | |
194 | 202 |
* @see java.awt.Shape#contains(java.awt.geom.Point2D) |
195 | 203 |
*/ |
196 | 204 |
public boolean contains(Point2D p) { |
197 |
return geometry.contains(p); |
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
try { |
|
209 |
return containsPoint(geometry, p.getX(), p.getY()); |
|
210 |
} catch (GeometryOperationException e) { |
|
211 |
logger.error("While doing contains: " + e.getMessage(), e); |
|
212 |
return true; |
|
213 |
} |
|
198 | 214 |
} |
199 | 215 |
|
200 | 216 |
/* (non-Javadoc) |
201 | 217 |
* @see java.awt.Shape#contains(java.awt.geom.Rectangle2D) |
202 | 218 |
*/ |
203 | 219 |
public boolean contains(Rectangle2D r) { |
204 |
return geometry.contains(r); |
|
220 |
try { |
|
221 |
return containsRectangle(geometry, |
|
222 |
r.getMinX(), |
|
223 |
r.getMinY(), |
|
224 |
r.getWidth(), |
|
225 |
r.getHeight()); |
|
226 |
} catch (GeometryOperationException e) { |
|
227 |
logger.error("While doing contains: " + e.getMessage(), e); |
|
228 |
return true; |
|
229 |
} |
|
205 | 230 |
} |
206 | 231 |
|
207 | 232 |
/* (non-Javadoc) |
208 | 233 |
* @see java.awt.Shape#contains(double, double) |
209 | 234 |
*/ |
210 | 235 |
public boolean contains(double x, double y) { |
211 |
return geometry.contains(x, y); |
|
236 |
try { |
|
237 |
return containsPoint(geometry, x, y); |
|
238 |
} catch (GeometryOperationException e) { |
|
239 |
logger.error("While doing contains: " + e.getMessage(), e); |
|
240 |
return true; |
|
241 |
} |
|
212 | 242 |
} |
213 | 243 |
|
214 | 244 |
/* (non-Javadoc) |
215 | 245 |
* @see java.awt.Shape#contains(double, double, double, double) |
216 | 246 |
*/ |
217 | 247 |
public boolean contains(double x, double y, double w, double h) { |
218 |
return geometry.contains(x, y, w, h); |
|
248 |
try { |
|
249 |
return containsRectangle(geometry, x, y, w, h); |
|
250 |
} catch (GeometryOperationException e) { |
|
251 |
logger.error("While doing contains: " + e.getMessage(), e); |
|
252 |
return true; |
|
253 |
} |
|
219 | 254 |
} |
220 | 255 |
|
221 | 256 |
/* (non-Javadoc) |
... | ... | |
229 | 264 |
* @see java.awt.Shape#intersects(double, double, double, double) |
230 | 265 |
*/ |
231 | 266 |
public boolean intersects(double x, double y, double w, double h) { |
232 |
return geometry.intersects(x, y, w, h); |
|
267 |
try { |
|
268 |
return intersectsRectangle(geometry, x, y, w, h); |
|
269 |
} catch (GeometryOperationException e) { |
|
270 |
logger.error("While doing intersects: " + e.getMessage(), e); |
|
271 |
return true; |
|
272 |
} |
|
233 | 273 |
} |
234 | 274 |
|
235 | 275 |
/* (non-Javadoc) |
... | ... | |
239 | 279 |
return geometry.compareTo(arg0); |
240 | 280 |
} |
241 | 281 |
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
/** |
|
291 |
* Utility method |
|
292 |
* @param geometry |
|
293 |
* @param x |
|
294 |
* @param y |
|
295 |
* @return |
|
296 |
* @throws GeometryOperationException |
|
297 |
*/ |
|
298 |
protected boolean containsPoint(Geometry geom, double x, double y) throws GeometryOperationException { |
|
299 |
|
|
300 |
// exclude disjoint |
|
301 |
Envelope env = geom.getEnvelope(); |
|
302 |
if (x > env.getMaximum(0)) return false; |
|
303 |
if (y > env.getMaximum(1)) return false; |
|
304 |
if (x < env.getMinimum(0)) return false; |
|
305 |
if (y < env.getMinimum(1)) return false; |
|
306 |
|
|
307 |
// boxes overlap, need long version |
|
308 |
|
|
309 |
Geometry geompoint = null; |
|
310 |
try { |
|
311 |
geompoint = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM2D); |
|
312 |
} catch (Exception e1) { |
|
313 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e1); |
|
314 |
} |
|
315 |
|
|
316 |
DefaultRelationshipGeometryOperationContext drgoc = |
|
317 |
new DefaultRelationshipGeometryOperationContext(geompoint); |
|
318 |
|
|
319 |
Object resp = null; |
|
320 |
boolean respboolean = true; |
|
321 |
try { |
|
322 |
resp = geom.invokeOperation(GeometryOperation.OPERATION_CONTAINS_CODE, drgoc); |
|
323 |
respboolean = ((Boolean) resp).booleanValue(); |
|
324 |
} catch (Exception e) { |
|
325 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e); |
|
326 |
} |
|
242 | 327 |
|
328 |
return respboolean; |
|
329 |
} |
|
330 |
|
|
331 |
/** |
|
332 |
* |
|
333 |
* @param geometry |
|
334 |
* @param x |
|
335 |
* @param y |
|
336 |
* @param w |
|
337 |
* @param h |
|
338 |
* @return |
|
339 |
*/ |
|
340 |
protected boolean containsRectangle(Geometry geom, double x, double y, |
|
341 |
double w, double h) throws GeometryOperationException { |
|
342 |
|
|
343 |
|
|
344 |
// exclude disjoint boxes |
|
345 |
Envelope env = geom.getEnvelope(); |
|
346 |
if (x > env.getMaximum(0)) return false; |
|
347 |
if ((x+w) < env.getMinimum(0)) return false; |
|
348 |
if (y > env.getMaximum(1)) return false; |
|
349 |
if ((y+h) < env.getMinimum(1)) return false; |
|
350 |
|
|
351 |
if (w == 0 && h == 0) { |
|
352 |
return containsPoint(geom, x, y); |
|
353 |
} |
|
354 |
|
|
355 |
// boxes overlap, need long version |
|
356 |
Geometry rectgeom = null; |
|
357 |
GeneralPathX gpx = new GeneralPathX(); |
|
358 |
gpx.moveTo(x, y); |
|
359 |
gpx.lineTo(x+w, y); |
|
360 |
gpx.lineTo(x+w, y+h); |
|
361 |
gpx.lineTo(x, y+h); |
|
362 |
gpx.lineTo(x, y); |
|
363 |
|
|
364 |
try { |
|
365 |
rectgeom = GeometryLocator.getGeometryManager().createSurface(gpx, SUBTYPES.GEOM2D); |
|
366 |
} catch (Exception e1) { |
|
367 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e1); |
|
368 |
} |
|
369 |
|
|
370 |
DefaultRelationshipGeometryOperationContext drgoc = |
|
371 |
new DefaultRelationshipGeometryOperationContext(rectgeom); |
|
372 |
|
|
373 |
Object resp = null; |
|
374 |
boolean respboolean = true; |
|
375 |
try { |
|
376 |
resp = geom.invokeOperation(GeometryOperation.OPERATION_CONTAINS_CODE, drgoc); |
|
377 |
respboolean = ((Boolean) resp).booleanValue(); |
|
378 |
} catch (Exception e) { |
|
379 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e); |
|
380 |
} |
|
381 |
|
|
382 |
return respboolean; |
|
383 |
} |
|
384 |
|
|
385 |
|
|
386 |
/** |
|
387 |
* |
|
388 |
* @param geom |
|
389 |
* @param x |
|
390 |
* @param y |
|
391 |
* @param w |
|
392 |
* @param h |
|
393 |
* @return |
|
394 |
*/ |
|
395 |
protected boolean intersectsRectangle(Geometry geom, double x, double y, |
|
396 |
double w, double h) throws GeometryOperationException { |
|
397 |
|
|
398 |
// exclude disjoint boxes |
|
399 |
Envelope env = geom.getEnvelope(); |
|
400 |
if (x > env.getMaximum(0)) return false; |
|
401 |
if ((x+w) < env.getMinimum(0)) return false; |
|
402 |
if (y > env.getMaximum(1)) return false; |
|
403 |
if ((y+h) < env.getMinimum(1)) return false; |
|
404 |
|
|
405 |
// boxes overlap, need long version |
|
406 |
Geometry rectgeom = null; |
|
407 |
GeneralPathX gpx = new GeneralPathX(); |
|
408 |
gpx.moveTo(x, y); |
|
409 |
gpx.lineTo(x+w, y); |
|
410 |
gpx.lineTo(x+w, y+h); |
|
411 |
gpx.lineTo(x, y+h); |
|
412 |
gpx.lineTo(x, y); |
|
413 |
|
|
414 |
try { |
|
415 |
rectgeom = GeometryLocator.getGeometryManager().createSurface(gpx, SUBTYPES.GEOM2D); |
|
416 |
} catch (Exception e1) { |
|
417 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_INTERSECTS_CODE, e1); |
|
418 |
} |
|
419 |
|
|
420 |
DefaultRelationshipGeometryOperationContext drgoc = |
|
421 |
new DefaultRelationshipGeometryOperationContext(rectgeom); |
|
422 |
|
|
423 |
Object resp = null; |
|
424 |
boolean respboolean = true; |
|
425 |
try { |
|
426 |
resp = geom.invokeOperation(GeometryOperation.OPERATION_INTERSECTS_CODE, drgoc); |
|
427 |
respboolean = ((Boolean) resp).booleanValue(); |
|
428 |
} catch (Exception e) { |
|
429 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_INTERSECTS_CODE, e); |
|
430 |
} |
|
431 |
|
|
432 |
return respboolean; |
|
433 |
|
|
434 |
|
|
435 |
} |
|
436 |
|
|
243 | 437 |
} |
244 | 438 |
|
Also available in: Unified diff