svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / spatialindex / SpatialIndexJTSQuadtree.java @ 43513
History | View | Annotate | Download (5.69 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 modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.geom.jts.spatialindex; |
24 |
|
25 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
26 |
import java.io.File; |
27 |
import java.io.FileInputStream; |
28 |
import java.io.FileNotFoundException; |
29 |
import java.io.FileOutputStream; |
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
import org.apache.commons.lang3.SerializationUtils; |
33 |
|
34 |
import org.gvsig.fmap.geom.GeometryManager; |
35 |
import org.gvsig.fmap.geom.SpatialIndex; |
36 |
import org.gvsig.fmap.geom.SpatialIndexFactory; |
37 |
import org.gvsig.fmap.geom.jts.GeometryJTS; |
38 |
import org.gvsig.tools.dynobject.DynObject; |
39 |
import org.gvsig.tools.visitor.Visitor; |
40 |
|
41 |
public class SpatialIndexJTSQuadtree extends AbstractSpatialIndex implements SpatialIndex { |
42 |
|
43 |
private com.vividsolutions.jts.index.quadtree.Quadtree index = null; |
44 |
private boolean modified; |
45 |
|
46 |
public SpatialIndexJTSQuadtree(GeometryManager geometryManager, SpatialIndexFactory factory, DynObject parameters) {
|
47 |
super(geometryManager, factory, parameters);
|
48 |
this.index = null; |
49 |
this.modified = false; |
50 |
open(); |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public void open() { |
55 |
this.index = new com.vividsolutions.jts.index.quadtree.Quadtree(); |
56 |
File f = (File) this.getParameter("file"); |
57 |
if( f!=null && f.exists() ) { |
58 |
try {
|
59 |
this.load(f);
|
60 |
} catch (FileNotFoundException ex) { |
61 |
throw new RuntimeException("Can't load spatial index from '"+f.getAbsolutePath()+"'."); |
62 |
} |
63 |
} |
64 |
} |
65 |
|
66 |
@Override
|
67 |
public void close() { |
68 |
File f = (File) this.getParameter("file"); |
69 |
if( f!=null && modified ) { |
70 |
try {
|
71 |
this.save(f);
|
72 |
} catch (FileNotFoundException ex) { |
73 |
throw new RuntimeException("Can't save spatial index to '"+f.getAbsolutePath()+"'."); |
74 |
} |
75 |
} |
76 |
this.index = null; |
77 |
} |
78 |
|
79 |
private void load(File fname) throws FileNotFoundException { |
80 |
FileInputStream is = new FileInputStream(fname); |
81 |
Object x = SerializationUtils.deserialize(is);
|
82 |
this.index = (Quadtree) x;
|
83 |
this.modified = false; |
84 |
} |
85 |
|
86 |
private void save(File fname) throws FileNotFoundException { |
87 |
FileOutputStream os = new FileOutputStream(fname); |
88 |
SerializationUtils.serialize(this.index, os);
|
89 |
this.modified = false; |
90 |
} |
91 |
|
92 |
private com.vividsolutions.jts.geom.Geometry asJTS(org.gvsig.fmap.geom.Geometry geom) {
|
93 |
return ((GeometryJTS)geom).getJTS();
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public long size() { |
98 |
return this.index.size(); |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void query(org.gvsig.fmap.geom.primitive.Envelope envelope, |
103 |
Visitor visitor) { |
104 |
com.vividsolutions.jts.index.ItemVisitor visitor_jts = new JTSVisitorWrapper(visitor);
|
105 |
com.vividsolutions.jts.geom.Envelope env_jts = asJTS(envelope.getGeometry()).getEnvelopeInternal(); |
106 |
this.index.query(env_jts, visitor_jts);
|
107 |
|
108 |
} |
109 |
|
110 |
@Override
|
111 |
public Iterator query(org.gvsig.fmap.geom.primitive.Envelope envelope,long limit) { |
112 |
if( limit!=0 ) { |
113 |
throw new UnsupportedOperationException("Not supported yet."); |
114 |
} |
115 |
com.vividsolutions.jts.geom.Envelope env_jts = asJTS(envelope.getGeometry()).getEnvelopeInternal(); |
116 |
List result = this.index.query(env_jts); |
117 |
return result.iterator();
|
118 |
} |
119 |
|
120 |
@Override
|
121 |
public Iterator queryNearest(org.gvsig.fmap.geom.primitive.Envelope envelope, long limit) { |
122 |
throw new UnsupportedOperationException("Not supported yet."); |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public Iterator queryAll() { |
127 |
List result = this.index.queryAll(); |
128 |
return result.iterator();
|
129 |
} |
130 |
|
131 |
@Override
|
132 |
public void insert(org.gvsig.fmap.geom.primitive.Envelope envelope, Object data) { |
133 |
com.vividsolutions.jts.geom.Envelope env_jts = asJTS(envelope.getGeometry()).getEnvelopeInternal(); |
134 |
index.insert(env_jts, data); |
135 |
this.modified = true; |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public boolean remove(org.gvsig.fmap.geom.primitive.Envelope envelope, Object data) { |
140 |
com.vividsolutions.jts.geom.Envelope env_jts = asJTS(envelope.getGeometry()).getEnvelopeInternal(); |
141 |
this.modified = true; |
142 |
return index.remove(env_jts, data);
|
143 |
} |
144 |
|
145 |
@Override
|
146 |
public void removeAll() { |
147 |
index = new com.vividsolutions.jts.index.quadtree.Quadtree();
|
148 |
this.modified = true; |
149 |
} |
150 |
|
151 |
private class JTSVisitorWrapper implements com.vividsolutions.jts.index.ItemVisitor { |
152 |
|
153 |
private Visitor visitor = null; |
154 |
|
155 |
public JTSVisitorWrapper(Visitor visitor) {
|
156 |
this.visitor = visitor;
|
157 |
} |
158 |
|
159 |
@Override
|
160 |
public void visitItem(Object arg0) { |
161 |
try {
|
162 |
this.visitor.visit(arg0);
|
163 |
} catch (Exception e) { |
164 |
throw new RuntimeException(); |
165 |
} |
166 |
} |
167 |
|
168 |
} |
169 |
|
170 |
} |