Revision 29972 branches/v2_0_0_prep/libraries/libFMap_geometries/src/org/gvsig/fmap/geom/primitive/impl/AbstractPrimitive.java
AbstractPrimitive.java | ||
---|---|---|
12 | 12 |
import org.gvsig.fmap.geom.GeometryLocator; |
13 | 13 |
import org.gvsig.fmap.geom.GeometryManager; |
14 | 14 |
import org.gvsig.fmap.geom.handler.Handler; |
15 |
import org.gvsig.fmap.geom.operation.GeometryOperation; |
|
15 | 16 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
16 | 17 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
17 | 18 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
19 |
import org.gvsig.fmap.geom.operation.relationship.DefaultRelationshipGeometryOperationContext; |
|
20 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
18 | 21 |
import org.gvsig.fmap.geom.primitive.FShape; |
19 | 22 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
20 | 23 |
import org.gvsig.fmap.geom.primitive.Primitive; |
21 | 24 |
import org.gvsig.fmap.geom.type.GeometryType; |
25 |
import org.slf4j.Logger; |
|
26 |
import org.slf4j.LoggerFactory; |
|
22 | 27 |
|
23 | 28 |
|
24 | 29 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
... | ... | |
77 | 82 |
* @author Jorge Piera Llodr? (jorge.piera@iver.es) |
78 | 83 |
*/ |
79 | 84 |
public abstract class AbstractPrimitive implements Primitive, FShape, Serializable { |
85 |
|
|
86 |
private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class); |
|
87 |
|
|
80 | 88 |
private static final long serialVersionUID = -4334977368955260872L; |
81 | 89 |
protected String id = null; |
82 | 90 |
protected IProjection projection = null; |
... | ... | |
197 | 205 |
* @see org.gvsig.fmap.geom.Geometry#fastIntersects(double, double, double, double) |
198 | 206 |
*/ |
199 | 207 |
public boolean fastIntersects(double x, double y, double w, double h) { |
200 |
return intersects(x,y,w,h); |
|
208 |
|
|
209 |
boolean resp = true; |
|
210 |
|
|
211 |
try { |
|
212 |
resp = intersectsRectangle(this, x, y, w, h); |
|
213 |
} catch (GeometryOperationException e) { |
|
214 |
logger.error("While doing fastintersects: " + e.getMessage(), e); |
|
215 |
} |
|
216 |
|
|
217 |
return resp; |
|
201 | 218 |
} |
202 | 219 |
|
203 | 220 |
/* |
... | ... | |
414 | 431 |
} |
415 | 432 |
|
416 | 433 |
|
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
|
439 |
|
|
440 |
/** |
|
441 |
* Utility method |
|
442 |
* @param geometry |
|
443 |
* @param x |
|
444 |
* @param y |
|
445 |
* @return |
|
446 |
* @throws GeometryOperationException |
|
447 |
*/ |
|
448 |
protected boolean containsPoint(Geometry geom, double x, double y) throws GeometryOperationException { |
|
449 |
|
|
450 |
// exclude disjoint |
|
451 |
Envelope env = geom.getEnvelope(); |
|
452 |
if (x > env.getMaximum(0)) return false; |
|
453 |
if (y > env.getMaximum(1)) return false; |
|
454 |
if (x < env.getMinimum(0)) return false; |
|
455 |
if (y < env.getMinimum(1)) return false; |
|
456 |
|
|
457 |
// boxes overlap, need long version |
|
458 |
|
|
459 |
Geometry geompoint = null; |
|
460 |
try { |
|
461 |
geompoint = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM2D); |
|
462 |
} catch (Exception e1) { |
|
463 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e1); |
|
464 |
} |
|
465 |
|
|
466 |
DefaultRelationshipGeometryOperationContext drgoc = |
|
467 |
new DefaultRelationshipGeometryOperationContext(geompoint); |
|
468 |
|
|
469 |
Object resp = null; |
|
470 |
boolean respboolean = true; |
|
471 |
try { |
|
472 |
resp = geom.invokeOperation(GeometryOperation.OPERATION_CONTAINS_CODE, drgoc); |
|
473 |
respboolean = ((Boolean) resp).booleanValue(); |
|
474 |
} catch (Exception e) { |
|
475 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e); |
|
476 |
} |
|
477 |
|
|
478 |
return respboolean; |
|
479 |
} |
|
480 |
|
|
481 |
/** |
|
482 |
* |
|
483 |
* @param geometry |
|
484 |
* @param x |
|
485 |
* @param y |
|
486 |
* @param w |
|
487 |
* @param h |
|
488 |
* @return |
|
489 |
*/ |
|
490 |
protected boolean containsRectangle(Geometry geom, double x, double y, |
|
491 |
double w, double h) throws GeometryOperationException { |
|
492 |
|
|
493 |
|
|
494 |
// exclude disjoint boxes |
|
495 |
Envelope env = geom.getEnvelope(); |
|
496 |
if (x > env.getMaximum(0)) return false; |
|
497 |
if ((x+w) < env.getMinimum(0)) return false; |
|
498 |
if (y > env.getMaximum(1)) return false; |
|
499 |
if ((y+h) < env.getMinimum(1)) return false; |
|
500 |
|
|
501 |
if (w == 0 && h == 0) { |
|
502 |
return containsPoint(geom, x, y); |
|
503 |
} |
|
504 |
|
|
505 |
// boxes overlap, need long version |
|
506 |
Geometry rectgeom = null; |
|
507 |
GeneralPathX gpx = new GeneralPathX(); |
|
508 |
gpx.moveTo(x, y); |
|
509 |
gpx.lineTo(x+w, y); |
|
510 |
gpx.lineTo(x+w, y+h); |
|
511 |
gpx.lineTo(x, y+h); |
|
512 |
gpx.lineTo(x, y); |
|
513 |
|
|
514 |
try { |
|
515 |
rectgeom = GeometryLocator.getGeometryManager().createSurface(gpx, SUBTYPES.GEOM2D); |
|
516 |
} catch (Exception e1) { |
|
517 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e1); |
|
518 |
} |
|
519 |
|
|
520 |
DefaultRelationshipGeometryOperationContext drgoc = |
|
521 |
new DefaultRelationshipGeometryOperationContext(rectgeom); |
|
522 |
|
|
523 |
Object resp = null; |
|
524 |
boolean respboolean = true; |
|
525 |
try { |
|
526 |
resp = geom.invokeOperation(GeometryOperation.OPERATION_CONTAINS_CODE, drgoc); |
|
527 |
respboolean = ((Boolean) resp).booleanValue(); |
|
528 |
} catch (Exception e) { |
|
529 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e); |
|
530 |
} |
|
531 |
|
|
532 |
return respboolean; |
|
533 |
} |
|
534 |
|
|
535 |
|
|
536 |
/** |
|
537 |
* |
|
538 |
* @param geom |
|
539 |
* @param x |
|
540 |
* @param y |
|
541 |
* @param w |
|
542 |
* @param h |
|
543 |
* @return |
|
544 |
*/ |
|
545 |
protected boolean intersectsRectangle(Geometry geom, double x, double y, |
|
546 |
double w, double h) throws GeometryOperationException { |
|
547 |
|
|
548 |
// exclude disjoint boxes |
|
549 |
Envelope env = geom.getEnvelope(); |
|
550 |
if (x > env.getMaximum(0)) return false; |
|
551 |
if ((x+w) < env.getMinimum(0)) return false; |
|
552 |
if (y > env.getMaximum(1)) return false; |
|
553 |
if ((y+h) < env.getMinimum(1)) return false; |
|
554 |
|
|
555 |
// boxes overlap, need long version |
|
556 |
Geometry rectgeom = null; |
|
557 |
GeneralPathX gpx = new GeneralPathX(); |
|
558 |
gpx.moveTo(x, y); |
|
559 |
gpx.lineTo(x+w, y); |
|
560 |
gpx.lineTo(x+w, y+h); |
|
561 |
gpx.lineTo(x, y+h); |
|
562 |
gpx.lineTo(x, y); |
|
563 |
|
|
564 |
try { |
|
565 |
rectgeom = GeometryLocator.getGeometryManager().createSurface(gpx, SUBTYPES.GEOM2D); |
|
566 |
} catch (Exception e1) { |
|
567 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_INTERSECTS_CODE, e1); |
|
568 |
} |
|
569 |
|
|
570 |
DefaultRelationshipGeometryOperationContext drgoc = |
|
571 |
new DefaultRelationshipGeometryOperationContext(rectgeom); |
|
572 |
|
|
573 |
Object resp = null; |
|
574 |
boolean respboolean = true; |
|
575 |
try { |
|
576 |
resp = geom.invokeOperation(GeometryOperation.OPERATION_INTERSECTS_CODE, drgoc); |
|
577 |
respboolean = ((Boolean) resp).booleanValue(); |
|
578 |
} catch (Exception e) { |
|
579 |
throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_INTERSECTS_CODE, e); |
|
580 |
} |
|
581 |
|
|
582 |
return respboolean; |
|
583 |
|
|
584 |
|
|
585 |
} |
|
586 |
|
|
587 |
|
|
588 |
|
|
417 | 589 |
} |
Also available in: Unified diff