Revision 46134 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

View differences:

SpatialCache.java
24 24
package org.gvsig.fmap.mapcontext.layers;
25 25

  
26 26
import java.util.ArrayList;
27
import java.util.HashMap;
27 28
import java.util.Iterator;
28 29
import java.util.List;
30
import java.util.Map;
29 31
import org.gvsig.fmap.geom.Geometry;
30 32
import org.gvsig.fmap.geom.GeometryLocator;
31 33
import org.gvsig.fmap.geom.SpatialIndex;
......
34 36
import org.slf4j.Logger;
35 37
import org.slf4j.LoggerFactory;
36 38

  
37
public class SpatialCache  {
38
    
39
public class SpatialCache {
40

  
39 41
    private static final Logger logger = LoggerFactory.getLogger(SpatialCache.class);
40 42
    private int maxFeatures = 1000; // Por defecto, pero se puede cambiar
41
	private int fastNumTotalRegs=0;
42
	private SpatialIndex index = null;
43
        private boolean overflown = false;
44
	
45
	private boolean enabled = false;
43
    private int fastNumTotalRegs = 0;
44
    private SpatialIndex index = null;
45
    private boolean overflown = false;
46
    private Map<String, Boolean> enabledContexts;
46 47

  
47
	public SpatialCache() {
48
		try {
49
			this.index = GeometryLocator.getGeometryManager().createDefaultMemorySpatialIndex();
50
		} catch (Exception e) {
51
			logger.info("Can't create spatial index", e);
52
		}
53
	}
54
	
55
	public int getMaxFeatures() {
56
		return maxFeatures;
57
	}
48
    private boolean enabled = false;
58 49

  
59
	public void setMaxFeatures(int maxFeatures) {
60
		this.maxFeatures = maxFeatures;
61
	}
50
    public SpatialCache() {
51
        enabledContexts = new HashMap<>();
52
        try {
53
            this.index = GeometryLocator.getGeometryManager().createDefaultMemorySpatialIndex();
54
        } catch (Exception e) {
55
            logger.info("Can't create spatial index", e);
56
        }
57
    }
62 58

  
63
	public synchronized void insert(Envelope bounds, Geometry geom) {
64
		if (isEnabled()) {
65
                    if (getMaxFeatures() >= size()) {
66
                        this.index.insert(bounds, geom);
67
			fastNumTotalRegs++;
68
                    } else {
69
                        overflown = true;
70
                    }
71
		}
72
	}
59
    public int getMaxFeatures() {
60
        return maxFeatures;
61
    }
73 62

  
74
	public synchronized void query(Envelope searchEnv, Visitor visitor)
75
	{
76
		this.index.query(searchEnv, visitor);
77
		
78
	}
79
	public synchronized List query(Envelope searchEnv)
80
	{
81
		List result = new ArrayList();
82
		Iterator it = index.query(searchEnv);
83
		while( it.hasNext() ) {
84
			result.add( it.next());
85
		}
86
		return result;
87
	}
63
    public void setMaxFeatures(int maxFeatures) {
64
        this.maxFeatures = maxFeatures;
65
    }
88 66

  
89
	public void insert(Envelope itemEnv, Object item) {
90
		if (isEnabled()) {
91
                    if (getMaxFeatures() >= size()) {
92
			this.index.insert(itemEnv, item);
93
			fastNumTotalRegs++;
94
                    } else {
95
                        overflown = true;
96
                    }
97
		}
98
	}
67
    public synchronized void insert(Envelope bounds, Geometry geom) {
68
        if (isEnabled()) {
69
            if (getMaxFeatures() >= size()) {
70
                this.index.insert(bounds, geom);
71
                fastNumTotalRegs++;
72
            } else {
73
                overflown = true;
74
            }
75
        }
76
    }
99 77

  
100
	public boolean remove(Envelope itemEnv, Object item) {
101
		boolean resul = this.index.remove(itemEnv,item);
102
		if (resul)
103
			fastNumTotalRegs--;
104
		return resul;
105
	}
78
    public synchronized void query(Envelope searchEnv, Visitor visitor) {
79
        this.index.query(searchEnv, visitor);
106 80

  
107
	public int size() {
108
		return fastNumTotalRegs;
109
	}
81
    }
110 82

  
111
	public void clearAll() {
112
		index.removeAll();
113
		fastNumTotalRegs = 0;
114
                overflown = false;
115
	}
83
    public synchronized List query(Envelope searchEnv) {
84
        List result = new ArrayList();
85
        Iterator it = index.query(searchEnv);
86
        while (it.hasNext()) {
87
            result.add(it.next());
88
        }
89
        return result;
90
    }
116 91

  
117
	public void remove(org.gvsig.fmap.geom.primitive.Envelope bounds, Geometry geom) {
92
    public void insert(Envelope itemEnv, Object item) {
93
        if (isEnabled()) {
94
            if (getMaxFeatures() >= size()) {
95
                this.index.insert(itemEnv, item);
96
                fastNumTotalRegs++;
97
            } else {
98
                overflown = true;
99
            }
100
        }
101
    }
102

  
103
    public boolean remove(Envelope itemEnv, Object item) {
104
        boolean resul = this.index.remove(itemEnv, item);
105
        if (resul) {
106
            fastNumTotalRegs--;
107
        }
108
        return resul;
109
    }
110

  
111
    public int size() {
112
        return fastNumTotalRegs;
113
    }
114

  
115
    public void removeAll() {
116
        index.removeAll();
117
        fastNumTotalRegs = 0;
118
        overflown = false;
119
    }
120

  
121
    public void remove(org.gvsig.fmap.geom.primitive.Envelope bounds, Geometry geom) {
118 122
        index.remove(bounds, geom);
119
	}
123
    }
120 124

  
121
	public boolean isEnabled() {
122
		return enabled;
123
	}
125
    public boolean isEnabled() {
126
        if (enabled) {
127
            return true;
128
        }
129
        for (Boolean value : this.enabledContexts.values()) {
130
            if (value) {
131
                return true;
132
            }
133
        }
134
        return false;
135
    }
124 136

  
125
	public void setEnabled(boolean enabled) {
126
		this.enabled = enabled;
127
	}
128
        
129
        public boolean isOverflown() {
130
            return overflown;
137
    public boolean isEnabled(String contextId) {
138
        return this.enabledContexts.getOrDefault(contextId, false);
139
    }
140

  
141
    public void setEnabled(boolean enabled) {
142
        this.enabled = enabled;
143
    }
144

  
145
    public void setEnabled(String contextId, boolean enabled) {
146
        this.enabledContexts.put(contextId, enabled);
147
    }
148

  
149
    public boolean isOverflown() {
150
        return overflown;
151
    }
152
    
153
    public boolean isContextEnabled() {
154
        for (Boolean value : this.enabledContexts.values()) {
155
            if (value) {
156
                return true;
157
            }
131 158
        }
159
        return false;
160
    }
132 161
}

Also available in: Unified diff