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

History | View | Annotate | Download (4.68 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.HashMap;
28
import java.util.Iterator;
29
import java.util.List;
30
import java.util.Map;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.GeometryLocator;
33
import org.gvsig.fmap.geom.SpatialIndex;
34
import org.gvsig.fmap.geom.primitive.Envelope;
35
import org.gvsig.tools.visitor.Visitor;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

    
39
public class SpatialCache {
40

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

    
48
    private boolean enabled = false;
49

    
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
    }
58

    
59
    public int getMaxFeatures() {
60
        return maxFeatures;
61
    }
62

    
63
    public void setMaxFeatures(int maxFeatures) {
64
        this.maxFeatures = maxFeatures;
65
    }
66

    
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
    }
77

    
78
    public synchronized void query(Envelope searchEnv, Visitor visitor) {
79
        this.index.query(searchEnv, visitor);
80

    
81
    }
82

    
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
    }
91

    
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) {
122
        index.remove(bounds, geom);
123
    }
124

    
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
    }
136

    
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
            }
158
        }
159
        return false;
160
    }
161
}