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.workbench.ui.cursortool;
034:
035: import java.awt.event.MouseEvent;
036: import java.awt.geom.NoninvertibleTransformException;
037: import java.util.ArrayList;
038: import java.util.Collection;
039: import java.util.HashMap;
040: import java.util.HashSet;
041: import java.util.Iterator;
042: import java.util.List;
043: import java.util.Map;
044: import java.util.Set;
045:
046: import com.vividsolutions.jts.geom.Envelope;
047: import com.vividsolutions.jump.feature.Feature;
048: import com.vividsolutions.jump.geom.EnvelopeIntersector;
049: import com.vividsolutions.jump.workbench.model.Layer;
050:
051: /**
052: * Allows the user to specify features by clicking on them or drawing a box
053: * around them. Works with invalid features (using EnvelopeIntersector).
054: */
055: public abstract class SpecifyFeaturesTool extends DragTool {
056: public SpecifyFeaturesTool() {
057: }
058:
059: protected Iterator candidateLayersIterator() {
060: return getPanel().getLayerManager().iterator();
061: }
062:
063: /**
064: * @param envelope
065: * the envelope, which may have zero area
066: * @return those features of the layer that intersect the given envelope; an
067: * empty FeatureCollection if no features intersect it
068: */
069: private static Set intersectingFeatures(Layer layer,
070: Envelope envelope) {
071: HashSet intersectingFeatures = new HashSet();
072: List candidateFeatures = layer.getFeatureCollectionWrapper()
073: .query(envelope);
074: String a = ""
075: + layer.getFeatureCollectionWrapper()
076: .getUltimateWrappee();
077: String b = ""
078: + layer.getFeatureCollectionWrapper()
079: .getUltimateWrappee().size();
080: for (Iterator i = candidateFeatures.iterator(); i.hasNext();) {
081: Feature feature = (Feature) i.next();
082:
083: // optimization - if the feature envelope is completely inside the
084: // query envelope it must be selected
085: if (envelope.contains(feature.getGeometry()
086: .getEnvelopeInternal())
087: || EnvelopeIntersector.intersects(feature
088: .getGeometry(), envelope)) {
089: intersectingFeatures.add(feature);
090: }
091: }
092:
093: return intersectingFeatures;
094: }
095:
096: public void mouseClicked(MouseEvent e) {
097: try {
098: super .mouseClicked(e);
099: setViewSource(e.getPoint());
100: setViewDestination(e.getPoint());
101: fireGestureFinished();
102: } catch (Throwable t) {
103: getPanel().getContext().handleThrowable(t);
104: }
105: }
106:
107: protected Set specifiedFeatures()
108: throws NoninvertibleTransformException {
109: HashSet allFeatures = new HashSet();
110:
111: for (Iterator i = layerToSpecifiedFeaturesMap().values()
112: .iterator(); i.hasNext();) {
113: Set features = (Set) i.next();
114: allFeatures.addAll(features);
115: }
116:
117: return allFeatures;
118: }
119:
120: /**
121: * Returns the layers containing the specified features, and the specified
122: * features themselves.
123: */
124: protected Map layerToSpecifiedFeaturesMap()
125: throws NoninvertibleTransformException {
126: return layerToSpecifiedFeaturesMap(candidateLayersIterator(),
127: getBoxInModelCoordinates());
128: }
129:
130: public static Map layerToSpecifiedFeaturesMap(
131: Iterator layerIterator, Envelope boxInModelCoordinates)
132: throws NoninvertibleTransformException {
133: HashMap layerToFeaturesMap = new HashMap();
134:
135: for (Iterator i = layerIterator; i.hasNext();) {
136: Layer layer = (Layer) i.next();
137:
138: if (!layer.isVisible()) {
139: continue;
140: }
141:
142: Set intersectingFeatures = intersectingFeatures(layer,
143: boxInModelCoordinates);
144:
145: if (intersectingFeatures.isEmpty()) {
146: continue;
147: }
148:
149: layerToFeaturesMap.put(layer, intersectingFeatures);
150: }
151:
152: return layerToFeaturesMap;
153: }
154:
155: /**
156: * @param layers
157: * Layers to filter in
158: */
159: protected Collection specifiedFeatures(Collection layers)
160: throws NoninvertibleTransformException {
161: ArrayList specifiedFeatures = new ArrayList();
162: Map layerToSpecifiedFeaturesMap = layerToSpecifiedFeaturesMap();
163:
164: for (Iterator i = layerToSpecifiedFeaturesMap.keySet()
165: .iterator(); i.hasNext();) {
166: Layer layer = (Layer) i.next();
167:
168: if (!layers.contains(layer)) {
169: continue;
170: }
171:
172: specifiedFeatures
173: .addAll((Collection) layerToSpecifiedFeaturesMap
174: .get(layer));
175: }
176:
177: return specifiedFeatures;
178: }
179: }
|