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: * JUMP is Copyright (C) 2003 Vivid Solutions
006: *
007: * This program implements extensions to JUMP and is
008: * Copyright (C) Stefan Steiniger.
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or (at your option) any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: *
024: * For more information, contact:
025: * Stefan Steiniger
026: * perriger@gmx.de
027: */
028: /***********************************************
029: * created on 22.06.2006
030: * last modified:
031: *
032: * author: sstein
033: *
034: * description:
035: * contains some method to extract sets of features
036: * which fullfill some spatial criterion
037: *
038: ***********************************************/package org.openjump.core.spatialAttributeOps;
039:
040: import java.util.ArrayList;
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import com.vividsolutions.jts.geom.Geometry;
045: import com.vividsolutions.jts.geom.Point;
046: import com.vividsolutions.jts.index.quadtree.Quadtree;
047: import com.vividsolutions.jump.feature.Feature;
048:
049: /**
050: *
051: * contains some method to extract sets of features
052: * which fullfill some spatial criterion<p>
053: * notes:<p>
054: * - use "intersects" only for polygon geometries (condition intersection area > 0).<p>
055: * - "contains" can be used for polygons and points (centroid from polygon is calculated)<p>
056: *
057: * @author sstein
058: *
059: */
060: public class SpatialRelationOp {
061: public final static int CONTAINS = 0;
062: public final static int INTERSECTS = 1;
063: public final static int COVEREDBY = 2;
064:
065: //public final static int TOUCHES = 3;
066:
067: public static String getName(int spatialRel) {
068: String retval = "";
069: if (spatialRel == 0) {
070: retval = "contains";
071: } else if (spatialRel == 1) {
072: retval = "intersects";
073: }
074: return retval;
075: }
076:
077: /**
078: * note: if input feature is point and spatial attribute is "intersect" or "covered by" the
079: * candidate features are selected from a 10.0m radius
080: * @param spatialRelation
081: * @param featureTree
082: * @param g
083: * @param radius
084: * @return ArrayList of Feature fullfilling the spatial criterion
085: */
086: public static List evaluateSpatial(int spatialRelation,
087: Quadtree featureTree, Geometry g, double radius) {
088:
089: List foundItems = new ArrayList();
090:
091: if (spatialRelation == SpatialRelationOp.CONTAINS) {
092: Geometry buffer = g.buffer(radius);
093: List candidates = featureTree.query(buffer
094: .getEnvelopeInternal());
095: if (g instanceof Point) {
096: radius = 10.0;
097: Geometry buffer2 = g.buffer(radius);
098: candidates = featureTree.query(buffer2
099: .getEnvelopeInternal());
100: }
101: for (Iterator iter = candidates.iterator(); iter.hasNext();) {
102: Feature candidate = (Feature) iter.next();
103: boolean retval = buffer.contains(candidate
104: .getGeometry().getCentroid());
105: if (retval) {
106: foundItems.add(candidate);
107: }
108: }
109: } else if (spatialRelation == SpatialRelationOp.INTERSECTS) {
110: Geometry buffer = g.buffer(radius);
111: List candidates = featureTree.query(buffer
112: .getEnvelopeInternal());
113: if (g instanceof Point) {
114: radius = 10.0;
115: Geometry buffer2 = g.buffer(radius);
116: candidates = featureTree.query(buffer2
117: .getEnvelopeInternal());
118: //-- reset to point geom
119: buffer = g;
120: }
121: for (Iterator iter = candidates.iterator(); iter.hasNext();) {
122: Feature candidate = (Feature) iter.next();
123: Geometry geom = buffer.intersection(candidate
124: .getGeometry());
125: if (geom.getArea() > 0) {
126: foundItems.add(candidate);
127: }
128: }
129: } else if (spatialRelation == SpatialRelationOp.COVEREDBY) {
130: Geometry buffer = g.buffer(radius);
131: List candidates = featureTree.query(buffer
132: .getEnvelopeInternal());
133: if (g instanceof Point) {
134: radius = 10.0;
135: Geometry buffer2 = g.buffer(radius);
136: candidates = featureTree.query(buffer2
137: .getEnvelopeInternal());
138: //-- reset to point geom
139: buffer = g;
140: }
141: for (Iterator iter = candidates.iterator(); iter.hasNext();) {
142: Feature candidate = (Feature) iter.next();
143: if (buffer.coveredBy(candidate.getGeometry())) {
144: foundItems.add(candidate);
145: }
146: }
147: } else {
148: System.out
149: .println("SpatialRelationOp: spatial relation does not exit");
150: }
151: return foundItems;
152: }
153: }
|