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

History | View | Annotate | Download (3.24 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

    
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.GeometryLocator;
32
import org.gvsig.fmap.geom.SpatialIndex;
33
import org.gvsig.fmap.geom.primitive.Envelope;
34
import org.gvsig.tools.visitor.Visitor;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
public class SpatialCache  {
39
    
40
    private static final Logger logger = LoggerFactory.getLogger(SpatialCache.class);
41
    private int maxFeatures = 1000; // Por defecto, pero se puede cambiar
42
        private int fastNumTotalRegs=0;
43
        private SpatialIndex index = null;
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() && getMaxFeatures() >= size()) {
65
                    this.index.insert(bounds, geom);
66
                }
67
        }
68

    
69
        public synchronized void query(Envelope searchEnv, Visitor visitor)
70
        {
71
                this.index.query(searchEnv, visitor);
72
                
73
        }
74
        public synchronized List query(Envelope searchEnv)
75
        {
76
                List result = new ArrayList();
77
                Iterator it = index.query(searchEnv);
78
                while( it.hasNext() ) {
79
                        result.add( it.next());
80
                }
81
                return result;
82
        }
83

    
84
        public void insert(Envelope itemEnv, Object item) {
85
                if (isEnabled() && getMaxFeatures() >= size()) {
86
                        this.index.insert(itemEnv, item);
87
                        fastNumTotalRegs++;
88
                }
89
        }
90

    
91
        public boolean remove(Envelope itemEnv, Object item) {
92
                boolean resul = this.index.remove(itemEnv,item);
93
                if (resul)
94
                        fastNumTotalRegs--;
95
                return resul;
96
        }
97

    
98
        public int size() {
99
                return fastNumTotalRegs;
100
        }
101

    
102
        public void clearAll() {
103
                index.removeAll();
104
                fastNumTotalRegs = 0;
105
        }
106

    
107
        public void remove(org.gvsig.fmap.geom.primitive.Envelope bounds, Geometry geom) {
108
        index.remove(bounds, geom);
109
        }
110

    
111
        public boolean isEnabled() {
112
                return enabled;
113
        }
114

    
115
        public void setEnabled(boolean enabled) {
116
                this.enabled = enabled;
117
        }
118
}