Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / EquidistanceCADTool.java @ 9243

History | View | Annotate | Download (15.1 KB)

1 8937 caballero
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * 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
40
 */
41
package com.iver.cit.gvsig.gui.cad.tools;
42
43
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45
import java.awt.event.InputEvent;
46
import java.awt.geom.Point2D;
47
import java.io.IOException;
48
import java.util.ArrayList;
49
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.CADExtension;
52
import com.iver.cit.gvsig.fmap.ViewPort;
53
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
54
import com.iver.cit.gvsig.fmap.core.FShape;
55
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
56
import com.iver.cit.gvsig.fmap.core.Handler;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
59
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
60
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
61
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
62
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
63
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
64
import com.iver.cit.gvsig.gui.cad.CADTool;
65
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
66
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.EquidistanceCADToolContext;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.EquidistanceCADToolContext.EquidistanceCADToolState;
69
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
70
import com.vividsolutions.jts.algorithm.CGAlgorithms;
71
import com.vividsolutions.jts.geom.Coordinate;
72
import com.vividsolutions.jts.geom.Geometry;
73
import com.vividsolutions.jts.geom.GeometryFactory;
74
import com.vividsolutions.jts.geom.LineString;
75
import com.vividsolutions.jts.geom.LinearRing;
76
import com.vividsolutions.jts.geom.Point;
77
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier;
78
79
80
/**
81
 * Herramienta para crear una geometr?a equidistante a otra.
82
 *
83
 * @author Vicente Caballero Navarro
84
 */
85
public class EquidistanceCADTool extends DefaultCADTool {
86
    private EquidistanceCADToolContext _fsm;
87
    private Point2D firstPoint=new Point2D.Double(800000,4500000);
88
    private Point2D secondPoint=new Point2D.Double(810000,4500000);
89
        private double distance=10;
90
        private double distancePos=java.lang.Double.MAX_VALUE;
91
        private LineString distanceLine;
92
        /**
93
     * Crea un nuevo EquidistanceCADTool.
94
     */
95
    public EquidistanceCADTool() {
96
    }
97
98
    /**
99
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
100
     * carga previa a la utilizaci?n de la herramienta.
101
     */
102
    public void init() {
103
        _fsm = new EquidistanceCADToolContext(this);
104
105
    }
106
107
    private Coordinate[] getParallel(Point2D[] points,double distance) {
108
            Point2D[] pper=new Point2D[2];
109
            double angle=Math.toRadians(90)+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
    }
117
   /* (non-Javadoc)
118
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
119
     */
120
    public void transition(double x, double y, InputEvent event) {
121
        _fsm.addPoint(x, y, event);
122
    }
123
124
    /* (non-Javadoc)
125
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
126
     */
127
    public void transition(double d) {
128
        _fsm.addValue(d);
129
    }
130
131
    /* (non-Javadoc)
132
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
133
     */
134
    public void transition(String s) throws CommandException {
135
            if (!super.changeCommand(s)){
136
                    _fsm.addOption(s);
137
            }
138
    }
139
140
    /**
141
     * DOCUMENT ME!
142
     */
143
    public void selection() {
144
       ArrayList selectedRows=getSelectedRows();
145
        if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
146
            CADExtension.setCADTool("_selection",false);
147
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
148
                "_Equidistance");
149
        }
150
    }
151
152
    /**
153
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
154
     * editableFeatureSource que ya estar? creado.
155
     *
156
     * @param x par?metro x del punto que se pase en esta transici?n.
157
     * @param y par?metro y del punto que se pase en esta transici?n.
158
     */
159
    public void addPoint(double x, double y,InputEvent event) {
160
        EquidistanceCADToolState actualState = (EquidistanceCADToolState) _fsm.getPreviousState();
161
        String status = actualState.getName();
162
        if (status.equals("Equidistance.Distance")) {
163
                firstPoint = new Point2D.Double(x, y);
164
            } else if (status.equals("Equidistance.SecondPointDistance")) {
165
                    secondPoint = new Point2D.Double(x,y);
166
                    distance=secondPoint.distance(firstPoint);
167
            } else if (status.equals("Equidistance.Position")) {
168
                    ArrayList selectedRow = getSelectedRows();
169
            ArrayList selectedRowAux=new ArrayList();
170
            VectorialLayerEdited vle = getVLE();
171
            VectorialEditableAdapter vea = vle.getVEA();
172
            PluginServices.getMDIManager().setWaitCursor();
173
                    try {
174
                            vea.startComplexRow();
175
                            for (int i = 0; i < selectedRow.size(); i++) {
176
                                    DefaultRowEdited row = (DefaultRowEdited) selectedRow
177
                                                    .get(i);
178
                                    DefaultFeature fea = (DefaultFeature) row.getLinkedRow()
179
                                                    .cloneRow();
180
181
                                    IGeometry geometry=compute(fea,new Point2D.Double(x,y));
182
                                    addGeometry(geometry);
183
                            }
184
185
                            vea.endComplexRow(getName());
186
                            vle.setSelectionCache(VectorialLayerEdited.SAVEPREVIOUS, selectedRowAux);
187
                            //clearSelection();
188
                            //selectedRow.addAll(selectedRowAux);
189
                    } catch (DriverIOException e) {
190
                            e.printStackTrace();
191
                    } catch (IOException e1) {
192
                            e1.printStackTrace();
193
                    }
194
                    PluginServices.getMDIManager().restoreCursor();
195
            }
196
    }
197
198
    private IGeometry compute(DefaultFeature fea, Point2D position){
199
                IGeometry geometry = fea.getGeometry();
200
                int typeGeometry = geometry.getGeometryType();
201
                Geometry g = geometry.toJTSGeometry();
202
                GeometryFactory factory=g.getFactory();
203
            Coordinate[] c2 = new Coordinate[2];
204
                Geometry g2 = null;
205
                Coordinate coordinatePosition=new Coordinate(position.getX(),position.getY());
206
                Point pPos=factory.createPoint(coordinatePosition);
207
                switch (typeGeometry) {
208
                case FShape.LINE:
209
210
                        LineString lR =factory.createLineString(g.getCoordinates());
211
                        g = TopologyPreservingSimplifier.simplify(lR, 10d);
212
                        Geometry gLine = g.getGeometryN(0);
213
                        Coordinate[] coordinatesLine = gLine.getCoordinates();
214
                        int numPointsLine = gLine.getNumPoints();
215
                        if (numPointsLine < 1)
216
                                return null;
217
                        LineString[] lineStrings = new LineString[numPointsLine - 1];
218
219
                        for (int j = 1; j < numPointsLine; j = j + 1) {
220
                                c2[0] = coordinatesLine[j - 1];
221
                                c2[1] = coordinatesLine[j];
222
                                LineString lS=factory.createLineString(c2);
223
                                if (lS.distance(pPos)<distancePos) {
224
                                        distancePos=lS.distance(pPos);
225
                                        distanceLine=(LineString)factory.createGeometry(lS);
226
                                }
227
                        }
228
                        setDistanceLine(coordinatePosition);
229
230
                        for (int j = 1; j < numPointsLine; j = j + 1) {
231
                                c2[0] = coordinatesLine[j - 1];
232
                                c2[1] = coordinatesLine[j];
233
                                Point2D[] points = new Point2D[2];
234
                                points[0] = new Point2D.Double(c2[0].x, c2[0].y);
235
                                points[1] = new Point2D.Double(c2[1].x, c2[1].y);
236
                                lineStrings[j - 1] = factory.createLineString(
237
                                                getParallel(points, distance));
238
                        }
239
240
                        for (int i = 0; i < lineStrings.length - 1; i++) {
241
                                Coordinate coord = lineStrings[i].getCoordinateN(0);
242
                                Point2D p1 = new Point2D.Double(coord.x, coord.y);
243
                                coord = lineStrings[i].getCoordinateN(1);
244
                                Point2D p2 = new Point2D.Double(coord.x, coord.y);
245
                                coord = lineStrings[i + 1].getCoordinateN(0);
246
                                Point2D p3 = new Point2D.Double(coord.x, coord.y);
247
                                coord = lineStrings[i + 1].getCoordinateN(1);
248
                                Point2D p4 = new Point2D.Double(coord.x, coord.y);
249
                                Point2D intersection = UtilFunctions.getIntersection(p1, p2,
250
                                                p3, p4);
251
                                Coordinate[] coords1 = new Coordinate[2];
252
                                coords1[0] = lineStrings[i].getCoordinateN(0);
253
                                coords1[1] = new Coordinate(intersection.getX(), intersection
254
                                                .getY());
255
                                lineStrings[i] = factory.createLineString(coords1);
256
                                Coordinate[] coords2 = new Coordinate[2];
257
                                coords2[0] = coords1[1];
258
                                coords2[1] = lineStrings[i + 1].getCoordinateN(1);
259
                                lineStrings[i + 1] = factory.createLineString(
260
                                                coords2);
261
                        }
262
                        g2 = factory.createMultiLineString(lineStrings);
263
                        return FConverter.jts_to_igeometry(g2);
264
                case FShape.POLYGON:
265
                case FShape.CIRCLE:
266
                case FShape.ELLIPSE:
267
                        g = TopologyPreservingSimplifier.simplify(g, 10d);
268
                        Geometry gPolygon = g.getGeometryN(0);
269
                        setDistancePolygon(gPolygon,pPos);
270
                        Coordinate[] coordinates = gPolygon.getCoordinates();
271
                        int numPointsPolygon = gPolygon.getNumPoints();
272
                        if (numPointsPolygon < 2)
273
                                return null;
274
                        LineString[] polygonStrings = new LineString[numPointsPolygon];
275
                        for (int j = 1; j < numPointsPolygon; j = j + 1) {
276
                                c2[0] = coordinates[j - 1];
277
                                c2[1] = coordinates[j];
278
                                Point2D[] points = new Point2D[2];
279
                                points[0] = new Point2D.Double(c2[0].x, c2[0].y);
280
                                points[1] = new Point2D.Double(c2[1].x, c2[1].y);
281
                                polygonStrings[j - 1] = factory.createLineString(
282
                                                getParallel(points, distance));
283
                        }
284
                        for (int i = 0; i < polygonStrings.length - 2; i++) {
285
                                Coordinate coord = polygonStrings[i].getCoordinateN(0);
286
                                Point2D p1 = new Point2D.Double(coord.x, coord.y);
287
                                coord = polygonStrings[i].getCoordinateN(1);
288
                                Point2D p2 = new Point2D.Double(coord.x, coord.y);
289
                                coord = polygonStrings[i + 1].getCoordinateN(0);
290
                                Point2D p3 = new Point2D.Double(coord.x, coord.y);
291
                                coord = polygonStrings[i + 1].getCoordinateN(1);
292
                                Point2D p4 = new Point2D.Double(coord.x, coord.y);
293
                                Point2D intersection = UtilFunctions.getIntersection(p1, p2,
294
                                                p3, p4);
295
                                Coordinate[] coords1 = new Coordinate[2];
296
                                coords1[0] = polygonStrings[i].getCoordinateN(0);
297
                                coords1[1] = new Coordinate(intersection.getX(), intersection
298
                                                .getY());
299
                                polygonStrings[i] = factory.createLineString(
300
                                                coords1);
301
                                Coordinate[] coords2 = new Coordinate[2];
302
                                coords2[0] = coords1[1];
303
                                coords2[1] = polygonStrings[i + 1].getCoordinateN(1);
304
                                polygonStrings[i + 1] = factory.createLineString(
305
                                                coords2);
306
                        }
307
308
                        // /////////////
309
                        Coordinate coord = polygonStrings[0].getCoordinateN(0);
310
                        Point2D p1 = new Point2D.Double(coord.x, coord.y);
311
                        coord = polygonStrings[0].getCoordinateN(1);
312
                        Point2D p2 = new Point2D.Double(coord.x, coord.y);
313
                        coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(0);
314
                        Point2D p3 = new Point2D.Double(coord.x, coord.y);
315
                        coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(1);
316
                        Point2D p4 = new Point2D.Double(coord.x, coord.y);
317
318
                        Point2D intersection = UtilFunctions
319
                                        .getIntersection(p1, p2, p3, p4);
320
                        Coordinate[] coords1 = new Coordinate[2];
321
                        coords1[0] = polygonStrings[polygonStrings.length - 2]
322
                                        .getCoordinateN(1);
323
                        coords1[1] = new Coordinate(intersection.getX(), intersection
324
                                        .getY());
325
                        polygonStrings[polygonStrings.length - 1] = factory
326
                                        .createLineString(coords1);
327
//                        LineString[] pol=new LineString[polygonStrings.length-1];
328
//                        for (int i=0;i<pol.length;i++) {
329
//                                pol[i]=polygonStrings[i];
330
//                        }
331
                        Coordinate[] coords2 = new Coordinate[2];
332
                        coords2[0] = coords1[1];
333
                        coords2[1] = polygonStrings[0].getCoordinateN(1);
334
                        polygonStrings[0] = factory.createLineString(coords2);
335
                        // ////////////////////////////////
336
                        Geometry geometryCollection = factory
337
                                        .createGeometryCollection(polygonStrings);
338
                        LinearRing linearRing = factory.createLinearRing(
339
                                        geometryCollection.getCoordinates());
340
                        LinearRing[] holes = new LinearRing[1];
341
                        holes[0] = factory.createLinearRing(new Coordinate[0]);
342
                        g2 = factory.createPolygon(linearRing, holes);
343
                        return FConverter.jts_to_igeometry(g2);
344
345
                case FShape.MULTIPOINT:
346
                case FShape.POINT:
347
                        break;
348
                default:
349
                        break;
350
                }
351
                return null;
352
        }
353
354
    private void setDistanceLine(Coordinate position) {
355
            Coordinate pStart=distanceLine.getCoordinateN(0);
356
            Coordinate pEnd=distanceLine.getCoordinateN(distanceLine.getNumPoints()-1);
357
            int pos=CGAlgorithms.computeOrientation(pStart,pEnd,position);
358
                   if (pos>0)
359
                            distance=Math.abs(distance);
360
                    else
361
                            distance=-Math.abs(distance);
362
                   distancePos=java.lang.Double.MAX_VALUE;
363
                   distanceLine=null;
364
        }
365
366
        private void setDistancePolygon(Geometry polygon, Geometry position) {
367
                boolean intersects=polygon.intersects(position);
368
                if (intersects)
369
                        distance=-Math.abs(distance);
370
                else
371
                        distance=Math.abs(distance);
372
        }
373
374
        /**
375
     * M?todo para dibujar la lo necesario para el estado en el que nos
376
     * encontremos.
377
     *
378
     * @param g Graphics sobre el que dibujar.
379
     * @param x par?metro x del punto que se pase para dibujar.
380
     * @param y par?metro x del punto que se pase para dibujar.
381
     */
382
    public void drawOperation(Graphics g, double x, double y) {
383
384
    }
385
    /**
386
         * Add a diferent option.
387
         *
388
         * @param s
389
         *            Diferent option.
390
         */
391
    public void addOption(String s) {
392
//            EquidistanceCADToolState actualState = (EquidistanceCADToolState) _fsm
393
//                                .getPreviousState();
394
//                String status = actualState.getName();
395
//                if (status.equals("Equidistance.Distance")) {
396
//                        distance=java.lang.Double.parseDouble(s);
397
//                }
398
    }
399
400
    /*
401
         * (non-Javadoc)
402
         *
403
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
404
         */
405
    public void addValue(double d) {
406
            EquidistanceCADToolState actualState = (EquidistanceCADToolState) _fsm
407
                .getPreviousState();
408
            String status = actualState.getName();
409
            if (status.equals("Equidistance.Distance")) {
410
                    distance=d;
411
            }
412
    }
413
414
        public String getName() {
415
                return PluginServices.getText(this,"Equidistance_");
416
        }
417
418
        public String toString() {
419
                return "_Equidistance";
420
        }
421
422
        public boolean isApplicable(int shapeType) {
423
                if (shapeType==FShape.POINT ||
424
                                shapeType==FShape.MULTIPOINT) {
425
                        return false;
426
                }
427
                return true;
428
        }
429
}