root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / topology / extractEndpointsOfLines / ExtractEndpointsOfLinesAlgorithm.java @ 59
History | View | Annotate | Download (3.76 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.topology.extractEndpointsOfLines; |
4 |
|
5 |
import com.vividsolutions.jts.geom.Coordinate; |
6 |
import com.vividsolutions.jts.geom.GeometryFactory; |
7 |
import com.vividsolutions.jts.geom.Point; |
8 |
|
9 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
10 |
import es.unex.sextante.core.GeoAlgorithm; |
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.dataObjects.vectorFilters.BoundingBoxFilter; |
16 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
17 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
18 |
import es.unex.sextante.outputs.OutputVectorLayer; |
19 |
|
20 |
|
21 |
public class ExtractEndpointsOfLinesAlgorithm |
22 |
extends
|
23 |
GeoAlgorithm { |
24 |
|
25 |
public static final String LAYER = "LAYER"; |
26 |
public static final String RESULT = "RESULT"; |
27 |
|
28 |
|
29 |
@Override
|
30 |
public void defineCharacteristics() { |
31 |
|
32 |
setName(Sextante.getText("Extract_endpoints_of_lines"));
|
33 |
setGroup(Sextante.getText("Topology"));
|
34 |
setUserCanDefineAnalysisExtent(true);
|
35 |
|
36 |
try {
|
37 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Input_Layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_LINE, true); |
38 |
|
39 |
addOutputVectorLayer(RESULT, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_POINT);
|
40 |
|
41 |
} |
42 |
catch (final RepeatedParameterNameException e) { |
43 |
Sextante.addErrorToLog(e); |
44 |
} |
45 |
|
46 |
} |
47 |
|
48 |
|
49 |
@Override
|
50 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
51 |
|
52 |
int i = 0; |
53 |
final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(ExtractEndpointsOfLinesAlgorithm.LAYER);
|
54 |
if (!m_bIsAutoExtent) {
|
55 |
layerIn.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
56 |
} |
57 |
|
58 |
final Class[] in_ftypes = layerIn.getFieldTypes(); |
59 |
final Class[] out_ftypes = new Class[in_ftypes.length + 2]; |
60 |
System.arraycopy(in_ftypes, 0, out_ftypes, 0, in_ftypes.length); |
61 |
out_ftypes[out_ftypes.length - 2] = Integer.class; |
62 |
out_ftypes[out_ftypes.length - 1] = Integer.class; |
63 |
|
64 |
final String[] in_fnames = layerIn.getFieldNames(); |
65 |
final String[] out_fnames = new String[in_ftypes.length + 2]; |
66 |
System.arraycopy(in_fnames, 0, out_fnames, 0, in_fnames.length); |
67 |
out_fnames[out_fnames.length - 2] = "LINEFID"; |
68 |
out_fnames[out_fnames.length - 1] = "ISSTART"; |
69 |
|
70 |
final IVectorLayer driver = getNewVectorLayer(ExtractEndpointsOfLinesAlgorithm.RESULT, Sextante.getText("Endpoints"), |
71 |
OutputVectorLayer.SHAPE_TYPE_POINT, out_ftypes, out_fnames); |
72 |
|
73 |
final IFeatureIterator iter = layerIn.iterator();
|
74 |
final int iTotal = layerIn.getShapesCount(); |
75 |
final Object[] values = new Object[out_fnames.length]; |
76 |
int count = 0; |
77 |
final GeometryFactory gf = new GeometryFactory(); |
78 |
while (iter.hasNext() && setProgress(i, iTotal)) {
|
79 |
final IFeature feature = iter.next();
|
80 |
final Coordinate[] coords = feature.getGeometry().getCoordinates(); |
81 |
final Object[] aux_values = feature.getRecord().getValues(); |
82 |
System.arraycopy(aux_values, 0, values, 0, aux_values.length); |
83 |
values[values.length - 2] = count;
|
84 |
values[values.length - 1] = 0; |
85 |
final Point startPoint = gf.createPoint(coords[0]); |
86 |
driver.addFeature(startPoint, values); |
87 |
|
88 |
count++; |
89 |
|
90 |
System.arraycopy(aux_values, 0, values, 0, aux_values.length); |
91 |
values[values.length - 2] = count;
|
92 |
values[values.length - 1] = 1; |
93 |
final Point endPoint = gf.createPoint(coords[coords.length - 1]); |
94 |
driver.addFeature(endPoint, values); |
95 |
setProgress(i, iTotal); |
96 |
i++; |
97 |
} |
98 |
iter.close(); |
99 |
|
100 |
return !m_Task.isCanceled();
|
101 |
|
102 |
} |
103 |
|
104 |
} |