Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / editing / memory / SpatialManager.java @ 47670

History | View | Annotate | Download (9.25 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.impl.editing.memory;
25

    
26
import java.util.Iterator;
27
import org.gvsig.fmap.dal.exception.CloneException;
28
import org.gvsig.fmap.dal.exception.CreateException;
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.feature.EditableFeature;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
33
import org.gvsig.fmap.dal.feature.FeatureIndexes;
34
import org.gvsig.fmap.dal.feature.FeatureReference;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
37
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
38
import org.gvsig.fmap.geom.GeometryLocator;
39
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
40
import org.gvsig.fmap.geom.primitive.Envelope;
41
import org.gvsig.tools.logger.FilteredLogger;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
@SuppressWarnings("UseSpecificCatch")
46
public class SpatialManager {
47
    private static final Logger LOG = LoggerFactory.getLogger(SpatialManager.class);
48
    
49
    protected boolean isFullExtentDirty = true;
50
    private DefaultFeatureStore featureStore;
51
//    private FeatureIndex featureIndex = null;
52
    private Envelope fullEnvelope = null;
53
//    private List feaOperation=new ArrayList();
54
    private boolean noSpatialData = false;
55

    
56
//    public SpatialManager(DefaultFeatureStore featureStore, Envelope originalEnvelope)
57
//            throws DataException {
58
//        this(featureStore);
59
//    }
60
    
61
    public SpatialManager(DefaultFeatureStore featureStore)
62
            throws DataException {
63
        this.featureStore=featureStore;
64
        FeatureIndexes findexes=featureStore.getIndexes();
65
        // Comprobamos si hay algun campo espacial a manejar
66

    
67
        FeatureType fType = this.featureStore.getDefaultFeatureType();
68
        // TODO Multy FType !!
69
        if (fType.getDefaultGeometryAttributeIndex() < 0) {
70
            noSpatialData = true;
71
            return;
72
        }
73
        FeatureAttributeDescriptor attr = fType.getDefaultGeometryAttribute();
74
        this.isFullExtentDirty = true;
75
        if (!fType.hasOID()) {
76
            return;
77
        }
78

    
79
//        Iterator iterator = findexes.iterator();
80
//        FeatureIndex index;
81
//        while (iterator.hasNext()) {
82
//            index = (FeatureIndex) iterator.next();
83
//            if (index.getAttributeNames().size() == 1
84
//                    && index.getAttributeNames().contains(attr.getName())) {
85
//                featureIndex = index;
86
//                break;
87
//            }
88
//        }
89
//
90
//        if (featureIndex instanceof DefaultFeatureIndex) {
91
//            ((DefaultFeatureIndex) featureIndex).setValid(true);
92
//        }
93
    }
94

    
95
    public void updateFeature(Feature feature, Feature oldFeature) {
96
        if (noSpatialData) {
97
            return;
98
        }
99
        try {
100
            if( this.fullEnvelope==null ) {
101
                isFullExtentDirty = true;
102
            } else if( !isFullExtentDirty &&  
103
                !this.fullEnvelope.contains(feature.getDefaultEnvelope()) ) {
104
                isFullExtentDirty = true;
105
            }
106
        } catch(Throwable t) {
107
            isFullExtentDirty = true;
108
        }
109
    }
110

    
111
    public void insertFeature(Feature feature) {
112
        if (noSpatialData ) {
113
            return;
114
        }
115
        try {
116
            if( this.fullEnvelope==null ) {
117
                isFullExtentDirty = true;
118
            } else if( !isFullExtentDirty &&  
119
                !this.fullEnvelope.contains(feature.getDefaultEnvelope()) ) {
120
                isFullExtentDirty = true;
121
            }
122
        } catch(Throwable t) {
123
            isFullExtentDirty = true;
124
        }
125
    }
126

    
127
    public void deleteFeature(Feature feature) {
128
        if (noSpatialData) {
129
            return;
130
        }
131
        // Si en edicion se provoca un disminucion en el envelope. no hacemos nada 
132
        // para premiar la velocidad de pintado. Ya se resolvera al finalizar edicion.
133
//        isFullExtentDirty = true;
134
    }
135

    
136
    public void clear() {
137
    }
138

    
139
    public Envelope getEnvelope() throws DataException {
140
        if (noSpatialData) {
141
            return null;
142
        }
143
        if(this.isFullExtentDirty) {
144
            Envelope envelope = getProviderEnvelope();
145
            if (envelope == null) {
146
                try {
147
                    envelope = GeometryLocator.getGeometryManager().createEnvelope(
148
                            this.featureStore.getDefaultFeatureType().getDefaultGeometryAttribute().getGeomType().getSubType());
149
                } catch (CreateEnvelopeException ex) {
150
                    throw new CreateException("envelope", ex);
151
                }
152
            } else {
153
                try {
154
                    envelope = (Envelope) envelope.clone();
155
                } catch (Exception ex) {
156
                    throw new CloneException(ex);
157
                }
158
            }
159

    
160
            FeatureManager featManager = featureStore.getFeatureManager();
161

    
162
            // Si en edicion se provoca un disminucion en el envelope. no hacemos nada 
163
            // para premiar la velocidad de pintado. Ya se resolvera al finalizar edicion.
164

    
165
            Iterator<EditableFeature> inserted = featManager.getInsertedFeatures();
166
            if(inserted != null){
167
                FilteredLogger logger = new FilteredLogger(LOG, "SpatialManagerGetEnvelope", 10);
168
                while (inserted.hasNext()) {
169
                    EditableFeature next = inserted.next();
170
                    try {
171
                        envelope.add(next.getDefaultEnvelope());
172
                    } catch (Exception e) {
173
                        String s = "unknown";
174
                        try {
175
                            s = next.getReference().toString();
176
                        } catch (Exception e2){
177
                            //Do nothing
178
                        }
179
                        logger.warn("Can't add envelope from "+s+" feature", e);
180
                    }
181
                }
182
            }
183

    
184
            Iterator<EditableFeature> updated = featManager.getUpdatedFeatures();
185
            if(updated != null){
186
                while (updated.hasNext()) {
187
                    EditableFeature next = updated.next();
188
                    envelope.add(next.getDefaultEnvelope());
189
                }
190
            }
191
            this.fullEnvelope = envelope;
192
            this.isFullExtentDirty = false;
193
        }
194
        return this.fullEnvelope;
195
    }
196
    
197
//    public void cancelModifies() {
198
//        if (noSpatialData) {
199
//            return;
200
//        }
201
//        if (featureIndex != null){
202
//            for (int i = feaOperation.size()-1 ; i>=0 ; i--){
203
//                try {
204
//                    FeatureOperation fo = (FeatureOperation) feaOperation.get(i);
205
//                    if (fo.getOperation() == FeatureOperation.INSERT){                     
206
//                        featureIndex.delete(fo.getFeatureReference().getFeature());
207
//                    }else if (fo.getOperation() == FeatureOperation.DELETE){
208
//                        featureIndex.insert(fo.getFeatureReference().getFeature());
209
//                    }
210
//                } catch (DataException e) {
211
//                    LOG.error("Error canceling the edition", e);
212
//                }           
213
//            }
214
//        }
215
//        isFullExtentDirty = true;
216
//    }
217

    
218
    private class FeatureOperation{
219
        final static int INSERT=0;
220
        final static int DELETE=1;
221
        private FeatureReference ref;
222
        private int operation;
223
        public FeatureOperation(FeatureReference fe,int op){
224
            ref=fe;
225
            operation=op;
226
        }
227
        public FeatureReference getFeatureReference() {
228
            return ref;
229
        }
230
        public void setFeatureReference(FeatureReference ref) {
231
            this.ref = ref;
232
        }
233
        public int getOperation() {
234
            return operation;
235
        }
236
        public void setOperation(int operation) {
237
            this.operation = operation;
238
        }
239
    }
240
    
241
    private Envelope getProviderEnvelope() throws DataException {
242
        FeatureStoreProvider provider = this.featureStore.getProvider();
243
        Envelope envelope = provider.getEnvelope();
244
        return envelope;
245
    }
246

    
247
}