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.plugin.analysis;
034:
035: import javax.swing.JComboBox;
036:
037: import com.vividsolutions.jump.I18N;
038: import com.vividsolutions.jump.feature.FeatureCollection;
039: import com.vividsolutions.jump.feature.FeatureSchema;
040: import com.vividsolutions.jump.task.TaskMonitor;
041: import com.vividsolutions.jump.tools.AttributeMapping;
042: import com.vividsolutions.jump.tools.OverlayEngine;
043: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
044: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
045: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
046: import com.vividsolutions.jump.workbench.plugin.ThreadedPlugIn;
047: import com.vividsolutions.jump.workbench.ui.GUIUtil;
048: import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
049: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
050:
051: /**
052: *
053: * Creates a new layer containing intersections of all pairs of
054: * features from two input layers. Splits {@link
055: * com.vividsolutions.jts.geom.MultiPolygon Multipolygons} and {@link
056: * com.vividsolutions.jts.geom.GeometryCollection
057: * GeometryCollections}, and filters out non-Polygons.
058: */
059:
060: public class OverlayPlugIn extends AbstractPlugIn implements
061: ThreadedPlugIn {
062: private String POLYGON_OUTPUT = I18N
063: .get("ui.plugin.analysis.OverlayPlugIn.limit-output-to-polygons-only");
064: private String FIRST_LAYER = I18N
065: .get("ui.plugin.analysis.OverlayPlugIn.first-layer");
066: private String SECOND_LAYER = I18N
067: .get("ui.plugin.analysis.OverlayPlugIn.second-layer");
068: private String TRANSFER_ATTRIBUTES_FROM_FIRST_LAYER = I18N
069: .get("ui.plugin.analysis.OverlayPlugIn.transfer-attributes-from-first-layer");
070: private String TRANSFER_ATTRIBUTES_FROM_SECOND_LAYER = I18N
071: .get("ui.plugin.analysis.OverlayPlugIn.transfer-attributes-from-second-layer");
072: private MultiInputDialog dialog;
073: private OverlayEngine overlayEngine;
074:
075: public OverlayPlugIn() {
076: }
077:
078: private String categoryName = StandardCategoryNames.RESULT;
079:
080: public void setCategoryName(String value) {
081: categoryName = value;
082: }
083:
084: public boolean execute(PlugInContext context) throws Exception {
085: //[sstein, 15.07.2006] placed here again otherwise language settings wont work for i18n
086: POLYGON_OUTPUT = I18N
087: .get("ui.plugin.analysis.OverlayPlugIn.limit-output-to-polygons-only");
088: FIRST_LAYER = I18N
089: .get("ui.plugin.analysis.OverlayPlugIn.first-layer");
090: SECOND_LAYER = I18N
091: .get("ui.plugin.analysis.OverlayPlugIn.second-layer");
092: TRANSFER_ATTRIBUTES_FROM_FIRST_LAYER = I18N
093: .get("ui.plugin.analysis.OverlayPlugIn.transfer-attributes-from-first-layer");
094: TRANSFER_ATTRIBUTES_FROM_SECOND_LAYER = I18N
095: .get("ui.plugin.analysis.OverlayPlugIn.transfer-attributes-from-second-layer");
096:
097: overlayEngine = prompt(context);
098:
099: return overlayEngine != null;
100: }
101:
102: private OverlayEngine prompt(PlugInContext context) {
103: //Unlike ValidatePlugIn, here we always call #initDialog because we want
104: //to update the layer comboboxes. [Jon Aquino]
105: initDialog(context);
106: dialog.setVisible(true);
107:
108: if (!dialog.wasOKPressed()) {
109: return null;
110: }
111:
112: OverlayEngine e = new OverlayEngine();
113: e.setAllowingPolygonsOnly(dialog.getBoolean(POLYGON_OUTPUT));
114: e.setSplittingGeometryCollections(dialog
115: .getBoolean(POLYGON_OUTPUT));
116:
117: return e;
118: }
119:
120: private void initDialog(PlugInContext context) {
121: dialog = new MultiInputDialog(context.getWorkbenchFrame(),
122: getName(), true);
123: dialog.setSideBarImage(IconLoader.icon("Overlay.gif"));
124: dialog
125: .setSideBarDescription(I18N
126: .get("ui.plugin.analysis.OverlayPlugIn.create-new-layer-containing-intersections-of-all-pairs-of-input-features"));
127: String fieldName = FIRST_LAYER;
128: JComboBox addLayerComboBox = dialog.addLayerComboBox(fieldName,
129: context.getCandidateLayer(0), null, context
130: .getLayerManager());
131: String fieldName1 = SECOND_LAYER;
132: JComboBox addLayerComboBox1 = dialog.addLayerComboBox(
133: fieldName1, context.getCandidateLayer(1), null, context
134: .getLayerManager());
135: dialog
136: .addCheckBox(
137: POLYGON_OUTPUT,
138: true,
139: I18N
140: .get("ui.plugin.analysis.OverlayPlugIn.splits-multipolygons-and-geometry-and-filters-out-non-polygons"));
141: dialog.addCheckBox(TRANSFER_ATTRIBUTES_FROM_FIRST_LAYER, true);
142: dialog.addCheckBox(TRANSFER_ATTRIBUTES_FROM_SECOND_LAYER, true);
143: GUIUtil.centreOnWindow(dialog);
144: }
145:
146: public void run(TaskMonitor monitor, PlugInContext context)
147: throws Exception {
148: FeatureCollection a = dialog.getLayer(FIRST_LAYER)
149: .getFeatureCollectionWrapper();
150: FeatureCollection b = dialog.getLayer(SECOND_LAYER)
151: .getFeatureCollectionWrapper();
152: FeatureCollection overlay = overlayEngine.overlay(a, b,
153: mapping(a, b), monitor);
154: context.getLayerManager().addCategory(categoryName);
155: context.addLayer(categoryName, I18N
156: .get("ui.plugin.analysis.OverlayPlugIn.overlay"),
157: overlay);
158: }
159:
160: private AttributeMapping mapping(FeatureCollection a,
161: FeatureCollection b) {
162: return new AttributeMapping(dialog
163: .getBoolean(TRANSFER_ATTRIBUTES_FROM_FIRST_LAYER) ? a
164: .getFeatureSchema() : new FeatureSchema(), dialog
165: .getBoolean(TRANSFER_ATTRIBUTES_FROM_SECOND_LAYER) ? b
166: .getFeatureSchema() : new FeatureSchema());
167: }
168: }
|