001: /*
002: * Created on 11.02.2005 for PIROL
003: *
004: * SVN header information:
005: * $Author: javamap $
006: * $Rev: 856 $
007: * $Date: 2007-06-18 21:15:27 -0700 (Mon, 18 Jun 2007) $
008: * $Id: SelectionTools.java 856 2007-06-19 04:15:27Z javamap $
009: */
010: package de.fho.jump.pirol.utilities.apiTools;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.Map;
016:
017: import com.vividsolutions.jts.geom.Geometry;
018: import com.vividsolutions.jump.feature.Feature;
019: import com.vividsolutions.jump.feature.FeatureCollection;
020: import com.vividsolutions.jump.feature.FeatureCollectionWrapper;
021: import com.vividsolutions.jump.workbench.model.FenceLayerFinder;
022: import com.vividsolutions.jump.workbench.model.Layer;
023: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
024: import com.vividsolutions.jump.workbench.ui.SelectionManager;
025:
026: /**
027: * Class to easily handle selections and selection tools. Also has methods to
028: * find features by given geometries.
029: *
030: * @author Ole Rahn
031: *
032: * FH Osnabrück - University of Applied Sciences Osnabrück
033: * Project PIROL 2005
034: * Daten- und Wissensmanagement
035: *
036: */
037:
038: public class SelectionTools extends ToolToMakeYourLifeEasier {
039: protected PlugInContext context = null;
040:
041: public SelectionTools(PlugInContext context) {
042: super ();
043: this .context = context;
044: }
045:
046: /**
047: * create a selection out of the given features that is visible in the Jump map
048: *@param features features to be selected
049: */
050: public void selectFeatures(List features) {
051: SelectionTools.selectFeatures(features, this .context);
052: }
053:
054: public static void selectLayer(PlugInContext context, Layer layer) {
055: // TODO: how to select a layer???
056: }
057:
058: public static void selectFeatures(List features,
059: PlugInContext context) {
060: SelectionManager sm = context.getLayerViewPanel()
061: .getSelectionManager();
062: sm.clear();
063:
064: Map layer2FeatList = LayerTools.getLayer2FeatureMap(features,
065: context);
066: Layer[] layersWithFeatures = (Layer[]) layer2FeatList.keySet()
067: .toArray(new Layer[0]);
068:
069: for (int i = 0; i < layersWithFeatures.length; i++) {
070: sm.getFeatureSelection().selectItems(
071: layersWithFeatures[i],
072: (ArrayList) layer2FeatList
073: .get(layersWithFeatures[i]));
074: }
075:
076: }
077:
078: public static List<Feature> getSelectedFeaturesFromLayer(
079: PlugInContext context, Layer layer) {
080: return new ArrayList<Feature>(context.getLayerViewPanel()
081: .getSelectionManager().getFeaturesWithSelectedItems(
082: layer));
083: }
084:
085: public static List<Feature> getSelectedFeatures(
086: PlugInContext context) {
087: return new ArrayList<Feature>(context.getLayerViewPanel()
088: .getSelectionManager().getFeaturesWithSelectedItems());
089: }
090:
091: public List<Feature> getSelectedFeatures() {
092: return SelectionTools.getSelectedFeatures(this .context);
093: }
094:
095: /**
096: *
097: *@param context current PlugIn context
098: *@return the geometry of the current fence, or null if there is currently no fence
099: */
100: public static Geometry getFenceGeometry(PlugInContext context) {
101: Layer fence = new FenceLayerFinder(context.getLayerViewPanel())
102: .getLayer();
103: if (fence == null)
104: return null;
105: FeatureCollectionWrapper fenceCollWrap = fence
106: .getFeatureCollectionWrapper();
107: Feature fencePolygon = (Feature) fenceCollWrap
108: .getUltimateWrappee().getFeatures().get(0);
109: return fencePolygon.getGeometry();
110: }
111:
112: /**
113: *@return the geometry of the current fence, or null if there is currently no fence
114: */
115: public Geometry getFenceGeometry() {
116: return SelectionTools.getFenceGeometry(this .context);
117: }
118:
119: public List getFeaturesInFence() {
120: Geometry fenceGeometry = this .getFenceGeometry();
121: return this .getFeaturesInGeometry(fenceGeometry);
122: }
123:
124: public List getFeaturesInGeometry(Geometry fenceGeometry) {
125: ArrayList featsInFence = new ArrayList();
126: List layers = this .context.getLayerManager().getVisibleLayers(
127: false);
128:
129: Iterator iter = layers.iterator();
130: Layer tmp;
131: while (iter.hasNext()) {
132: tmp = (Layer) iter.next();
133: featsInFence
134: .addAll(SelectionTools
135: .getFeaturesInFenceInLayerAsList(tmp,
136: fenceGeometry));
137: }
138:
139: return featsInFence;
140: }
141:
142: /**
143: * Get a list of those features from the given layer that are included by the given fence geometry.
144: * @param layer - Layer to search in
145: * @param fenceGeometry - Geometry to search in
146: */
147: public static Feature[] getFeaturesInFenceInLayer(Layer layer,
148: Geometry fenceGeometry) {
149: return SelectionTools.getFeaturesOnTheSameSpot(layer,
150: fenceGeometry, false);
151: }
152:
153: /**
154: * Get a list of those features from the given layer that are included by the given fence geometry.
155: * @param layer - Layer to search in
156: * @param fenceGeometry - Geometry to search in
157: */
158: public static List getFeaturesInFenceInLayerAsList(Layer layer,
159: Geometry fenceGeometry) {
160: Feature[] featureArray = SelectionTools
161: .getFeaturesOnTheSameSpot(layer, fenceGeometry, false);
162: ArrayList result = new ArrayList();
163:
164: for (int i = 0; i < featureArray.length; i++) {
165: result.add(featureArray[i]);
166: }
167:
168: return result;
169: }
170:
171: /**
172: * Get a list of features (a sub list of the given array) that are included by the given fence geometry.
173: * @param featArray - Array of features to search in
174: * @param fenceGeometry - Geometry to search in
175: */
176: public static Feature[] getFeaturesInFenceInLayer(
177: Feature[] featArray, Geometry fenceGeometry) {
178: return SelectionTools.getFeaturesOnTheSameSpot(featArray,
179: fenceGeometry, false);
180: }
181:
182: /**
183: * Get a list of those features from the given layer that are included by the given fence geometry.
184: * @param layer - Layer to search in
185: * @param fenceGeometry - Geometry to search in
186: * @param bothWays - sets if it's also a hit if the feature's geometry includes the fence geometry
187: */
188: public static Feature[] getFeaturesOnTheSameSpot(Layer layer,
189: Geometry fenceGeometry, boolean bothWays) {
190: FeatureCollection featColl = layer
191: .getFeatureCollectionWrapper().getUltimateWrappee();
192: return SelectionTools.getFeaturesOnTheSameSpot(
193: FeatureCollectionTools
194: .FeatureCollection2FeatureArray(featColl),
195: fenceGeometry, bothWays);
196: }
197:
198: /**
199: * Get a list of features (a sub list of the given array) that are included by the given fence geometry.
200: * @param featArray - Array of features to search in
201: * @param fenceGeometry - Geometry to search in
202: * @param bothWays - sets if it's also a hit if the feature's geometry includes the fence geometry
203: */
204: public static Feature[] getFeaturesOnTheSameSpot(
205: Feature[] featArray, Geometry fenceGeometry,
206: boolean bothWays) {
207: ArrayList featuresInsideTheFence = new ArrayList();
208:
209: // Envelope fenceEnv = fenceGeometry.getEnvelopeInternal();
210: // Envelope featEnv;
211: Geometry featGeom = null;
212:
213: Feature feat;
214:
215: int numFeats = featArray.length;
216:
217: for (int i = 0; i < numFeats; i++) {
218: feat = featArray[i];
219:
220: featGeom = feat.getGeometry();
221: /*
222: featEnv = featGeom.getEnvelopeInternal();
223:
224: // quick test, first:
225: if (!fenceEnv.contains(featEnv) && !fenceEnv.intersects(featEnv) && !bothWays){
226: continue;
227: } else if (bothWays){
228: if (!fenceEnv.contains(featEnv) && !fenceEnv.intersects(featGeom.getEnvelopeInternal()) && !featEnv.contains(fenceEnv) )
229: continue;
230: }
231: */
232: if (!fenceGeometry.disjoint(featGeom)) {
233: featuresInsideTheFence.add(feat);
234: } else if (bothWays) {
235: if (!featGeom.disjoint(fenceGeometry)) {
236: featuresInsideTheFence.add(feat);
237: }
238: }
239: }
240: return (Feature[]) featuresInsideTheFence
241: .toArray(new Feature[0]);
242: }
243: }
|