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