Statistics
| Revision:

svn-gvsig-desktop / tags / dal_time_support_Build_1 / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / SpatialManager.java @ 36111

History | View | Annotate | Download (8.93 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");
126
                    }catch (Exception e) {
127
                        LOG.info("It can not create an index", e);
128
                    }
129
                }
130
        }
131

    
132

    
133

    
134
    /**
135
     * DOCUMENT ME!
136
     *
137
     * @param feature DOCUMENT ME!
138
     * @param oldFeature DOCUMENT ME!
139
     */
140
    public void updateFeature(Feature feature, Feature oldFeature) {
141
            if (noSpatialData) {
142
                        return;
143
                }
144
            if (featureIndex != null) {
145
                        featureIndex.delete(oldFeature);
146
                        feaOperation.add(new FeatureOperation(((DefaultFeature) oldFeature)
147
                                        .getReference(), FeatureOperation.DELETE));
148
                        featureIndex.insert(feature);
149
                        feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
150
                                        .getReference(), FeatureOperation.INSERT));
151
                        // } else {
152
                        // fullEnvelope.add(feature.getDefaultEnvelope());
153
                }
154
        isFullExtentDirty = true;
155
    }
156

    
157
    /**
158
     * DOCUMENT ME!
159
     *
160
     * @param feature DOCUMENT ME!
161
     */
162
    public void insertFeature(Feature feature) {
163
            if (noSpatialData) {
164
                        return;
165
                }
166
            if (featureIndex != null) {
167
                        featureIndex.insert(feature);
168
                        feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
169
                                        .getReference(), FeatureOperation.INSERT));
170
                } else if (!isFullExtentDirty) {
171
                        fullEnvelope.add(feature.getDefaultEnvelope());
172
                }
173
    }
174

    
175
    /**
176
     * DOCUMENT ME!
177
     *
178
     * @param feature DOCUMENT ME!
179
     */
180
    public void deleteFeature(Feature feature) {
181
            if (noSpatialData) {
182
                        return;
183
                }
184
            if (featureIndex != null) {
185
                        featureIndex.delete(feature);
186
                        feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
187
                                        .getReference(), FeatureOperation.DELETE));
188
                }
189
            isFullExtentDirty = true;
190
    }
191

    
192
        public void clear() {
193
        }
194

    
195
        public Envelope getEnvelope() throws DataException {
196
            if (noSpatialData) {
197
                        return null;
198
                }
199
            if (!isFullExtentDirty){
200
                    return this.fullEnvelope;
201
            }
202

    
203
                // FIXME in every changes when anyone ask for envelope it was regenerated.
204
                //       if we assume that the envelope may not be the minimum in edit mode
205
                //       this call must be very much faster
206

    
207

    
208
                FeatureAttributeDescriptor attr = featureStore.getDefaultFeatureType()
209
                                .getAttributeDescriptor(
210
                                                featureStore.getDefaultFeatureType()
211
                                                                .getDefaultGeometryAttributeIndex());
212
                Envelope fullEnvelope = null;
213

    
214
                FeatureSet set = null;
215
                DisposableIterator iterator = null;
216
                try {
217
                        set = featureStore.getFeatureSet();
218
                        iterator = set.fastIterator();
219
                        //First while to initialize the feature envelope
220
                        while (iterator.hasNext()) {
221
                Feature feature = (Feature) iterator.next();
222
                Envelope envelope = feature.getDefaultEnvelope();
223
                if (envelope != null){
224
                    fullEnvelope = (Envelope)envelope.clone();
225
                    break;
226
                }
227
                        }                        
228
                        //Second while to add new evelopes tho the full envelope
229
                        while (iterator.hasNext()) {
230
                                Feature feature = (Feature) iterator.next();
231
                                Envelope envelope = feature.getDefaultEnvelope();                        
232
                                if(envelope!=null){
233
                                        fullEnvelope.add(envelope);
234
                                }
235
                        }
236
                        //Creating an empty envelope by default
237
                        if (fullEnvelope == null){
238
                            fullEnvelope = GeometryLocator.getGeometryManager().createEnvelope(
239
                    attr.getGeometrySubType());
240
                        }
241
                } catch (Exception e) {
242
            throw new RuntimeException(e);
243
        } finally {
244
                        if (iterator != null) {
245
                                iterator.dispose();
246
                        }
247
                        if (set != null) {
248
                                set.dispose();
249
                        }
250
                }
251
                this.fullEnvelope = fullEnvelope;
252
                this.isFullExtentDirty = false;
253
                return fullEnvelope;
254
        }
255

    
256

    
257

    
258
        public void cancelModifies() {
259
            if (noSpatialData) {
260
                        return;
261
                }
262
            if (featureIndex != null){
263
                        Iterator iterator=feaOperation.iterator();
264
                        while (iterator.hasNext()) {
265
                                try {
266
                                        FeatureOperation fo = (FeatureOperation) iterator.next();
267
                                        if (fo.getOperation()==FeatureOperation.INSERT){
268
                                                featureIndex.delete(fo.getFeatureReference().getFeature());
269
                                        }else if (fo.getOperation()==FeatureOperation.DELETE){
270
                                                featureIndex.insert(fo.getFeatureReference().getFeature());
271
                                        }
272
                                } catch (DataException e) {
273
                                        // TODO Auto-generated catch block
274
                                        e.printStackTrace();
275
                                }
276
                        }
277
                }
278
            if (originalEnvelope!=null){
279
                    fullEnvelope = originalEnvelope.getGeometry().getEnvelope();
280
            } else {
281
                    fullEnvelope = null;
282
            }
283
                isFullExtentDirty = false;
284
        }
285

    
286
        private class FeatureOperation{
287
                final static int INSERT=0;
288
                final static int DELETE=1;
289
                private FeatureReference ref;
290
                private int operation;
291
                public FeatureOperation(FeatureReference fe,int op){
292
                        ref=fe;
293
                        operation=op;
294
                }
295
                public FeatureReference getFeatureReference() {
296
                        return ref;
297
                }
298
                public void setFeatureReference(FeatureReference ref) {
299
                        this.ref = ref;
300
                }
301
                public int getOperation() {
302
                        return operation;
303
                }
304
                public void setOperation(int operation) {
305
                        this.operation = operation;
306
                }
307
        }
308

    
309
}