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 / WrappedSpatialIndex.java
History | View | Annotate | Download (7.45 KB)
1 | 43371 | fdiaz | /* gvSIG. Desktop Geographic Information System.
|
---|---|---|---|
2 | *
|
||
3 | * Copyright ? 2007-2017 gvSIG Association
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
18 | * MA 02110-1301, USA.
|
||
19 | *
|
||
20 | * For any additional information, do not hesitate to contact us
|
||
21 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
22 | */
|
||
23 | package org.gvsig.fmap.dal.feature.impl; |
||
24 | |||
25 | import java.util.ArrayList; |
||
26 | import java.util.Iterator; |
||
27 | import java.util.List; |
||
28 | import org.gvsig.fmap.dal.feature.Feature; |
||
29 | import org.gvsig.fmap.dal.feature.FeatureReference; |
||
30 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
31 | 45647 | fdiaz | import org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferenceFactory; |
32 | 43371 | fdiaz | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
33 | import org.gvsig.fmap.geom.Geometry; |
||
34 | import org.gvsig.fmap.geom.SpatialIndex; |
||
35 | import org.gvsig.fmap.geom.SpatialIndexFactory; |
||
36 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
37 | import org.gvsig.tools.exception.BaseException; |
||
38 | import org.gvsig.tools.service.Manager; |
||
39 | import org.gvsig.tools.visitor.VisitCanceledException; |
||
40 | import org.gvsig.tools.visitor.Visitor; |
||
41 | |||
42 | /**
|
||
43 | * @author fdiaz
|
||
44 | *
|
||
45 | */
|
||
46 | public class WrappedSpatialIndex implements SpatialIndex { |
||
47 | |||
48 | private final SpatialIndex index; |
||
49 | private final FeatureStore store; |
||
50 | |||
51 | protected class Oid2FeatureReferenceVisitor implements Visitor { |
||
52 | |||
53 | private final Visitor wrapedVisitor; |
||
54 | |||
55 | public Oid2FeatureReferenceVisitor(Visitor wrapedVisitor) {
|
||
56 | this.wrapedVisitor = wrapedVisitor;
|
||
57 | } |
||
58 | |||
59 | @Override
|
||
60 | public void visit(Object oid) throws VisitCanceledException, BaseException { |
||
61 | 45647 | fdiaz | FeatureReference ref = FeatureReferenceFactory.createFromOID(store, oid); |
62 | 43371 | fdiaz | wrapedVisitor.visit(ref); |
63 | } |
||
64 | |||
65 | } |
||
66 | |||
67 | 44041 | jjdelcerro | protected class Oid2FeatureReferenceIterator implements Iterator<FeatureReference> { |
68 | 43371 | fdiaz | |
69 | private final Iterator wrapedIterator; |
||
70 | |||
71 | public Oid2FeatureReferenceIterator(Iterator wrapedIterator) { |
||
72 | this.wrapedIterator = wrapedIterator;
|
||
73 | } |
||
74 | |||
75 | @Override
|
||
76 | public boolean hasNext() { |
||
77 | return this.wrapedIterator.hasNext(); |
||
78 | } |
||
79 | |||
80 | @Override
|
||
81 | 44041 | jjdelcerro | public FeatureReference next() {
|
82 | 43371 | fdiaz | Object oid = this.wrapedIterator.next(); |
83 | 45647 | fdiaz | FeatureReference ref = FeatureReferenceFactory.createFromOID( |
84 | store, oid); |
||
85 | 43371 | fdiaz | return ref;
|
86 | } |
||
87 | |||
88 | @Override
|
||
89 | public void remove() { |
||
90 | this.wrapedIterator.remove();
|
||
91 | } |
||
92 | } |
||
93 | |||
94 | public WrappedSpatialIndex(SpatialIndex index, FeatureStore store) {
|
||
95 | this.index = index;
|
||
96 | this.store = store;
|
||
97 | } |
||
98 | |||
99 | @Override
|
||
100 | public SpatialIndexFactory getFactory() {
|
||
101 | return this.index.getFactory(); |
||
102 | } |
||
103 | |||
104 | @Override
|
||
105 | public void open() { |
||
106 | this.index.open();
|
||
107 | } |
||
108 | |||
109 | @Override
|
||
110 | public void close() { |
||
111 | this.index.close();
|
||
112 | } |
||
113 | |||
114 | @Override
|
||
115 | public void query(Envelope envelope, Visitor visitor) { |
||
116 | this.index.query(envelope, new Oid2FeatureReferenceVisitor(visitor)); |
||
117 | } |
||
118 | |||
119 | @Override
|
||
120 | public void query(Geometry geom, Visitor visitor) { |
||
121 | this.index.query(geom, new Oid2FeatureReferenceVisitor(visitor)); |
||
122 | } |
||
123 | |||
124 | @Override
|
||
125 | 44041 | jjdelcerro | public Iterator<FeatureReference> query(Envelope envelope, long limit) { |
126 | 43371 | fdiaz | return new Oid2FeatureReferenceIterator(this.index.query(envelope, limit)); |
127 | } |
||
128 | |||
129 | @Override
|
||
130 | 44041 | jjdelcerro | public Iterator<FeatureReference> query(Envelope envelope) { |
131 | 43371 | fdiaz | return new Oid2FeatureReferenceIterator(this.index.query(envelope)); |
132 | } |
||
133 | |||
134 | @Override
|
||
135 | 44041 | jjdelcerro | public Iterator<FeatureReference> query(Geometry geom, long limit) { |
136 | 43371 | fdiaz | return new Oid2FeatureReferenceIterator(this.index.query(geom, limit)); |
137 | } |
||
138 | |||
139 | @Override
|
||
140 | 44041 | jjdelcerro | public Iterator<FeatureReference> query(Geometry geom) { |
141 | 43371 | fdiaz | return new Oid2FeatureReferenceIterator(this.index.query(geom)); |
142 | } |
||
143 | |||
144 | @Override
|
||
145 | 44041 | jjdelcerro | public Iterator<FeatureReference> queryNearest(Envelope envelope, long limit) { |
146 | 43371 | fdiaz | return new Oid2FeatureReferenceIterator(this.index.queryNearest(envelope, limit)); |
147 | } |
||
148 | |||
149 | @Override
|
||
150 | 44041 | jjdelcerro | public Iterator <FeatureReference>queryNearest(Envelope envelope) { |
151 | 43371 | fdiaz | return new Oid2FeatureReferenceIterator(this.index.queryNearest(envelope)); |
152 | } |
||
153 | |||
154 | @Override
|
||
155 | 44041 | jjdelcerro | public Iterator<FeatureReference> queryNearest(Geometry geom, long limit) { |
156 | 43371 | fdiaz | return new Oid2FeatureReferenceIterator(this.index.queryNearest(geom, limit)); |
157 | } |
||
158 | |||
159 | @Override
|
||
160 | 44041 | jjdelcerro | public Iterator<FeatureReference> queryNearest(Geometry geom) { |
161 | 43371 | fdiaz | return new Oid2FeatureReferenceIterator(this.index.queryNearest(geom)); |
162 | } |
||
163 | |||
164 | @Override
|
||
165 | 44041 | jjdelcerro | public Iterator<FeatureReference> queryAll() { |
166 | 43371 | fdiaz | return new Oid2FeatureReferenceIterator(this.index.queryAll()); |
167 | } |
||
168 | |||
169 | @Override
|
||
170 | 44041 | jjdelcerro | public List<FeatureReference> queryAsList(Envelope envelope) { |
171 | 43371 | fdiaz | return asList( this.query(envelope)); |
172 | } |
||
173 | |||
174 | @Override
|
||
175 | 44041 | jjdelcerro | public List<FeatureReference> queryAsList(Geometry geom) { |
176 | 43371 | fdiaz | return asList( this.query(geom)); |
177 | } |
||
178 | |||
179 | @Override
|
||
180 | 44041 | jjdelcerro | public List<FeatureReference> queryAllAsList() { |
181 | 43371 | fdiaz | return asList( this.queryAll()); |
182 | } |
||
183 | |||
184 | @Override
|
||
185 | public void insert(Envelope envelope, Object data) { |
||
186 | this.index.insert(envelope, getOID(data));
|
||
187 | } |
||
188 | |||
189 | @Override
|
||
190 | public void insert(Geometry geom, Object data) { |
||
191 | this.index.insert(geom, getOID(data));
|
||
192 | } |
||
193 | |||
194 | @Override
|
||
195 | public void insert(Geometry geom) { |
||
196 | this.index.insert(geom);
|
||
197 | } |
||
198 | |||
199 | @Override
|
||
200 | public boolean remove(Envelope envelope, Object data) { |
||
201 | return this.index.remove(envelope, getOID(data)); |
||
202 | } |
||
203 | |||
204 | @Override
|
||
205 | public boolean remove(Geometry geom, Object data) { |
||
206 | return this.index.remove(geom, getOID(data)); |
||
207 | } |
||
208 | |||
209 | @Override
|
||
210 | public boolean remove(Geometry geom) { |
||
211 | return this.index.remove(geom); |
||
212 | } |
||
213 | |||
214 | @Override
|
||
215 | public void removeAll() { |
||
216 | this.index.removeAll();
|
||
217 | } |
||
218 | |||
219 | @Override
|
||
220 | public long size() { |
||
221 | return this.index.size(); |
||
222 | } |
||
223 | |||
224 | @Override
|
||
225 | public void flush() { |
||
226 | this.index.flush();
|
||
227 | } |
||
228 | |||
229 | @Override
|
||
230 | public Manager getManager() {
|
||
231 | return this.index.getManager(); |
||
232 | } |
||
233 | |||
234 | protected Object getOID(Object obj) { |
||
235 | if( obj == null ) { |
||
236 | return null; |
||
237 | } |
||
238 | if( obj instanceof FeatureReferenceProviderServices ) { |
||
239 | return ((FeatureReferenceProviderServices)obj).getOID();
|
||
240 | } |
||
241 | if( obj instanceof Feature ) { |
||
242 | FeatureReference f = ((Feature)obj).getReference(); |
||
243 | return ((FeatureReferenceProviderServices)f).getOID();
|
||
244 | } |
||
245 | if( obj instanceof Number ) { |
||
246 | return obj;
|
||
247 | } |
||
248 | throw new IllegalArgumentException("Can't get OID from object of type '"+obj.getClass().getName()+"'."); |
||
249 | } |
||
250 | |||
251 | 44041 | jjdelcerro | protected List<FeatureReference> asList(Iterator it) { |
252 | 43371 | fdiaz | List l = new ArrayList(); |
253 | while (it.hasNext()) {
|
||
254 | l.add(it.next()); |
||
255 | } |
||
256 | return l;
|
||
257 | } |
||
258 | } |