root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / resolvePolygonHoles / NearestNeighbourFinder.java @ 59
History | View | Annotate | Download (1.65 KB)
1 | 59 | nbrodin | package es.unex.sextante.vectorTools.resolvePolygonHoles; |
---|---|---|---|
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 | final float x, y; |
||
27 | |||
28 | task.setProgressText(Sextante.getText("Creating_index"));
|
||
29 | |||
30 | m_Tree = new STRtree();
|
||
31 | |||
32 | m_iShapes = layer.getShapesCount(); |
||
33 | i = 0;
|
||
34 | final IFeatureIterator iter = layer.iterator();
|
||
35 | while (iter.hasNext() && !task.isCanceled()) {
|
||
36 | final IFeature feature = iter.next();
|
||
37 | final Geometry geom = feature.getGeometry();
|
||
38 | final Envelope bounds = geom.getEnvelopeInternal();
|
||
39 | m_Tree.insert(bounds, feature); |
||
40 | if (i % 100 == 0) { |
||
41 | task.setProgress(i, m_iShapes); |
||
42 | } |
||
43 | i++; |
||
44 | } |
||
45 | iter.close(); |
||
46 | } |
||
47 | |||
48 | |||
49 | public IFeature[] getClosestFeatures(final Geometry geom) { |
||
50 | |||
51 | final Envelope search = geom.getEnvelopeInternal();
|
||
52 | final List<IFeature> list = m_Tree.query(search); |
||
53 | |||
54 | return list.toArray(new IFeature[0]); |
||
55 | |||
56 | } |
||
57 | |||
58 | } |