Revision 37138 branches/v2_0_0_prep/extensions/extEditing/src/org/gvsig/editing/gui/cad/tools/EquidistanceCADTool.java

View differences:

EquidistanceCADTool.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
1
/* gvSIG. Geographic Information System of the Valencian Government
2 2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
5 6
 * This program is free software; you can redistribute it and/or
6 7
 * modify it under the terms of the GNU General Public License
7 8
 * as published by the Free Software Foundation; either version 2
8 9
 * of the License, or (at your option) any later version.
9
 *
10
 * 
10 11
 * This program is distributed in the hope that it will be useful,
11 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 14
 * GNU General Public License for more details.
14
 *
15
 * 
15 16
 * You should have received a copy of the GNU General Public License
16 17
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
40 21
 */
41 22
package org.gvsig.editing.gui.cad.tools;
42 23

  
......
44 25
import java.awt.geom.Point2D;
45 26
import java.util.ArrayList;
46 27

  
28
import com.vividsolutions.jts.algorithm.CGAlgorithms;
29
import com.vividsolutions.jts.geom.Coordinate;
30
import com.vividsolutions.jts.geom.GeometryFactory;
31
import com.vividsolutions.jts.geom.LineString;
32
import com.vividsolutions.jts.geom.LinearRing;
33
import com.vividsolutions.jts.geom.Point;
34
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier;
35

  
47 36
import org.gvsig.andami.PluginServices;
48 37
import org.gvsig.editing.CADExtension;
49 38
import org.gvsig.editing.gui.cad.DefaultCADTool;
......
53 42
import org.gvsig.editing.layers.VectorialLayerEdited;
54 43
import org.gvsig.fmap.dal.exception.DataException;
55 44
import org.gvsig.fmap.dal.exception.ReadException;
56
import org.gvsig.tools.dispose.DisposableIterator;
57 45
import org.gvsig.fmap.dal.feature.Feature;
58 46
import org.gvsig.fmap.dal.feature.FeatureSelection;
59 47
import org.gvsig.fmap.dal.feature.FeatureSet;
......
66 54
import org.gvsig.fmap.geom.util.Converter;
67 55
import org.gvsig.fmap.geom.util.UtilFunctions;
68 56
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
57
import org.gvsig.tools.dispose.DisposableIterator;
69 58

  
70
import com.vividsolutions.jts.algorithm.CGAlgorithms;
71
import com.vividsolutions.jts.geom.Coordinate;
72
import com.vividsolutions.jts.geom.GeometryFactory;
73
import com.vividsolutions.jts.geom.LineString;
74
import com.vividsolutions.jts.geom.LinearRing;
75
import com.vividsolutions.jts.geom.Point;
76
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier;
77

  
78 59
/**
79 60
 * Herramienta para crear una geometr?a equidistante a otra.
80
 *
61
 * 
81 62
 * @author Vicente Caballero Navarro
82 63
 */
83 64
public class EquidistanceCADTool extends DefaultCADTool {
84
	private EquidistanceCADToolContext _fsm;
85
	private Point2D firstPoint = new Point2D.Double(800000, 4500000);
86
	private Point2D secondPoint = new Point2D.Double(810000, 4500000);
87
	private double distance = 10;
88
	private double distancePos = java.lang.Double.MAX_VALUE;
89
	private LineString distanceLine;
90 65

  
91
	/**
92
	 * Crea un nuevo EquidistanceCADTool.
93
	 */
94
	public EquidistanceCADTool() {
95
	}
66
    private EquidistanceCADToolContext _fsm;
67
    private Point2D firstPoint = new Point2D.Double(800000, 4500000);
68
    private Point2D secondPoint = new Point2D.Double(810000, 4500000);
69
    private double distance = 10;
70
    private double distancePos = java.lang.Double.MAX_VALUE;
71
    private LineString distanceLine;
96 72

  
97
	/**
98
	 * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
99
	 * carga previa a la utilizaci?n de la herramienta.
100
	 */
101
	public void init() {
102
		_fsm = new EquidistanceCADToolContext(this);
73
    /**
74
     * Crea un nuevo EquidistanceCADTool.
75
     */
76
    public EquidistanceCADTool() {
77
    }
103 78

  
104
	}
79
    /**
80
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
81
     * carga previa a la utilizaci?n de la herramienta.
82
     */
83
    public void init() {
84
        _fsm = new EquidistanceCADToolContext(this);
105 85

  
106
	private Coordinate[] getParallel(Point2D[] points, double distance) {
107
		Point2D[] pper = new Point2D[2];
108
		double angle = Math.toRadians(90)
109
				+ UtilFunctions.getAngle(points[0], points[1]);
110
		pper[0] = UtilFunctions.getPoint(points[0], angle, distance);
111
		pper[1] = UtilFunctions.getPoint(points[1], angle, distance);
112
		Coordinate[] result = new Coordinate[2];
113
		result[0] = new Coordinate(pper[0].getX(), pper[0].getY());
114
		result[1] = new Coordinate(pper[1].getX(), pper[1].getY());
115
		return result;
116
	}
86
    }
117 87

  
118
	/*
119
	 * (non-Javadoc)
120
	 *
121
	 * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
122
	 *      double, double)
123
	 */
124
	public void transition(double x, double y, InputEvent event) {
125
		_fsm.addPoint(x, y, event);
126
	}
88
    private Coordinate[] getParallel(Point2D[] points, double distance) {
89
        Point2D[] pper = new Point2D[2];
90
        double angle =
91
            Math.toRadians(90) + UtilFunctions.getAngle(points[0], points[1]);
92
        pper[0] = UtilFunctions.getPoint(points[0], angle, distance);
93
        pper[1] = UtilFunctions.getPoint(points[1], angle, distance);
94
        Coordinate[] result = new Coordinate[2];
95
        result[0] = new Coordinate(pper[0].getX(), pper[0].getY());
96
        result[1] = new Coordinate(pper[1].getX(), pper[1].getY());
97
        return result;
98
    }
127 99

  
128
	/*
129
	 * (non-Javadoc)
130
	 *
131
	 * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
132
	 *      double)
133
	 */
134
	public void transition(double d) {
135
		_fsm.addValue(d);
136
	}
100
    /*
101
     * (non-Javadoc)
102
     * 
103
     * @see
104
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
105
     * .layers.FBitSet,
106
     * double, double)
107
     */
108
    public void transition(double x, double y, InputEvent event) {
109
        _fsm.addPoint(x, y, event);
110
    }
137 111

  
138
	/*
139
	 * (non-Javadoc)
140
	 *
141
	 * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
142
	 *      java.lang.String)
143
	 */
144
	public void transition(String s) throws CommandException {
145
		if (!super.changeCommand(s)) {
146
			_fsm.addOption(s);
147
		}
148
	}
112
    /*
113
     * (non-Javadoc)
114
     * 
115
     * @see
116
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
117
     * .layers.FBitSet,
118
     * double)
119
     */
120
    public void transition(double d) {
121
        _fsm.addValue(d);
122
    }
149 123

  
150
	/**
151
	 * DOCUMENT ME!
152
	 */
153
	public void selection() {
154
		FeatureSet selection = null;
155
		try {
156
			selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
124
    /*
125
     * (non-Javadoc)
126
     * 
127
     * @see
128
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
129
     * .layers.FBitSet,
130
     * java.lang.String)
131
     */
132
    public void transition(String s) throws CommandException {
133
        if (!super.changeCommand(s)) {
134
            _fsm.addOption(s);
135
        }
136
    }
157 137

  
158
			if (selection.getSize() == 0
159
					&& !CADExtension
160
							.getCADTool()
161
							.getClass()
162
							.getName()
163
							.equals(
164
									"com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
165
				CADExtension.setCADTool("_selection", false);
166
				((SelectionCADTool) CADExtension.getCADTool())
167
						.setNextTool("_Equidistance");
168
			}
169
		} catch (ReadException e) {
170
			// TODO Auto-generated catch block
171
			e.printStackTrace();
172
		} catch (DataException e) {
173
			// TODO Auto-generated catch block
174
			e.printStackTrace();
175
		}
176
	}
138
    /**
139
     * DOCUMENT ME!
140
     */
141
    public void selection() {
142
        FeatureSet selection = null;
143
        try {
144
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
177 145

  
178
	/**
179
	 * Equivale al transition del prototipo pero sin pasarle como par?metro el
180
	 * editableFeatureSource que ya estar? creado.
181
	 *
182
	 * @param x
183
	 *            par?metro x del punto que se pase en esta transici?n.
184
	 * @param y
185
	 *            par?metro y del punto que se pase en esta transici?n.
186
	 */
187
	public void addPoint(double x, double y, InputEvent event) {
188
		EquidistanceCADToolState actualState = (EquidistanceCADToolState) _fsm
189
				.getPreviousState();
190
		String status = actualState.getName();
191
		if (status.equals("Equidistance.Distance")) {
192
			firstPoint = new Point2D.Double(x, y);
193
		} else if (status.equals("Equidistance.SecondPointDistance")) {
194
			secondPoint = new Point2D.Double(x, y);
195
			distance = secondPoint.distance(firstPoint);
196
		} else if (status.equals("Equidistance.Position")) {
197
			VectorialLayerEdited vle = getVLE();
198
			FeatureStore featureStore = null;
199
			try {
200
				featureStore = vle.getFeatureStore();
201
			} catch (ReadException e) {
202
				// TODO Auto-generated catch block
203
				e.printStackTrace();
204
			}
205
			ArrayList selectedRowAux = new ArrayList();
206
			DisposableIterator iterator = null;
207
			try {
208
				iterator = ((FeatureSelection) featureStore.getSelection())
209
						.iterator();
146
            if (selection.getSize() == 0
147
                && !CADExtension
148
                    .getCADTool()
149
                    .getClass()
150
                    .getName()
151
                    .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
152
                CADExtension.setCADTool("_selection", false);
153
                ((SelectionCADTool) CADExtension.getCADTool())
154
                    .setNextTool("_Equidistance");
155
            }
156
        } catch (ReadException e) {
157
            // TODO Auto-generated catch block
158
            e.printStackTrace();
159
        } catch (DataException e) {
160
            // TODO Auto-generated catch block
161
            e.printStackTrace();
162
        }
163
    }
210 164

  
211
				PluginServices.getMDIManager().setWaitCursor();
212
				featureStore.beginEditingGroup(getName());
213
				while (iterator.hasNext()) {
214
					Feature feature = (Feature) iterator.next();
215
					Geometry geometry = compute(feature, new Point2D.Double(x,
216
							y));
217
					insertAndSelectGeometry(geometry);
218
				}
165
    /**
166
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
167
     * editableFeatureSource que ya estar? creado.
168
     * 
169
     * @param x
170
     *            par?metro x del punto que se pase en esta transici?n.
171
     * @param y
172
     *            par?metro y del punto que se pase en esta transici?n.
173
     */
174
    public void addPoint(double x, double y, InputEvent event) {
175
        EquidistanceCADToolState actualState =
176
            (EquidistanceCADToolState) _fsm.getPreviousState();
177
        String status = actualState.getName();
178
        if (status.equals("Equidistance.Distance")) {
179
            firstPoint = new Point2D.Double(x, y);
180
        } else
181
            if (status.equals("Equidistance.SecondPointDistance")) {
182
                secondPoint = new Point2D.Double(x, y);
183
                distance = secondPoint.distance(firstPoint);
184
            } else
185
                if (status.equals("Equidistance.Position")) {
186
                    VectorialLayerEdited vle = getVLE();
187
                    FeatureStore featureStore = null;
188
                    try {
189
                        featureStore = vle.getFeatureStore();
190
                    } catch (ReadException e) {
191
                        // TODO Auto-generated catch block
192
                        e.printStackTrace();
193
                    }
194
                    ArrayList selectedRowAux = new ArrayList();
195
                    DisposableIterator iterator = null;
196
                    try {
197
                        iterator =
198
                            ((FeatureSelection) featureStore.getSelection())
199
                                .iterator();
219 200

  
220
				featureStore.endEditingGroup();
221
			} catch (DataException e) {
222
				// TODO Auto-generated catch block
223
				e.printStackTrace();
224
			} catch (CreateGeometryException e) {
225
				// TODO Auto-generated catch block
226
				e.printStackTrace();
227
			} finally {
228
				if (iterator != null) {
229
					iterator.dispose();
230
				}
231
			}
232
			PluginServices.getMDIManager().restoreCursor();
233
		}
234
	}
201
                        PluginServices.getMDIManager().setWaitCursor();
202
                        featureStore.beginEditingGroup(getName());
203
                        while (iterator.hasNext()) {
204
                            Feature feature = (Feature) iterator.next();
205
                            Geometry geometry =
206
                                compute(feature, new Point2D.Double(x, y));
207
                            insertAndSelectGeometry(geometry);
208
                        }
235 209

  
236
	private Geometry compute(Feature fea, Point2D position) throws CreateGeometryException {
237
		Geometry geometry = (fea.getDefaultGeometry())
238
				.cloneGeometry();
239
		int typeGeometry = geometry.getType();
240
		com.vividsolutions.jts.geom.Geometry g = null;
241
		try {
242
			g = (com.vividsolutions.jts.geom.Geometry) geometry
243
					.invokeOperation(ToJTS.CODE, null);
244
		} catch (GeometryOperationNotSupportedException e) {
245
			// TODO Auto-generated catch block
246
			e.printStackTrace();
247
		} catch (GeometryOperationException e) {
248
			// TODO Auto-generated catch block
249
			e.printStackTrace();
250
		}
210
                        featureStore.endEditingGroup();
211
                    } catch (DataException e) {
212
                        // TODO Auto-generated catch block
213
                        e.printStackTrace();
214
                    } catch (CreateGeometryException e) {
215
                        // TODO Auto-generated catch block
216
                        e.printStackTrace();
217
                    } finally {
218
                        if (iterator != null) {
219
                            iterator.dispose();
220
                        }
221
                    }
222
                    PluginServices.getMDIManager().restoreCursor();
223
                }
224
    }
251 225

  
252
		// com.vividsolutions.jts.geom.Geometry g = geometry.toJTSGeometry();
253
		GeometryFactory factory = g.getFactory();
254
		Coordinate[] c2 = new Coordinate[2];
255
		com.vividsolutions.jts.geom.Geometry g2 = null;
256
		Coordinate coordinatePosition = new Coordinate(position.getX(),
257
				position.getY());
258
		Point pPos = factory.createPoint(coordinatePosition);
259
		switch (typeGeometry) {
260
		case Geometry.TYPES.CURVE:
226
    private Geometry compute(Feature fea, Point2D position)
227
        throws CreateGeometryException {
228
        Geometry geometry = (fea.getDefaultGeometry()).cloneGeometry();
229
        int typeGeometry = geometry.getType();
230
        com.vividsolutions.jts.geom.Geometry g = null;
231
        try {
232
            g =
233
                (com.vividsolutions.jts.geom.Geometry) geometry
234
                    .invokeOperation(ToJTS.CODE, null);
235
        } catch (GeometryOperationNotSupportedException e) {
236
            // TODO Auto-generated catch block
237
            e.printStackTrace();
238
        } catch (GeometryOperationException e) {
239
            // TODO Auto-generated catch block
240
            e.printStackTrace();
241
        }
261 242

  
262
			LineString lR = factory.createLineString(g.getCoordinates());
263
			g = TopologyPreservingSimplifier.simplify(lR, 10d);
264
			com.vividsolutions.jts.geom.Geometry gLine = g.getGeometryN(0);
265
			Coordinate[] coordinatesLine = gLine.getCoordinates();
266
			int numPointsLine = gLine.getNumPoints();
267
			if (numPointsLine < 1) {
268
				return null;
269
			}
270
			LineString[] lineStrings = new LineString[numPointsLine - 1];
243
        // com.vividsolutions.jts.geom.Geometry g = geometry.toJTSGeometry();
244
        GeometryFactory factory = g.getFactory();
245
        Coordinate[] c2 = new Coordinate[2];
246
        com.vividsolutions.jts.geom.Geometry g2 = null;
247
        Coordinate coordinatePosition =
248
            new Coordinate(position.getX(), position.getY());
249
        Point pPos = factory.createPoint(coordinatePosition);
250
        switch (typeGeometry) {
251
        case Geometry.TYPES.CURVE:
271 252

  
272
			for (int j = 1; j < numPointsLine; j = j + 1) {
273
				c2[0] = coordinatesLine[j - 1];
274
				c2[1] = coordinatesLine[j];
275
				LineString lS = factory.createLineString(c2);
276
				if (lS.distance(pPos) < distancePos) {
277
					distancePos = lS.distance(pPos);
278
					distanceLine = (LineString) factory.createGeometry(lS);
279
				}
280
			}
281
			setDistanceLine(coordinatePosition);
253
            LineString lR = factory.createLineString(g.getCoordinates());
254
            g = TopologyPreservingSimplifier.simplify(lR, 10d);
255
            com.vividsolutions.jts.geom.Geometry gLine = g.getGeometryN(0);
256
            Coordinate[] coordinatesLine = gLine.getCoordinates();
257
            int numPointsLine = gLine.getNumPoints();
258
            if (numPointsLine < 1) {
259
                return null;
260
            }
261
            LineString[] lineStrings = new LineString[numPointsLine - 1];
282 262

  
283
			for (int j = 1; j < numPointsLine; j = j + 1) {
284
				c2[0] = coordinatesLine[j - 1];
285
				c2[1] = coordinatesLine[j];
286
				Point2D[] points = new Point2D[2];
287
				points[0] = new Point2D.Double(c2[0].x, c2[0].y);
288
				points[1] = new Point2D.Double(c2[1].x, c2[1].y);
289
				lineStrings[j - 1] = factory.createLineString(getParallel(
290
						points, distance));
291
			}
263
            for (int j = 1; j < numPointsLine; j = j + 1) {
264
                c2[0] = coordinatesLine[j - 1];
265
                c2[1] = coordinatesLine[j];
266
                LineString lS = factory.createLineString(c2);
267
                if (lS.distance(pPos) < distancePos) {
268
                    distancePos = lS.distance(pPos);
269
                    distanceLine = (LineString) factory.createGeometry(lS);
270
                }
271
            }
272
            setDistanceLine(coordinatePosition);
292 273

  
293
			for (int i = 0; i < lineStrings.length - 1; i++) {
294
				Coordinate coord = lineStrings[i].getCoordinateN(0);
295
				Point2D p1 = new Point2D.Double(coord.x, coord.y);
296
				coord = lineStrings[i].getCoordinateN(1);
297
				Point2D p2 = new Point2D.Double(coord.x, coord.y);
298
				coord = lineStrings[i + 1].getCoordinateN(0);
299
				Point2D p3 = new Point2D.Double(coord.x, coord.y);
300
				coord = lineStrings[i + 1].getCoordinateN(1);
301
				Point2D p4 = new Point2D.Double(coord.x, coord.y);
302
				Point2D intersection = UtilFunctions.getIntersection(p1, p2,
303
						p3, p4);
304
				Coordinate[] coords1 = new Coordinate[2];
305
				coords1[0] = lineStrings[i].getCoordinateN(0);
306
				coords1[1] = new Coordinate(intersection.getX(), intersection
307
						.getY());
308
				lineStrings[i] = factory.createLineString(coords1);
309
				Coordinate[] coords2 = new Coordinate[2];
310
				coords2[0] = coords1[1];
311
				coords2[1] = lineStrings[i + 1].getCoordinateN(1);
312
				lineStrings[i + 1] = factory.createLineString(coords2);
313
			}
314
			g2 = factory.createMultiLineString(lineStrings);
315
			return Converter.jtsToGeometry(g2);
316
		case Geometry.TYPES.SURFACE:
317
		case Geometry.TYPES.CIRCLE:
318
		case Geometry.TYPES.ELLIPSE:
319
			g = TopologyPreservingSimplifier.simplify(g, 10d);
320
			com.vividsolutions.jts.geom.Geometry gPolygon = g.getGeometryN(0);
321
			setDistancePolygon(gPolygon, pPos);
322
			Coordinate[] coordinates = gPolygon.getCoordinates();
323
			int numPointsPolygon = gPolygon.getNumPoints();
324
			if (numPointsPolygon < 2) {
325
				return null;
326
			}
327
			LineString[] polygonStrings = new LineString[numPointsPolygon];
328
			for (int j = 1; j < numPointsPolygon; j = j + 1) {
329
				c2[0] = coordinates[j - 1];
330
				c2[1] = coordinates[j];
331
				Point2D[] points = new Point2D[2];
332
				points[0] = new Point2D.Double(c2[0].x, c2[0].y);
333
				points[1] = new Point2D.Double(c2[1].x, c2[1].y);
334
				polygonStrings[j - 1] = factory.createLineString(getParallel(
335
						points, distance));
336
			}
337
			for (int i = 0; i < polygonStrings.length - 2; i++) {
338
				Coordinate coord = polygonStrings[i].getCoordinateN(0);
339
				Point2D p1 = new Point2D.Double(coord.x, coord.y);
340
				coord = polygonStrings[i].getCoordinateN(1);
341
				Point2D p2 = new Point2D.Double(coord.x, coord.y);
342
				coord = polygonStrings[i + 1].getCoordinateN(0);
343
				Point2D p3 = new Point2D.Double(coord.x, coord.y);
344
				coord = polygonStrings[i + 1].getCoordinateN(1);
345
				Point2D p4 = new Point2D.Double(coord.x, coord.y);
346
				Point2D intersection = UtilFunctions.getIntersection(p1, p2,
347
						p3, p4);
348
				Coordinate[] coords1 = new Coordinate[2];
349
				coords1[0] = polygonStrings[i].getCoordinateN(0);
350
				coords1[1] = new Coordinate(intersection.getX(), intersection
351
						.getY());
352
				polygonStrings[i] = factory.createLineString(coords1);
353
				Coordinate[] coords2 = new Coordinate[2];
354
				coords2[0] = coords1[1];
355
				coords2[1] = polygonStrings[i + 1].getCoordinateN(1);
356
				polygonStrings[i + 1] = factory.createLineString(coords2);
357
			}
274
            for (int j = 1; j < numPointsLine; j = j + 1) {
275
                c2[0] = coordinatesLine[j - 1];
276
                c2[1] = coordinatesLine[j];
277
                Point2D[] points = new Point2D[2];
278
                points[0] = new Point2D.Double(c2[0].x, c2[0].y);
279
                points[1] = new Point2D.Double(c2[1].x, c2[1].y);
280
                lineStrings[j - 1] =
281
                    factory.createLineString(getParallel(points, distance));
282
            }
358 283

  
359
			Coordinate coord = polygonStrings[0].getCoordinateN(0);
360
			Point2D p1 = new Point2D.Double(coord.x, coord.y);
361
			coord = polygonStrings[0].getCoordinateN(1);
362
			Point2D p2 = new Point2D.Double(coord.x, coord.y);
363
			coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(0);
364
			Point2D p3 = new Point2D.Double(coord.x, coord.y);
365
			coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(1);
366
			Point2D p4 = new Point2D.Double(coord.x, coord.y);
284
            for (int i = 0; i < lineStrings.length - 1; i++) {
285
                Coordinate coord = lineStrings[i].getCoordinateN(0);
286
                Point2D p1 = new Point2D.Double(coord.x, coord.y);
287
                coord = lineStrings[i].getCoordinateN(1);
288
                Point2D p2 = new Point2D.Double(coord.x, coord.y);
289
                coord = lineStrings[i + 1].getCoordinateN(0);
290
                Point2D p3 = new Point2D.Double(coord.x, coord.y);
291
                coord = lineStrings[i + 1].getCoordinateN(1);
292
                Point2D p4 = new Point2D.Double(coord.x, coord.y);
293
                Point2D intersection =
294
                    UtilFunctions.getIntersection(p1, p2, p3, p4);
295
                Coordinate[] coords1 = new Coordinate[2];
296
                coords1[0] = lineStrings[i].getCoordinateN(0);
297
                coords1[1] =
298
                    new Coordinate(intersection.getX(), intersection.getY());
299
                lineStrings[i] = factory.createLineString(coords1);
300
                Coordinate[] coords2 = new Coordinate[2];
301
                coords2[0] = coords1[1];
302
                coords2[1] = lineStrings[i + 1].getCoordinateN(1);
303
                lineStrings[i + 1] = factory.createLineString(coords2);
304
            }
305
            g2 = factory.createMultiLineString(lineStrings);
306
            return Converter.jtsToGeometry(g2);
307
        case Geometry.TYPES.SURFACE:
308
        case Geometry.TYPES.CIRCLE:
309
        case Geometry.TYPES.ELLIPSE:
310
            g = TopologyPreservingSimplifier.simplify(g, 10d);
311
            com.vividsolutions.jts.geom.Geometry gPolygon = g.getGeometryN(0);
312
            setDistancePolygon(gPolygon, pPos);
313
            Coordinate[] coordinates = gPolygon.getCoordinates();
314
            int numPointsPolygon = gPolygon.getNumPoints();
315
            if (numPointsPolygon < 2) {
316
                return null;
317
            }
318
            LineString[] polygonStrings = new LineString[numPointsPolygon];
319
            for (int j = 1; j < numPointsPolygon; j = j + 1) {
320
                c2[0] = coordinates[j - 1];
321
                c2[1] = coordinates[j];
322
                Point2D[] points = new Point2D[2];
323
                points[0] = new Point2D.Double(c2[0].x, c2[0].y);
324
                points[1] = new Point2D.Double(c2[1].x, c2[1].y);
325
                polygonStrings[j - 1] =
326
                    factory.createLineString(getParallel(points, distance));
327
            }
328
            for (int i = 0; i < polygonStrings.length - 2; i++) {
329
                Coordinate coord = polygonStrings[i].getCoordinateN(0);
330
                Point2D p1 = new Point2D.Double(coord.x, coord.y);
331
                coord = polygonStrings[i].getCoordinateN(1);
332
                Point2D p2 = new Point2D.Double(coord.x, coord.y);
333
                coord = polygonStrings[i + 1].getCoordinateN(0);
334
                Point2D p3 = new Point2D.Double(coord.x, coord.y);
335
                coord = polygonStrings[i + 1].getCoordinateN(1);
336
                Point2D p4 = new Point2D.Double(coord.x, coord.y);
337
                Point2D intersection =
338
                    UtilFunctions.getIntersection(p1, p2, p3, p4);
339
                Coordinate[] coords1 = new Coordinate[2];
340
                coords1[0] = polygonStrings[i].getCoordinateN(0);
341
                coords1[1] =
342
                    new Coordinate(intersection.getX(), intersection.getY());
343
                polygonStrings[i] = factory.createLineString(coords1);
344
                Coordinate[] coords2 = new Coordinate[2];
345
                coords2[0] = coords1[1];
346
                coords2[1] = polygonStrings[i + 1].getCoordinateN(1);
347
                polygonStrings[i + 1] = factory.createLineString(coords2);
348
            }
367 349

  
368
			Point2D intersection = UtilFunctions
369
					.getIntersection(p1, p2, p3, p4);
370
			Coordinate[] coords1 = new Coordinate[2];
371
			coords1[0] = polygonStrings[polygonStrings.length - 2]
372
					.getCoordinateN(1);
373
			coords1[1] = new Coordinate(intersection.getX(), intersection
374
					.getY());
375
			polygonStrings[polygonStrings.length - 1] = factory
376
					.createLineString(coords1);
377
			Coordinate[] coords2 = new Coordinate[2];
378
			coords2[0] = coords1[1];
379
			coords2[1] = polygonStrings[0].getCoordinateN(1);
380
			polygonStrings[0] = factory.createLineString(coords2);
381
			com.vividsolutions.jts.geom.Geometry geometryCollection = factory
382
					.createGeometryCollection(polygonStrings);
383
			LinearRing linearRing = factory.createLinearRing(geometryCollection
384
					.getCoordinates());
385
			LinearRing[] holes = new LinearRing[1];
386
			holes[0] = factory.createLinearRing(new Coordinate[0]);
387
			g2 = factory.createPolygon(linearRing, holes);
388
			return Converter.jtsToGeometry(g2);
350
            Coordinate coord = polygonStrings[0].getCoordinateN(0);
351
            Point2D p1 = new Point2D.Double(coord.x, coord.y);
352
            coord = polygonStrings[0].getCoordinateN(1);
353
            Point2D p2 = new Point2D.Double(coord.x, coord.y);
354
            coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(0);
355
            Point2D p3 = new Point2D.Double(coord.x, coord.y);
356
            coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(1);
357
            Point2D p4 = new Point2D.Double(coord.x, coord.y);
389 358

  
390
		case Geometry.TYPES.MULTIPOINT:
391
		case Geometry.TYPES.POINT:
392
			break;
393
		default:
394
			break;
395
		}
396
		return null;
397
	}
359
            Point2D intersection =
360
                UtilFunctions.getIntersection(p1, p2, p3, p4);
361
            Coordinate[] coords1 = new Coordinate[2];
362
            coords1[0] =
363
                polygonStrings[polygonStrings.length - 2].getCoordinateN(1);
364
            coords1[1] =
365
                new Coordinate(intersection.getX(), intersection.getY());
366
            polygonStrings[polygonStrings.length - 1] =
367
                factory.createLineString(coords1);
368
            Coordinate[] coords2 = new Coordinate[2];
369
            coords2[0] = coords1[1];
370
            coords2[1] = polygonStrings[0].getCoordinateN(1);
371
            polygonStrings[0] = factory.createLineString(coords2);
372
            com.vividsolutions.jts.geom.Geometry geometryCollection =
373
                factory.createGeometryCollection(polygonStrings);
374
            LinearRing linearRing =
375
                factory.createLinearRing(geometryCollection.getCoordinates());
376
            LinearRing[] holes = new LinearRing[1];
377
            holes[0] = factory.createLinearRing(new Coordinate[0]);
378
            g2 = factory.createPolygon(linearRing, holes);
379
            return Converter.jtsToGeometry(g2);
398 380

  
399
	private void setDistanceLine(Coordinate position) {
400
		Coordinate pStart = distanceLine.getCoordinateN(0);
401
		Coordinate pEnd = distanceLine.getCoordinateN(distanceLine
402
				.getNumPoints() - 1);
403
		int pos = CGAlgorithms.computeOrientation(pStart, pEnd, position);
404
		if (pos > 0) {
405
			distance = Math.abs(distance);
406
		} else {
407
			distance = -Math.abs(distance);
408
		}
409
		distancePos = java.lang.Double.MAX_VALUE;
410
		distanceLine = null;
411
	}
381
        case Geometry.TYPES.MULTIPOINT:
382
        case Geometry.TYPES.POINT:
383
            break;
384
        default:
385
            break;
386
        }
387
        return null;
388
    }
412 389

  
413
	private void setDistancePolygon(
414
			com.vividsolutions.jts.geom.Geometry polygon,
415
			com.vividsolutions.jts.geom.Geometry position) {
416
		boolean intersects = polygon.intersects(position);
417
		if (intersects) {
418
			distance = -Math.abs(distance);
419
		} else {
420
			distance = Math.abs(distance);
421
		}
422
	}
390
    private void setDistanceLine(Coordinate position) {
391
        Coordinate pStart = distanceLine.getCoordinateN(0);
392
        Coordinate pEnd =
393
            distanceLine.getCoordinateN(distanceLine.getNumPoints() - 1);
394
        int pos = CGAlgorithms.computeOrientation(pStart, pEnd, position);
395
        if (pos > 0) {
396
            distance = Math.abs(distance);
397
        } else {
398
            distance = -Math.abs(distance);
399
        }
400
        distancePos = java.lang.Double.MAX_VALUE;
401
        distanceLine = null;
402
    }
423 403

  
424
	/**
425
	 * M?todo para dibujar la lo necesario para el estado en el que nos
426
	 * encontremos.
427
	 *
428
	 * @param g
429
	 *            Graphics sobre el que dibujar.
430
	 * @param x
431
	 *            par?metro x del punto que se pase para dibujar.
432
	 * @param y
433
	 *            par?metro x del punto que se pase para dibujar.
434
	 */
435
	public void drawOperation(MapControlDrawer renderer, double x, double y) {
404
    private void setDistancePolygon(
405
        com.vividsolutions.jts.geom.Geometry polygon,
406
        com.vividsolutions.jts.geom.Geometry position) {
407
        boolean intersects = polygon.intersects(position);
408
        if (intersects) {
409
            distance = -Math.abs(distance);
410
        } else {
411
            distance = Math.abs(distance);
412
        }
413
    }
436 414

  
437
	}
415
    /**
416
     * M?todo para dibujar la lo necesario para el estado en el que nos
417
     * encontremos.
418
     * 
419
     * @param g
420
     *            Graphics sobre el que dibujar.
421
     * @param x
422
     *            par?metro x del punto que se pase para dibujar.
423
     * @param y
424
     *            par?metro x del punto que se pase para dibujar.
425
     */
426
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
438 427

  
439
	/**
440
	 * Add a diferent option.
441
	 *
442
	 * @param s
443
	 *            Diferent option.
444
	 */
445
	public void addOption(String s) {
446
		// EquidistanceCADToolState actualState = (EquidistanceCADToolState)
447
		// _fsm
448
		// .getPreviousState();
449
		// String status = actualState.getName();
450
		// if (status.equals("Equidistance.Distance")) {
451
		// distance=java.lang.Double.parseDouble(s);
452
		// }
453
	}
428
    }
454 429

  
455
	/*
456
	 * (non-Javadoc)
457
	 *
458
	 * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
459
	 */
460
	public void addValue(double d) {
461
		EquidistanceCADToolState actualState = (EquidistanceCADToolState) _fsm
462
				.getPreviousState();
463
		String status = actualState.getName();
464
		if (status.equals("Equidistance.Distance")) {
465
			distance = d;
466
		}
467
	}
430
    /**
431
     * Add a diferent option.
432
     * 
433
     * @param s
434
     *            Diferent option.
435
     */
436
    public void addOption(String s) {
437
        // EquidistanceCADToolState actualState = (EquidistanceCADToolState)
438
        // _fsm
439
        // .getPreviousState();
440
        // String status = actualState.getName();
441
        // if (status.equals("Equidistance.Distance")) {
442
        // distance=java.lang.Double.parseDouble(s);
443
        // }
444
    }
468 445

  
469
	public String getName() {
470
		return PluginServices.getText(this, "Equidistance_");
471
	}
446
    /*
447
     * (non-Javadoc)
448
     * 
449
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
450
     */
451
    public void addValue(double d) {
452
        EquidistanceCADToolState actualState =
453
            (EquidistanceCADToolState) _fsm.getPreviousState();
454
        String status = actualState.getName();
455
        if (status.equals("Equidistance.Distance")) {
456
            distance = d;
457
        }
458
    }
472 459

  
473
	public String toString() {
474
		return "_Equidistance";
475
	}
460
    public String getName() {
461
        return PluginServices.getText(this, "Equidistance_");
462
    }
476 463

  
477
	public boolean isApplicable(int shapeType) {
478
		if (shapeType == Geometry.TYPES.POINT
479
				|| shapeType == Geometry.TYPES.MULTIPOINT) {
480
			return false;
481
		}
482
		return true;
483
	}
464
    public String toString() {
465
        return "_Equidistance";
466
    }
467

  
468
    public boolean isApplicable(int shapeType) {
469
        if (shapeType == Geometry.TYPES.POINT
470
            || shapeType == Geometry.TYPES.MULTIPOINT) {
471
            return false;
472
        }
473
        return true;
474
    }
484 475
}

Also available in: Unified diff