Statistics
| Revision:

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

History | View | Annotate | Download (8.01 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.gvsig.fmap.dal.exception.DataException;
49
import org.gvsig.fmap.dal.feature.Feature;
50
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
51
import org.gvsig.fmap.dal.feature.FeatureIndex;
52
import org.gvsig.fmap.dal.feature.FeatureIndexes;
53
import org.gvsig.fmap.dal.feature.FeatureReference;
54
import org.gvsig.fmap.dal.feature.FeatureSet;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.dal.feature.FeatureType;
57
import org.gvsig.fmap.geom.GeometryLocator;
58
import org.gvsig.fmap.geom.primitive.Envelope;
59
import org.gvsig.tools.dispose.DisposableIterator;
60

    
61
/**
62
 * DOCUMENT ME!
63
 *
64
 * @author Vicente Caballero Navarro
65
 */
66
public class SpatialManager {
67
    protected boolean isFullExtentDirty = true;
68
        private FeatureStore featureStore;
69
        private FeatureIndex featureIndex = null;
70
        private Envelope originalEnvelope = null;
71
        private Envelope fullEnvelope = null;
72
        private ArrayList feaOperation=new ArrayList();
73
        private boolean noSpatialData = false;
74

    
75
        public SpatialManager(FeatureStore featureStore, Envelope originalEnvelope)
76
                        throws DataException {
77
                this.featureStore=featureStore;
78
                FeatureIndexes findexes=featureStore.getIndexes();
79
                // Comprobamos si hay algun campo espacial a manejar
80

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

    
106
                Iterator iterator = findexes.iterator();
107
                FeatureIndex index;
108
                while (iterator.hasNext()) {
109
                        index = (FeatureIndex) iterator.next();
110
                        if (index.getAttributeNames().size() == 1
111
                                        && index.getAttributeNames().contains(attr.getName())) {
112
                                featureIndex = index;
113
                                break;
114
                        }
115
                }
116

    
117
                if (featureIndex == null) {
118
                        featureIndex = featureStore.createIndex(fType, attr.getName(),
119
                                        "QuadtreeJts");
120
                }
121
        }
122

    
123

    
124

    
125
    /**
126
     * DOCUMENT ME!
127
     *
128
     * @param feature DOCUMENT ME!
129
     * @param oldFeature DOCUMENT ME!
130
     */
131
    public void updateFeature(Feature feature, Feature oldFeature) {
132
            if (noSpatialData) {
133
                        return;
134
                }
135
            if (featureIndex != null) {
136
                        featureIndex.delete(oldFeature);
137
                        feaOperation.add(new FeatureOperation(((DefaultFeature) oldFeature)
138
                                        .getReference(), FeatureOperation.DELETE));
139
                        featureIndex.insert(feature);
140
                        feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
141
                                        .getReference(), FeatureOperation.INSERT));
142
                        // } else {
143
                        // fullEnvelope.add(feature.getDefaultEnvelope());
144
                }
145
        isFullExtentDirty = true;
146
    }
147

    
148
    /**
149
     * DOCUMENT ME!
150
     *
151
     * @param feature DOCUMENT ME!
152
     */
153
    public void insertFeature(Feature feature) {
154
            if (noSpatialData) {
155
                        return;
156
                }
157
            if (featureIndex != null) {
158
                        featureIndex.insert(feature);
159
                        feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
160
                                        .getReference(), FeatureOperation.INSERT));
161
                } else if (!isFullExtentDirty) {
162
                        fullEnvelope.add(feature.getDefaultEnvelope());
163
                }
164
    }
165

    
166
    /**
167
     * DOCUMENT ME!
168
     *
169
     * @param feature DOCUMENT ME!
170
     */
171
    public void deleteFeature(Feature feature) {
172
            if (noSpatialData) {
173
                        return;
174
                }
175
            if (featureIndex != null) {
176
                        featureIndex.delete(feature);
177
                        feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
178
                                        .getReference(), FeatureOperation.DELETE));
179
                }
180
            isFullExtentDirty = true;
181
    }
182

    
183
        public void clear() {
184
        }
185

    
186
        public Envelope getEnvelope() throws DataException {
187
            if (noSpatialData) {
188
                        return null;
189
                }
190
            if (!isFullExtentDirty){
191
                    return this.fullEnvelope;
192
            }
193

    
194
                // FIXME in every changes when anyone ask for envelope it was regenerated.
195
                //       if we assume that the envelope may not be the minimum in edit mode
196
                //       this call must be very much faster
197

    
198

    
199
                FeatureAttributeDescriptor attr = featureStore.getDefaultFeatureType()
200
                                .getAttributeDescriptor(
201
                                                featureStore.getDefaultFeatureType()
202
                                                                .getDefaultGeometryAttributeIndex());
203
                Envelope fullEnvelope = null;
204
                try {
205
                        fullEnvelope = GeometryLocator.getGeometryManager().createEnvelope(
206
                                        attr.getGeometrySubType());
207
                } catch (Exception e) {
208
                        // FIXME Exception
209
                        throw new RuntimeException(e);
210
                }
211
                FeatureSet set = null;
212
                DisposableIterator iterator = null;
213
                try {
214
                        set = featureStore.getFeatureSet();
215
                        iterator = set.fastIterator();
216
                        while (iterator.hasNext()) {
217
                                Feature feature = (Feature) iterator.next();
218
                                Envelope envelope = feature.getDefaultEnvelope();
219
                                fullEnvelope.add(envelope);
220
                        }
221
                } finally {
222
                        if (iterator != null) {
223
                                iterator.dispose();
224
                        }
225
                        if (set != null) {
226
                                set.dispose();
227
                        }
228
                }
229
                this.fullEnvelope = fullEnvelope;
230
                this.isFullExtentDirty = false;
231
                return fullEnvelope;
232
        }
233

    
234

    
235

    
236
        public void cancelModifies() {
237
            if (noSpatialData) {
238
                        return;
239
                }
240
            if (featureIndex != null){
241
                        Iterator iterator=feaOperation.iterator();
242
                        while (iterator.hasNext()) {
243
                                try {
244
                                        FeatureOperation fo = (FeatureOperation) iterator.next();
245
                                        if (fo.getOperation()==FeatureOperation.INSERT){
246
                                                featureIndex.delete(fo.getFeatureReference().getFeature());
247
                                        }else if (fo.getOperation()==FeatureOperation.DELETE){
248
                                                featureIndex.insert(fo.getFeatureReference().getFeature());
249
                                        }
250
                                } catch (DataException e) {
251
                                        // TODO Auto-generated catch block
252
                                        e.printStackTrace();
253
                                }
254
                        }
255
                }
256
            fullEnvelope = originalEnvelope.getGeometry().getEnvelope();
257
                isFullExtentDirty = false;
258
        }
259

    
260
        private class FeatureOperation{
261
                final static int INSERT=0;
262
                final static int DELETE=1;
263
                private FeatureReference ref;
264
                private int operation;
265
                public FeatureOperation(FeatureReference fe,int op){
266
                        ref=fe;
267
                        operation=op;
268
                }
269
                public FeatureReference getFeatureReference() {
270
                        return ref;
271
                }
272
                public void setFeatureReference(FeatureReference ref) {
273
                        this.ref = ref;
274
                }
275
                public int getOperation() {
276
                        return operation;
277
                }
278
                public void setOperation(int operation) {
279
                        this.operation = operation;
280
                }
281
        }
282

    
283
}