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 @ 47505
History | View | Annotate | Download (8.38 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.SimpleTaskStatus; |
49 |
import org.gvsig.tools.visitor.Visitor; |
50 |
import org.slf4j.Logger; |
51 |
import org.slf4j.LoggerFactory; |
52 |
|
53 |
@SuppressWarnings("UseSpecificCatch") |
54 |
public class SpatialCache { |
55 |
|
56 |
private static final Logger logger = LoggerFactory.getLogger(SpatialCache.class); |
57 |
private int maxFeatures = 1000; // Por defecto, pero se puede cambiar |
58 |
private int fastNumTotalRegs = 0; |
59 |
private SpatialIndex index = null; |
60 |
private boolean overflown = false; |
61 |
private Map<String, Boolean> enabledContexts; |
62 |
|
63 |
private boolean enabled = false; |
64 |
private AbstractCancellableTask refreshTask;
|
65 |
|
66 |
public SpatialCache() {
|
67 |
enabledContexts = new HashMap<>(); |
68 |
refreshTask = null;
|
69 |
try {
|
70 |
this.index = GeometryLocator.getGeometryManager().createDefaultMemorySpatialIndex();
|
71 |
} catch (Exception e) { |
72 |
logger.info("Can't create spatial index", e);
|
73 |
} |
74 |
} |
75 |
|
76 |
public int getMaxFeatures() { |
77 |
return maxFeatures;
|
78 |
} |
79 |
|
80 |
public void setMaxFeatures(int maxFeatures) { |
81 |
this.maxFeatures = maxFeatures;
|
82 |
} |
83 |
|
84 |
public synchronized void insert(Envelope bounds, Geometry geom) { |
85 |
if (isEnabled()) {
|
86 |
if (getMaxFeatures() >= size()) {
|
87 |
this.index.insert(bounds, geom);
|
88 |
fastNumTotalRegs++; |
89 |
} else {
|
90 |
overflown = true;
|
91 |
} |
92 |
} |
93 |
} |
94 |
|
95 |
public synchronized void query(Envelope searchEnv, Visitor visitor) { |
96 |
if (!isEnabled()) {
|
97 |
return;
|
98 |
} |
99 |
this.index.query(searchEnv, visitor);
|
100 |
|
101 |
} |
102 |
|
103 |
public synchronized List query(Envelope searchEnv) { |
104 |
if (!isEnabled()) {
|
105 |
return Collections.EMPTY_LIST; |
106 |
} |
107 |
List result = new ArrayList(); |
108 |
Iterator it = index.query(searchEnv);
|
109 |
while (it.hasNext()) {
|
110 |
result.add(it.next()); |
111 |
} |
112 |
return result;
|
113 |
} |
114 |
|
115 |
public synchronized void insert(Envelope itemEnv, Object item) { |
116 |
if (isEnabled()) {
|
117 |
if (getMaxFeatures() >= size()) {
|
118 |
this.index.insert(itemEnv, item);
|
119 |
fastNumTotalRegs++; |
120 |
} else {
|
121 |
overflown = true;
|
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
public synchronized boolean remove(Envelope itemEnv, Object item) { |
127 |
boolean resul = this.index.remove(itemEnv, item); |
128 |
if (resul) {
|
129 |
fastNumTotalRegs--; |
130 |
} |
131 |
return resul;
|
132 |
} |
133 |
|
134 |
public int size() { |
135 |
return fastNumTotalRegs;
|
136 |
} |
137 |
|
138 |
public synchronized void removeAll() { |
139 |
if( this.refreshTask!=null ) { |
140 |
this.refreshTask.cancelRequest();
|
141 |
} |
142 |
index.removeAll(); |
143 |
fastNumTotalRegs = 0;
|
144 |
overflown = false;
|
145 |
} |
146 |
|
147 |
public synchronized void remove(Envelope bounds, Geometry geom) { |
148 |
index.remove(bounds, geom); |
149 |
} |
150 |
|
151 |
public boolean isEnabled() { |
152 |
if (enabled) {
|
153 |
return true; |
154 |
} |
155 |
for (Boolean value : this.enabledContexts.values()) { |
156 |
if (value) {
|
157 |
return true; |
158 |
} |
159 |
} |
160 |
return false; |
161 |
} |
162 |
|
163 |
public boolean isEnabled(String contextId) { |
164 |
return this.enabledContexts.getOrDefault(contextId, false); |
165 |
} |
166 |
|
167 |
public void setEnabled(boolean enabled) { |
168 |
this.enabled = enabled;
|
169 |
} |
170 |
|
171 |
public void setEnabled(String contextId, boolean enabled) { |
172 |
this.enabledContexts.put(contextId, enabled);
|
173 |
} |
174 |
|
175 |
public boolean isOverflown() { |
176 |
return overflown;
|
177 |
} |
178 |
|
179 |
public boolean isContextEnabled() { |
180 |
for (Boolean value : this.enabledContexts.values()) { |
181 |
if (value) {
|
182 |
return true; |
183 |
} |
184 |
} |
185 |
return false; |
186 |
} |
187 |
|
188 |
public synchronized void refresh(String name, FeatureStore store, Envelope envelope, IProjection projection) { |
189 |
if( store == null |
190 |
|| envelope == null || envelope.isEmpty() || envelope.isCollapsed()
|
191 |
|| projection == null ) {
|
192 |
return;
|
193 |
} |
194 |
removeAll(); |
195 |
this.refreshTask = new AbstractCancellableTask("SpatialCacheRefresh_"+name) { |
196 |
@Override
|
197 |
public void run() { |
198 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
199 |
DisposeUtils.bind(store); |
200 |
FeatureSet fset = null;
|
201 |
DisposableIterator<Feature> it = null;
|
202 |
SimpleTaskStatus status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("SpatialCache "+name);
|
203 |
try {
|
204 |
status.setAutoremove(true);
|
205 |
status.add(); |
206 |
Evaluator evaluator = SpatialEvaluatorsFactory.getInstance().contains(envelope, projection, store); |
207 |
FeatureQuery query = store.createFeatureQuery(); |
208 |
query.addFilter(evaluator); |
209 |
query.setLimit(getMaxFeatures()); |
210 |
status.message(i18n.getTranslation("_Searching_geometries"));
|
211 |
fset = store.getFeatureSet(query); |
212 |
status.setRangeOfValues(0, fset.size64());
|
213 |
it = fset.fastIterator(); |
214 |
while( it.hasNext() ) {
|
215 |
if( this.isCancellationRequested() || status.isCancellationRequested() ) { |
216 |
status.cancel(); |
217 |
return;
|
218 |
} |
219 |
Geometry geom = it.next().getDefaultGeometry(); |
220 |
insert(geom.getEnvelope(), geom); |
221 |
status.incrementCurrentValue(); |
222 |
} |
223 |
status.terminate(); |
224 |
} catch (Exception ex) { |
225 |
logger.warn("Can't refresh spatial cache of '"+name+"'", ex); |
226 |
status.abort(); |
227 |
} finally {
|
228 |
if( this == refreshTask ) { |
229 |
refreshTask = null;
|
230 |
} |
231 |
if( status.isRunning() ) {
|
232 |
status.terminate(); |
233 |
} |
234 |
DisposeUtils.disposeQuietly(it); |
235 |
DisposeUtils.disposeQuietly(fset); |
236 |
DisposeUtils.disposeQuietly(store); |
237 |
} |
238 |
} |
239 |
}; |
240 |
this.refreshTask.setPriority((Thread.MAX_PRIORITY+Thread.MIN_PRIORITY)/4); |
241 |
this.refreshTask.start();
|
242 |
} |
243 |
|
244 |
} |