Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / EquidistanceCADTool.java @ 39573

History | View | Annotate | Download (18.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.editing.gui.cad.tools;
23

    
24
import java.awt.event.InputEvent;
25
import java.awt.geom.Point2D;
26
import java.util.ArrayList;
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

    
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.editing.CADExtension;
38
import org.gvsig.editing.gui.cad.DefaultCADTool;
39
import org.gvsig.editing.gui.cad.exception.CommandException;
40
import org.gvsig.editing.gui.cad.tools.smc.EquidistanceCADToolContext;
41
import org.gvsig.editing.gui.cad.tools.smc.EquidistanceCADToolContext.EquidistanceCADToolState;
42
import org.gvsig.editing.layers.VectorialLayerEdited;
43
import org.gvsig.fmap.dal.exception.DataException;
44
import org.gvsig.fmap.dal.exception.ReadException;
45
import org.gvsig.fmap.dal.feature.Feature;
46
import org.gvsig.fmap.dal.feature.FeatureSelection;
47
import org.gvsig.fmap.dal.feature.FeatureSet;
48
import org.gvsig.fmap.dal.feature.FeatureStore;
49
import org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.exception.CreateGeometryException;
51
import org.gvsig.fmap.geom.operation.GeometryOperationException;
52
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
53
import org.gvsig.fmap.geom.operation.tojts.ToJTS;
54
import org.gvsig.fmap.geom.util.Converter;
55
import org.gvsig.fmap.geom.util.UtilFunctions;
56
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
57
import org.gvsig.tools.dispose.DisposableIterator;
58

    
59
/**
60
 * Herramienta para crear una geometr?a equidistante a otra.
61
 * 
62
 * @author Vicente Caballero Navarro
63
 */
64
public class EquidistanceCADTool extends DefaultCADTool {
65

    
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;
72

    
73
    /**
74
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
75
     * carga previa a la utilizaci?n de la herramienta.
76
     */
77
    public void init() {
78
        _fsm = new EquidistanceCADToolContext(this);
79

    
80
    }
81

    
82
    private Coordinate[] getParallel(Point2D[] points, double distance) {
83
        Point2D[] pper = new Point2D[2];
84
        double angle =
85
            Math.toRadians(90) + UtilFunctions.getAngle(points[0], points[1]);
86
        pper[0] = UtilFunctions.getPoint(points[0], angle, distance);
87
        pper[1] = UtilFunctions.getPoint(points[1], angle, distance);
88
        Coordinate[] result = new Coordinate[2];
89
        result[0] = new Coordinate(pper[0].getX(), pper[0].getY());
90
        result[1] = new Coordinate(pper[1].getX(), pper[1].getY());
91
        return result;
92
    }
93

    
94
    /*
95
     * (non-Javadoc)
96
     * 
97
     * @see
98
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
99
     * .layers.FBitSet,
100
     * double, double)
101
     */
102
    public void transition(double x, double y, InputEvent event) {
103
        _fsm.addPoint(x, y, event);
104
    }
105

    
106
    /*
107
     * (non-Javadoc)
108
     * 
109
     * @see
110
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
111
     * .layers.FBitSet,
112
     * double)
113
     */
114
    public void transition(double d) {
115
        _fsm.addValue(d);
116
    }
117

    
118
    /*
119
     * (non-Javadoc)
120
     * 
121
     * @see
122
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
123
     * .layers.FBitSet,
124
     * java.lang.String)
125
     */
126
    public void transition(String s) throws CommandException {
127
        if (!super.changeCommand(s)) {
128
            _fsm.addOption(s);
129
        }
130
    }
131

    
132
    /**
133
     * DOCUMENT ME!
134
     */
135
    public void selection() {
136
        FeatureSet selection = null;
137
        try {
138
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
139

    
140
            if (selection.getSize() == 0
141
                && !SelectionCADTool.isInstance(CADExtension.getCADTool(), true)) {
142
                
143
                CADExtension.setCADTool("_selection", false);
144
                ((SelectionCADTool) CADExtension.getCADTool())
145
                    .setNextTool("_Equidistance");
146
            }
147
        } catch (ReadException e) {
148
            // TODO Auto-generated catch block
149
            e.printStackTrace();
150
        } catch (DataException e) {
151
            // TODO Auto-generated catch block
152
            e.printStackTrace();
153
        }
154
    }
155

    
156
    /**
157
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
158
     * editableFeatureSource que ya estar? creado.
159
     * 
160
     * @param x
161
     *            par?metro x del punto que se pase en esta transici?n.
162
     * @param y
163
     *            par?metro y del punto que se pase en esta transici?n.
164
     */
165
    public void addPoint(double x, double y, InputEvent event) {
166
        EquidistanceCADToolState actualState =
167
            (EquidistanceCADToolState) _fsm.getPreviousState();
168
        String status = actualState.getName();
169
        if (status.equals("Equidistance.Distance")) {
170
            firstPoint = new Point2D.Double(x, y);
171
        } else
172
            if (status.equals("Equidistance.SecondPointDistance")) {
173
                secondPoint = new Point2D.Double(x, y);
174
                distance = secondPoint.distance(firstPoint);
175
            } else
176
                if (status.equals("Equidistance.Position")) {
177
                    VectorialLayerEdited vle = getVLE();
178
                    FeatureStore featureStore = null;
179
                    try {
180
                        featureStore = vle.getFeatureStore();
181
                    } catch (ReadException e) {
182
                        // TODO Auto-generated catch block
183
                        e.printStackTrace();
184
                    }
185
                    ArrayList selectedRowAux = new ArrayList();
186
                    DisposableIterator iterator = null;
187
                    try {
188
                        iterator =
189
                            ((FeatureSelection) featureStore.getSelection())
190
                                .iterator();
191

    
192
                        PluginServices.getMDIManager().setWaitCursor();
193
                        featureStore.beginEditingGroup(getName());
194
                        while (iterator.hasNext()) {
195
                            Feature feature = (Feature) iterator.next();
196
                            Geometry geometry =
197
                                compute(feature, new Point2D.Double(x, y));
198
                            insertAndSelectGeometry(geometry);
199
                        }
200

    
201
                        featureStore.endEditingGroup();
202
                    } catch (DataException e) {
203
                        // TODO Auto-generated catch block
204
                        e.printStackTrace();
205
                    } catch (CreateGeometryException e) {
206
                        // TODO Auto-generated catch block
207
                        e.printStackTrace();
208
                    } finally {
209
                        if (iterator != null) {
210
                            iterator.dispose();
211
                        }
212
                    }
213
                    PluginServices.getMDIManager().restoreCursor();
214
                }
215
    }
216

    
217
    private Geometry compute(Feature fea, Point2D position)
218
        throws CreateGeometryException {
219
        Geometry geometry = (fea.getDefaultGeometry()).cloneGeometry();
220
        int typeGeometry = geometry.getType();
221
        com.vividsolutions.jts.geom.Geometry g = null;
222
        try {
223
            g =
224
                (com.vividsolutions.jts.geom.Geometry) geometry
225
                    .invokeOperation(ToJTS.CODE, null);
226
        } catch (GeometryOperationNotSupportedException e) {
227
            // TODO Auto-generated catch block
228
            e.printStackTrace();
229
        } catch (GeometryOperationException e) {
230
            // TODO Auto-generated catch block
231
            e.printStackTrace();
232
        }
233

    
234
        // com.vividsolutions.jts.geom.Geometry g = geometry.toJTSGeometry();
235
        GeometryFactory factory = g.getFactory();
236
        Coordinate[] c2 = new Coordinate[2];
237
        com.vividsolutions.jts.geom.Geometry g2 = null;
238
        Coordinate coordinatePosition =
239
            new Coordinate(position.getX(), position.getY());
240
        Point pPos = factory.createPoint(coordinatePosition);
241
        switch (typeGeometry) {
242
        case CURVE:
243

    
244
            LineString lR = factory.createLineString(g.getCoordinates());
245
            g = TopologyPreservingSimplifier.simplify(lR, 10d);
246
            com.vividsolutions.jts.geom.Geometry gLine = g.getGeometryN(0);
247
            Coordinate[] coordinatesLine = gLine.getCoordinates();
248
            int numPointsLine = gLine.getNumPoints();
249
            if (numPointsLine < 1) {
250
                return null;
251
            }
252
            LineString[] lineStrings = new LineString[numPointsLine - 1];
253

    
254
            for (int j = 1; j < numPointsLine; j = j + 1) {
255
                c2[0] = coordinatesLine[j - 1];
256
                c2[1] = coordinatesLine[j];
257
                LineString lS = factory.createLineString(c2);
258
                if (lS.distance(pPos) < distancePos) {
259
                    distancePos = lS.distance(pPos);
260
                    distanceLine = (LineString) factory.createGeometry(lS);
261
                }
262
            }
263
            setDistanceLine(coordinatePosition);
264

    
265
            for (int j = 1; j < numPointsLine; j = j + 1) {
266
                c2[0] = coordinatesLine[j - 1];
267
                c2[1] = coordinatesLine[j];
268
                Point2D[] points = new Point2D[2];
269
                points[0] = new Point2D.Double(c2[0].x, c2[0].y);
270
                points[1] = new Point2D.Double(c2[1].x, c2[1].y);
271
                lineStrings[j - 1] =
272
                    factory.createLineString(getParallel(points, distance));
273
            }
274

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

    
341
            Coordinate coord = polygonStrings[0].getCoordinateN(0);
342
            Point2D p1 = new Point2D.Double(coord.x, coord.y);
343
            coord = polygonStrings[0].getCoordinateN(1);
344
            Point2D p2 = new Point2D.Double(coord.x, coord.y);
345
            coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(0);
346
            Point2D p3 = new Point2D.Double(coord.x, coord.y);
347
            coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(1);
348
            Point2D p4 = new Point2D.Double(coord.x, coord.y);
349

    
350
            Point2D intersection =
351
                UtilFunctions.getIntersection(p1, p2, p3, p4);
352
            Coordinate[] coords1 = new Coordinate[2];
353
            coords1[0] =
354
                polygonStrings[polygonStrings.length - 2].getCoordinateN(1);
355
            coords1[1] =
356
                new Coordinate(intersection.getX(), intersection.getY());
357
            polygonStrings[polygonStrings.length - 1] =
358
                factory.createLineString(coords1);
359
            Coordinate[] coords2 = new Coordinate[2];
360
            coords2[0] = coords1[1];
361
            coords2[1] = polygonStrings[0].getCoordinateN(1);
362
            polygonStrings[0] = factory.createLineString(coords2);
363
            com.vividsolutions.jts.geom.Geometry geometryCollection =
364
                factory.createGeometryCollection(polygonStrings);
365
            LinearRing linearRing =
366
                factory.createLinearRing(geometryCollection.getCoordinates());
367
            LinearRing[] holes = new LinearRing[1];
368
            holes[0] = factory.createLinearRing(new Coordinate[0]);
369
            g2 = factory.createPolygon(linearRing, holes);
370
            return Converter.jtsToGeometry(g2);
371

    
372
        default:
373
            break;
374
        }
375
        return null;
376
    }
377

    
378
    private void setDistanceLine(Coordinate position) {
379
        Coordinate pStart = distanceLine.getCoordinateN(0);
380
        Coordinate pEnd =
381
            distanceLine.getCoordinateN(distanceLine.getNumPoints() - 1);
382
        int pos = CGAlgorithms.computeOrientation(pStart, pEnd, position);
383
        if (pos > 0) {
384
            distance = Math.abs(distance);
385
        } else {
386
            distance = -Math.abs(distance);
387
        }
388
        distancePos = java.lang.Double.MAX_VALUE;
389
        distanceLine = null;
390
    }
391

    
392
    private void setDistancePolygon(
393
        com.vividsolutions.jts.geom.Geometry polygon,
394
        com.vividsolutions.jts.geom.Geometry position) {
395
        boolean intersects = polygon.intersects(position);
396
        if (intersects) {
397
            distance = -Math.abs(distance);
398
        } else {
399
            distance = Math.abs(distance);
400
        }
401
    }
402

    
403
    /**
404
     * M?todo para dibujar la lo necesario para el estado en el que nos
405
     * encontremos.
406
     * 
407
     * @param g
408
     *            Graphics sobre el que dibujar.
409
     * @param x
410
     *            par?metro x del punto que se pase para dibujar.
411
     * @param y
412
     *            par?metro x del punto que se pase para dibujar.
413
     */
414
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
415

    
416
    }
417

    
418
    /**
419
     * Add a diferent option.
420
     * 
421
     * @param s
422
     *            Diferent option.
423
     */
424
    public void addOption(String s) {
425
        // EquidistanceCADToolState actualState = (EquidistanceCADToolState)
426
        // _fsm
427
        // .getPreviousState();
428
        // String status = actualState.getName();
429
        // if (status.equals("Equidistance.Distance")) {
430
        // distance=java.lang.Double.parseDouble(s);
431
        // }
432
    }
433

    
434
    /*
435
     * (non-Javadoc)
436
     * 
437
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
438
     */
439
    public void addValue(double d) {
440
        EquidistanceCADToolState actualState =
441
            (EquidistanceCADToolState) _fsm.getPreviousState();
442
        String status = actualState.getName();
443
        if (status.equals("Equidistance.Distance")) {
444
            distance = d;
445
        }
446
    }
447

    
448
    public String getName() {
449
        return PluginServices.getText(this, "Equidistance_");
450
    }
451

    
452
    public String toString() {
453
        return "_Equidistance";
454
    }
455

    
456
    @Override
457
    protected int[] getSupportedGeometryTypes() {
458
        return new int[] { CURVE, CIRCLE, ELLIPSE, SURFACE };
459
    }
460

    
461
}