001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.geom;
034:
035: import com.vividsolutions.jts.algorithm.SimplePointInAreaLocator;
036: import com.vividsolutions.jts.geom.*;
037:
038: public class EnvelopeIntersector {
039: private static LineSegmentEnvelopeIntersector lineSegmentEnvelopeIntersector = new LineSegmentEnvelopeIntersector();
040:
041: public EnvelopeIntersector() {
042: }
043:
044: /**
045: * Returns whether the Geometry and the Envelope intersect, even if the
046: * Geometry is invalid. Checks whether any points or line segments of the
047: * geometry intersect the Envelope, or whether the Envelope is wholly
048: * contained in any polygons.
049: * <p>
050: * This code will only work correctly on valid geometries.
051: * (For instance, it may return <code>false</code> if the Envelope is
052: * wholly contained in an <b>invalid</b> polygon.
053: *
054: * @param envelope the Envelope to test
055: * @param geometry the Geometry to test, whether valid or not
056: * @return whether the Envelope intersects the Geometry
057: */
058: public static boolean intersects(Geometry geometry,
059: Envelope envelope) {
060: if (envelope.isNull()) {
061: return false;
062: }
063:
064: if (!envelope.intersects(geometry.getEnvelopeInternal())) {
065: return false;
066: }
067:
068: if (geometry instanceof GeometryCollection) {
069: GeometryCollection gc = (GeometryCollection) geometry;
070:
071: for (int i = 0; i < gc.getNumGeometries(); i++) {
072: if (intersects(gc.getGeometryN(i), envelope)) {
073: return true;
074: }
075: }
076:
077: return false;
078: }
079:
080: if (intersectsBoundary(geometry, envelope)) {
081: return true;
082: }
083:
084: if (geometry instanceof Polygon) {
085: return contains((Polygon) geometry, new Coordinate(envelope
086: .getMinX(), envelope.getMinY()));
087: }
088:
089: return false;
090: }
091:
092: private static boolean contains(Polygon polygon, Coordinate c) {
093: return SimplePointInAreaLocator.containsPointInPolygon(c,
094: polygon);
095: }
096:
097: private static boolean intersectsBoundary(Geometry geometry,
098: final Envelope envelope) {
099: final BooleanWrapper intersects = new BooleanWrapper(false);
100: geometry.apply(new GeometryComponentFilter() {
101: public void filter(Geometry geometry) {
102: Coordinate[] coordinates = geometry.getCoordinates();
103:
104: if (intersects.value) {
105: return;
106: }
107:
108: if (envelope.contains(coordinates[0])) {
109: intersects.value = true;
110: }
111:
112: for (int i = 1; i < coordinates.length; i++) { //1
113:
114: if (intersectsLineSegment(coordinates[i],
115: coordinates[i - 1], envelope)) {
116: intersects.value = true;
117: }
118: }
119: }
120: });
121:
122: return intersects.value;
123: }
124:
125: private static boolean intersectsLineSegment(Coordinate a,
126: Coordinate b, Envelope envelope) {
127: return lineSegmentEnvelopeIntersector.touches(a, b, envelope);
128: }
129:
130: private static class BooleanWrapper {
131: public boolean value;
132:
133: public BooleanWrapper(boolean value) {
134: this.value = value;
135: }
136: }
137: }
|