svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / spatialindex / RTreeJsi.java @ 10977
History | View | Annotate | Download (4.4 KB)
1 | 5417 | azabala | /*
|
---|---|---|---|
2 | * Created on 15-may-2006
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | *
|
||
6 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
7 | *
|
||
8 | * This program is free software; you can redistribute it and/or
|
||
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | *
|
||
13 | * This program is distributed in the hope that it will be useful,
|
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | *
|
||
18 | * You should have received a copy of the GNU General Public License
|
||
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
21 | *
|
||
22 | * For more information, contact:
|
||
23 | *
|
||
24 | * Generalitat Valenciana
|
||
25 | * Conselleria d'Infraestructures i Transport
|
||
26 | * Av. Blasco Ib??ez, 50
|
||
27 | * 46010 VALENCIA
|
||
28 | * SPAIN
|
||
29 | *
|
||
30 | * +34 963862235
|
||
31 | * gvsig@gva.es
|
||
32 | * www.gvsig.gva.es
|
||
33 | *
|
||
34 | * or
|
||
35 | *
|
||
36 | * IVER T.I. S.A
|
||
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | *
|
||
41 | * +34 963163400
|
||
42 | * dac@iver.es
|
||
43 | */
|
||
44 | /* CVS MESSAGES:
|
||
45 | *
|
||
46 | * $Id$
|
||
47 | * $Log$
|
||
48 | 10627 | caballero | * Revision 1.3 2007-03-06 17:08:59 caballero
|
49 | * Exceptions
|
||
50 | *
|
||
51 | * Revision 1.2 2006/06/05 16:59:08 azabala
|
||
52 | 5676 | azabala | * implementada busqueda de vecino mas proximo a partir de rectangulos
|
53 | *
|
||
54 | * Revision 1.1 2006/05/24 21:58:04 azabala
|
||
55 | 5417 | azabala | * *** empty log message ***
|
56 | *
|
||
57 | *
|
||
58 | */
|
||
59 | package com.iver.cit.gvsig.fmap.spatialindex; |
||
60 | |||
61 | import java.awt.Point; |
||
62 | import java.awt.geom.Rectangle2D; |
||
63 | import java.util.ArrayList; |
||
64 | import java.util.List; |
||
65 | import java.util.Properties; |
||
66 | |||
67 | 10627 | caballero | import com.infomatiq.jsi.IntProcedure; |
68 | import com.infomatiq.jsi.Rectangle; |
||
69 | import com.infomatiq.jsi.rtree.RTree; |
||
70 | 5417 | azabala | |
71 | /**
|
||
72 | * RTree spatial index implementation based in library
|
||
73 | * JSI (java spatial index).
|
||
74 | *
|
||
75 | * http://jsi.sourceforge.net/
|
||
76 | *
|
||
77 | * This RTree has better performance that Spatial Index Library
|
||
78 | * RTree, and that JTS'RTree, because
|
||
79 | * it uses the GNU's Trove Collections API.
|
||
80 | *
|
||
81 | * We are doing some probes with it, because it offers
|
||
82 | * a Nearest Neighbour algorithm implementation
|
||
83 | * (useful for Spatial Join geoprocess, for example).
|
||
84 | *
|
||
85 | * It isnt persistent, and We've found some problems
|
||
86 | * with delete operations.
|
||
87 | *
|
||
88 | *
|
||
89 | *
|
||
90 | *
|
||
91 | * @author azabala
|
||
92 | *
|
||
93 | */
|
||
94 | public class RTreeJsi implements ISpatialIndex, INearestNeighbourFinder { |
||
95 | private RTree rtree;
|
||
96 | |||
97 | public RTreeJsi(){
|
||
98 | rtree = new RTree();
|
||
99 | } |
||
100 | |||
101 | public void create(){ |
||
102 | Properties props = new Properties(); |
||
103 | // props.setProperty("MaxNodeEntries", "500");
|
||
104 | // props.setProperty("MinNodeEntries", "200");
|
||
105 | rtree.init(props); |
||
106 | } |
||
107 | |||
108 | class ListIntProcedure implements IntProcedure{ |
||
109 | ArrayList solution = new ArrayList(); |
||
110 | |||
111 | public boolean execute(int arg0) { |
||
112 | solution.add(new Integer(arg0)); |
||
113 | return true; |
||
114 | } |
||
115 | |||
116 | public List getSolution(){ |
||
117 | return solution;
|
||
118 | } |
||
119 | } |
||
120 | |||
121 | |||
122 | public List query(Rectangle2D rect) { |
||
123 | ListIntProcedure solution = new ListIntProcedure();
|
||
124 | rtree.intersects(toJsiRect(rect), solution); |
||
125 | return solution.getSolution();
|
||
126 | } |
||
127 | |||
128 | private Rectangle toJsiRect(Rectangle2D rect){ |
||
129 | Rectangle jsiRect = new Rectangle((float)rect.getMinX(), |
||
130 | (float)rect.getMinY(),
|
||
131 | (float)rect.getMaxX(),
|
||
132 | (float)rect.getMaxY());
|
||
133 | return jsiRect;
|
||
134 | } |
||
135 | |||
136 | public void insert(Rectangle2D rect, int index) { |
||
137 | rtree.add(toJsiRect(rect), index); |
||
138 | } |
||
139 | |||
140 | public void delete(Rectangle2D rect, int index) { |
||
141 | rtree.delete(toJsiRect(rect), index); |
||
142 | } |
||
143 | |||
144 | public List findNNearest(int numberOfNearest, Point point) { |
||
145 | ListIntProcedure solution = new ListIntProcedure();
|
||
146 | com.infomatiq.jsi.Point jsiPoint = |
||
147 | new com.infomatiq.jsi.Point(point.x, point.y);
|
||
148 | //FIXME REVISAR
|
||
149 | rtree.nearest(jsiPoint, solution, Float.POSITIVE_INFINITY);
|
||
150 | return solution.getSolution();
|
||
151 | } |
||
152 | |||
153 | public List findNNearest(int numberOfNearest, Rectangle2D rect) { |
||
154 | 5676 | azabala | ListIntProcedure solution = new ListIntProcedure();
|
155 | com.infomatiq.jsi.Rectangle jsiRect = |
||
156 | toJsiRect(rect); |
||
157 | |||
158 | rtree.nearest(jsiRect, solution, Float.POSITIVE_INFINITY);
|
||
159 | return solution.getSolution();
|
||
160 | 5417 | azabala | } |
161 | |||
162 | public static void main(String[] args){ |
||
163 | RTreeJsi q = new RTreeJsi();
|
||
164 | q.create(); |
||
165 | |||
166 | for(int i = 1; i <= 99; i++){ |
||
167 | q.insert(new Rectangle2D.Double(100+i, 200+i, 200+i, 500+i),i); |
||
168 | } |
||
169 | |||
170 | for(int i = 1; i <= 99; i++){ |
||
171 | q.delete(new Rectangle2D.Double(100+i, 200+i, 200+i, 500+i),i); |
||
172 | System.out.println("Rect="+i); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | } |