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: 05.06.2006
030: * last modified:
031: *
032: *
033: * @author sstein
034: *
035: * description:
036: * created voronoi regions/thiessen polygons from a set of points.
037: * The Delauney algorithm used for the triangulation is by L. Paul Chew and
038: * his free demonstration java-applet.<p>
039: * http://www.cs.cornell.edu/Info/People/chew/Delaunay.html
040:
041: *****************************************************/package org.openjump.core.ui.plugin.tools;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Iterator;
046:
047: import javax.swing.JComboBox;
048:
049: import org.openjump.core.graph.delauneySimplexInsert.DTriangulationForJTS;
050:
051: import com.vividsolutions.jts.geom.Geometry;
052: import com.vividsolutions.jts.geom.Point;
053: import com.vividsolutions.jump.I18N;
054: import com.vividsolutions.jump.feature.Feature;
055: import com.vividsolutions.jump.feature.FeatureCollection;
056: import com.vividsolutions.jump.feature.FeatureDatasetFactory;
057: import com.vividsolutions.jump.task.TaskMonitor;
058: import com.vividsolutions.jump.workbench.WorkbenchContext;
059: import com.vividsolutions.jump.workbench.model.Layer;
060: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
061: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
062: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
063: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
064: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
065: import com.vividsolutions.jump.workbench.plugin.ThreadedPlugIn;
066: import com.vividsolutions.jump.workbench.ui.GUIUtil;
067: import com.vividsolutions.jump.workbench.ui.MenuNames;
068: import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
069: import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
070:
071: /**
072: * @description:
073: * Creates voronoi regions from a set of points.
074: * The Delauney algorithm used for the triangulation is by L. Paul Chew and
075: * his free demonstration java-applet.<p>
076: * http://www.cs.cornell.edu/Info/People/chew/Delaunay.html
077: *
078: * @author sstein
079: *
080: **/
081: public class CreateThiessenPolygonsPlugIn extends AbstractPlugIn
082: implements ThreadedPlugIn {
083:
084: private String sName = "Create Thiessen Polygons";
085: private String CLAYER = "select point layer";
086: private String sideBarText = "Creates a Delaunay triangulation and returns the Voronoi regions.";
087: private String msgCreateDG = "create triangulation";
088: private String msgCreatePolys = "create polygons from voronoi edges";
089: private String msgNoPoint = "no point geometry";
090: private Layer itemlayer = null;
091:
092: public void initialize(PlugInContext context) throws Exception {
093:
094: this .CLAYER = I18N
095: .get("org.openjump.core.ui.plugin.tools.CreateThiessenPolygonsPlugIn.select-point-layer");
096: this .sName = I18N
097: .get("org.openjump.core.ui.plugin.tools.CreateThiessenPolygonsPlugIn.Create-Thiessen-Polygons");
098: this .sideBarText = I18N
099: .get("org.openjump.core.ui.plugin.tools.CreateThiessenPolygonsPlugIn.Creates-a-Delaunay-triangulation-and-returns-the-Voronoi-regions");
100: this .msgCreateDG = I18N
101: .get("org.openjump.core.ui.plugin.tools.CreateThiessenPolygonsPlugIn.create-triangulation");
102: this .msgCreatePolys = I18N
103: .get("org.openjump.core.ui.plugin.tools.CreateThiessenPolygonsPlugIn.create-polygons-from-voronoi-edges");
104: this .msgNoPoint = I18N
105: .get("org.openjump.core.ui.plugin.tools.CreateThiessenPolygonsPlugIn.no-point-geometry");
106:
107: FeatureInstaller featureInstaller = new FeatureInstaller(
108: context.getWorkbenchContext());
109: featureInstaller.addMainMenuItem(this , //exe
110: new String[] { MenuNames.TOOLS,
111: MenuNames.TOOLS_GENERATE }, //menu path
112: this .sName + "{pos:2}", //name methode .getName recieved by AbstractPlugIn
113: false, //checkbox
114: null, //icon
115: createEnableCheck(context.getWorkbenchContext())); //enable check
116: }
117:
118: public static MultiEnableCheck createEnableCheck(
119: WorkbenchContext workbenchContext) {
120: EnableCheckFactory checkFactory = new EnableCheckFactory(
121: workbenchContext);
122:
123: return new MultiEnableCheck().add(checkFactory
124: .createAtLeastNLayersMustExistCheck(1));
125: }
126:
127: public boolean execute(PlugInContext context) throws Exception {
128: this .reportNothingToUndoYet(context);
129:
130: MultiInputDialog dialog = new MultiInputDialog(context
131: .getWorkbenchFrame(), getName(), true);
132: setDialogValues(dialog, context);
133: GUIUtil.centreOnWindow(dialog);
134: dialog.setVisible(true);
135: if (!dialog.wasOKPressed()) {
136: return false;
137: }
138: getDialogValues(dialog);
139: return true;
140: }
141:
142: private void setDialogValues(MultiInputDialog dialog,
143: PlugInContext context) {
144: dialog.setSideBarDescription(this .sideBarText);
145: JComboBox addLayerComboBoxBuild = dialog.addLayerComboBox(
146: this .CLAYER, context.getCandidateLayer(0), null,
147: context.getLayerManager());
148: }
149:
150: private void getDialogValues(MultiInputDialog dialog) {
151: this .itemlayer = dialog.getLayer(this .CLAYER);
152: }
153:
154: public void run(TaskMonitor monitor, PlugInContext context)
155: throws Exception {
156: this .createGraph(context, monitor);
157: System.gc();
158: }
159:
160: private boolean createGraph(PlugInContext context,
161: TaskMonitor monitor) throws Exception {
162: System.gc(); //flush garbage collector
163: // --------------------------
164: //-- get selected items
165: final Collection features = this .itemlayer
166: .getFeatureCollectionWrapper().getFeatures();
167: ArrayList points = new ArrayList();
168: for (Iterator iter = features.iterator(); iter.hasNext();) {
169: Feature f = (Feature) iter.next();
170: Geometry g = f.getGeometry();
171: if (g instanceof Point) {
172: points.add(f.getGeometry());
173: } else {
174: context.getWorkbenchFrame().warnUser(this .msgNoPoint);
175: }
176: }
177: if (points.size() > 0) {
178: monitor.report(this .msgCreateDG);
179: DTriangulationForJTS tri = new DTriangulationForJTS(points);
180:
181: //ArrayList nodes = tri.drawAllSites();
182: //FeatureCollection myCollA = FeatureDatasetFactory.createFromGeometry(nodes);
183: //context.addLayer(StandardCategoryNames.WORKING, "sites", myCollA);
184:
185: //ArrayList nodes2 = tri.getInitialSimmplexAsJTSPoints();
186: //FeatureCollection myCollD = FeatureDatasetFactory.createFromGeometry(nodes2);
187: //context.addLayer(StandardCategoryNames.WORKING, "cornerpoints", myCollD);
188:
189: //ArrayList edges = tri.drawAllVoronoi();
190: //FeatureCollection myCollB = FeatureDatasetFactory.createFromGeometry(edges);
191: //context.addLayer(StandardCategoryNames.WORKING, "voronoi edges", myCollB);
192:
193: //ArrayList bbox = new ArrayList();
194: //bbox.add(tri.getThiessenBoundingBox());
195: //FeatureCollection myCollE = FeatureDatasetFactory.createFromGeometry(bbox);
196: //context.addLayer(StandardCategoryNames.WORKING, "bbox", myCollE);
197:
198: monitor.report(this .msgCreatePolys);
199: Collection polys = tri.getThiessenPolys();
200: FeatureCollection myCollC = FeatureDatasetFactory
201: .createFromGeometry(polys);
202: context.addLayer(StandardCategoryNames.WORKING,
203: "Thiessen polygons", myCollC);
204: } else {
205: context.getWorkbenchFrame().warnUser(this .msgNoPoint);
206: }
207: return true;
208: }
209: }
|