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

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

    
37
public class SpatialCache  {
38
    
39
    private static final Logger logger = LoggerFactory.getLogger(SpatialCache.class);
40
    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;
46

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

    
59
        public void setMaxFeatures(int maxFeatures) {
60
                this.maxFeatures = maxFeatures;
61
        }
62

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

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

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

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

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

    
111
        public void clearAll() {
112
                index.removeAll();
113
                fastNumTotalRegs = 0;
114
                overflown = false;
115
        }
116

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

    
121
        public boolean isEnabled() {
122
                return enabled;
123
        }
124

    
125
        public void setEnabled(boolean enabled) {
126
                this.enabled = enabled;
127
        }
128
        
129
        public boolean isOverflown() {
130
            return overflown;
131
        }
132
}