Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableAdapter.java @ 19509

History | View | Annotate | Download (34.4 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 16545 2007-11-22 08:29:42Z 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.DefaultFeatureIterator;
305
import com.iver.cit.gvsig.fmap.drivers.featureiterators.IndexedSptQueryFeatureIterator;
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, 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,index);
567
//                        System.out.println("Est? borrado : " + borrado);
568
//                        System.out.println("Index.lenght : " + this.index.size());
569
                        isFullExtentDirty = true;
570
                }
571
                setSelection(new FBitSet());
572
                fireAfterRemoveRow(index, sourceType);
573
                return feat;
574
        }
575

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

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

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

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

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

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

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

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

    
688
                                df = (DefaultFeature) getRow(inverse).getLinkedRow();
689

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

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

    
725
                                        Rectangle2D r = g.getBounds2D();
726
                                        this.fmapSpatialIndex.delete(r, calculatedIndex);
727

    
728
                                        // Se recupera la geometr?a
729
                                        // expansionFile.validateRow(previousExpansionFileIndex);
730

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

    
746
                        }
747
                } catch (ReadDriverException e) {
748
                        throw new EditionCommandException(writer.getName(),e);
749
                }
750
                //fireAfterModifyRow(calculatedIndex, sourceType);
751
        }
752

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

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

    
787
                IGeometry g = null;
788
                try {
789
                        g = ((IFeature) getRow(getInversedIndex(index)).getLinkedRow()).getGeometry();
790
                } catch (ReadDriverException e) {
791
                        throw new EditionCommandException(writer.getName(),e);
792
                }
793

    
794
                Rectangle2D r = g.getBounds2D();
795
                this.fmapSpatialIndex.insert(r, index);
796
        }
797

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

    
817
                        super.undoAddRow(calculatedIndex, sourceType);
818
                        setSelection(new FBitSet());
819
                } catch (ReadDriverException e) {
820
                        throw new EditionCommandException(writer.getName(),e);
821
                }
822
        }
823

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

    
853
        /**
854
         * @throws ExpansionFileReadException
855
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
856
         */
857
        public Rectangle2D getFullExtent() throws ReadDriverException, ExpansionFileReadException {
858
                if (fullExtent == null) {
859
                        fullExtent = ova.getFullExtent();
860
                }
861

    
862
                if (isFullExtentDirty) {
863
                        fullExtent = reCalculateFullExtent();
864
                        isFullExtentDirty = false;
865
                }
866
                return fullExtent;
867
        }
868

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

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

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

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

    
942
                }
943
                int num = ((Integer) relations.get(integer)).intValue();
944
                DefaultRowEdited feat;
945
                feat = (DefaultRowEdited) expansionFile.getRow(num);
946
                if (feat.getStatus() == IRowEdited.STATUS_DELETED)
947
                        return null;
948
                IGeometry geom = ((IFeature) feat.getLinkedRow()).getGeometry();
949
                return geom.getBounds2D();// getGeometry();
950

    
951
        }
952

    
953
        public int getShapeType(int index) throws ReadDriverException {
954
                return ova.getShapeType();
955
        }
956

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

    
985
//        public void setSpatialIndex(SpatialIndex spatialIndex) {
986
//                index = (Quadtree) spatialIndex;
987
//                QuadtreeJts fmapidx = new QuadtreeJts(index);
988
//                setSpatialIndex(fmapidx);
989
//
990
//        }
991

    
992
        public void setFullExtent(Rectangle2D fullExtent2) {
993
                fullExtent = fullExtent2;
994
        }
995

    
996
        /**
997
         * DOCUMENT ME!
998
         *
999
         * @return DOCUMENT ME!
1000
         */
1001
        public Image getSelectionImage() {
1002
                return selectionImage;
1003
        }
1004

    
1005
        public Image getHandlersImage() {
1006
                return handlersImage;
1007
        }
1008

    
1009
        /**
1010
         * DOCUMENT ME!
1011
         *
1012
         * @param i
1013
         *            DOCUMENT ME!
1014
         */
1015
        public void setSelectionImage(Image i) {
1016
                selectionImage = i;
1017
        }
1018

    
1019
        public void setHandlersImage(BufferedImage handlersImage) {
1020
                this.handlersImage = handlersImage;
1021
        }
1022

    
1023
        public IFeatureIterator getFeatureIterator() throws ReadDriverException {
1024
                return new DefaultFeatureIterator(this, projection, null, null);
1025
        }
1026

    
1027

    
1028
        public IFeatureIterator getFeatureIterator(String[] fields, IProjection newProjection)
1029
        throws ReadDriverException{
1030
                return new DefaultFeatureIterator(this, projection, newProjection, fields);
1031
        }
1032

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

    
1044
                return new AttrQueryFeatureIterator(this, projection, newProjection, sql);
1045
        }
1046

    
1047

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

    
1066
        public ISpatialIndex getSpatialIndex() {
1067
                return fmapSpatialIndex;
1068
        }
1069

    
1070
        public void setSpatialIndex(ISpatialIndex spatialIndex) {
1071
//                this.fmapSpatialIndex = spatialIndex;
1072
        }
1073

    
1074
        public void setProjection(IProjection projection) {
1075
                this.projection = projection;
1076
        }
1077

    
1078
        public IProjection getProjection() {
1079
                return projection;
1080
        }
1081

    
1082
        public void cancelEdition(int sourceType) throws CancelEditingLayerException {
1083
                super.cancelEdition(sourceType);
1084
                fmapSpatialIndex=null;
1085
                setWriter(null);
1086
                System.gc();
1087
        }
1088

    
1089
        /**
1090
         * Inserta las coordenadas de transformaci?n.
1091
         *
1092
         * @param ct Coordenadas de transformaci?n.
1093
         */
1094
        public void setCoordTrans(ICoordTrans ct) {
1095
                this.ct=ct;
1096
        }
1097

    
1098
}