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.test;
034:
035: import java.io.IOException;
036: import java.util.Arrays;
037: import java.util.Date;
038: import java.util.List;
039:
040: import com.vividsolutions.jts.geom.Coordinate;
041: import com.vividsolutions.jts.geom.Geometry;
042: import com.vividsolutions.jts.geom.GeometryFactory;
043: import com.vividsolutions.jts.io.ParseException;
044: import com.vividsolutions.jts.io.WKTReader;
045: import com.vividsolutions.jump.I18N;
046: import com.vividsolutions.jump.feature.AttributeType;
047: import com.vividsolutions.jump.feature.BasicFeature;
048: import com.vividsolutions.jump.feature.Feature;
049: import com.vividsolutions.jump.feature.FeatureCollection;
050: import com.vividsolutions.jump.feature.FeatureDataset;
051: import com.vividsolutions.jump.feature.FeatureSchema;
052: import com.vividsolutions.jump.workbench.model.Layer;
053: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
054: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
055: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
056: import com.vividsolutions.jump.workbench.ui.MenuNames;
057:
058: public class RandomTrianglesPlugIn extends AbstractPlugIn {
059: private static int dummyLayerCount = 0;
060: private GeometryFactory geometryFactory = new GeometryFactory();
061: private WKTReader wktReader = new WKTReader(geometryFactory);
062: private List cities = Arrays.asList(new String[] { "Alabama",
063: "Alaska", "Arizona", "Arkansas", "California", "Colorado",
064: "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii",
065: "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
066: "Kentucky", "Louisiana", "Maine", "Maryland",
067: "Massachusetts", "Michigan", "Minnesota", "Mississippi",
068: "Missouri", "Montana", "Nebraska", "Nevada",
069: "New Hampshire", "New Jersey", "New Mexico", "New York",
070: "North Carolina", "North Dakota", "Ohio", "Oklahoma",
071: "Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
072: "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
073: "Virginia", "Washington", "West Virginia", "Wisconsin",
074: "Wyoming" });
075:
076: public RandomTrianglesPlugIn() {
077: }
078:
079: public void initialize(PlugInContext context) throws Exception {
080: context.getFeatureInstaller().addLayerViewMenuItem(
081: this ,
082: new String[] { MenuNames.TOOLS,
083: MenuNames.TOOLS_GENERATE }, getName());
084: }
085:
086: public boolean execute(PlugInContext context)
087: throws ParseException, IOException {
088: return execute(context, 50);
089: }
090:
091: public boolean execute(PlugInContext context, int layerSize)
092: throws ParseException, IOException {
093: // String inputValue = JOptionPane.showInputDialog(
094: // context.getWorkbenchFrame(),
095: // "Number of layers to generate (Default = 2):", getName(), JOptionPane.QUESTION_MESSAGE);
096: // if (inputValue == null) { return false; } //User hit Cancel [Jon Aquino]
097: // int n;
098: // try {
099: // n = Integer.parseInt(inputValue);
100: // }
101: // catch (NumberFormatException e) {
102: // n = 2;
103: // }
104: int n = 1;
105:
106: for (int i = 0; i < n; i++) {
107: generateLayer(context, layerSize);
108: }
109:
110: return true;
111: }
112:
113: private void generateLayer(PlugInContext context, int size)
114: throws ParseException, IOException {
115: dummyLayerCount++;
116:
117: FeatureSchema featureSchema = new FeatureSchema();
118: featureSchema.addAttribute("Geometry", AttributeType.GEOMETRY);
119: featureSchema.addAttribute("City", AttributeType.STRING);
120: featureSchema.addAttribute("A Code", AttributeType.DATE);
121:
122: //Put GEOMETRY in this unusual position to test robustness of
123: //AttributeTableModel [Jon Aquino]
124: // featureSchema.addAttribute("Geometry", AttributeType.GEOMETRY);
125: featureSchema.addAttribute("B Code", AttributeType.INTEGER);
126: featureSchema.addAttribute("C Code", AttributeType.DOUBLE);
127: featureSchema.addAttribute("D Code", AttributeType.STRING);
128: featureSchema.addAttribute("E Code", AttributeType.STRING);
129: featureSchema.addAttribute("F Code", AttributeType.STRING);
130: featureSchema.addAttribute("G Code", AttributeType.STRING);
131: featureSchema.addAttribute("H Code", AttributeType.STRING);
132: featureSchema.addAttribute("I Code", AttributeType.STRING);
133: featureSchema.addAttribute("J Code", AttributeType.STRING);
134: featureSchema.addAttribute("K Code", AttributeType.STRING);
135: featureSchema.addAttribute("L Code", AttributeType.STRING);
136: featureSchema.addAttribute("M Code", AttributeType.STRING);
137: featureSchema.addAttribute("N Code", AttributeType.STRING);
138: featureSchema.addAttribute("O Code", AttributeType.STRING);
139: featureSchema.addAttribute("P Code", AttributeType.STRING);
140:
141: FeatureCollection featureCollection = new FeatureDataset(
142: featureSchema);
143: addFeature(cornerSquare(), featureCollection);
144:
145: for (int i = 0; i < size; i++) {
146: addFeature(randomTriangle(), featureCollection);
147: }
148:
149: Layer layer = context.addLayer(StandardCategoryNames.WORKING,
150: I18N.get("ui.test.RandomTriangle.random-triangles"),
151: featureCollection);
152: layer.setDescription("ABCDE");
153: }
154:
155: private Geometry cornerSquare() throws ParseException {
156: return wktReader
157: .read("POLYGON ((-50 -50, 50 -50, 50 50, -50 50, -50 -50))");
158: }
159:
160: private void addFeature(Geometry geometry,
161: FeatureCollection featureCollection) {
162: Feature feature = new BasicFeature(featureCollection
163: .getFeatureSchema());
164: feature.setAttribute("Geometry", geometry);
165: feature.setAttribute("City", cities.get((int) Math.floor(Math
166: .random()
167: * cities.size())));
168: feature.setAttribute("A Code", new Date());
169: feature.setAttribute("B Code", new Integer(
170: (int) (Math.random() * 100000)));
171: feature.setAttribute("C Code", new Double(
172: Math.random() * 100000));
173: feature.setAttribute("D Code", new Date((int) Math.pow(Math
174: .random() * 100000, 20)).toString());
175: feature.setAttribute("E Code", ""
176: + (int) (Math.random() * 100000));
177: feature.setAttribute("F Code", ""
178: + (int) (Math.random() * 100000));
179: feature.setAttribute("G Code", ""
180: + (int) (Math.random() * 100000));
181: feature.setAttribute("H Code", ""
182: + (int) (Math.random() * 100000));
183: feature.setAttribute("I Code", ""
184: + (int) (Math.random() * 100000));
185: feature.setAttribute("J Code", ""
186: + (int) (Math.random() * 100000));
187: feature.setAttribute("K Code", ""
188: + (int) (Math.random() * 100000));
189: feature.setAttribute("L Code", ""
190: + (int) (Math.random() * 100000));
191: feature.setAttribute("M Code", ""
192: + (int) (Math.random() * 100000));
193: feature.setAttribute("N Code", ""
194: + (int) (Math.random() * 100000));
195: feature.setAttribute("O Code", ""
196: + (int) (Math.random() * 100000));
197: feature.setAttribute("P Code", ""
198: + (int) (Math.random() * 100000));
199:
200: if (Math.random() > 0.8) {
201: feature.setAttribute("E Code", null);
202: }
203:
204: featureCollection.add(feature);
205: }
206:
207: private Geometry randomTriangle() {
208: int perturbation = 30;
209:
210: int x = (int) (Math.random() * 700);
211: int y = (int) (Math.random() * 700);
212: Coordinate firstPoint = perturbedPoint(x, y, perturbation);
213:
214: return geometryFactory.createPolygon(
215: geometryFactory
216: .createLinearRing(new Coordinate[] {
217: firstPoint,
218: perturbedPoint(x, y, perturbation),
219: perturbedPoint(x, y, perturbation),
220: firstPoint }), null);
221: }
222:
223: private Coordinate perturbedPoint(int x, int y, int perturbation) {
224: return new Coordinate(x + (Math.random() * perturbation), y
225: + (Math.random() * perturbation));
226: }
227:
228: public void setCities(List cities) {
229: this.cities = cities;
230: }
231:
232: }
|