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

History | View | Annotate | Download (9.25 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
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 40559 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * 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 40559 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20 40435 jjdelcerro
 *
21 40559 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40435 jjdelcerro
 */
24 43840 jjdelcerro
package org.gvsig.fmap.dal.feature.impl.editing.memory;
25 40435 jjdelcerro
26
import java.util.Iterator;
27 45837 fdiaz
import org.gvsig.fmap.dal.exception.CloneException;
28
import org.gvsig.fmap.dal.exception.CreateException;
29 40435 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
30 45837 fdiaz
import org.gvsig.fmap.dal.feature.EditableFeature;
31 40435 jjdelcerro
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 45837 fdiaz
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
37
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
38 40435 jjdelcerro
import org.gvsig.fmap.geom.GeometryLocator;
39 45837 fdiaz
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
40 40435 jjdelcerro
import org.gvsig.fmap.geom.primitive.Envelope;
41 47670 fdiaz
import org.gvsig.tools.logger.FilteredLogger;
42 40435 jjdelcerro
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44
45 47034 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
46 40435 jjdelcerro
public class SpatialManager {
47
    private static final Logger LOG = LoggerFactory.getLogger(SpatialManager.class);
48
49
    protected boolean isFullExtentDirty = true;
50 45837 fdiaz
    private DefaultFeatureStore featureStore;
51 47034 jjdelcerro
//    private FeatureIndex featureIndex = null;
52 40435 jjdelcerro
    private Envelope fullEnvelope = null;
53 47034 jjdelcerro
//    private List feaOperation=new ArrayList();
54 40435 jjdelcerro
    private boolean noSpatialData = false;
55
56 47034 jjdelcerro
//    public SpatialManager(DefaultFeatureStore featureStore, Envelope originalEnvelope)
57
//            throws DataException {
58
//        this(featureStore);
59
//    }
60
61
    public SpatialManager(DefaultFeatureStore featureStore)
62 40435 jjdelcerro
            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 45837 fdiaz
        FeatureAttributeDescriptor attr = fType.getDefaultGeometryAttribute();
74 47034 jjdelcerro
        this.isFullExtentDirty = true;
75 40435 jjdelcerro
        if (!fType.hasOID()) {
76
            return;
77
        }
78
79 47034 jjdelcerro
//        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 40435 jjdelcerro
    }
94
95
    public void updateFeature(Feature feature, Feature oldFeature) {
96
        if (noSpatialData) {
97
            return;
98
        }
99 45785 fdiaz
        try {
100 47034 jjdelcerro
            if( this.fullEnvelope==null ) {
101
                isFullExtentDirty = true;
102
            } else if( !isFullExtentDirty &&
103
                !this.fullEnvelope.contains(feature.getDefaultEnvelope()) ) {
104
                isFullExtentDirty = true;
105 45785 fdiaz
            }
106 47034 jjdelcerro
        } catch(Throwable t) {
107
            isFullExtentDirty = true;
108 45785 fdiaz
        }
109 40435 jjdelcerro
    }
110
111
    public void insertFeature(Feature feature) {
112 47034 jjdelcerro
        if (noSpatialData ) {
113 40435 jjdelcerro
            return;
114
        }
115 47034 jjdelcerro
        try {
116
            if( this.fullEnvelope==null ) {
117
                isFullExtentDirty = true;
118
            } else if( !isFullExtentDirty &&
119
                !this.fullEnvelope.contains(feature.getDefaultEnvelope()) ) {
120
                isFullExtentDirty = true;
121 40435 jjdelcerro
            }
122 47034 jjdelcerro
        } catch(Throwable t) {
123 40435 jjdelcerro
            isFullExtentDirty = true;
124 47034 jjdelcerro
        }
125 40435 jjdelcerro
    }
126
127
    public void deleteFeature(Feature feature) {
128
        if (noSpatialData) {
129
            return;
130
        }
131 47034 jjdelcerro
        // 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 40435 jjdelcerro
    }
135
136
    public void clear() {
137
    }
138
139
    public Envelope getEnvelope() throws DataException {
140
        if (noSpatialData) {
141
            return null;
142
        }
143 45837 fdiaz
        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 40435 jjdelcerro
160 45837 fdiaz
            FeatureManager featManager = featureStore.getFeatureManager();
161 40435 jjdelcerro
162 45837 fdiaz
            // 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 40435 jjdelcerro
165 45837 fdiaz
            Iterator<EditableFeature> inserted = featManager.getInsertedFeatures();
166
            if(inserted != null){
167 47670 fdiaz
                FilteredLogger logger = new FilteredLogger(LOG, "SpatialManagerGetEnvelope", 10);
168 45837 fdiaz
                while (inserted.hasNext()) {
169
                    EditableFeature next = inserted.next();
170 47670 fdiaz
                    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 40435 jjdelcerro
                }
182 45837 fdiaz
            }
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 40435 jjdelcerro
                }
190
            }
191 45837 fdiaz
            this.fullEnvelope = envelope;
192
            this.isFullExtentDirty = false;
193 40435 jjdelcerro
        }
194 45837 fdiaz
        return this.fullEnvelope;
195 40435 jjdelcerro
    }
196 45837 fdiaz
197 47034 jjdelcerro
//    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 40435 jjdelcerro
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 45837 fdiaz
241
    private Envelope getProviderEnvelope() throws DataException {
242
        FeatureStoreProvider provider = this.featureStore.getProvider();
243
        Envelope envelope = provider.getEnvelope();
244
        return envelope;
245
    }
246 40435 jjdelcerro
247
}