Statistics
| Revision:

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

History | View | Annotate | Download (9.13 KB)

1 19399 vcaballero
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 24496 jmvivo
package org.gvsig.fmap.dal.feature.impl;
44 19399 vcaballero
45 25809 vcaballero
import java.util.ArrayList;
46 25565 vcaballero
import java.util.Iterator;
47
48 34130 jpiera
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50
51 25565 vcaballero
import org.gvsig.fmap.dal.exception.DataException;
52 24496 jmvivo
import org.gvsig.fmap.dal.feature.Feature;
53 25987 jmvivo
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
54 25565 vcaballero
import org.gvsig.fmap.dal.feature.FeatureIndex;
55 25809 vcaballero
import org.gvsig.fmap.dal.feature.FeatureIndexes;
56
import org.gvsig.fmap.dal.feature.FeatureReference;
57 28319 jmvivo
import org.gvsig.fmap.dal.feature.FeatureSet;
58 25565 vcaballero
import org.gvsig.fmap.dal.feature.FeatureStore;
59 25987 jmvivo
import org.gvsig.fmap.dal.feature.FeatureType;
60 28319 jmvivo
import org.gvsig.fmap.geom.GeometryLocator;
61 25277 jmvivo
import org.gvsig.fmap.geom.primitive.Envelope;
62 33205 cordinyana
import org.gvsig.tools.dispose.DisposableIterator;
63 23754 jjdelcerro
64 19399 vcaballero
/**
65
 * DOCUMENT ME!
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class SpatialManager {
70 34130 jpiera
    private static final Logger LOG = LoggerFactory.getLogger(SpatialManager.class);
71
72 19399 vcaballero
    protected boolean isFullExtentDirty = true;
73 25565 vcaballero
        private FeatureStore featureStore;
74
        private FeatureIndex featureIndex = null;
75 28319 jmvivo
        private Envelope originalEnvelope = null;
76
        private Envelope fullEnvelope = null;
77 25809 vcaballero
        private ArrayList feaOperation=new ArrayList();
78 25987 jmvivo
        private boolean noSpatialData = false;
79 19399 vcaballero
80 28319 jmvivo
        public SpatialManager(FeatureStore featureStore, Envelope originalEnvelope)
81
                        throws DataException {
82 25565 vcaballero
                this.featureStore=featureStore;
83 25809 vcaballero
                FeatureIndexes findexes=featureStore.getIndexes();
84 25987 jmvivo
                // 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 28319 jmvivo
                this.originalEnvelope = originalEnvelope;
95 35961 jpiera
                if ((originalEnvelope != null) && (!originalEnvelope.isEmpty())) {
96 28319 jmvivo
                        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 25987 jmvivo
111
                Iterator iterator = findexes.iterator();
112
                FeatureIndex index;
113 25809 vcaballero
                while (iterator.hasNext()) {
114 25987 jmvivo
                        index = (FeatureIndex) iterator.next();
115
                        if (index.getAttributeNames().size() == 1
116
                                        && index.getAttributeNames().contains(attr.getName())) {
117
                                featureIndex = index;
118 36190 cordinyana
                break;
119 25987 jmvivo
                        }
120 25565 vcaballero
                }
121 28319 jmvivo
122
                if (featureIndex == null) {
123 34130 jpiera
                    try{
124
                        featureIndex = featureStore.createIndex(fType, attr.getName(),
125 36207 cordinyana
                        "QuadtreeJts", null);
126 34130 jpiera
                    }catch (Exception e) {
127
                        LOG.info("It can not create an index", e);
128
                    }
129 25809 vcaballero
                }
130 36190 cordinyana
131
        if (featureIndex instanceof DefaultFeatureIndex) {
132
            ((DefaultFeatureIndex) featureIndex).setValid(true);
133
        }
134 25565 vcaballero
        }
135
136
137
138 19399 vcaballero
    /**
139
     * DOCUMENT ME!
140
     *
141
     * @param feature DOCUMENT ME!
142
     * @param oldFeature DOCUMENT ME!
143
     */
144 21045 jmvivo
    public void updateFeature(Feature feature, Feature oldFeature) {
145 25987 jmvivo
            if (noSpatialData) {
146
                        return;
147
                }
148 28319 jmvivo
            if (featureIndex != null) {
149
                        featureIndex.delete(oldFeature);
150
                        feaOperation.add(new FeatureOperation(((DefaultFeature) oldFeature)
151
                                        .getReference(), FeatureOperation.DELETE));
152
                        featureIndex.insert(feature);
153
                        feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
154
                                        .getReference(), FeatureOperation.INSERT));
155
                        // } else {
156
                        // fullEnvelope.add(feature.getDefaultEnvelope());
157
                }
158 19399 vcaballero
        isFullExtentDirty = true;
159
    }
160
161
    /**
162
     * DOCUMENT ME!
163
     *
164
     * @param feature DOCUMENT ME!
165
     */
166 21045 jmvivo
    public void insertFeature(Feature feature) {
167 25987 jmvivo
            if (noSpatialData) {
168
                        return;
169
                }
170 28319 jmvivo
            if (featureIndex != null) {
171
                        featureIndex.insert(feature);
172
                        feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
173
                                        .getReference(), FeatureOperation.INSERT));
174 36233 fdiaz
                    isFullExtentDirty = true;
175 28319 jmvivo
                } else if (!isFullExtentDirty) {
176
                        fullEnvelope.add(feature.getDefaultEnvelope());
177
                }
178 19399 vcaballero
    }
179
180
    /**
181
     * DOCUMENT ME!
182
     *
183
     * @param feature DOCUMENT ME!
184
     */
185 21045 jmvivo
    public void deleteFeature(Feature feature) {
186 25987 jmvivo
            if (noSpatialData) {
187
                        return;
188
                }
189 28319 jmvivo
            if (featureIndex != null) {
190
                        featureIndex.delete(feature);
191
                        feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
192
                                        .getReference(), FeatureOperation.DELETE));
193
                }
194 25565 vcaballero
            isFullExtentDirty = true;
195 19399 vcaballero
    }
196 19488 vcaballero
197
        public void clear() {
198
        }
199 25277 jmvivo
200 28319 jmvivo
        public Envelope getEnvelope() throws DataException {
201 25987 jmvivo
            if (noSpatialData) {
202
                        return null;
203
                }
204 28319 jmvivo
            if (!isFullExtentDirty){
205
                    return this.fullEnvelope;
206
            }
207
208
                // FIXME in every changes when anyone ask for envelope it was regenerated.
209
                //       if we assume that the envelope may not be the minimum in edit mode
210
                //       this call must be very much faster
211
212
213
                FeatureAttributeDescriptor attr = featureStore.getDefaultFeatureType()
214
                                .getAttributeDescriptor(
215
                                                featureStore.getDefaultFeatureType()
216
                                                                .getDefaultGeometryAttributeIndex());
217
                Envelope fullEnvelope = null;
218 35482 jpiera
219 28319 jmvivo
                FeatureSet set = null;
220
                DisposableIterator iterator = null;
221 25565 vcaballero
                try {
222 28319 jmvivo
                        set = featureStore.getFeatureSet();
223
                        iterator = set.fastIterator();
224 35482 jpiera
                        //First while to initialize the feature envelope
225 25565 vcaballero
                        while (iterator.hasNext()) {
226 35482 jpiera
                Feature feature = (Feature) iterator.next();
227
                Envelope envelope = feature.getDefaultEnvelope();
228
                if (envelope != null){
229
                    fullEnvelope = (Envelope)envelope.clone();
230
                    break;
231
                }
232
                        }
233
                        //Second while to add new evelopes tho the full envelope
234
                        while (iterator.hasNext()) {
235 25565 vcaballero
                                Feature feature = (Feature) iterator.next();
236 35482 jpiera
                                Envelope envelope = feature.getDefaultEnvelope();
237 34978 fdiaz
                                if(envelope!=null){
238
                                        fullEnvelope.add(envelope);
239
                                }
240 25565 vcaballero
                        }
241 35482 jpiera
                        //Creating an empty envelope by default
242
                        if (fullEnvelope == null){
243
                            fullEnvelope = GeometryLocator.getGeometryManager().createEnvelope(
244
                    attr.getGeometrySubType());
245
                        }
246
                } catch (Exception e) {
247
            throw new RuntimeException(e);
248
        } finally {
249 28319 jmvivo
                        if (iterator != null) {
250
                                iterator.dispose();
251
                        }
252
                        if (set != null) {
253
                                set.dispose();
254
                        }
255 25565 vcaballero
                }
256 28319 jmvivo
                this.fullEnvelope = fullEnvelope;
257
                this.isFullExtentDirty = false;
258 25565 vcaballero
                return fullEnvelope;
259 25277 jmvivo
        }
260 25809 vcaballero
261
262
263
        public void cancelModifies() {
264 25987 jmvivo
            if (noSpatialData) {
265
                        return;
266
                }
267 28319 jmvivo
            if (featureIndex != null){
268
                        Iterator iterator=feaOperation.iterator();
269
                        while (iterator.hasNext()) {
270
                                try {
271
                                        FeatureOperation fo = (FeatureOperation) iterator.next();
272
                                        if (fo.getOperation()==FeatureOperation.INSERT){
273
                                                featureIndex.delete(fo.getFeatureReference().getFeature());
274
                                        }else if (fo.getOperation()==FeatureOperation.DELETE){
275
                                                featureIndex.insert(fo.getFeatureReference().getFeature());
276
                                        }
277
                                } catch (DataException e) {
278
                                        // TODO Auto-generated catch block
279
                                        e.printStackTrace();
280 25809 vcaballero
                                }
281
                        }
282
                }
283 34978 fdiaz
            if (originalEnvelope!=null){
284
                    fullEnvelope = originalEnvelope.getGeometry().getEnvelope();
285
            } else {
286
                    fullEnvelope = null;
287
            }
288 28319 jmvivo
                isFullExtentDirty = false;
289 25809 vcaballero
        }
290 25987 jmvivo
291 25809 vcaballero
        private class FeatureOperation{
292
                final static int INSERT=0;
293
                final static int DELETE=1;
294
                private FeatureReference ref;
295
                private int operation;
296
                public FeatureOperation(FeatureReference fe,int op){
297
                        ref=fe;
298
                        operation=op;
299
                }
300
                public FeatureReference getFeatureReference() {
301
                        return ref;
302
                }
303
                public void setFeatureReference(FeatureReference ref) {
304
                        this.ref = ref;
305
                }
306
                public int getOperation() {
307
                        return operation;
308
                }
309
                public void setOperation(int operation) {
310
                        this.operation = operation;
311
                }
312
        }
313 28319 jmvivo
314 19399 vcaballero
}