svn-gvsig-desktop / tags / v2_0_0_Build_2056 / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / layers / SpatialCache.java @ 39022
History | View | Annotate | Download (2.93 KB)
1 |
package org.gvsig.fmap.mapcontext.layers; |
---|---|
2 |
|
3 |
import java.util.List; |
4 |
|
5 |
import org.gvsig.fmap.geom.Geometry; |
6 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
7 |
|
8 |
import com.vividsolutions.jts.geom.Envelope; |
9 |
import com.vividsolutions.jts.index.ItemVisitor; |
10 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
11 |
|
12 |
import org.slf4j.Logger; |
13 |
import org.slf4j.LoggerFactory; |
14 |
|
15 |
public class SpatialCache { |
16 |
|
17 |
private static final Logger logger = LoggerFactory.getLogger(SpatialCache.class); |
18 |
int maxFeatures = 1000; // Por defecto, pero se puede cambiar |
19 |
int fastNumTotalRegs=0; |
20 |
Quadtree quadTree = new Quadtree();
|
21 |
|
22 |
private boolean enabled = false; |
23 |
|
24 |
public int getMaxFeatures() { |
25 |
return maxFeatures;
|
26 |
} |
27 |
|
28 |
public void setMaxFeatures(int maxFeatures) { |
29 |
this.maxFeatures = maxFeatures;
|
30 |
} |
31 |
|
32 |
/**
|
33 |
* M?todo de conveniencia
|
34 |
*
|
35 |
* @param r
|
36 |
* @param igeometry
|
37 |
*/
|
38 |
public synchronized void insert(org.gvsig.fmap.geom.primitive.Envelope bounds, Geometry geom) { |
39 |
if (isEnabled() && getMaxFeatures() >= size()) {
|
40 |
|
41 |
Geometry bounds_geom = bounds.getGeometry(); |
42 |
com.vividsolutions.jts.geom.Geometry env_jts = null;
|
43 |
|
44 |
try {
|
45 |
env_jts = (com.vividsolutions.jts.geom.Geometry) bounds_geom.invokeOperation( |
46 |
ToJTS.CODE, null);
|
47 |
Envelope env = env_jts.getEnvelopeInternal(); |
48 |
this.insert(env, geom);
|
49 |
} catch (Exception e) { |
50 |
logger.info("Error while inserting (bounds,geometry).", e);
|
51 |
} |
52 |
|
53 |
} |
54 |
} |
55 |
|
56 |
public synchronized void query(Envelope searchEnv, ItemVisitor visitor) |
57 |
{ |
58 |
quadTree.query(searchEnv, visitor); |
59 |
} |
60 |
public synchronized List query(Envelope searchEnv) |
61 |
{ |
62 |
return quadTree.query(searchEnv);
|
63 |
} |
64 |
|
65 |
public void insert(Envelope itemEnv, Object item) { |
66 |
if (isEnabled() && getMaxFeatures() >= size()) {
|
67 |
quadTree.insert(itemEnv, item); |
68 |
fastNumTotalRegs++; |
69 |
} |
70 |
} |
71 |
|
72 |
public boolean remove(Envelope itemEnv, Object item) { |
73 |
boolean resul = quadTree.remove(itemEnv, item);
|
74 |
if (resul)
|
75 |
fastNumTotalRegs--; |
76 |
return resul;
|
77 |
} |
78 |
|
79 |
public int size() { |
80 |
return fastNumTotalRegs;
|
81 |
} |
82 |
|
83 |
public void clearAll() { |
84 |
quadTree = new Quadtree();
|
85 |
fastNumTotalRegs = 0;
|
86 |
} |
87 |
|
88 |
public void remove(org.gvsig.fmap.geom.primitive.Envelope bounds, Geometry geom) { |
89 |
|
90 |
Geometry bounds_geom = bounds.getGeometry(); |
91 |
com.vividsolutions.jts.geom.Geometry env_jts = null;
|
92 |
|
93 |
try {
|
94 |
env_jts = (com.vividsolutions.jts.geom.Geometry) bounds_geom.invokeOperation( |
95 |
ToJTS.CODE, null);
|
96 |
Envelope env = env_jts.getEnvelopeInternal(); |
97 |
this.remove(env, geom);
|
98 |
} catch (Exception e) { |
99 |
logger.info("Error while removing (bounds,geometry).", e);
|
100 |
} |
101 |
|
102 |
} |
103 |
|
104 |
public boolean isEnabled() { |
105 |
return enabled;
|
106 |
} |
107 |
|
108 |
public void setEnabled(boolean enabled) { |
109 |
this.enabled = enabled;
|
110 |
} |
111 |
|
112 |
|
113 |
} |