Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.base / src / main / java / org / gvsig / sextante / app / algorithm / base / spatialindex / RTreeJsi.java @ 59

History | View | Annotate | Download (3.88 KB)

1
/*
2
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2010 Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 */
20

    
21
package org.gvsig.sextante.app.algorithm.base.spatialindex;
22

    
23
import java.awt.geom.Point2D;
24
import java.awt.geom.Rectangle2D;
25
import java.util.ArrayList;
26
import java.util.Iterator;
27
import java.util.List;
28
import java.util.Properties;
29

    
30
import com.infomatiq.jsi.IntProcedure;
31
import com.infomatiq.jsi.Rectangle;
32
import com.infomatiq.jsi.rtree.RTree;
33

    
34
/**
35
 * RTree spatial index implementation based in library
36
 * JSI (java spatial index).
37
 *
38
 * http://jsi.sourceforge.net/
39
 *
40
 * This RTree has better performance that Spatial Index Library
41
 * RTree, and that JTS'RTree, because
42
 * it uses the GNU's Trove Collections API.
43
 *
44
 * We are doing some probes with it, because it offers
45
 * a Nearest Neighbour algorithm implementation
46
 * (useful for Spatial Join geoprocess, for example).
47
 *
48
 * It isnt persistent, and We've found some problems
49
 * with delete operations.
50
 *
51
 * @author azabala
52
 *
53
 */
54
public class RTreeJsi implements ISpatialIndex, INearestNeighbourFinder {
55
        private RTree rtree;
56

    
57
        public RTreeJsi(){
58
                rtree = new RTree();
59
        }
60

    
61
        public void create(){
62
                Properties props = new Properties();
63
                rtree.init(props);
64
        }
65

    
66
        class ListIntProcedure implements IntProcedure {
67
                ArrayList<Integer> solution = new ArrayList<Integer>();
68

    
69
                public boolean execute(int arg0) {
70
                        solution.add(new Integer(arg0));
71
                        return true;
72
                }
73

    
74
                @SuppressWarnings("unchecked")
75
                public List getSolution(){
76
                        return solution;
77
                }
78
        }
79

    
80

    
81
        @SuppressWarnings("unchecked")
82
        public List query(Rectangle2D rect) {
83
                ListIntProcedure solution = new ListIntProcedure();
84
                rtree.intersects(toJsiRect(rect), solution);
85
                return solution.getSolution();
86
        }
87

    
88
        private Rectangle toJsiRect(Rectangle2D rect){
89
                Rectangle jsiRect = new Rectangle((float)rect.getMinX(),
90
                                (float)rect.getMinY(),
91
                                (float)rect.getMaxX(),
92
                                (float)rect.getMaxY());
93
                return jsiRect;
94
        }
95

    
96
        public void insert(Rectangle2D rect, int index) {
97
                rtree.add(toJsiRect(rect), index);
98
        }
99

    
100
        public void delete(Rectangle2D rect, int index) {
101
                rtree.delete(toJsiRect(rect), index);
102
        }
103

    
104
        @SuppressWarnings("unchecked")
105
        public List findNNearest(int numberOfNearest, Point2D point) {
106
                ListIntProcedure solution = new ListIntProcedure();
107
                com.infomatiq.jsi.Point jsiPoint =
108
                        new com.infomatiq.jsi.Point((float)point.getX(), (float)point.getY());
109
                rtree.nearest(jsiPoint, solution, Float.POSITIVE_INFINITY);
110
                return solution.getSolution();
111
        }
112

    
113
        @SuppressWarnings("unchecked")
114
        public List findNNearest(int numberOfNearest, Rectangle2D rect) {
115
                ListIntProcedure solution = new ListIntProcedure();
116
                com.infomatiq.jsi.Rectangle jsiRect = toJsiRect(rect);
117
                rtree.nearest(jsiRect, solution, Float.POSITIVE_INFINITY);
118
                return solution.getSolution();
119
        }
120

    
121
        @SuppressWarnings("unchecked")
122
        public Iterator iterator(){
123
                return rtree.iterator();
124
        }
125

    
126
        public static void main(String[] args){
127
                RTreeJsi q = new RTreeJsi();
128
                q.create();
129

    
130
                        for(int i = 1; i <= 99; i++){
131
                                q.insert(new Rectangle2D.Double(100+i, 200+i, 200+i, 500+i),i);
132
                        }
133

    
134
                        for(int i = 1; i <= 99; i++){
135
                                q.delete(new Rectangle2D.Double(100+i, 200+i, 200+i, 500+i),i);
136
                                System.out.println("Rect="+i);
137
                        }
138
        }
139

    
140
}
141