Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / SpatialCache.java @ 47430

History | View | Annotate | Download (8.43 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontext.layers;
25

    
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.HashMap;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Map;
32
import org.cresques.cts.IProjection;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureQuery;
35
import org.gvsig.fmap.dal.feature.FeatureSet;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.geom.Geometry;
38
import org.gvsig.fmap.geom.GeometryLocator;
39
import org.gvsig.fmap.geom.SpatialIndex;
40
import org.gvsig.fmap.geom.primitive.Envelope;
41
import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory;
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.dispose.DisposableIterator;
44
import org.gvsig.tools.dispose.DisposeUtils;
45
import org.gvsig.tools.evaluator.Evaluator;
46
import org.gvsig.tools.i18n.I18nManager;
47
import org.gvsig.tools.task.AbstractCancellableTask;
48
import org.gvsig.tools.task.CancellableTask;
49
import org.gvsig.tools.task.SimpleTaskStatus;
50
import org.gvsig.tools.visitor.Visitor;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
@SuppressWarnings("UseSpecificCatch")
55
public class SpatialCache {
56

    
57
    private static final Logger logger = LoggerFactory.getLogger(SpatialCache.class);
58
    private int maxFeatures = 1000; // Por defecto, pero se puede cambiar
59
    private int fastNumTotalRegs = 0;
60
    private SpatialIndex index = null;
61
    private boolean overflown = false;
62
    private Map<String, Boolean> enabledContexts;
63

    
64
    private boolean enabled = false;
65
    private AbstractCancellableTask refreshTask;
66

    
67
    public SpatialCache() {
68
        enabledContexts = new HashMap<>();
69
        refreshTask = null;
70
        try {
71
            this.index = GeometryLocator.getGeometryManager().createDefaultMemorySpatialIndex();
72
        } catch (Exception e) {
73
            logger.info("Can't create spatial index", e);
74
        }
75
    }
76

    
77
    public int getMaxFeatures() {
78
        return maxFeatures;
79
    }
80

    
81
    public void setMaxFeatures(int maxFeatures) {
82
        this.maxFeatures = maxFeatures;
83
    }
84

    
85
    public synchronized void insert(Envelope bounds, Geometry geom) {
86
        if (isEnabled()) {
87
            if (getMaxFeatures() >= size()) {
88
                this.index.insert(bounds, geom);
89
                fastNumTotalRegs++;
90
            } else {
91
                overflown = true;
92
            }
93
        }
94
    }
95

    
96
    public synchronized void query(Envelope searchEnv, Visitor visitor) {
97
        if (!isEnabled()) {
98
            return;
99
        }
100
        this.index.query(searchEnv, visitor);
101

    
102
    }
103

    
104
    public synchronized List query(Envelope searchEnv) {
105
        if (!isEnabled()) {
106
            return Collections.EMPTY_LIST;
107
        }
108
        List result = new ArrayList();
109
        Iterator it = index.query(searchEnv);
110
        while (it.hasNext()) {
111
            result.add(it.next());
112
        }
113
        return result;
114
    }
115

    
116
    public synchronized void insert(Envelope itemEnv, Object item) {
117
        if (isEnabled()) {
118
            if (getMaxFeatures() >= size()) {
119
                this.index.insert(itemEnv, item);
120
                fastNumTotalRegs++;
121
            } else {
122
                overflown = true;
123
            }
124
        }
125
    }
126

    
127
    public synchronized boolean remove(Envelope itemEnv, Object item) {
128
        boolean resul = this.index.remove(itemEnv, item);
129
        if (resul) {
130
            fastNumTotalRegs--;
131
        }
132
        return resul;
133
    }
134

    
135
    public int size() {
136
        return fastNumTotalRegs;
137
    }
138

    
139
    public synchronized void removeAll() {
140
        if( this.refreshTask!=null ) {
141
            this.refreshTask.cancelRequest();
142
        }
143
        index.removeAll();
144
        fastNumTotalRegs = 0;
145
        overflown = false;
146
    }
147

    
148
    public synchronized void remove(Envelope bounds, Geometry geom) {
149
        index.remove(bounds, geom);
150
    }
151

    
152
    public boolean isEnabled() {
153
        if (enabled) {
154
            return true;
155
        }
156
        for (Boolean value : this.enabledContexts.values()) {
157
            if (value) {
158
                return true;
159
            }
160
        }
161
        return false;
162
    }
163

    
164
    public boolean isEnabled(String contextId) {
165
        return this.enabledContexts.getOrDefault(contextId, false);
166
    }
167

    
168
    public void setEnabled(boolean enabled) {
169
        this.enabled = enabled;
170
    }
171

    
172
    public void setEnabled(String contextId, boolean enabled) {
173
        this.enabledContexts.put(contextId, enabled);
174
    }
175

    
176
    public boolean isOverflown() {
177
        return overflown;
178
    }
179
    
180
    public boolean isContextEnabled() {
181
        for (Boolean value : this.enabledContexts.values()) {
182
            if (value) {
183
                return true;
184
            }
185
        }
186
        return false;
187
    }
188
    
189
    public synchronized void refresh(String name, FeatureStore store, Envelope envelope, IProjection projection) {
190
        if( store == null  
191
                || envelope == null || envelope.isEmpty() || envelope.isCollapsed() 
192
                || projection == null ) {
193
            return;
194
        }
195
        removeAll();
196
        this.refreshTask = new AbstractCancellableTask("SpatialCacheRefresh_"+name) {
197
            @Override
198
            public void run() {
199
                I18nManager i18n = ToolsLocator.getI18nManager();
200
                DisposeUtils.bind(store);
201
                FeatureSet fset = null;
202
                DisposableIterator<Feature> it = null;
203
                SimpleTaskStatus status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("SpatialCache "+name);
204
                try {
205
                    status.setAutoremove(true);
206
                    status.add();
207
                    Evaluator evaluator = SpatialEvaluatorsFactory.getInstance().contains(envelope, projection, store);
208
                    FeatureQuery query = store.createFeatureQuery();
209
                    query.addFilter(evaluator);
210
                    query.setLimit(getMaxFeatures());
211
                    status.message(i18n.getTranslation("_Searching_geometries"));
212
                    fset = store.getFeatureSet(query);
213
                    status.setRangeOfValues(0, fset.size64());
214
                    it = fset.fastIterator();
215
                    while( it.hasNext() ) {
216
                        if( this.isCancellationRequested() || status.isCancellationRequested() ) {
217
                            status.cancel();
218
                            return;
219
                        }
220
                        Geometry geom = it.next().getDefaultGeometry();
221
                        insert(geom.getEnvelope(), geom);
222
                        status.incrementCurrentValue();
223
                    }
224
                    status.terminate();
225
                } catch (Exception ex) {
226
                    logger.warn("Can't refresh spatial cache of '"+name+"'", ex);
227
                    status.abort();
228
                } finally {
229
                    if( this == refreshTask ) {
230
                        refreshTask = null;
231
                    }
232
                    if( status.isRunning() ) {
233
                        status.terminate();
234
                    }
235
                    DisposeUtils.disposeQuietly(it);
236
                    DisposeUtils.disposeQuietly(fset);
237
                    DisposeUtils.disposeQuietly(store);
238
                }
239
            }
240
        };
241
        this.refreshTask.setPriority((Thread.MAX_PRIORITY+Thread.MIN_PRIORITY)/4);
242
        this.refreshTask.start();
243
    }
244
    
245
}