root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / rTree / SextanteRTree.java @ 59
History | View | Annotate | Download (2.5 KB)
1 |
package es.unex.sextante.rTree; |
---|---|
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.Coordinate; |
9 |
import com.vividsolutions.jts.geom.Geometry; |
10 |
|
11 |
import es.unex.sextante.closestpts.Point3D; |
12 |
import es.unex.sextante.core.ITaskMonitor; |
13 |
import es.unex.sextante.core.Sextante; |
14 |
import es.unex.sextante.dataObjects.IFeature; |
15 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
16 |
import es.unex.sextante.dataObjects.IVectorLayer; |
17 |
import es.unex.sextante.exceptions.IteratorException; |
18 |
|
19 |
public class SextanteRTree { |
20 |
|
21 |
private final RTree m_Tree; |
22 |
private final int m_iPoints; |
23 |
private final double m_dX[]; |
24 |
private final double m_dY[]; |
25 |
private final double m_dZ[]; |
26 |
|
27 |
|
28 |
public SextanteRTree(final IVectorLayer layer, |
29 |
final int iField, |
30 |
final ITaskMonitor task) {
|
31 |
|
32 |
int i;
|
33 |
float x, y;
|
34 |
|
35 |
task.setProgressText(Sextante.getText("Creating_index"));
|
36 |
m_Tree = new RTree();
|
37 |
m_Tree.init(new Properties()); |
38 |
m_iPoints = layer.getShapesCount(); |
39 |
m_dX = new double[m_iPoints]; |
40 |
m_dY = new double[m_iPoints]; |
41 |
m_dZ = new double[m_iPoints]; |
42 |
i = 0;
|
43 |
final IFeatureIterator iter = layer.iterator();
|
44 |
while (iter.hasNext() && !task.isCanceled()) {
|
45 |
IFeature feature; |
46 |
try {
|
47 |
feature = iter.next(); |
48 |
final Geometry geom = feature.getGeometry();
|
49 |
final Coordinate coord = geom.getCoordinate();
|
50 |
x = (float) coord.x;
|
51 |
y = (float) coord.y;
|
52 |
final Rectangle rectangle = new Rectangle(x, y, x, y); |
53 |
m_Tree.add(rectangle, i); |
54 |
m_dX[i] = x; |
55 |
m_dY[i] = y; |
56 |
try {
|
57 |
m_dZ[i] = Double.parseDouble(feature.getRecord().getValue(iField).toString());
|
58 |
} |
59 |
catch (final Exception e) { |
60 |
m_dZ[i] = 0;
|
61 |
} |
62 |
if (i % 50 == 0) { |
63 |
task.setProgress(i, m_iPoints); |
64 |
} |
65 |
i++; |
66 |
} |
67 |
catch (final IteratorException e1) { |
68 |
//skip point
|
69 |
} |
70 |
} |
71 |
|
72 |
} |
73 |
|
74 |
|
75 |
public Point3D getClosestPoint(final double x, |
76 |
final double y) { |
77 |
|
78 |
final Point pt = new Point((float) x, (float) y); |
79 |
final int iIdx = m_Tree.nearest(pt); |
80 |
return new Point3D(m_dX[iIdx], m_dY[iIdx], m_dZ[iIdx]); |
81 |
|
82 |
} |
83 |
|
84 |
|
85 |
} |