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