root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / medialAxis / MedialAxisAlgorithm.java @ 59
History | View | Annotate | Download (3.92 KB)
1 | 59 | nbrodin | |
---|---|---|---|
2 | |||
3 | package es.unex.sextante.vectorTools.medialAxis; |
||
4 | |||
5 | import java.util.ArrayList; |
||
6 | |||
7 | import com.vividsolutions.jts.geom.Coordinate; |
||
8 | import com.vividsolutions.jts.geom.GeometryFactory; |
||
9 | import com.vividsolutions.jts.geom.LineString; |
||
10 | import com.vividsolutions.jts.geom.MultiLineString; |
||
11 | import com.vividsolutions.jts.geom.Polygon; |
||
12 | |||
13 | import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
||
14 | import es.unex.sextante.core.GeoAlgorithm; |
||
15 | import es.unex.sextante.core.Sextante; |
||
16 | import es.unex.sextante.dataObjects.IFeature; |
||
17 | import es.unex.sextante.dataObjects.IFeatureIterator; |
||
18 | import es.unex.sextante.dataObjects.IVectorLayer; |
||
19 | import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
||
20 | import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
||
21 | import es.unex.sextante.exceptions.RepeatedParameterNameException; |
||
22 | import es.unex.sextante.outputs.OutputVectorLayer; |
||
23 | import gishur.core.SimpleList; |
||
24 | import gishur.x.XPoint; |
||
25 | import gishur.x.XPolygon; |
||
26 | import gishur.x.XSegment; |
||
27 | import gishur.x.voronoi.Skeleton; |
||
28 | |||
29 | |||
30 | public class MedialAxisAlgorithm |
||
31 | extends
|
||
32 | GeoAlgorithm { |
||
33 | |||
34 | public static final String LAYER = "LAYER"; |
||
35 | public static final String RESULT = "RESULT"; |
||
36 | |||
37 | |||
38 | @Override
|
||
39 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
40 | |||
41 | int i = 0; |
||
42 | |||
43 | final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(LAYER);
|
||
44 | if (!m_bIsAutoExtent) {
|
||
45 | layerIn.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
||
46 | } |
||
47 | final IVectorLayer result = getNewVectorLayer(RESULT, Sextante.getText("Medial_Axis"), IVectorLayer.SHAPE_TYPE_LINE, |
||
48 | layerIn.getFieldTypes(), layerIn.getFieldNames()); |
||
49 | |||
50 | final IFeatureIterator iter = layerIn.iterator();
|
||
51 | final int iTotal = layerIn.getShapesCount(); |
||
52 | while (iter.hasNext() && setProgress(i, iTotal)) {
|
||
53 | final IFeature feature = iter.next();
|
||
54 | final MultiLineString axis = getMedialAxis(((Polygon) feature.getGeometry().getGeometryN(0))); |
||
55 | result.addFeature(axis, feature.getRecord().getValues()); |
||
56 | i++; |
||
57 | } |
||
58 | iter.close(); |
||
59 | |||
60 | return !m_Task.isCanceled();
|
||
61 | } |
||
62 | |||
63 | |||
64 | @Override
|
||
65 | public void defineCharacteristics() { |
||
66 | |||
67 | setName(Sextante.getText("Medial_Axis"));
|
||
68 | setGroup(Sextante.getText("Tools_for_polygon_layers"));
|
||
69 | |||
70 | try {
|
||
71 | m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Polygons"), AdditionalInfoVectorLayer.SHAPE_TYPE_POLYGON, true); |
||
72 | addOutputVectorLayer(RESULT, Sextante.getText("Medial_Axis"), OutputVectorLayer.SHAPE_TYPE_LINE);
|
||
73 | } |
||
74 | catch (final RepeatedParameterNameException e) { |
||
75 | Sextante.addErrorToLog(e); |
||
76 | } |
||
77 | |||
78 | } |
||
79 | |||
80 | |||
81 | private MultiLineString getMedialAxis(final Polygon poly) { |
||
82 | |||
83 | final ArrayList<LineString> list = new ArrayList<LineString>(); |
||
84 | final GeometryFactory gf = new GeometryFactory(); |
||
85 | final Coordinate[] coords = poly.getExteriorRing().getCoordinates(); |
||
86 | final XPoint xpoints[] = new XPoint[coords.length]; |
||
87 | for (int i = 0; i < coords.length; i++) { |
||
88 | xpoints[i] = new XPoint(coords[i].x, coords[i].y);
|
||
89 | } |
||
90 | final XPolygon xpoly = new XPolygon(xpoints); |
||
91 | final Skeleton skeleton = new Skeleton(xpoly); |
||
92 | skeleton.execute(); |
||
93 | final SimpleList lines = skeleton.getLines(false); |
||
94 | final Object[] array = lines.convertValuesToArray(); |
||
95 | |||
96 | for (int i = 0; i < array.length; i++) { |
||
97 | final XSegment segment = (XSegment) array[i];
|
||
98 | System.out.println(segment.toString());
|
||
99 | final Coordinate[] segmentCoords = new Coordinate[] { |
||
100 | new Coordinate(segment.getStartPoint().x, segment.getStartPoint().y),
|
||
101 | new Coordinate(segment.getEndPoint().x, segment.getEndPoint().y) };
|
||
102 | list.add(gf.createLineString(segmentCoords)); |
||
103 | } |
||
104 | |||
105 | return gf.createMultiLineString(list.toArray(new LineString[0])); |
||
106 | |||
107 | |||
108 | } |
||
109 | |||
110 | } |