root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / NNSpatialJoin / SextanteRTree.java @ 59
History | View | Annotate | Download (2.21 KB)
1 |
package es.unex.sextante.vectorTools.NNSpatialJoin; |
---|---|
2 |
|
3 |
import java.util.Properties; |
4 |
|
5 |
import com.infomatiq.jsi.Point; |
6 |
import com.infomatiq.jsi.Rectangle; |
7 |
import com.infomatiq.jsi.rtree.RTree; |
8 |
import com.vividsolutions.jts.geom.Envelope; |
9 |
import com.vividsolutions.jts.geom.Geometry; |
10 |
|
11 |
import es.unex.sextante.core.ITaskMonitor; |
12 |
import es.unex.sextante.core.Sextante; |
13 |
import es.unex.sextante.dataObjects.IFeature; |
14 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
15 |
import es.unex.sextante.dataObjects.IVectorLayer; |
16 |
import es.unex.sextante.exceptions.IteratorException; |
17 |
|
18 |
public class SextanteRTree { |
19 |
|
20 |
private final RTree m_Tree; |
21 |
private final int m_iShapes; |
22 |
private final IFeature[] m_Features; |
23 |
|
24 |
|
25 |
//TODO:this tree loads all features in memory, so it will not work with large datasets
|
26 |
public SextanteRTree(final IVectorLayer layer, |
27 |
final ITaskMonitor task) {
|
28 |
|
29 |
int i;
|
30 |
|
31 |
task.setProgressText(Sextante.getText("Creating_index"));
|
32 |
m_Tree = new RTree();
|
33 |
m_Tree.init(new Properties()); |
34 |
|
35 |
m_iShapes = layer.getShapesCount(); |
36 |
m_Features = new IFeature[m_iShapes];
|
37 |
i = 0;
|
38 |
final IFeatureIterator iter = layer.iterator();
|
39 |
while (iter.hasNext() && !task.isCanceled()) {
|
40 |
try {
|
41 |
final IFeature feature = iter.next();
|
42 |
final Geometry geom = feature.getGeometry();
|
43 |
final Envelope bounds = geom.getEnvelopeInternal();
|
44 |
final Rectangle rect = new Rectangle((float) bounds.getMinX(), (float) bounds.getMinY(), (float) bounds.getMaxX(), |
45 |
(float) bounds.getMaxY());
|
46 |
m_Tree.add(rect, new Integer(i)); |
47 |
m_Features[i] = feature; |
48 |
|
49 |
if (i % 50 == 0) { |
50 |
task.setProgress(i, m_iShapes); |
51 |
} |
52 |
i++; |
53 |
} |
54 |
catch (final IteratorException e1) { |
55 |
//skip feature
|
56 |
} |
57 |
} |
58 |
iter.close(); |
59 |
|
60 |
} |
61 |
|
62 |
|
63 |
public IFeature getClosestFeature(final double x, |
64 |
final double y) { |
65 |
|
66 |
final Point pt = new Point((float) x, (float) y); |
67 |
final int iIdx = m_Tree.nearest(pt); |
68 |
return m_Features[iIdx];
|
69 |
|
70 |
} |
71 |
|
72 |
|
73 |
} |