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.warp;
034:
035: import java.util.Iterator;
036:
037: import com.vividsolutions.jts.geom.Coordinate;
038: import com.vividsolutions.jts.geom.LineString;
039: import com.vividsolutions.jts.util.Assert;
040: import com.vividsolutions.jump.I18N;
041: import com.vividsolutions.jump.feature.Feature;
042: import com.vividsolutions.jump.feature.FeatureCollection;
043: import com.vividsolutions.jump.warp.AffineTransform;
044: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
045: import com.vividsolutions.jump.workbench.plugin.*;
046: import com.vividsolutions.jump.workbench.ui.MenuNames;
047:
048: /**
049: * Applies an affine transform to the selected layers. The affine transform is
050: * specified using three vectors drawn by the user.
051: */
052: public class AffineTransformPlugIn extends AbstractPlugIn {
053: public AffineTransformPlugIn() {
054: }
055:
056: public static EnableCheck getEnableCheck(
057: EnableCheckFactory checkFactory) {
058: return new MultiEnableCheck()
059: .add(
060: checkFactory
061: .createWindowWithLayerViewPanelMustBeActiveCheck())
062: .add(
063: checkFactory
064: .createWindowWithLayerNamePanelMustBeActiveCheck())
065: .add(
066: checkFactory
067: .createExactlyNLayersMustBeSelectedCheck(1))
068: .add(
069: checkFactory
070: .createBetweenNAndMVectorsMustBeDrawnCheck(
071: 1, 3));
072: }
073:
074: public void initialize(PlugInContext context) throws Exception {
075: context.getFeatureInstaller().addMainMenuItem(this ,
076: new String[] { MenuNames.TOOLS, MenuNames.TOOLS_WARP },
077: getName(), false, null,
078: getEnableCheck(context.getCheckFactory()));
079: }
080:
081: public boolean execute(PlugInContext context) throws Exception {
082: AffineTransform transform = affineTransform(context);
083: FeatureCollection featureCollection = transform
084: .transform(context.getSelectedLayer(0)
085: .getFeatureCollectionWrapper());
086: context.getLayerManager().addLayer(
087: StandardCategoryNames.WORKING,
088: I18N.get("ui.warp.AffineTransformPlugIn.affined") + " "
089: + context.getSelectedLayer(0).getName(),
090: featureCollection);
091: checkValid(featureCollection, context);
092:
093: return true;
094: }
095:
096: public static void checkValid(FeatureCollection featureCollection,
097: PlugInContext context) {
098: for (Iterator i = featureCollection.iterator(); i.hasNext();) {
099: Feature feature = (Feature) i.next();
100:
101: if (!feature.getGeometry().isValid()) {
102: context
103: .getLayerViewPanel()
104: .getContext()
105: .warnUser(
106: I18N
107: .get("ui.warp.AffineTransformPlugIn.some-geometries-are-not-valid"));
108:
109: return;
110: }
111: }
112: }
113:
114: /**
115: *@return either the tip or the tail coordinate of the nth vector
116: */
117: private Coordinate vectorCoordinate(int n, boolean tip,
118: PlugInContext context,
119: WarpingVectorLayerFinder vectorLayerManager) {
120: LineString vector = (LineString) vectorLayerManager
121: .getVectors().get(n);
122:
123: return tip ? vector.getCoordinateN(1) : vector
124: .getCoordinateN(0);
125: }
126:
127: private AffineTransform affineTransform(PlugInContext context) {
128: WarpingVectorLayerFinder vlm = new WarpingVectorLayerFinder(
129: context);
130:
131: switch (vlm.getVectors().size()) {
132: case 1:
133: return new AffineTransform(vectorCoordinate(0, false,
134: context, vlm), vectorCoordinate(0, true, context,
135: vlm));
136:
137: case 2:
138: return new AffineTransform(vectorCoordinate(0, false,
139: context, vlm), vectorCoordinate(0, true, context,
140: vlm), vectorCoordinate(1, false, context, vlm),
141: vectorCoordinate(1, true, context, vlm));
142:
143: case 3:
144: return new AffineTransform(vectorCoordinate(0, false,
145: context, vlm), vectorCoordinate(0, true, context,
146: vlm), vectorCoordinate(1, false, context, vlm),
147: vectorCoordinate(1, true, context, vlm),
148: vectorCoordinate(2, false, context, vlm),
149: vectorCoordinate(2, true, context, vlm));
150: }
151:
152: Assert.shouldNeverReachHere();
153:
154: return null;
155: }
156: }
|