root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / closestpts / NearestNeighbourFinder.java @ 59
History | View | Annotate | Download (2.86 KB)
1 |
package es.unex.sextante.closestpts; |
---|---|
2 |
|
3 |
import java.util.List; |
4 |
|
5 |
import com.vividsolutions.jts.geom.Coordinate; |
6 |
import com.vividsolutions.jts.geom.Envelope; |
7 |
import com.vividsolutions.jts.geom.Geometry; |
8 |
import com.vividsolutions.jts.index.strtree.STRtree; |
9 |
|
10 |
import es.unex.sextante.core.ITaskMonitor; |
11 |
import es.unex.sextante.core.Sextante; |
12 |
import es.unex.sextante.dataObjects.IFeature; |
13 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
14 |
import es.unex.sextante.dataObjects.IVectorLayer; |
15 |
import es.unex.sextante.exceptions.IteratorException; |
16 |
|
17 |
public class NearestNeighbourFinder { |
18 |
|
19 |
private final STRtree m_Tree; |
20 |
private final int m_iPoints; |
21 |
private final double m_dX[]; |
22 |
private final double m_dY[]; |
23 |
private final double m_dZ[]; |
24 |
|
25 |
|
26 |
public NearestNeighbourFinder(final IVectorLayer layer, |
27 |
final int iField, |
28 |
final ITaskMonitor task) throws IteratorException { |
29 |
|
30 |
int i;
|
31 |
float x, y;
|
32 |
|
33 |
task.setProgressText(Sextante.getText("Creating_index"));
|
34 |
|
35 |
m_Tree = new STRtree();
|
36 |
|
37 |
m_iPoints = layer.getShapesCount(); |
38 |
m_dX = new double[m_iPoints]; |
39 |
m_dY = new double[m_iPoints]; |
40 |
m_dZ = new double[m_iPoints]; |
41 |
i = 0;
|
42 |
final IFeatureIterator iter = layer.iterator();
|
43 |
while (iter.hasNext() && !task.isCanceled()) {
|
44 |
final IFeature feature = iter.next();
|
45 |
final Geometry geom = feature.getGeometry();
|
46 |
final Coordinate coord = geom.getCoordinate();
|
47 |
x = (float) coord.x;
|
48 |
y = (float) coord.y;
|
49 |
final Envelope bounds = geom.getEnvelopeInternal();
|
50 |
m_Tree.insert(bounds, new Integer(i)); |
51 |
m_dX[i] = x; |
52 |
m_dY[i] = y; |
53 |
try {
|
54 |
m_dZ[i] = Double.parseDouble(feature.getRecord().getValue(iField).toString());
|
55 |
} |
56 |
catch (final NumberFormatException e) { |
57 |
m_dZ[i] = 0;
|
58 |
} |
59 |
if (i % 50 == 0) { |
60 |
task.setProgress(i, m_iPoints); |
61 |
} |
62 |
i++; |
63 |
} |
64 |
iter.close(); |
65 |
} |
66 |
|
67 |
|
68 |
public PtAndDistance[] getClosestPoints(final double x, |
69 |
final double y, |
70 |
final double dDistance) { |
71 |
|
72 |
int i;
|
73 |
int iID;
|
74 |
double dDist;
|
75 |
final Coordinate pt = new Coordinate(x, y); |
76 |
final Envelope search = new Envelope(pt); |
77 |
search.expandBy(dDistance); |
78 |
final List<Integer> list = m_Tree.query(search); |
79 |
final int iSize = list.size(); |
80 |
final PtAndDistance pts[] = new PtAndDistance[iSize]; |
81 |
for (i = 0; i < iSize; i++) { |
82 |
iID = list.get(i).intValue(); |
83 |
dDist = pt.distance(new Coordinate(m_dX[iID], m_dY[iID]));
|
84 |
pts[i] = new PtAndDistance(new Point3D(m_dX[iID], m_dY[iID], m_dZ[iID]), dDist); |
85 |
} |
86 |
|
87 |
return pts;
|
88 |
|
89 |
} |
90 |
|
91 |
} |