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 / WrappedSpatialIndex.java @ 43371

History | View | Annotate | Download (7.07 KB)

1
/* 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
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.SpatialIndex;
34
import org.gvsig.fmap.geom.SpatialIndexFactory;
35
import org.gvsig.fmap.geom.primitive.Envelope;
36
import org.gvsig.tools.exception.BaseException;
37
import org.gvsig.tools.service.Manager;
38
import org.gvsig.tools.visitor.VisitCanceledException;
39
import org.gvsig.tools.visitor.Visitor;
40

    
41
/**
42
 * @author fdiaz
43
 *
44
 */
45
public class WrappedSpatialIndex implements SpatialIndex {
46

    
47
    private final SpatialIndex index;
48
    private final FeatureStore store;
49

    
50
    protected class Oid2FeatureReferenceVisitor implements Visitor {
51

    
52
        private final Visitor wrapedVisitor;
53

    
54
        public Oid2FeatureReferenceVisitor(Visitor wrapedVisitor) {
55
            this.wrapedVisitor = wrapedVisitor;
56
        }
57

    
58
        @Override
59
        public void visit(Object oid) throws VisitCanceledException, BaseException {
60
            DefaultFeatureReference ref = new DefaultFeatureReference(store, oid);
61
            wrapedVisitor.visit(ref);
62
        }
63

    
64
    }
65

    
66
    protected class Oid2FeatureReferenceIterator implements Iterator {
67

    
68
        private final Iterator wrapedIterator;
69

    
70
        public Oid2FeatureReferenceIterator(Iterator wrapedIterator) {
71
            this.wrapedIterator = wrapedIterator;
72
        }
73

    
74
        @Override
75
        public boolean hasNext() {
76
            return this.wrapedIterator.hasNext();
77
        }
78

    
79
        @Override
80
        public Object next() {
81
            Object oid = this.wrapedIterator.next();
82
            DefaultFeatureReference ref = new DefaultFeatureReference(store, oid);
83
            return ref;
84
        }
85

    
86
        @Override
87
        public void remove() {
88
            this.wrapedIterator.remove();
89
        }
90
    }
91

    
92
    public WrappedSpatialIndex(SpatialIndex index, FeatureStore store) {
93
        this.index = index;
94
        this.store = store;
95
    }
96

    
97
    @Override
98
    public SpatialIndexFactory getFactory() {
99
        return this.index.getFactory();
100
    }
101

    
102
    @Override
103
    public void open() {
104
        this.index.open();
105
    }
106

    
107
    @Override
108
    public void close() {
109
        this.index.close();
110
    }
111

    
112
    @Override
113
    public void query(Envelope envelope, Visitor visitor) {
114
        this.index.query(envelope, new Oid2FeatureReferenceVisitor(visitor));
115
    }
116

    
117
    @Override
118
    public void query(Geometry geom, Visitor visitor) {
119
        this.index.query(geom, new Oid2FeatureReferenceVisitor(visitor));
120
    }
121

    
122
    @Override
123
    public Iterator query(Envelope envelope, long limit) {
124
        return new Oid2FeatureReferenceIterator(this.index.query(envelope, limit));
125
    }
126

    
127
    @Override
128
    public Iterator query(Envelope envelope) {
129
        return new Oid2FeatureReferenceIterator(this.index.query(envelope));
130
    }
131

    
132
    @Override
133
    public Iterator query(Geometry geom, long limit) {
134
        return new Oid2FeatureReferenceIterator(this.index.query(geom, limit));
135
    }
136

    
137
    @Override
138
    public Iterator query(Geometry geom) {
139
        return new Oid2FeatureReferenceIterator(this.index.query(geom));
140
    }
141

    
142
    @Override
143
    public Iterator queryNearest(Envelope envelope, long limit) {
144
        return new Oid2FeatureReferenceIterator(this.index.queryNearest(envelope, limit));
145
    }
146

    
147
    @Override
148
    public Iterator queryNearest(Envelope envelope) {
149
        return new Oid2FeatureReferenceIterator(this.index.queryNearest(envelope));
150
    }
151

    
152
    @Override
153
    public Iterator queryNearest(Geometry geom, long limit) {
154
        return new Oid2FeatureReferenceIterator(this.index.queryNearest(geom, limit));
155
    }
156

    
157
    @Override
158
    public Iterator queryNearest(Geometry geom) {
159
        return new Oid2FeatureReferenceIterator(this.index.queryNearest(geom));
160
    }
161

    
162
    @Override
163
    public Iterator queryAll() {
164
        return new Oid2FeatureReferenceIterator(this.index.queryAll());
165
    }
166

    
167
    @Override
168
    public List queryAsList(Envelope envelope) {
169
        return asList( this.query(envelope));
170
    }
171

    
172
    @Override
173
    public List queryAsList(Geometry geom) {
174
        return asList( this.query(geom));
175
    }
176

    
177
    @Override
178
    public List queryAllAsList() {
179
        return asList( this.queryAll());
180
    }
181

    
182
    @Override
183
    public void insert(Envelope envelope, Object data) {
184
        this.index.insert(envelope, getOID(data));
185
    }
186

    
187
    @Override
188
    public void insert(Geometry geom, Object data) {
189
        this.index.insert(geom, getOID(data));
190
    }
191

    
192
    @Override
193
    public void insert(Geometry geom) {
194
        this.index.insert(geom);
195
    }
196

    
197
    @Override
198
    public boolean remove(Envelope envelope, Object data) {
199
        return this.index.remove(envelope, getOID(data));
200
    }
201

    
202
    @Override
203
    public boolean remove(Geometry geom, Object data) {
204
        return this.index.remove(geom, getOID(data));
205
    }
206

    
207
    @Override
208
    public boolean remove(Geometry geom) {
209
        return this.index.remove(geom);
210
    }
211

    
212
    @Override
213
    public void removeAll() {
214
        this.index.removeAll();
215
    }
216

    
217
    @Override
218
    public long size() {
219
        return this.index.size();
220
    }
221

    
222
    @Override
223
    public void flush() {
224
        this.index.flush();
225
    }
226

    
227
    @Override
228
    public Manager getManager() {
229
        return this.index.getManager();
230
    }
231

    
232
    protected Object getOID(Object obj) {
233
        if( obj == null ) {
234
            return null;
235
        }
236
        if( obj instanceof FeatureReferenceProviderServices ) {
237
            return ((FeatureReferenceProviderServices)obj).getOID();
238
        }
239
        if( obj instanceof Feature ) {
240
            FeatureReference f = ((Feature)obj).getReference();
241
            return ((FeatureReferenceProviderServices)f).getOID();
242
        }
243
        if( obj instanceof Number ) {
244
            return obj;
245
        }
246
        throw new IllegalArgumentException("Can't get OID from object of type '"+obj.getClass().getName()+"'.");
247
    }
248

    
249
    protected List asList(Iterator it) {
250
        List l = new ArrayList();
251
        while (it.hasNext()) {
252
            l.add(it.next());
253
        }
254
        return l;
255
    }
256
}