svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.impl / src / main / java / org / gvsig / fmap / geom / impl / SpatialIndexJTS.java @ 40559
History | View | Annotate | Download (4.87 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.geom.impl; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Collection; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
|
31 |
import org.gvsig.fmap.geom.Geometry; |
32 |
import org.gvsig.fmap.geom.SpatialIndex; |
33 |
import org.gvsig.fmap.geom.primitive.Envelope; |
34 |
import org.gvsig.fmap.geom.util.Converter; |
35 |
import org.gvsig.tools.visitor.Visitor; |
36 |
|
37 |
public class SpatialIndexJTS implements SpatialIndex { |
38 |
|
39 |
private class JTSVisitorWrapper implements com.vividsolutions.jts.index.ItemVisitor { |
40 |
|
41 |
private Visitor visitor = null; |
42 |
public JTSVisitorWrapper(Visitor visitor) {
|
43 |
this.visitor = visitor;
|
44 |
} |
45 |
public void visitItem(Object arg0) { |
46 |
try {
|
47 |
this.visitor.visit(arg0);
|
48 |
} catch (Exception e) { |
49 |
throw new RuntimeException(); |
50 |
} |
51 |
} |
52 |
|
53 |
} |
54 |
private com.vividsolutions.jts.index.quadtree.Quadtree index = null; |
55 |
|
56 |
public SpatialIndexJTS() {
|
57 |
index = new com.vividsolutions.jts.index.quadtree.Quadtree();
|
58 |
} |
59 |
|
60 |
private com.vividsolutions.jts.geom.Geometry getJTS(org.gvsig.fmap.geom.Geometry geom) {
|
61 |
return Converter.geometryToJts(geom);
|
62 |
} |
63 |
|
64 |
public long size() { |
65 |
return this.index.size(); |
66 |
} |
67 |
|
68 |
public void query(org.gvsig.fmap.geom.primitive.Envelope envelope, |
69 |
Visitor visitor) { |
70 |
com.vividsolutions.jts.index.ItemVisitor visitor_jts = new JTSVisitorWrapper(visitor);
|
71 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(envelope.getGeometry()).getEnvelopeInternal(); |
72 |
this.index.query(env_jts, visitor_jts);
|
73 |
|
74 |
} |
75 |
|
76 |
public void query(org.gvsig.fmap.geom.Geometry geom, Visitor visitor) { |
77 |
com.vividsolutions.jts.index.ItemVisitor visitor_jts = new JTSVisitorWrapper(visitor);
|
78 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(geom).getEnvelopeInternal(); |
79 |
this.index.query(env_jts, visitor_jts);
|
80 |
} |
81 |
|
82 |
public Iterator query(org.gvsig.fmap.geom.primitive.Envelope envelope) { |
83 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(envelope.getGeometry()).getEnvelopeInternal(); |
84 |
List result = this.index.query(env_jts); |
85 |
return result.iterator();
|
86 |
} |
87 |
public Iterator queryAll() { |
88 |
List result = this.index.queryAll(); |
89 |
return result.iterator();
|
90 |
} |
91 |
|
92 |
public Iterator query(org.gvsig.fmap.geom.Geometry geom) { |
93 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(geom).getEnvelopeInternal(); |
94 |
List result = this.index.query(env_jts); |
95 |
return result.iterator();
|
96 |
} |
97 |
|
98 |
public void insert(org.gvsig.fmap.geom.Geometry geom, Object data) { |
99 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(geom).getEnvelopeInternal(); |
100 |
index.insert(env_jts, data); |
101 |
} |
102 |
|
103 |
public void insert(org.gvsig.fmap.geom.Geometry geom) { |
104 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(geom).getEnvelopeInternal(); |
105 |
index.insert(env_jts, geom); |
106 |
} |
107 |
|
108 |
public void insert(org.gvsig.fmap.geom.primitive.Envelope envelope, Object data) { |
109 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(envelope.getGeometry()).getEnvelopeInternal(); |
110 |
index.insert(env_jts, data); |
111 |
} |
112 |
|
113 |
public boolean remove(org.gvsig.fmap.geom.Geometry geom) { |
114 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(geom).getEnvelopeInternal(); |
115 |
return index.remove (env_jts, geom);
|
116 |
} |
117 |
|
118 |
public boolean remove(org.gvsig.fmap.geom.Geometry geom, Object data) { |
119 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(geom).getEnvelopeInternal(); |
120 |
return index.remove (env_jts, data);
|
121 |
} |
122 |
|
123 |
public boolean remove(org.gvsig.fmap.geom.primitive.Envelope envelope, Object data) { |
124 |
com.vividsolutions.jts.geom.Envelope env_jts = getJTS(envelope.getGeometry()).getEnvelopeInternal(); |
125 |
return index.remove (env_jts, data);
|
126 |
} |
127 |
|
128 |
public void removeAll() { |
129 |
index = new com.vividsolutions.jts.index.quadtree.Quadtree();
|
130 |
} |
131 |
|
132 |
public List queryAsList(Envelope envelope) { |
133 |
return IteratorToList( query(envelope) );
|
134 |
} |
135 |
|
136 |
public List queryAsList(Geometry geom) { |
137 |
return IteratorToList( query(geom) );
|
138 |
} |
139 |
|
140 |
public List queryAllAsList() { |
141 |
return IteratorToList( queryAll() );
|
142 |
} |
143 |
|
144 |
private List IteratorToList(Iterator it) { |
145 |
List l = new ArrayList(); |
146 |
while( it.hasNext() ) {
|
147 |
l.add(it.next()); |
148 |
} |
149 |
return l;
|
150 |
} |
151 |
} |