Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableAdapter.java @ 28367

History | View | Annotate | Download (33.8 KB)

1
/*
2
 * Created on 12-ene-2006 by fjp
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: VectorialEditableAdapter.java 28367 2009-05-04 15:30:10Z vcaballero $
47
 * $Log$
48
 * Revision 1.76  2007-09-19 16:25:04  jaume
49
 * ReadExpansionFileException removed from this context
50
 *
51
 * Revision 1.75  2007/07/25 06:52:08  caballero
52
 * reproject
53
 *
54
 * Revision 1.74  2007/06/22 10:52:30  caballero
55
 * start y stop
56
 *
57
 * Revision 1.73  2007/05/29 19:09:07  azabala
58
 * using of spatial index in spatial iteration now depends of the query area (to avoid using index in full scan queries)
59
 *
60
 * Revision 1.72  2007/05/07 09:02:47  caballero
61
 * editing memory
62
 *
63
 * Revision 1.71  2007/04/25 14:42:24  caballero
64
 * BoundShape
65
 *
66
 * Revision 1.70  2007/04/19 17:28:24  azabala
67
 * changes in ReadableVectorial interface (new iteration modes)
68
 *
69
 * Revision 1.69  2007/03/06 17:08:55  caballero
70
 * Exceptions
71
 *
72
 * Revision 1.68  2007/02/19 12:36:22  caballero
73
 * *** empty log message ***
74
 *
75
 * Revision 1.67  2007/02/13 11:31:08  caballero
76
 * optimizo cuando el cambio es alfanum?rico
77
 *
78
 * Revision 1.66  2007/02/12 08:53:37  caballero
79
 * FieldExpresion
80
 *
81
 * Revision 1.65  2007/02/08 12:03:07  caballero
82
 * comentar println
83
 *
84
 * Revision 1.64  2006/09/21 17:32:11  azabala
85
 * *** empty log message ***
86
 *
87
 * Revision 1.62  2006/08/10 08:20:31  caballero
88
 * flatness
89
 *
90
 * Revision 1.61  2006/07/20 13:03:07  fjp
91
 * nuevos campos
92
 *
93
 * Revision 1.60  2006/07/12 11:48:41  fjp
94
 * Draft to add, remove and delete fields
95
 *
96
 * Revision 1.59  2006/07/12 10:34:52  fjp
97
 * Draft to add, remove and delete fields
98
 *
99
 * Revision 1.58  2006/07/06 08:31:29  fjp
100
 * Draft to add, remove and delete fields
101
 *
102
 * Revision 1.57  2006/06/22 11:38:12  caballero
103
 * soluci?n error al borrar geometr?as
104
 *
105
 * Revision 1.56  2006/06/16 10:44:01  fjp
106
 * Por mandato de Vicente
107
 *
108
 * Revision 1.54  2006/06/01 16:15:16  fjp
109
 * Escritura que permite crear drivers de manera m?s sencilla.
110
 *
111
 * Revision 1.53  2006/05/30 13:03:41  fjp
112
 * setFlatness solo se debe aplicar a bases de datos espaciales.
113
 *
114
 * Revision 1.52  2006/05/24 09:29:30  caballero
115
 * a?adir flatness
116
 *
117
 * Revision 1.51  2006/05/16 16:07:19  fjp
118
 * snapping. Revisar
119
 *
120
 * Revision 1.50  2006/05/16 07:07:46  caballero
121
 * Modificar la geometr?a desde fuera
122
 *
123
 * Revision 1.49  2006/05/15 10:52:23  caballero
124
 * Saber si se realiza una operaci?n desde la vista o desde la tabla.
125
 *
126
 * Revision 1.48  2006/05/09 15:58:37  caballero
127
 * Para el IGN
128
 *
129
 * Revision 1.47  2006/05/09 10:28:28  caballero
130
 * faltaba controlar undo y redo
131
 *
132
 * Revision 1.46  2006/04/27 06:44:56  caballero
133
 * Soluci?n undo y redo de anotaciones
134
 *
135
 * Revision 1.45  2006/04/18 06:56:55  caballero
136
 * Cambiar VectorialAdapter por ReadableVectorial
137
 *
138
 * Revision 1.44  2006/04/12 17:13:39  fjp
139
 * *** empty log message ***
140
 *
141
 * Revision 1.43  2006/04/11 12:12:29  fjp
142
 * Con edici?n en PostGIS y guardando pseudo-arcos en los shapes.
143
 *
144
 * Revision 1.42  2006/04/11 06:53:20  fjp
145
 * Preparando el driver de escritura PostGIS
146
 *
147
 * Revision 1.41  2006/04/04 11:27:16  fjp
148
 * Consola escuchando bien y selecci?n en tabla sincronizada cuando hay edici?n
149
 *
150
 * Revision 1.40  2006/04/03 11:04:48  caballero
151
 * Posibilidad de a?adir una anotaci?n
152
 *
153
 * Revision 1.39  2006/03/29 06:26:37  caballero
154
 * acelerar con una imagen las herramientas
155
 *
156
 * Revision 1.38  2006/03/23 16:20:52  fjp
157
 * Un fallo un tanto inverosimil con el mapOverview
158
 *
159
 * Revision 1.37  2006/03/23 10:08:11  caballero
160
 * calculo del fullExtent recorriendo todas las geometr?as
161
 *
162
 * Revision 1.36  2006/03/22 11:46:29  caballero
163
 * Editar capa de anotaciones
164
 *
165
 * Revision 1.35  2006/02/28 18:15:22  fjp
166
 * Consola de CAD
167
 *
168
 * Revision 1.34  2006/02/24 11:30:32  fjp
169
 * FUNCIONA!!! (Creo)
170
 *
171
 * Revision 1.33  2006/02/24 07:57:58  fjp
172
 * FUNCIONA!!! (Creo)
173
 *
174
 * Revision 1.32  2006/02/23 17:55:45  fjp
175
 * Preparando para poder editar con el EditionManager
176
 *
177
 * Revision 1.31  2006/02/21 16:44:08  fjp
178
 * Preparando para poder editar con el EditionManager
179
 *
180
 * Revision 1.30  2006/02/20 18:14:59  fjp
181
 * Preparando para poder editar con el EditionManager
182
 *
183
 * Revision 1.29  2006/02/20 10:32:54  fjp
184
 * Preparando para poder editar con el EditionManager
185
 *
186
 * Revision 1.28  2006/02/17 13:40:03  fjp
187
 * Preparando para poder editar con el EditionManager
188
 *
189
 * Revision 1.27  2006/02/17 10:41:14  fjp
190
 * Evento de edici?n lanzado cuando una capa se pone en edici?n
191
 *
192
 * Revision 1.26  2006/02/17 08:21:19  fjp
193
 * *** empty log message ***
194
 *
195
 * Revision 1.25  2006/02/16 09:38:10  fjp
196
 * Preparando compatibilidad para bases de datos (y de paso, acelerando :-)
197
 *
198
 * Revision 1.24  2006/02/16 09:06:28  caballero
199
 * commandStack
200
 *
201
 * Revision 1.23  2006/02/15 18:16:02  fjp
202
 * POR TERMINAR
203
 *
204
 * Revision 1.22  2006/02/13 18:18:31  fjp
205
 * POR TERMINAR
206
 *
207
 * Revision 1.21  2006/02/10 13:28:23  caballero
208
 * poder cambiar la selecci?n
209
 *
210
 * Revision 1.20  2006/02/09 13:11:54  caballero
211
 * *** empty log message ***
212
 *
213
 * Revision 1.19  2006/02/08 16:45:29  caballero
214
 * elimnar c?dio no usado
215
 *
216
 * Revision 1.18  2006/02/08 15:18:45  caballero
217
 * control de las rows eliminadas
218
 *
219
 * Revision 1.17  2006/02/07 10:18:44  caballero
220
 * Con BoundedShape
221
 *
222
 * Revision 1.16  2006/02/06 12:01:41  caballero
223
 * driver del ova
224
 *
225
 * Revision 1.15  2006/02/03 14:09:32  fjp
226
 * Preparando edici?n
227
 *
228
 * Revision 1.14  2006/02/03 12:16:33  fjp
229
 * Preparando edici?n
230
 *
231
 * Revision 1.13  2006/02/03 11:54:12  caballero
232
 * tablas con vectorialEditableAdapter en edici?n
233
 *
234
 * Revision 1.11  2006/01/31 08:10:05  caballero
235
 * cambio de feature a row
236
 *
237
 * Revision 1.10  2006/01/30 08:18:14  caballero
238
 * m?todos para deshacer y rehacer
239
 *
240
 * Revision 1.9  2006/01/23 17:30:28  caballero
241
 * coger los datos del ova
242
 *
243
 * Revision 1.8  2006/01/23 16:16:16  caballero
244
 * getRowIndex
245
 *
246
 * Revision 1.7  2006/01/20 08:37:10  fjp
247
 * Preparando la edici?n
248
 *
249
 * Revision 1.6  2006/01/19 12:48:20  caballero
250
 * poder modificar su vectorial Adapter
251
 *
252
 * Revision 1.5  2006/01/19 09:28:11  fjp
253
 * Preparando la edici?n
254
 *
255
 * Revision 1.4  2006/01/17 10:24:02  fjp
256
 * Preparando edici?n
257
 *
258
 * Revision 1.3  2006/01/16 12:47:38  fjp
259
 * Preparando edici?n
260
 *
261
 * Revision 1.2  2006/01/16 11:23:00  fjp
262
 * Preparando edici?n
263
 *
264
 * Revision 1.1  2006/01/12 13:39:14  fjp
265
 * preaparar edicion
266
 *
267
 *
268
 */
269
package com.iver.cit.gvsig.fmap.edition;
270

    
271
import java.awt.Image;
272
import java.awt.geom.Rectangle2D;
273
import java.awt.image.BufferedImage;
274
import java.io.IOException;
275
import java.util.List;
276

    
277
import org.cresques.cts.ICoordTrans;
278
import org.cresques.cts.IProjection;
279

    
280
import com.hardcode.driverManager.Driver;
281
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
282
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
283
import com.hardcode.gdbms.engine.data.DataSource;
284
import com.hardcode.gdbms.engine.data.driver.DriverException;
285
import com.hardcode.gdbms.engine.values.Value;
286
import com.iver.cit.gvsig.exceptions.commands.EditionCommandException;
287
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
288
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
289
import com.iver.cit.gvsig.exceptions.layers.CancelEditingLayerException;
290
import com.iver.cit.gvsig.exceptions.layers.StartEditionLayerException;
291
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
292
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
293
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
294
import com.iver.cit.gvsig.fmap.core.FNullGeometry;
295
import com.iver.cit.gvsig.fmap.core.IFeature;
296
import com.iver.cit.gvsig.fmap.core.IGeometry;
297
import com.iver.cit.gvsig.fmap.core.IRow;
298
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
299
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
300
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
301
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
302
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
303
import com.iver.cit.gvsig.fmap.drivers.featureiterators.AttrQueryFeatureIterator;
304
import com.iver.cit.gvsig.fmap.drivers.featureiterators.AttrQuerySelectionFeatureIterator;
305
import com.iver.cit.gvsig.fmap.drivers.featureiterators.DefaultFeatureIterator;
306
import com.iver.cit.gvsig.fmap.drivers.featureiterators.SpatialQueryFeatureIterator;
307
import com.iver.cit.gvsig.fmap.layers.FBitSet;
308
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
309
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex;
310
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts;
311

    
312
/**
313
 * @author fjp
314
 *
315
 */
316
public class VectorialEditableAdapter extends EditableAdapter implements
317
                ReadableVectorial, BoundedShapes {
318
        protected ReadableVectorial ova;
319

    
320
        protected ISpatialIndex fmapSpatialIndex;
321

    
322
        protected Rectangle2D fullExtent;
323

    
324
        protected Image selectionImage;
325

    
326
        protected BufferedImage handlersImage;
327

    
328

    
329
        /*
330
         * azo; ReadableVectorial implementations need a reference to
331
         * IProjection (of the layer) and to ISpatialIndex (of the layer)
332
         * to allow iteration with different criteria
333
         * */
334
        protected IProjection projection;
335
//        protected ISpatialIndex fmapSpatialIndex;
336
        private ICoordTrans ct;
337

    
338
        //private double flatness=0.8;
339
        /*
340
         * private class MyFeatureIterator implements IFeatureIterator { int numReg =
341
         * 0; Rectangle2D rect; String epsg; IFeatureIterator origFeatIt; boolean
342
         * bHasNext = true;
343
         *
344
         * public MyFeatureIterator(Rectangle2D r, String strEPSG) throws
345
         * DriverException { rect = r; epsg = strEPSG; origFeatIt =
346
         * ova.getFeatureIterator(r, epsg); } public boolean hasNext() throws
347
         * DriverException { return bHasNext; }
348
         *
349
         * public IFeature next() throws DriverException { IFeature aux =
350
         * origFeatIt.next(); return null; }
351
         *
352
         * public void closeIterator() throws DriverException {
353
         *  }
354
         *  }
355
         */
356

    
357

    
358

    
359
        public VectorialEditableAdapter() {
360
                super();
361
        }
362

    
363
        public void setOriginalVectorialAdapter(ReadableVectorial rv) throws ReadDriverException {
364
                ova = rv;
365
                setOriginalDataSource(rv.getRecordset());
366
        }
367

    
368
        /*
369
         * (non-Javadoc)
370
         *
371
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#start()
372
         */
373
        public void start() throws ReadDriverException, InitializeDriverException {
374
                ova.start();
375
        }
376

    
377
        public IWriter getWriter() {
378
                if (ova.getDriver() instanceof IWriteable)
379
                {
380
                        IWriter writer = ((IWriteable) ova.getDriver()).getWriter();
381
                        if (writer instanceof ISpatialWriter)
382
                                return writer;
383
                }
384
                return null;
385
        }
386

    
387
        /*
388
         * (non-Javadoc)
389
         *
390
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#stop()
391
         */
392
        public void stop() throws ReadDriverException {
393
                ova.stop();
394
        }
395

    
396
        /*
397
         * (non-Javadoc)
398
         *
399
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
400
         */
401
        public IGeometry getShape(int rowIndex) throws ReadDriverException, ExpansionFileReadException {
402
                // Si no est? en el fichero de expansi?n
403
                int calculatedIndex = getCalculatedIndex(rowIndex);
404
                Integer integer = new Integer(calculatedIndex);
405
                if (!relations.containsKey(integer)) {
406
                        // Si ha sido eliminada
407
                        /*
408
                         * if (delRows.get(integer.intValue())) { return null; } else {
409
                         */
410
                        return ova.getShape(calculatedIndex);
411
                        // }
412
                }
413
                int num = ((Integer) relations.get(integer)).intValue();
414
                DefaultRowEdited feat;
415
                feat = (DefaultRowEdited) expansionFile.getRow(num);
416
                return ((IFeature) feat.getLinkedRow()).getGeometry()
417
                                        .cloneGeometry();// getGeometry();
418
        }
419

    
420
        /*
421
         * (non-Javadoc)
422
         *
423
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
424
         */
425
        public int getShapeType() throws ReadDriverException {
426
                return ova.getShapeType();
427
        }
428

    
429
        public ReadableVectorial getOriginalAdapter() {
430
                return ova;
431
        }
432

    
433
        public VectorialDriver getDriver() {
434
                return ova.getDriver();
435
        }
436

    
437
        public void setDriver(VectorialDriver driver) {
438
                this.ova.setDriver(driver);
439
        }
440

    
441
        public DriverAttributes getDriverAttributes() {
442
                return ova.getDriverAttributes();
443
        }
444

    
445
        /**
446
         * DOCUMENT ME!
447
         * @throws StartEditionLayerException
448
         * @throws StartWriterVisitorException
449
         *
450
         * @throws EditionException
451
         *             DOCUMENT ME!
452
         */
453
        public void startEdition(int sourceType) throws StartWriterVisitorException {
454
                super.startEdition(sourceType);
455
                Driver drv = ova.getDriver();
456
                if (drv instanceof IWriteable)
457
                {
458
                        setWriter(((IWriteable) drv).getWriter());
459
                }
460

    
461

    
462
                try {
463
                        expansionFile.open();
464
                        if (fmapSpatialIndex == null) {
465
                                // TODO: Si la capa dispone de un ?ndice espacial, hacer
466
                                // algo aqu? para que se use ese ?ndice espacial.
467
//                                index = new Quadtree();
468
                                fmapSpatialIndex = new QuadtreeJts();
469

    
470
                                for (int i = 0; i < ova.getShapeCount(); i++) {
471
                                        Rectangle2D r=null;
472
                                        if (ova.getDriver() instanceof BoundedShapes) {
473
                                                r = ((BoundedShapes) ova.getDriver()).getShapeBounds(i);
474
                                        } else {
475
                                                IGeometry g = null;
476
                                                g = ((DefaultFeature) ova.getFeature(i))
477
                                                                        .getGeometry();
478
                                                if (g == null) {
479
                                                        continue;
480
                                                }
481

    
482
                                                r = g.getBounds2D();
483
                                        }
484
                                        if (ct != null) {
485
                                                r = ct.convert(r);
486
                                        }
487
//                                        Envelope e = new Envelope(r.getX(),
488
//                                                        r.getX() + r.getWidth(), r.getY(), r.getY()
489
//                                                                        + r.getHeight());
490
                                        fmapSpatialIndex.insert(r, new Integer(i));
491
                                        if (fullExtent == null) {
492
                                                fullExtent = r;
493
                                        } else {
494
                                                fullExtent = fullExtent.createUnion(r);
495
                                        }
496
                                }
497
                        }
498
                } catch (ReadDriverException e) {
499
                        throw new StartWriterVisitorException(writer.getName(),e);
500
                }
501

    
502
//                System.err.println("Se han metido en el ?ndice "
503
//                                + index.queryAll().size() + " geometr?as");
504
        }
505

    
506
        /*
507
         * (non-Javadoc)
508
         *
509
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
510
         */
511
        public IRowEdited getRow(int index) throws ReadDriverException, ExpansionFileReadException {
512
                start();
513
                int calculatedIndex = getCalculatedIndex(index);
514
                Integer integer = new Integer(calculatedIndex);
515
                // Si no est? en el fichero de expansi?n
516
                DefaultRowEdited edRow = null;
517
                if (!relations.containsKey(integer)) {
518
                        edRow = new DefaultRowEdited(ova.getFeature(calculatedIndex),
519
                                        IRowEdited.STATUS_ORIGINAL, index);
520
                        stop();
521
                        return createExternalRow(edRow, 0);
522
                }
523
                int num = ((Integer) relations.get(integer)).intValue();
524
                IRowEdited aux = expansionFile.getRow(num);
525
                edRow = new DefaultRowEdited(aux.getLinkedRow().cloneRow(), aux
526
                                .getStatus(), index);
527
                stop();
528
                return edRow;
529
        }
530

    
531
        /**
532
         * Elimina una geometria. Si es una geometr?a original de la capa en edici?n
533
         * se marca como eliminada (haya sido modificada o no). Si es una geometr?a
534
         * a?adida posteriormente se invalida en el fichero de expansi?n, para que
535
         * una futura compactaci?n termine con ella.
536
         *
537
         * @param index
538
         *            ?ndice de la geometr?a.
539
         * @throws ReadDriverException
540
         * @throws ExpansionFileReadException
541
         *
542
         * @throws DriverIOException
543
         * @throws IOException
544
         */
545
        public IRow doRemoveRow(int index, int sourceType) throws ReadDriverException, ExpansionFileReadException{
546
                boolean cancel = fireBeforeRemoveRow(index, sourceType);
547
                if (cancel)
548
                        return null;
549
                // Llega el calculatedIndex
550
                Integer integer = new Integer(index);
551

    
552
                IFeature feat = null;
553
                delRows.set(index, true);
554
                // Si la geometr?a no ha sido modificada
555
                if (!relations.containsKey(integer)) {
556
                        feat = (DefaultFeature) (ova.getFeature(index));
557
                } else {
558
                        int num = ((Integer) relations.get(integer)).intValue();
559
                        feat = (IFeature) expansionFile.getRow(num).getLinkedRow();
560
                        // expansionFile.invalidateRow(num);
561
                }
562
                System.err.println("Elimina una Row en la posici?n: " + index);
563
                // Se actualiza el ?ndice
564
                if (feat != null) {
565
                        Rectangle2D r = feat.getGeometry().getBounds2D();
566
                        this.fmapSpatialIndex.delete(r,
567
                                        new Integer(index));
568
//                        System.out.println("Est? borrado : " + borrado);
569
//                        System.out.println("Index.lenght : " + this.index.size());
570
                        isFullExtentDirty = true;
571
                }
572
                setSelection(new FBitSet());
573
                fireAfterRemoveRow(index, sourceType);
574
                return feat;
575
        }
576

    
577
        /**
578
         * Si se intenta modificar una geometr?a original de la capa en edici?n se
579
         * a?ade al fichero de expansi?n y se registra la posici?n en la que se
580
         * a?adi?. Si se intenta modificar una geometria que se encuentra en el
581
         * fichero de expansi?n (por ser nueva o original pero modificada) se invoca
582
         * el m?todo modifyGeometry y se actualiza el ?ndice de la geometria en el
583
         * fichero.
584
         *
585
         * @param calculatedIndex
586
         *            DOCUMENT ME!
587
         * @param feat
588
         *            DOCUMENT ME!
589
         *
590
         * @return position inside ExpansionFile
591
         * @throws ReadDriverException
592
         * @throws ExpansionFileWriteException
593
         * @throws ExpansionFileReadException
594
         *
595
         * @throws IOException
596
         * @throws DriverIOException
597
         */
598
        public int doModifyRow(int calculatedIndex, IRow feat, int sourceType) throws ReadDriverException, ExpansionFileWriteException, ExpansionFileReadException {
599
                boolean cancel = fireBeforeModifyRow(feat, calculatedIndex, sourceType);
600
                if (cancel)
601
                        return -1;
602
                int posAnteriorInExpansionFile = -1;
603
                Integer integer = new Integer(calculatedIndex);
604

    
605
                IFeature featAnt = null;
606
//                System.err.println("Modifica una Row en la posici?n: "
607
//                                + calculatedIndex);
608
                // Si la geometr?a no ha sido modificada
609
                if (!relations.containsKey(integer)) {
610
                        int newPosition = expansionFile.addRow(feat,
611
                                        IRowEdited.STATUS_MODIFIED, actualIndexFields);
612
                        relations.put(integer, new Integer(newPosition));
613

    
614
                        if (sourceType == EditionEvent.GRAPHIC) {
615
                                // Se actualiza el ?ndice espacial
616
                                featAnt = (DefaultFeature) (ova.getFeature(calculatedIndex));
617
                                IGeometry g = featAnt.getGeometry();
618
                                Rectangle2D rAnt = g.getBounds2D();
619
                                Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
620
                                this.fmapSpatialIndex.delete(rAnt, new Integer(calculatedIndex));
621
                                this.fmapSpatialIndex.insert(r, new Integer(calculatedIndex));
622
                        }
623
                } else {
624
                        // Obtenemos el ?ndice en el fichero de expansi?n
625
                        int num = ((Integer) relations.get(integer)).intValue();
626
                        posAnteriorInExpansionFile=num;
627

    
628
                        // Obtenemos la geometr?a para actualiza el ?ndice
629
                        // espacialposteriormente
630
                        featAnt = (IFeature) expansionFile.getRow(num).getLinkedRow();
631

    
632
                        /*
633
                         * Se modifica la geometr?a y nos guardamos el ?ndice dentro del
634
                         * fichero de expansi?n en el que se encuentra la geometr?a
635
                         * modificada
636
                         */
637
                        num = expansionFile.modifyRow(num, feat, actualIndexFields);
638

    
639
                        /*
640
                         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
641
                         * fichero de expansi?n.
642
                         */
643
                        relations.put(integer, new Integer(num));
644
                        if (sourceType == EditionEvent.GRAPHIC) {
645
                                // Se modifica el ?ndice espacial
646
                                Rectangle2D rAnt = featAnt.getGeometry().getBounds2D();
647
                                Rectangle2D r = ((IFeature) feat).getGeometry().getBounds2D();
648
                                this.fmapSpatialIndex.delete(rAnt, new Integer(calculatedIndex));
649
                                this.fmapSpatialIndex.insert(r, new Integer(calculatedIndex));
650
                        }
651

    
652
                }
653
                isFullExtentDirty = true;
654
                fireAfterModifyRow(calculatedIndex, sourceType);
655
                return posAnteriorInExpansionFile;
656
        }
657

    
658
        /**
659
         * Actualiza en el mapa de ?ndices, la posici?n en la que estaba la
660
         * geometr?a antes de ser modificada. Se marca como v?lida, en caso de que
661
         * fuera una modificaci?n de una geometr?a que estuviese en el fichero de
662
         * expansi?n antes de ser modificada y se pone el puntero de escritura del
663
         * expansion file a justo despues de la penultima geometr?a
664
         *
665
         * @param calculatedIndex
666
         *            ?ndice de la geometr?a que se quiere deshacer su modificaci?n
667
         * @param previousExpansionFileIndex
668
         *            ?ndice que ten?a antes la geometr?a en el expansionFile. Si
669
         *            vale -1 quiere decir que es una modificaci?n de una geometr?a
670
         *            original y por tanto no hay que actualizar el mapa de indices
671
         *            sino eliminar su entrada.
672
         * @throws IOException
673
         * @throws DriverIOException
674
         */
675
        public int undoModifyRow(int calculatedIndex,
676
                        int previousExpansionFileIndex, int sourceType) throws EditionCommandException{
677
                try {
678
                        // Llega el CalculatedIndex
679
                        /*
680
                         * Si la acci?n de modificar se realiz? sobre una geometr?a original
681
                         */
682
                        if (previousExpansionFileIndex == -1) {
683

    
684
                                // Se obtiene la geometr?a para actualizar el ?ndice
685
                                // IGeometry g = ((DefaultFeature)
686
                                // getRow(calculatedIndex).getLinkedRow()).getGeometry();
687
                                int inverse = getInversedIndex(calculatedIndex);
688
                                DefaultFeature df;
689

    
690
                                df = (DefaultFeature) getRow(inverse).getLinkedRow();
691

    
692
                                boolean cancel = fireBeforeModifyRow(df, calculatedIndex,
693
                                                sourceType);
694
                                if (cancel)
695
                                        return -1;
696
                                IGeometry g = df.getGeometry();
697
                                // IGeometry g = ova.getShape(calculatedIndex);
698
                                Rectangle2D r = g.getBounds2D();
699

    
700
                                // Se elimina de las relaciones y del fichero de expansi?n
701
                                relations.remove(new Integer(calculatedIndex));
702
                                expansionFile.deleteLastRow();
703
                                if (sourceType == EditionEvent.GRAPHIC) {
704
                                        // Se actualizan los ?ndices
705
                                        IGeometry gAnt = ova.getShape(calculatedIndex);
706
                                        /*
707
                                         * IGeometry gAnt = ((DefaultFeature) getRow(calculatedIndex)
708
                                         * .getLinkedRow()).getGeometry();
709
                                         */
710
                                        Rectangle2D rAnt = gAnt.getBounds2D();
711
                                        this.fmapSpatialIndex.delete(r, new Integer(calculatedIndex));
712
                                        this.fmapSpatialIndex.insert(rAnt, new Integer(calculatedIndex));
713
                                }
714
                        } else {
715
                                // Se obtiene la geometr?a para actualizar el ?ndice
716
                                IGeometry g = null;
717
                                int inverse = getInversedIndex(calculatedIndex);
718
                                DefaultFeature df = (DefaultFeature) getRow(inverse).getLinkedRow();
719
                                boolean cancel = fireBeforeModifyRow(df, calculatedIndex,
720
                                                sourceType);
721
                                if (cancel)
722
                                        return -1;
723
                                if (sourceType == EditionEvent.GRAPHIC) {
724
                                        g = df.getGeometry();
725
                                        System.out.println("Actual: " + g.toString());
726

    
727
                                        Rectangle2D r = g.getBounds2D();
728
                                        this.fmapSpatialIndex.delete(r, new Integer(calculatedIndex));
729

    
730
                                        // Se recupera la geometr?a
731
                                        // expansionFile.validateRow(previousExpansionFileIndex);
732

    
733
                                        // Se actualizan los ?ndices
734
                                         g = ((IFeature)
735
                                         (expansionFile.getRow(previousExpansionFileIndex).getLinkedRow())).getGeometry();
736
                                        // System.out.println("Anterior a la que volvemos : " +
737
                                        // g.toString());
738
//                                        g = ((DefaultFeature) getRow(inverse).getLinkedRow())
739
//                                        .getGeometry();
740
                                        r = g.getBounds2D();
741
                                        this.fmapSpatialIndex.insert(r, new Integer(calculatedIndex));
742
                                }
743
                                // Se actualiza la relaci?n de ?ndices
744
                                // Integer integer = new Integer(geometryIndex);
745
                                int numAnt=((Integer)relations.get(new Integer(calculatedIndex))).intValue();
746
                                relations.put(new Integer(calculatedIndex), new Integer(
747
                                                previousExpansionFileIndex));
748
                                return numAnt;
749

    
750
                        }
751
                } catch (ReadDriverException e) {
752
                        throw new EditionCommandException(writer.getName(),e);
753
                }
754
                //fireAfterModifyRow(calculatedIndex, sourceType);
755
                return -1;
756
        }
757

    
758
        /**
759
         * A?ade una geometria al fichero de expansi?n y guarda la correspondencia
760
         * en la tabla relations.
761
         *
762
         * @param feat
763
         *            geometr?a a guardar.
764
         *
765
         * @return calculatedIndex
766
         * @throws ExpansionFileWriteException
767
         * @throws DriverIOException
768
         * @throws IOException
769
         */
770
        public int doAddRow(IRow feat, int sourceType) throws ReadDriverException, ExpansionFileWriteException{
771
                int calculatedIndex = super.doAddRow(feat, sourceType);
772
                // Actualiza el ?ndice espacial
773
                IGeometry g = ((IFeature) feat).getGeometry();
774
                Rectangle2D r = g.getBounds2D();
775
                fmapSpatialIndex.insert(r, new Integer(calculatedIndex));
776
                isFullExtentDirty = true;
777
                return calculatedIndex;
778
        }
779

    
780
        /**
781
         * Se desmarca como invalidada en el fichero de expansion o como eliminada
782
         * en el fichero original
783
         *
784
         * @param index
785
         *            DOCUMENT ME!
786
         * @throws IOException
787
         * @throws DriverIOException
788
         */
789
        public void undoRemoveRow(int index, int sourceType) throws EditionCommandException  {
790
                super.undoRemoveRow(index, sourceType);
791

    
792
                IGeometry g = null;
793
                try {
794
                        g = ((IFeature) getRow(getInversedIndex(index)).getLinkedRow()).getGeometry();
795
                } catch (ReadDriverException e) {
796
                        throw new EditionCommandException(writer.getName(),e);
797
                }
798

    
799
                Rectangle2D r = g.getBounds2D();
800
                this.fmapSpatialIndex.insert(r, new Integer(index));
801
        }
802

    
803
        /**
804
         * Se elimina del final del fichero de expansi?n poniendo el puntero de
805
         * escritura apuntando al final de la pen?ltima geometr?a. Deber? quitar la
806
         * relaci?n del mapa de relaciones
807
         *
808
         * @param fmapSpatialIndex
809
         *            ?ndice de la geometr?a que se a?adi?
810
         * @throws EditionCommandException
811
         * @throws DriverIOException
812
         * @throws IOException
813
         */
814
        public void undoAddRow(int calculatedIndex, int sourceType) throws EditionCommandException{
815
                int inverse = getInversedIndex(calculatedIndex);
816
                IGeometry g;
817
                try {
818
                        g = ((IFeature) getRow(inverse).getLinkedRow()).getGeometry();
819
                        Rectangle2D r = g.getBounds2D();
820
                        this.fmapSpatialIndex.delete(r, new Integer(calculatedIndex));
821

    
822
                        super.undoAddRow(calculatedIndex, sourceType);
823
                        setSelection(new FBitSet());
824
                } catch (ReadDriverException e) {
825
                        throw new EditionCommandException(writer.getName(),e);
826
                }
827
        }
828

    
829
        /**
830
         * Obtiene las geometr?as que se encuentran en el rect?ngulo que se pasa
831
         * como par?metro haciendo uso del ?ndice espacial
832
         *
833
         * @param r
834
         *            Rect?ngulo indicando la porci?n del espacio para el cual se
835
         *            quiere saber los ?ndices de las geometr?as que se encuentran
836
         *            dentro de ?l
837
         *
838
         * @return Array de ?ndices para su uso con getGeometry, removeGeometry, ...
839
         */
840
        /*
841
         * public int[] getRowsIndexes_OL(Rectangle2D r) { Envelope e = new
842
         * Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(), r.getY() +
843
         * r.getHeight()); List l = index.query(e); int[] indexes = new
844
         * int[l.size()];
845
         *
846
         * for (int index = 0; index < l.size(); index++) { Integer i = (Integer)
847
         * l.get(index); indexes[index] = getInversedIndex(i.intValue()); }
848
         *
849
         * return indexes; }
850
         */
851
        /**
852
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
853
         */
854
        public int getShapeCount() throws ReadDriverException {
855
                return getRowCount();
856
        }
857

    
858
        /**
859
         * @throws ExpansionFileReadException
860
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
861
         */
862
        public Rectangle2D getFullExtent() throws ReadDriverException, ExpansionFileReadException {
863
                if (fullExtent == null) {
864
                        fullExtent = ova.getFullExtent();
865
                }
866

    
867
                if (isFullExtentDirty) {
868
                        fullExtent = reCalculateFullExtent();
869
                        isFullExtentDirty = false;
870
                }
871
                return fullExtent;
872
        }
873

    
874
        /**
875
         * Use it BEFORE writing to a file.
876
         *
877
         * @return real full extent.
878
         * @throws ExpansionFileReadException
879
         * @throws ReadDriverException
880
         * @throws DriverIOException
881
         */
882
        public Rectangle2D reCalculateFullExtent() throws ReadDriverException, ExpansionFileReadException  {
883
                if (getShapeCount() > 0) {
884
                        fullExtent = getShape(0).getBounds2D();
885
                        for (int i = 1; i < getShapeCount(); i++) {
886
                                IGeometry geom=getShape(i);
887
                                if (fullExtent==null) {
888
                                        fullExtent=geom.getBounds2D();
889
                                        continue;
890
                                }
891
                                if (!(geom instanceof FNullGeometry)) {
892
                                        fullExtent.add(geom.getBounds2D());
893
                                }
894
                        }
895
                } else {
896
                        fullExtent = ova.getFullExtent();
897
                }
898
                return fullExtent;
899
        }
900

    
901
        /**
902
         * En la implementaci?n por defecto podemos hacer que cada feature tenga ID =
903
         * numero de registro. En el DBAdapter podr?amos "overrride" este m?todo y
904
         * poner como ID de la Feature el campo ?nico escogido en la base de datos.
905
         *
906
         * @param numReg
907
         * @return
908
         * @throws ExpansionFileReadException
909
         */
910
        public IFeature getFeature(int numReg) throws ReadDriverException, ExpansionFileReadException {
911
                IGeometry geom;
912
                IFeature feat = null;
913
                        geom = getShape(numReg);
914
                        DataSource rs = getRecordset();
915
                        Value[] regAtt = new Value[rs.getFieldCount()];
916
                        for (int fieldId = 0; fieldId < rs.getFieldCount(); fieldId++) {
917
                                regAtt[fieldId] = rs.getFieldValue(numReg, fieldId);
918
                        }
919
                        feat = new DefaultFeature(geom, regAtt, numReg + "");
920
                return feat;
921
        }
922

    
923
        public void stopEdition(IWriter writer, int sourceType) throws StopWriterVisitorException {
924
//                ISpatialWriter spatialWriter = (ISpatialWriter) writer;
925
//                spatialWriter.setFlatness(FConverter.flatness);
926
                super.stopEdition(writer, sourceType);
927
//                try {
928
//                        ova.getDriver().reload();
929
//                } catch (ReloadDriverException e) {
930
//                        throw new StopWriterVisitorException(writer.getName(),e);
931
//                }
932
//                fmapSpatialIndex=null;
933
                writer=null;
934
        }
935

    
936
        public Rectangle2D getShapeBounds(int index) throws ReadDriverException, ExpansionFileReadException {
937
                // Solo se utiliza cuando el driver es BoundedShapes
938
                // Si no est? en el fichero de expansi?n
939
                Integer integer = new Integer(index);
940
                if (!relations.containsKey(integer)) {
941
                        if (ova.getDriver() instanceof BoundedShapes) {
942
                                BoundedShapes bs = (BoundedShapes) ova.getDriver();
943
                                return bs.getShapeBounds(index);
944
                        }
945
                        return ova.getDriver().getShape(index).getBounds2D();
946

    
947
                }
948
                int num = ((Integer) relations.get(integer)).intValue();
949
                DefaultRowEdited feat;
950
                feat = (DefaultRowEdited) expansionFile.getRow(num);
951
                if (feat.getStatus() == IRowEdited.STATUS_DELETED)
952
                        return null;
953
                IGeometry geom = ((IFeature) feat.getLinkedRow()).getGeometry();
954
                return geom.getBounds2D();// getGeometry();
955

    
956
        }
957

    
958
        public int getShapeType(int index) throws ReadDriverException {
959
                return ova.getShapeType();
960
        }
961

    
962
        /**
963
         * Usar solo cuando est?s seguro de que puedes gastar memoria. Nosotros lo
964
         * usamos para las b?squedas por ?ndice espacial con el handle. La idea es
965
         * usarlo una vez, guardar las geometr?as que necesitas en ese extent y
966
         * trabajar con ellas hasta el siguiente cambio de extent.
967
         *
968
         * @param r
969
         * @param strEPSG
970
         * @return
971
         * @throws ExpansionFileReadException
972
         * @throws ReadDriverException
973
         * @throws DriverException
974
         */
975
        public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG) throws ReadDriverException, ExpansionFileReadException{
976
                // En esta clase suponemos random access.
977
                // Luego tendremos otra clase que sea VectorialEditableDBAdapter
978
                // que reescribir? este m?todo.
979
//                Envelope e = FConverter.convertRectangle2DtoEnvelope(r);
980
                List l = fmapSpatialIndex.query(r);
981
                IRowEdited[] feats = new IRowEdited[l.size()];
982
                for (int index = 0; index < l.size(); index++) {
983
                        Integer i = (Integer) l.get(index);
984
                        int inverse = getInversedIndex(i.intValue());
985
                        feats[index] = getRow(inverse);
986
                }
987
                return feats;
988
        }
989

    
990
//        public void setSpatialIndex(SpatialIndex spatialIndex) {
991
//                index = (Quadtree) spatialIndex;
992
//                QuadtreeJts fmapidx = new QuadtreeJts(index);
993
//                setSpatialIndex(fmapidx);
994
//
995
//        }
996

    
997
        public void setFullExtent(Rectangle2D fullExtent2) {
998
                fullExtent = fullExtent2;
999
        }
1000

    
1001
        /**
1002
         * DOCUMENT ME!
1003
         *
1004
         * @return DOCUMENT ME!
1005
         */
1006
        public Image getSelectionImage() {
1007
                return selectionImage;
1008
        }
1009

    
1010
        public Image getHandlersImage() {
1011
                return handlersImage;
1012
        }
1013

    
1014
        /**
1015
         * DOCUMENT ME!
1016
         *
1017
         * @param i
1018
         *            DOCUMENT ME!
1019
         */
1020
        public void setSelectionImage(Image i) {
1021
                selectionImage = i;
1022
        }
1023

    
1024
        public void setHandlersImage(BufferedImage handlersImage) {
1025
                this.handlersImage = handlersImage;
1026
        }
1027

    
1028
        public IFeatureIterator getFeatureIterator() throws ReadDriverException {
1029
                return new DefaultFeatureIterator(this, projection, null, null);
1030
        }
1031

    
1032

    
1033
        public IFeatureIterator getFeatureIterator(String[] fields, IProjection newProjection)
1034
        throws ReadDriverException{
1035
                return new DefaultFeatureIterator(this, projection, newProjection, fields);
1036
        }
1037

    
1038
        /**
1039
        * Return a feature iterator from a given sql statement.
1040
        * <br>
1041
        * In this case, the statement will have the "projection" operator
1042
        * (select campo1, campo2, ...etc) and the "selection" operator (where ....)
1043
        * @param sql statement which define a filter
1044
        * @return feature iterator
1045
        * */
1046
        public IFeatureIterator getFeatureIterator(String sql,
1047
                                                                IProjection newProjection) throws ReadDriverException{
1048

    
1049
                return new AttrQueryFeatureIterator(this, projection, newProjection, sql);
1050
        }
1051

    
1052

    
1053
        /**
1054
        * Makes an spatial query returning a feature iterator over the features which intersects
1055
        * or are contained in the rectangle query. Applies a restriction to the alphanumeric fields
1056
        * returned by the iterator.
1057
        * @param rect
1058
        * @param fields
1059
        * @return
1060
         * @throws ReadDriverException
1061
        */
1062
        public IFeatureIterator getFeatureIterator(Rectangle2D rect, String[] fields,
1063
                                                                        IProjection newProjection, boolean fastIteration) throws ReadDriverException{
1064
//                if(index == null){
1065
                        return new SpatialQueryFeatureIterator(this, projection, newProjection, fields, rect, fastIteration);
1066
//                }else{
1067
//                        return new IndexedSptQueryFeatureIterator(this, projection, newProjection, fields, rect, fmapSpatialIndex, fastIteration);
1068
//                }
1069
        }
1070

    
1071
        public ISpatialIndex getSpatialIndex() {
1072
                return fmapSpatialIndex;
1073
        }
1074

    
1075
        public void setSpatialIndex(ISpatialIndex spatialIndex) {
1076
//                this.fmapSpatialIndex = spatialIndex;
1077
        }
1078

    
1079
        public void setProjection(IProjection projection) {
1080
                this.projection = projection;
1081
        }
1082

    
1083
        public IProjection getProjection() {
1084
                return projection;
1085
        }
1086

    
1087
        public void cancelEdition(int sourceType) throws CancelEditingLayerException {
1088
                super.cancelEdition(sourceType);
1089
                fmapSpatialIndex=null;
1090
                setWriter(null);
1091
                System.gc();
1092
        }
1093

    
1094
        /**
1095
         * Inserta las coordenadas de transformaci?n.
1096
         *
1097
         * @param ct Coordenadas de transformaci?n.
1098
         */
1099
        public void setCoordTrans(ICoordTrans ct) {
1100
                this.ct=ct;
1101
        }
1102

    
1103
        public IFeatureIterator getFeatureIterator(String sql, IProjection newProjection, boolean withSelection) throws ReadDriverException {
1104
                if (withSelection)
1105
                        return new AttrQuerySelectionFeatureIterator(this, projection, newProjection, sql);
1106
                else
1107
                        return getFeatureIterator(sql,newProjection);
1108
        }
1109

    
1110
}