23 |
23 |
*/
|
24 |
24 |
package org.gvsig.fmap.dal.feature.impl.editing.memory;
|
25 |
25 |
|
26 |
|
import java.util.ArrayList;
|
27 |
26 |
import java.util.Iterator;
|
28 |
|
import java.util.logging.Level;
|
29 |
27 |
import org.gvsig.fmap.dal.exception.CloneException;
|
30 |
28 |
import org.gvsig.fmap.dal.exception.CreateException;
|
31 |
29 |
import org.gvsig.fmap.dal.exception.DataException;
|
32 |
30 |
import org.gvsig.fmap.dal.feature.EditableFeature;
|
33 |
31 |
import org.gvsig.fmap.dal.feature.Feature;
|
34 |
32 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
|
35 |
|
import org.gvsig.fmap.dal.feature.FeatureIndex;
|
36 |
33 |
import org.gvsig.fmap.dal.feature.FeatureIndexes;
|
37 |
34 |
import org.gvsig.fmap.dal.feature.FeatureReference;
|
38 |
35 |
import org.gvsig.fmap.dal.feature.FeatureType;
|
39 |
|
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
|
40 |
|
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureIndex;
|
41 |
36 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
|
42 |
37 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
|
43 |
38 |
import org.gvsig.fmap.geom.GeometryLocator;
|
... | ... | |
46 |
41 |
import org.slf4j.Logger;
|
47 |
42 |
import org.slf4j.LoggerFactory;
|
48 |
43 |
|
49 |
|
/**
|
50 |
|
* DOCUMENT ME!
|
51 |
|
*
|
52 |
|
* @author Vicente Caballero Navarro
|
53 |
|
*/
|
|
44 |
@SuppressWarnings("UseSpecificCatch")
|
54 |
45 |
public class SpatialManager {
|
55 |
46 |
private static final Logger LOG = LoggerFactory.getLogger(SpatialManager.class);
|
56 |
47 |
|
57 |
48 |
protected boolean isFullExtentDirty = true;
|
58 |
49 |
private DefaultFeatureStore featureStore;
|
59 |
|
private FeatureIndex featureIndex = null;
|
60 |
|
private Envelope originalEnvelope = null;
|
|
50 |
// private FeatureIndex featureIndex = null;
|
61 |
51 |
private Envelope fullEnvelope = null;
|
62 |
|
private ArrayList feaOperation=new ArrayList();
|
|
52 |
// private List feaOperation=new ArrayList();
|
63 |
53 |
private boolean noSpatialData = false;
|
64 |
54 |
|
65 |
|
public SpatialManager(DefaultFeatureStore featureStore, Envelope originalEnvelope)
|
|
55 |
// public SpatialManager(DefaultFeatureStore featureStore, Envelope originalEnvelope)
|
|
56 |
// throws DataException {
|
|
57 |
// this(featureStore);
|
|
58 |
// }
|
|
59 |
|
|
60 |
public SpatialManager(DefaultFeatureStore featureStore)
|
66 |
61 |
throws DataException {
|
67 |
62 |
this.featureStore=featureStore;
|
68 |
63 |
FeatureIndexes findexes=featureStore.getIndexes();
|
... | ... | |
75 |
70 |
return;
|
76 |
71 |
}
|
77 |
72 |
FeatureAttributeDescriptor attr = fType.getDefaultGeometryAttribute();
|
78 |
|
this.originalEnvelope = originalEnvelope;
|
79 |
|
if ((originalEnvelope != null) && (!originalEnvelope.isEmpty())) {
|
80 |
|
this.fullEnvelope = originalEnvelope.getGeometry().getEnvelope();
|
81 |
|
} else {
|
82 |
|
try {
|
83 |
|
this.fullEnvelope = GeometryLocator.getGeometryManager()
|
84 |
|
.createEnvelope(attr.getGeometrySubType());
|
85 |
|
} catch (Exception e) {
|
86 |
|
// FIXME Excpetion
|
87 |
|
throw new RuntimeException(e);
|
88 |
|
}
|
89 |
|
}
|
|
73 |
this.isFullExtentDirty = true;
|
90 |
74 |
if (!fType.hasOID()) {
|
91 |
75 |
return;
|
92 |
76 |
}
|
93 |
77 |
|
94 |
|
Iterator iterator = findexes.iterator();
|
95 |
|
FeatureIndex index;
|
96 |
|
while (iterator.hasNext()) {
|
97 |
|
index = (FeatureIndex) iterator.next();
|
98 |
|
if (index.getAttributeNames().size() == 1
|
99 |
|
&& index.getAttributeNames().contains(attr.getName())) {
|
100 |
|
featureIndex = index;
|
101 |
|
break;
|
102 |
|
}
|
103 |
|
}
|
104 |
|
|
105 |
|
if (featureIndex instanceof DefaultFeatureIndex) {
|
106 |
|
((DefaultFeatureIndex) featureIndex).setValid(true);
|
107 |
|
}
|
|
78 |
// Iterator iterator = findexes.iterator();
|
|
79 |
// FeatureIndex index;
|
|
80 |
// while (iterator.hasNext()) {
|
|
81 |
// index = (FeatureIndex) iterator.next();
|
|
82 |
// if (index.getAttributeNames().size() == 1
|
|
83 |
// && index.getAttributeNames().contains(attr.getName())) {
|
|
84 |
// featureIndex = index;
|
|
85 |
// break;
|
|
86 |
// }
|
|
87 |
// }
|
|
88 |
//
|
|
89 |
// if (featureIndex instanceof DefaultFeatureIndex) {
|
|
90 |
// ((DefaultFeatureIndex) featureIndex).setValid(true);
|
|
91 |
// }
|
108 |
92 |
}
|
109 |
93 |
|
110 |
|
|
111 |
|
|
112 |
|
/**
|
113 |
|
* DOCUMENT ME!
|
114 |
|
*
|
115 |
|
* @param feature DOCUMENT ME!
|
116 |
|
* @param oldFeature DOCUMENT ME!
|
117 |
|
*/
|
118 |
94 |
public void updateFeature(Feature feature, Feature oldFeature) {
|
119 |
95 |
if (noSpatialData) {
|
120 |
96 |
return;
|
121 |
97 |
}
|
122 |
98 |
try {
|
123 |
|
if (oldFeature != null) {
|
124 |
|
if (featureIndex != null) {
|
125 |
|
featureIndex.delete(oldFeature);
|
126 |
|
}
|
127 |
|
final FeatureReference reference = oldFeature.getReference();
|
128 |
|
feaOperation.add(new FeatureOperation(reference, FeatureOperation.DELETE));
|
|
99 |
if( this.fullEnvelope==null ) {
|
|
100 |
isFullExtentDirty = true;
|
|
101 |
} else if( !isFullExtentDirty &&
|
|
102 |
!this.fullEnvelope.contains(feature.getDefaultEnvelope()) ) {
|
|
103 |
isFullExtentDirty = true;
|
129 |
104 |
}
|
130 |
|
if (featureIndex != null) {
|
131 |
|
featureIndex.insert(feature);
|
132 |
|
}
|
133 |
|
feaOperation.add(
|
134 |
|
new FeatureOperation(
|
135 |
|
feature.getReference(),
|
136 |
|
FeatureOperation.INSERT
|
137 |
|
)
|
138 |
|
);
|
139 |
|
} catch (DataException e) {
|
140 |
|
throw new RuntimeException("Exception updating feature: "
|
141 |
|
+ feature, e);
|
|
105 |
} catch(Throwable t) {
|
|
106 |
isFullExtentDirty = true;
|
142 |
107 |
}
|
143 |
|
isFullExtentDirty = true;
|
144 |
108 |
}
|
145 |
109 |
|
146 |
|
/**
|
147 |
|
* DOCUMENT ME!
|
148 |
|
*
|
149 |
|
* @param feature DOCUMENT ME!
|
150 |
|
*/
|
151 |
110 |
public void insertFeature(Feature feature) {
|
152 |
|
if (noSpatialData) {
|
|
111 |
if (noSpatialData ) {
|
153 |
112 |
return;
|
154 |
113 |
}
|
155 |
|
try {
|
156 |
|
if (featureIndex != null) {
|
157 |
|
featureIndex.insert(feature);
|
158 |
|
}
|
159 |
|
feaOperation.add(new FeatureOperation(
|
160 |
|
((DefaultFeature) feature).getReference(),
|
161 |
|
FeatureOperation.INSERT));
|
162 |
|
} catch (DataException e) {
|
163 |
|
throw new RuntimeException("Exception inserting feature: "
|
164 |
|
+ feature, e);
|
|
114 |
try {
|
|
115 |
if( this.fullEnvelope==null ) {
|
|
116 |
isFullExtentDirty = true;
|
|
117 |
} else if( !isFullExtentDirty &&
|
|
118 |
!this.fullEnvelope.contains(feature.getDefaultEnvelope()) ) {
|
|
119 |
isFullExtentDirty = true;
|
165 |
120 |
}
|
|
121 |
} catch(Throwable t) {
|
166 |
122 |
isFullExtentDirty = true;
|
|
123 |
}
|
167 |
124 |
}
|
168 |
125 |
|
169 |
|
/**
|
170 |
|
* DOCUMENT ME!
|
171 |
|
*
|
172 |
|
* @param feature DOCUMENT ME!
|
173 |
|
*/
|
174 |
126 |
public void deleteFeature(Feature feature) {
|
175 |
127 |
if (noSpatialData) {
|
176 |
128 |
return;
|
177 |
129 |
}
|
178 |
|
try {
|
179 |
|
if (featureIndex != null) {
|
180 |
|
featureIndex.delete(feature);
|
181 |
|
}
|
182 |
|
feaOperation.add(new FeatureOperation(((DefaultFeature) feature)
|
183 |
|
.getReference(), FeatureOperation.DELETE));
|
184 |
|
} catch (DataException e) {
|
185 |
|
throw new RuntimeException("Exception deleting feature: "
|
186 |
|
+ feature, e);
|
187 |
|
}
|
188 |
|
isFullExtentDirty = true;
|
|
130 |
// Si en edicion se provoca un disminucion en el envelope. no hacemos nada
|
|
131 |
// para premiar la velocidad de pintado. Ya se resolvera al finalizar edicion.
|
|
132 |
// isFullExtentDirty = true;
|
189 |
133 |
}
|
190 |
134 |
|
191 |
135 |
public void clear() {
|
... | ... | |
196 |
140 |
return null;
|
197 |
141 |
}
|
198 |
142 |
if(this.isFullExtentDirty) {
|
199 |
|
FeatureAttributeDescriptor attr = featureStore.getDefaultFeatureType().getDefaultGeometryAttribute();
|
200 |
|
|
201 |
143 |
Envelope envelope = getProviderEnvelope();
|
202 |
144 |
if (envelope == null) {
|
203 |
145 |
try {
|
... | ... | |
240 |
182 |
return this.fullEnvelope;
|
241 |
183 |
}
|
242 |
184 |
|
243 |
|
public void cancelModifies() {
|
244 |
|
if (noSpatialData) {
|
245 |
|
return;
|
246 |
|
}
|
247 |
|
if (featureIndex != null){
|
248 |
|
for (int i = feaOperation.size()-1 ; i>=0 ; i--){
|
249 |
|
try {
|
250 |
|
FeatureOperation fo = (FeatureOperation) feaOperation.get(i);
|
251 |
|
if (fo.getOperation() == FeatureOperation.INSERT){
|
252 |
|
featureIndex.delete(fo.getFeatureReference().getFeature());
|
253 |
|
}else if (fo.getOperation() == FeatureOperation.DELETE){
|
254 |
|
featureIndex.insert(fo.getFeatureReference().getFeature());
|
255 |
|
}
|
256 |
|
} catch (DataException e) {
|
257 |
|
LOG.error("Error canceling the edition", e);
|
258 |
|
}
|
259 |
|
}
|
260 |
|
}
|
261 |
|
if (originalEnvelope!=null){
|
262 |
|
try {
|
263 |
|
fullEnvelope = (Envelope) originalEnvelope.clone();
|
264 |
|
} catch (CloneNotSupportedException e) {
|
265 |
|
// Should never happen
|
266 |
|
LOG.error("While cloning envelope", e);
|
267 |
|
}
|
268 |
|
} else {
|
269 |
|
fullEnvelope = null;
|
270 |
|
}
|
271 |
|
isFullExtentDirty = false;
|
272 |
|
}
|
|
185 |
// public void cancelModifies() {
|
|
186 |
// if (noSpatialData) {
|
|
187 |
// return;
|
|
188 |
// }
|
|
189 |
// if (featureIndex != null){
|
|
190 |
// for (int i = feaOperation.size()-1 ; i>=0 ; i--){
|
|
191 |
// try {
|
|
192 |
// FeatureOperation fo = (FeatureOperation) feaOperation.get(i);
|
|
193 |
// if (fo.getOperation() == FeatureOperation.INSERT){
|
|
194 |
// featureIndex.delete(fo.getFeatureReference().getFeature());
|
|
195 |
// }else if (fo.getOperation() == FeatureOperation.DELETE){
|
|
196 |
// featureIndex.insert(fo.getFeatureReference().getFeature());
|
|
197 |
// }
|
|
198 |
// } catch (DataException e) {
|
|
199 |
// LOG.error("Error canceling the edition", e);
|
|
200 |
// }
|
|
201 |
// }
|
|
202 |
// }
|
|
203 |
// isFullExtentDirty = true;
|
|
204 |
// }
|
273 |
205 |
|
274 |
206 |
private class FeatureOperation{
|
275 |
207 |
final static int INSERT=0;
|