Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_spatialindex / src / org / gvsig / fmap / data / index / spatial / AbstractIntBasedSpatialIndex.java @ 23758

History | View | Annotate | Download (3.79 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 {{Company}}   {{Task}}
26
*/
27
 
28

    
29
package org.gvsig.fmap.data.index.spatial;
30

    
31
import java.util.ArrayList;
32
import java.util.List;
33

    
34
import org.apache.log4j.Logger;
35
import org.gvsig.fmap.data.feature.Feature;
36
import org.gvsig.fmap.data.feature.FeatureID;
37
import org.gvsig.fmap.data.index.AbstractIndex;
38
import org.gvsig.fmap.data.index.IndexException;
39
import org.gvsig.fmap.data.index.IndexParameters;
40
import org.gvsig.fmap.data.index.QueryParameters;
41
import org.gvsig.fmap.geom.Geometry;
42
import org.gvsig.fmap.geom.primitive.Envelope;
43

    
44
/**
45
 * This class is a wrapper for spatial indexes based on int values so that they can use 
46
 * FeatureID instead.
47
 *
48
 * @author jyarza
49
 *
50
 */
51
public abstract class AbstractIntBasedSpatialIndex extends AbstractIndex implements SpatialIndex {
52

    
53
        private static Logger logger = Logger.getLogger(AbstractIntBasedSpatialIndex.class);
54
        
55
        /** Keeps the correspondence between the int value used internally and FeatureID */        
56
        private List ids = new ArrayList();
57
        
58
        public AbstractIntBasedSpatialIndex(IndexParameters params) throws IndexException {
59
                super(params);
60
        }
61
        
62
        public final void delete(Envelope env, FeatureID fid) {                
63
                int idx = ids.indexOf(fid);
64
                if (idx >= 0) {
65
                        this.delete(env, idx);
66
                }
67
        }
68
        
69
        public final void insert(Envelope env, FeatureID fid) {
70
                ids.add(fid);
71
                this.insert(env, ids.size()-1);
72
        }
73

    
74
        public final List query(QueryParameters params) throws IndexException {
75
                long start = System.currentTimeMillis();
76
                try {
77
                        SpatialQueryParameters sqp = (SpatialQueryParameters) params;
78
                        List result = query(sqp.getEnvelope());
79
                        
80
                        // replace integers by corresponding FeatureIDs in the list
81
                        for (int i=0; i<result.size(); i++) {
82
                                int value = ((Integer) result.get(i)).intValue();
83
                                result.set(i, ids.get(value));
84
                        }
85
                        long end = System.currentTimeMillis();
86
                        logger.debug("total index query time = " + (end-start) + " millis.");
87
                        return result;
88
                } catch (ClassCastException e) {
89
                        throw new IndexException(e);
90
                }
91
        }
92
        
93
        public final void delete(Object o, FeatureID fid) {
94
                delete((Envelope) o, fid);
95
        }
96

    
97
        public final void insert(Object o, FeatureID fid) {
98
                insert((Envelope) o, fid);                
99
        }
100
        
101
        /* 
102
         * overrides org.gvsig.fmap.data.index.AbstractIndex.insert because these spatial indexes 
103
         * use the geometry envelope instead of the geometry itself.
104
         */
105
        public final void insert(Feature feat) {
106
                Geometry geom = (Geometry) feat.get(getFeatureAttributeDescriptor().getName());
107
                insert(geom.getEnvelope(), feat.getID());                
108
        }
109
        
110
        /** Spatial indexes based on int values will implement this method for insertion */
111
        protected abstract void insert(Envelope env, int index);
112
        /** Spatial indexes based on int values will implement this method for deletion */
113
        protected abstract void delete(Envelope env, int index);
114
        /** Spatial indexes based on int values will implement this method for querying */
115
        protected abstract List query(Envelope env) throws IndexException;
116
}
117