001: /*
002: * Created on 08.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: LayerTools.java 856 2007-06-19 04:15:27Z javamap $
009: */
010: package de.fho.jump.pirol.utilities.apiTools;
011:
012: import java.awt.Color;
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Map;
019:
020: import com.vividsolutions.jts.geom.Geometry;
021: import com.vividsolutions.jump.feature.AttributeType;
022: import com.vividsolutions.jump.feature.BasicFeature;
023: import com.vividsolutions.jump.feature.Feature;
024: import com.vividsolutions.jump.feature.FeatureCollection;
025: import com.vividsolutions.jump.feature.FeatureDataset;
026: import com.vividsolutions.jump.feature.FeatureSchema;
027: import com.vividsolutions.jump.workbench.model.Category;
028: import com.vividsolutions.jump.workbench.model.Layer;
029: import com.vividsolutions.jump.workbench.model.Layerable;
030: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
031:
032: import de.fho.jump.pirol.utilities.FeatureCollection.PirolFeatureCollection;
033: import de.fho.jump.pirol.utilities.FeatureCollection.PirolFeatureCollectionRole;
034: import de.fho.jump.pirol.utilities.FeatureCollection.RoleTriangularIrregularNet;
035: import de.fho.jump.pirol.utilities.i18n.PirolPlugInMessages;
036: import de.fho.jump.pirol.utilities.settings.PirolPlugInSettings;
037:
038: /**
039: * Class for more convenient use of Layer objects. Offers methods e.g. to get features that reside within
040: * a given geometry or to easily add a result layer to the map.
041: *
042: * @author Ole Rahn
043: *
044: * FH Osnabrück - University of Applied Sciences Osnabrück
045: * Project PIROL 2005
046: * Daten- und Wissensmanagement
047: *
048: */
049: public class LayerTools extends ToolToMakeYourLifeEasier {
050: protected PlugInContext context = null;
051:
052: public LayerTools(PlugInContext context) {
053: super ();
054: this .context = context;
055: }
056:
057: public Layer[] getSelectedLayers() {
058: return this .context.getSelectedLayers();
059: }
060:
061: public List getSelectedFeatures() {
062: return SelectionTools.getSelectedFeatures(this .context);
063: }
064:
065: public Geometry getFenceGeometry() {
066: return SelectionTools.getFenceGeometry(this .context);
067: }
068:
069: public List getFeaturesInFence() {
070: SelectionTools st = new SelectionTools(this .context);
071: return st.getFeaturesInFence();
072: }
073:
074: public Feature[] getFeaturesInFenceInLayer(Layer layer,
075: Geometry fenceGeometry) {
076: return SelectionTools.getFeaturesInFenceInLayer(layer,
077: fenceGeometry);
078: }
079:
080: public Feature[] getFeaturesInFenceInLayer(Feature[] featArray,
081: Geometry fenceGeometry) {
082: return SelectionTools.getFeaturesInFenceInLayer(featArray,
083: fenceGeometry);
084: }
085:
086: public int getNumSelectedLayers() {
087: return LayerTools.getNumSelectedLayers(this .context);
088: }
089:
090: public static int getNumSelectedLayers(PlugInContext context) {
091: return context.getSelectedLayers().length;
092: }
093:
094: public static Layer addStandardResultLayer(String title,
095: FeatureCollection featCollection, PlugInContext context,
096: PirolFeatureCollectionRole role) {
097: return LayerTools.addStandardResultLayer(title, featCollection,
098: Color.yellow, context, role);
099: }
100:
101: public static Layer addAndSelectStandardResultLayer(String title,
102: FeatureCollection featCollection, Color color,
103: PlugInContext context, PirolFeatureCollectionRole role) {
104: return LayerTools.addStandardResultLayer(title, featCollection,
105: color, context, true, role);
106: }
107:
108: public static Layer addAndSelectStandardResultLayer(String title,
109: FeatureCollection featCollection, PlugInContext context,
110: PirolFeatureCollectionRole role) {
111: return LayerTools.addStandardResultLayer(title, featCollection,
112: Color.YELLOW, context, true, role);
113: }
114:
115: public static Layer addStandardResultLayer(String title,
116: FeatureCollection featCollection, Color color,
117: PlugInContext context, PirolFeatureCollectionRole role) {
118: return LayerTools.addStandardResultLayer(title, featCollection,
119: color, context, false, role);
120: }
121:
122: public static Layer addStandardResultLayer(String title,
123: FeatureCollection featCollection, Color color,
124: PlugInContext context, boolean select,
125: PirolFeatureCollectionRole role) {
126: if (featCollection == null || context == null
127: || LayerTools.getResultCategory(context) == null)
128: return null;
129:
130: Layer newLayer = null;
131:
132: if (!PirolFeatureCollection.class.isInstance(featCollection)) {
133: newLayer = new Layer(title, color,
134: new PirolFeatureCollection(featCollection, role),
135: context.getLayerManager());
136: } else {
137: if (role != null)
138: ((PirolFeatureCollection) featCollection).addRole(role);
139: newLayer = new Layer(title, color, featCollection, context
140: .getLayerManager());
141: }
142:
143: context.getLayerManager().addLayer(
144: PirolPlugInSettings.resultLayerCategory(), newLayer);
145:
146: if (select) {
147: SelectionTools.selectLayer(context, newLayer);
148: }
149:
150: return newLayer;
151: }
152:
153: public Layer addStandardResultLayer(String title,
154: FeatureCollection featCollection,
155: PirolFeatureCollectionRole role) {
156: return LayerTools.addStandardResultLayer(title, featCollection,
157: this .context, role);
158: }
159:
160: public Map getLayer2FeatureMap(List features) {
161: return LayerTools.getLayer2FeatureMap(features, this .context);
162: }
163:
164: public final static Layer putGeometryArrayIntoMap(
165: Geometry[] geometryArray, PlugInContext context) {
166: FeatureSchema fs = new FeatureSchema();
167: fs.addAttribute(PirolPlugInMessages.getString("geometry"),
168: AttributeType.GEOMETRY);
169: FeatureCollection fc = new FeatureDataset(fs);
170:
171: Feature f;
172: for (int i = 0; i < geometryArray.length; i++) {
173: f = new BasicFeature(fs);
174: f.setGeometry(geometryArray[i]);
175: fc.add(f);
176: }
177:
178: return LayerTools.addStandardResultLayer(PirolPlugInMessages
179: .getString("triangles"), fc, context,
180: new RoleTriangularIrregularNet());
181: }
182:
183: public static Map getLayer2FeatureMap(List features,
184: PlugInContext context) {
185: Layer layer = null;
186: List<Layer> layers = context.getLayerManager()
187: .getVisibleLayers(false);
188:
189: Iterator iter = layers.iterator();
190: Iterator featIter;
191: Map layer2FeatList = new HashMap();
192: List layerFeats;
193: Feature feat;
194:
195: while (iter.hasNext()) {
196: layer = (Layer) iter.next();
197: layerFeats = layer.getFeatureCollectionWrapper()
198: .getUltimateWrappee().getFeatures();
199: featIter = features.iterator();
200: while (featIter.hasNext()) {
201: feat = (Feature) featIter.next();
202:
203: if (layerFeats.contains(feat)) {
204: if (!layer2FeatList.containsKey(layer)) {
205: layer2FeatList.put(layer, new ArrayList());
206: }
207: ((ArrayList) layer2FeatList.get(layer)).add(feat);
208: }
209: }
210: }
211: return layer2FeatList;
212: }
213:
214: public Category getResultCategory() {
215: return LayerTools.getResultCategory(this .context);
216: }
217:
218: public static Category getResultCategory(PlugInContext context) {
219: Category category = context.getLayerManager().getCategory(
220: PirolPlugInSettings.resultLayerCategory());
221:
222: if (category == null) {
223: context.getLayerManager().addCategory(
224: PirolPlugInSettings.resultLayerCategory(), 0);
225: category = context.getLayerManager().getCategory(
226: PirolPlugInSettings.resultLayerCategory());
227: }
228:
229: return category;
230: }
231:
232: /**
233: * Get a given number of selected Layers.
234: * @param context the current PlugInContext
235: * @param num max. number of layers to return, -1 returns all selected layers
236: * @return a given number of selected Layers, null if no Layers are selected
237: */
238: public static Layer[] getSelectedLayers(PlugInContext context,
239: int num) {
240: Layer[] selLayers = context.getSelectedLayers();
241:
242: if (selLayers.length == 0) {
243: return null;
244: } else if (num <= 0) {
245: return selLayers;
246: } else {
247: Layer[] result = new Layer[num];
248:
249: for (int i = 0; i < selLayers.length && i < result.length; i++) {
250: result[i] = selLayers[i];
251: }
252:
253: return result;
254: }
255: }
256:
257: /**
258: * get one Layer that is selected
259: * @param context the current PlugInContext
260: * @return one selected Layer, null if no Layers are selected
261: */
262: public static Layer getSelectedLayer(PlugInContext context) {
263: Layer[] selLayers = LayerTools.getSelectedLayers(context, 1);
264:
265: if (selLayers == null || selLayers.length == 0) {
266: return null;
267: }
268: return selLayers[0];
269: }
270:
271: /**
272: * get one Layer that is selected
273: * @param context the current PlugInContext
274: * @return one selected Layer, null if no Layers are selected
275: */
276: public static Layerable getSelectedLayerable(PlugInContext context,
277: Class layerableClass) {
278:
279: Collection selLayers = context.getLayerNamePanel()
280: .selectedNodes(layerableClass);
281:
282: if (selLayers == null || selLayers.size() == 0) {
283: return null;
284: }
285:
286: return ((Layerable[]) selLayers.toArray(new Layerable[0]))[0];
287: }
288:
289: /**
290: * returns a layername, that is not yet used in the layername panel
291: */
292: public static String getUniqueLayerName(PlugInContext context,
293: String name) {
294:
295: return context.getLayerManager().uniqueLayerName(name);
296: }
297:
298: }
|