Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / SpatialManager.java @ 34978

History | View | Annotate | Download (8.39 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) {
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
                try {
214
                        fullEnvelope = GeometryLocator.getGeometryManager().createEnvelope(
215
                                        attr.getGeometrySubType());
216
                } catch (Exception e) {
217
                        // FIXME Exception
218
                        throw new RuntimeException(e);
219
                }
220
                FeatureSet set = null;
221
                DisposableIterator iterator = null;
222
                try {
223
                        set = featureStore.getFeatureSet();
224
                        iterator = set.fastIterator();
225
                        while (iterator.hasNext()) {
226
                                Feature feature = (Feature) iterator.next();
227
                                Envelope envelope = feature.getDefaultEnvelope();
228
                                if(envelope!=null){
229
                                        fullEnvelope.add(envelope);
230
                                }
231
                        }
232
                } finally {
233
                        if (iterator != null) {
234
                                iterator.dispose();
235
                        }
236
                        if (set != null) {
237
                                set.dispose();
238
                        }
239
                }
240
                this.fullEnvelope = fullEnvelope;
241
                this.isFullExtentDirty = false;
242
                return fullEnvelope;
243
        }
244

    
245

    
246

    
247
        public void cancelModifies() {
248
            if (noSpatialData) {
249
                        return;
250
                }
251
            if (featureIndex != null){
252
                        Iterator iterator=feaOperation.iterator();
253
                        while (iterator.hasNext()) {
254
                                try {
255
                                        FeatureOperation fo = (FeatureOperation) iterator.next();
256
                                        if (fo.getOperation()==FeatureOperation.INSERT){
257
                                                featureIndex.delete(fo.getFeatureReference().getFeature());
258
                                        }else if (fo.getOperation()==FeatureOperation.DELETE){
259
                                                featureIndex.insert(fo.getFeatureReference().getFeature());
260
                                        }
261
                                } catch (DataException e) {
262
                                        // TODO Auto-generated catch block
263
                                        e.printStackTrace();
264
                                }
265
                        }
266
                }
267
            if (originalEnvelope!=null){
268
                    fullEnvelope = originalEnvelope.getGeometry().getEnvelope();
269
            } else {
270
                    fullEnvelope = null;
271
            }
272
                isFullExtentDirty = false;
273
        }
274

    
275
        private class FeatureOperation{
276
                final static int INSERT=0;
277
                final static int DELETE=1;
278
                private FeatureReference ref;
279
                private int operation;
280
                public FeatureOperation(FeatureReference fe,int op){
281
                        ref=fe;
282
                        operation=op;
283
                }
284
                public FeatureReference getFeatureReference() {
285
                        return ref;
286
                }
287
                public void setFeatureReference(FeatureReference ref) {
288
                        this.ref = ref;
289
                }
290
                public int getOperation() {
291
                        return operation;
292
                }
293
                public void setOperation(int operation) {
294
                        this.operation = operation;
295
                }
296
        }
297

    
298
}