Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / SpatialManager.java @ 37297

History | View | Annotate | Download (11.1 KB)

1

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

    
43
package org.gvsig.fmap.dal.feature.impl;
44

    
45
import java.util.ArrayList;
46
import java.util.Iterator;
47

    
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.feature.Feature;
53
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
54
import org.gvsig.fmap.dal.feature.FeatureIndex;
55
import org.gvsig.fmap.dal.feature.FeatureIndexes;
56
import org.gvsig.fmap.dal.feature.FeatureReference;
57
import org.gvsig.fmap.dal.feature.FeatureSet;
58
import org.gvsig.fmap.dal.feature.FeatureStore;
59
import org.gvsig.fmap.dal.feature.FeatureType;
60
import org.gvsig.fmap.geom.GeometryLocator;
61
import org.gvsig.fmap.geom.primitive.Envelope;
62
import org.gvsig.tools.dispose.DisposableIterator;
63

    
64
/**
65
 * DOCUMENT ME!
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class SpatialManager {
70
    private static final Logger LOG = LoggerFactory.getLogger(SpatialManager.class);
71
    
72
    protected boolean isFullExtentDirty = true;
73
    private FeatureStore featureStore;
74
    private FeatureIndex featureIndex = null;
75
    private Envelope originalEnvelope = null;
76
    private Envelope fullEnvelope = null;
77
    private ArrayList feaOperation=new ArrayList();
78
    private boolean noSpatialData = false;
79

    
80
    public SpatialManager(FeatureStore featureStore, Envelope originalEnvelope)
81
            throws DataException {
82
        this.featureStore=featureStore;
83
        FeatureIndexes findexes=featureStore.getIndexes();
84
        // Comprobamos si hay algun campo espacial a manejar
85

    
86
        FeatureType fType = this.featureStore.getDefaultFeatureType();
87
        // TODO Multy FType !!
88
        if (fType.getDefaultGeometryAttributeIndex() < 0) {
89
            noSpatialData = true;
90
            return;
91
        }
92
        FeatureAttributeDescriptor attr = fType.getAttributeDescriptor(fType
93
                .getDefaultGeometryAttributeIndex());
94
        this.originalEnvelope = originalEnvelope;
95
        if ((originalEnvelope != null) && (!originalEnvelope.isEmpty())) {
96
            this.fullEnvelope = originalEnvelope.getGeometry().getEnvelope();
97
        } else {
98
            FeatureAttributeDescriptor geoAttr = fType.getAttributeDescriptor(fType.getDefaultGeometryAttributeIndex());
99
            try {
100
                this.fullEnvelope = GeometryLocator.getGeometryManager()
101
                        .createEnvelope(geoAttr.getGeometrySubType());
102
            } catch (Exception e) {
103
                // FIXME Excpetion
104
                throw new RuntimeException(e);
105
            }
106
        }
107
        if (!fType.hasOID()) {
108
            return;
109
        }
110

    
111
        Iterator iterator = findexes.iterator();
112
        FeatureIndex index;
113
        while (iterator.hasNext()) {
114
            index = (FeatureIndex) iterator.next();
115
            if (index.getAttributeNames().size() == 1
116
                    && index.getAttributeNames().contains(attr.getName())) {
117
                featureIndex = index;
118
                break;
119
            }
120
        }
121

    
122
        if (featureIndex == null) {
123
            try{
124
                featureIndex = featureStore.createIndex(fType, attr.getName(),
125
                        "QuadtreeJts", null);
126
            }catch (Exception e) {
127
                LOG.info("It can not create an index", e);
128
            }
129
        }
130

    
131
        if (featureIndex instanceof DefaultFeatureIndex) {
132
            ((DefaultFeatureIndex) featureIndex).setValid(true);
133
        }
134
    }
135

    
136

    
137

    
138
    /**
139
     * DOCUMENT ME!
140
     *
141
     * @param feature DOCUMENT ME!
142
     * @param oldFeature DOCUMENT ME!
143
     */
144
    public void updateFeature(Feature feature, Feature oldFeature) {
145
        if (noSpatialData) {
146
            return;
147
        }
148
        if (featureIndex != null) {
149
            try {
150
                featureIndex.delete(oldFeature);
151
                feaOperation.add(new FeatureOperation(((DefaultFeature) oldFeature)
152
                        .getReference(), FeatureOperation.DELETE));
153
                featureIndex.insert(feature);
154
                feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
155
                        .getReference(), FeatureOperation.INSERT));
156
            } catch (DataException e) {
157
                throw new RuntimeException("Exception updating feature: "
158
                    + feature, e);
159
            }
160
            // } else {
161
            // fullEnvelope.add(feature.getDefaultEnvelope());
162
        }
163
        isFullExtentDirty = true;
164
    }
165

    
166
    /**
167
     * DOCUMENT ME!
168
     *
169
     * @param feature DOCUMENT ME!
170
     */
171
    public void insertFeature(Feature feature) {
172
        if (noSpatialData) {
173
            return;
174
        }
175
        if (featureIndex != null) {
176
            try {
177
                featureIndex.insert(feature);
178
                feaOperation.add(new FeatureOperation(
179
                    ((DefaultFeature) feature).getReference(),
180
                    FeatureOperation.INSERT));
181
            } catch (DataException e) {
182
                throw new RuntimeException("Exception inserting feature: "
183
                    + feature, e);
184
            }
185
            isFullExtentDirty = true;
186
        } else if (!isFullExtentDirty) {
187
            fullEnvelope.add(feature.getDefaultEnvelope());
188
        }
189
    }
190

    
191
    /**
192
     * DOCUMENT ME!
193
     *
194
     * @param feature DOCUMENT ME!
195
     */
196
    public void deleteFeature(Feature feature) {
197
        if (noSpatialData) {
198
            return;
199
        }
200
        if (featureIndex != null) {
201
            try {
202
                featureIndex.delete(feature);
203
                feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
204
                        .getReference(), FeatureOperation.DELETE));
205
            } catch (DataException e) {
206
                throw new RuntimeException("Exception deleting feature: "
207
                    + feature, e);
208
            }
209
        }
210
        isFullExtentDirty = true;
211
    }
212

    
213
    public void clear() {
214
    }
215

    
216
    public Envelope getEnvelope() throws DataException {
217
        if (noSpatialData) {
218
            return null;
219
        }
220
        if (!isFullExtentDirty){
221
            return this.fullEnvelope;
222
        }
223

    
224
        // FIXME in every changes when anyone ask for envelope it was regenerated.
225
        //       if we assume that the envelope may not be the minimum in edit mode
226
        //       this call must be very much faster
227

    
228

    
229
        FeatureAttributeDescriptor attr = featureStore.getDefaultFeatureType()
230
                .getAttributeDescriptor(
231
                        featureStore.getDefaultFeatureType()
232
                                .getDefaultGeometryAttributeIndex());
233
        Envelope fullEnvelope = null;
234

    
235
        FeatureSet set = null;
236
        DisposableIterator iterator = null;
237
        try {
238
            set = featureStore.getFeatureSet();
239
            iterator = set.fastIterator();
240
            //First while to initialize the feature envelope
241
            while (iterator.hasNext()) {
242
                Feature feature = (Feature) iterator.next();
243
                Envelope envelope = feature.getDefaultEnvelope();
244
                if (envelope != null){
245
                    fullEnvelope = (Envelope)envelope.clone();
246
                    break;
247
                }
248
            }           
249
            //Second while to add new evelopes tho the full envelope
250
            while (iterator.hasNext()) {
251
                Feature feature = (Feature) iterator.next();
252
                Envelope envelope = feature.getDefaultEnvelope();           
253
                if(envelope!=null){
254
                    fullEnvelope.add(envelope);
255
                }
256
            }
257
            //Creating an empty envelope by default
258
            if (fullEnvelope == null){
259
                fullEnvelope = GeometryLocator.getGeometryManager().createEnvelope(
260
                    attr.getGeometrySubType());
261
            }
262
        } catch (Exception e) {
263
            throw new RuntimeException(e);
264
        } finally {
265
            if (iterator != null) {
266
                iterator.dispose();
267
            }
268
            if (set != null) {
269
                set.dispose();
270
            }
271
        }
272
        this.fullEnvelope = fullEnvelope;
273
        this.isFullExtentDirty = false;
274
        return fullEnvelope;
275
    }
276

    
277

    
278

    
279
    public void cancelModifies() {
280
        if (noSpatialData) {
281
            return;
282
        }
283
        if (featureIndex != null){
284
            for (int i = feaOperation.size()-1 ; i>=0 ; i--){
285
                try {
286
                    FeatureOperation fo = (FeatureOperation) feaOperation.get(i);
287
                    if (fo.getOperation() == FeatureOperation.INSERT){                     
288
                        featureIndex.delete(fo.getFeatureReference().getFeature());
289
                    }else if (fo.getOperation() == FeatureOperation.DELETE){
290
                        featureIndex.insert(fo.getFeatureReference().getFeature());
291
                    }
292
                } catch (DataException e) {
293
                    LOG.error("Error canceling the edition", e);
294
                }           
295
            }
296
        }
297
        if (originalEnvelope!=null){
298
            fullEnvelope = originalEnvelope.getGeometry().getEnvelope();
299
        } else {
300
            fullEnvelope = null;
301
        }
302
        isFullExtentDirty = false;
303
    }
304

    
305
    private class FeatureOperation{
306
        final static int INSERT=0;
307
        final static int DELETE=1;
308
        private FeatureReference ref;
309
        private int operation;
310
        public FeatureOperation(FeatureReference fe,int op){
311
            ref=fe;
312
            operation=op;
313
        }
314
        public FeatureReference getFeatureReference() {
315
            return ref;
316
        }
317
        public void setFeatureReference(FeatureReference ref) {
318
            this.ref = ref;
319
        }
320
        public int getOperation() {
321
            return operation;
322
        }
323
        public void setOperation(int operation) {
324
            this.operation = operation;
325
        }
326
    }
327

    
328
}