root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / InPolygonSpatialJoin / NearestNeighbourFinder.java @ 59
History | View | Annotate | Download (1.63 KB)
1 |
package es.unex.sextante.vectorTools.InPolygonSpatialJoin; |
---|---|
2 |
|
3 |
import java.util.List; |
4 |
|
5 |
import com.vividsolutions.jts.geom.Envelope; |
6 |
import com.vividsolutions.jts.geom.Geometry; |
7 |
import com.vividsolutions.jts.index.strtree.STRtree; |
8 |
|
9 |
import es.unex.sextante.core.ITaskMonitor; |
10 |
import es.unex.sextante.core.Sextante; |
11 |
import es.unex.sextante.dataObjects.IFeature; |
12 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
13 |
import es.unex.sextante.dataObjects.IVectorLayer; |
14 |
import es.unex.sextante.exceptions.IteratorException; |
15 |
|
16 |
public class NearestNeighbourFinder { |
17 |
|
18 |
private final STRtree m_Tree; |
19 |
private final int m_iShapes; |
20 |
|
21 |
|
22 |
public NearestNeighbourFinder(final IVectorLayer layer, |
23 |
final ITaskMonitor task) throws IteratorException { |
24 |
|
25 |
int i;
|
26 |
|
27 |
task.setProgressText(Sextante.getText("Creating_index"));
|
28 |
|
29 |
m_Tree = new STRtree();
|
30 |
|
31 |
m_iShapes = layer.getShapesCount(); |
32 |
i = 0;
|
33 |
final IFeatureIterator iter = layer.iterator();
|
34 |
while (iter.hasNext() && !task.isCanceled()) {
|
35 |
final IFeature feature = iter.next();
|
36 |
final Geometry geom = feature.getGeometry();
|
37 |
final Envelope bounds = geom.getEnvelopeInternal();
|
38 |
m_Tree.insert(bounds, feature); |
39 |
if (i % 100 == 0) { |
40 |
task.setProgress(i, m_iShapes); |
41 |
} |
42 |
i++; |
43 |
} |
44 |
iter.close(); |
45 |
} |
46 |
|
47 |
|
48 |
public IFeature[] getClosestPoints(final Geometry geom) { |
49 |
|
50 |
final Envelope search = geom.getEnvelopeInternal();
|
51 |
final List<IFeature> list = m_Tree.query(search); |
52 |
|
53 |
return list.toArray(new IFeature[0]); |
54 |
|
55 |
} |
56 |
|
57 |
} |