Revision 254 org.gvsig.toolbox/trunk/org.gvsig.toolbox/org.gvsig.toolbox.algorithm/src/main/java/es/unex/sextante/vectorTools/distanceTableBuffer/DistanceTableBufferAlgorithm.java
DistanceTableBufferAlgorithm.java | ||
---|---|---|
1 |
|
|
2 |
|
|
3 |
package es.unex.sextante.vectorTools.distanceTableBuffer; |
|
4 |
|
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.Arrays; |
|
7 |
|
|
8 |
import com.vividsolutions.jts.geom.Geometry; |
|
9 |
import com.vividsolutions.jts.operation.buffer.BufferOp; |
|
10 |
|
|
11 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
|
12 |
import es.unex.sextante.core.GeoAlgorithm; |
|
13 |
import es.unex.sextante.core.Sextante; |
|
14 |
import es.unex.sextante.dataObjects.IFeature; |
|
15 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
|
16 |
import es.unex.sextante.dataObjects.IVectorLayer; |
|
17 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
|
18 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
|
19 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
|
20 |
import es.unex.sextante.outputs.OutputVectorLayer; |
|
21 |
import es.unex.sextante.parameters.FixedTableModel; |
|
22 |
|
|
23 |
|
|
24 |
public class DistanceTableBufferAlgorithm |
|
25 |
extends |
|
26 |
GeoAlgorithm { |
|
27 |
|
|
28 |
|
|
29 |
public static final String RESULT = "RESULT"; |
|
30 |
public static final String NOTROUNDED = "NOTROUNDED"; |
|
31 |
public static final String LAYER = "LAYER"; |
|
32 |
public static final String DISTANCES = "DISTANCES"; |
|
33 |
|
|
34 |
private IVectorLayer m_Output; |
|
35 |
private boolean m_bRounded; |
|
36 |
private int m_iNumProcessed = 0; |
|
37 |
private Double[] m_Distances; |
|
38 |
|
|
39 |
|
|
40 |
@Override |
|
41 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
|
42 |
|
|
43 |
final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
|
44 |
if (!m_bIsAutoExtent) { |
|
45 |
layerIn.addFilter(new BoundingBoxFilter(m_AnalysisExtent)); |
|
46 |
} |
|
47 |
|
|
48 |
final FixedTableModel distances = (FixedTableModel) m_Parameters.getParameterValueAsObject(DISTANCES); |
|
49 |
final ArrayList<Double> distancesList = new ArrayList<Double>(); |
|
50 |
for (int i = 0; i < distances.getRowCount(); i++) { |
|
51 |
try { |
|
52 |
final double dDistance = Double.parseDouble(distances.getValueAt(i, 0).toString()); |
|
53 |
if (dDistance > 0) { |
|
54 |
distancesList.add(new Double(dDistance)); |
|
55 |
} |
|
56 |
} |
|
57 |
catch (final Exception e) { |
|
58 |
//ignore wrong values in table |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
if (distancesList.size() == 0) { |
|
63 |
throw new GeoAlgorithmExecutionException(Sextante.getText("No_Elements_In_Distance_List")); |
|
64 |
} |
|
65 |
|
|
66 |
m_Distances = distancesList.toArray(new Double[0]); |
|
67 |
Arrays.sort(m_Distances); |
|
68 |
|
|
69 |
m_bRounded = !m_Parameters.getParameterValueAsBoolean(NOTROUNDED); |
|
70 |
|
|
71 |
final int iFieldCount = layerIn.getFieldCount(); |
|
72 |
Class types[]; |
|
73 |
String[] sFieldNames; |
|
74 |
|
|
75 |
types = new Class[iFieldCount + 2]; |
|
76 |
types[0] = Integer.class; |
|
77 |
types[1] = Double.class; |
|
78 |
sFieldNames = new String[iFieldCount + 2]; |
|
79 |
sFieldNames[0] = "ID"; |
|
80 |
sFieldNames[1] = "DIST"; |
|
81 |
for (int i = 0; i < iFieldCount; i++) { |
|
82 |
sFieldNames[i + 2] = layerIn.getFieldName(i); |
|
83 |
types[i + 2] = layerIn.getFieldType(i); |
|
84 |
} |
|
85 |
|
|
86 |
m_Output = getNewVectorLayer(RESULT, "Buffer", IVectorLayer.SHAPE_TYPE_POLYGON, types, sFieldNames); |
|
87 |
|
|
88 |
int i = 0; |
|
89 |
final int iTotal = layerIn.getShapesCount(); |
|
90 |
final IFeatureIterator iter = layerIn.iterator(); |
|
91 |
while (iter.hasNext() && setProgress(i, iTotal)) { |
|
92 |
final IFeature feature = iter.next(); |
|
93 |
computeBuffer(feature.getGeometry(), feature.getRecord().getValues()); |
|
94 |
i++; |
|
95 |
} |
|
96 |
iter.close(); |
|
97 |
|
|
98 |
return !m_Task.isCanceled(); |
|
99 |
|
|
100 |
} |
|
101 |
|
|
102 |
|
|
103 |
@Override |
|
104 |
public void defineCharacteristics() { |
|
105 |
|
|
106 |
setName(Sextante.getText("Multiple_buffer")); |
|
107 |
setGroup(Sextante.getText("Buffers")); |
|
108 |
setUserCanDefineAnalysisExtent(true); |
|
109 |
|
|
110 |
try { |
|
111 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Input_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
|
112 |
m_Parameters.addFixedTable(DISTANCES, Sextante.getText("Distances_table"), |
|
113 |
new String[] { Sextante.getText("Distance") }, 3, false); |
|
114 |
m_Parameters.addBoolean(NOTROUNDED, Sextante.getText("Do_not_round_resulting_polygons"), false); |
|
115 |
|
|
116 |
addOutputVectorLayer(RESULT, Sextante.getText("Buffer"), OutputVectorLayer.SHAPE_TYPE_POLYGON); |
|
117 |
} |
|
118 |
catch (final RepeatedParameterNameException e) { |
|
119 |
Sextante.addErrorToLog(e); |
|
120 |
} |
|
121 |
|
|
122 |
} |
|
123 |
|
|
124 |
|
|
125 |
public void computeBuffer(final Geometry originalGeometry, |
|
126 |
final Object[] record) { |
|
127 |
|
|
128 |
Geometry solution = null; |
|
129 |
final Geometry inputParam = originalGeometry; |
|
130 |
|
|
131 |
int cap = BufferOp.CAP_ROUND; |
|
132 |
if (!m_bRounded) { |
|
133 |
cap = BufferOp.CAP_SQUARE; |
|
134 |
} |
|
135 |
|
|
136 |
Geometry previousExteriorRing = null; |
|
137 |
for (int i = 0; i < m_Distances.length; i++) { |
|
138 |
final double dDistRing = m_Distances[i].doubleValue(); |
|
139 |
final BufferOp bufOp = new BufferOp(inputParam); |
|
140 |
bufOp.setEndCapStyle(cap); |
|
141 |
final Geometry newGeometry = bufOp.getResultGeometry(dDistRing); |
|
142 |
if (previousExteriorRing != null) { |
|
143 |
solution = newGeometry.difference(previousExteriorRing); |
|
144 |
} |
|
145 |
else { |
|
146 |
solution = newGeometry; |
|
147 |
} |
|
148 |
m_iNumProcessed++; |
|
149 |
addFeature(solution, dDistRing, record); |
|
150 |
previousExteriorRing = newGeometry; |
|
151 |
} |
|
152 |
|
|
153 |
} |
|
154 |
|
|
155 |
|
|
156 |
protected void addFeature(final Geometry geom, |
|
157 |
final double dDistance, |
|
158 |
final Object[] record) { |
|
159 |
|
|
160 |
final Object[] values = new Object[2 + record.length]; |
|
161 |
values[0] = new Long(m_iNumProcessed); |
|
162 |
values[1] = new Double(dDistance); |
|
163 |
for (int i = 0; i < record.length; i++) { |
|
164 |
values[i + 2] = record[i]; |
|
165 |
} |
|
166 |
m_Output.addFeature(geom, values); |
|
167 |
|
|
168 |
} |
|
169 |
|
|
170 |
} |
|
1 |
|
|
2 |
|
|
3 |
package es.unex.sextante.vectorTools.distanceTableBuffer; |
|
4 |
|
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.Arrays; |
|
7 |
|
|
8 |
import com.vividsolutions.jts.geom.Geometry; |
|
9 |
import com.vividsolutions.jts.operation.buffer.BufferOp; |
|
10 |
|
|
11 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
|
12 |
import es.unex.sextante.core.GeoAlgorithm; |
|
13 |
import es.unex.sextante.core.Sextante; |
|
14 |
import es.unex.sextante.dataObjects.IFeature; |
|
15 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
|
16 |
import es.unex.sextante.dataObjects.IVectorLayer; |
|
17 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
|
18 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
|
19 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
|
20 |
import es.unex.sextante.outputs.OutputVectorLayer; |
|
21 |
import es.unex.sextante.parameters.FixedTableModel; |
|
22 |
|
|
23 |
|
|
24 |
public class DistanceTableBufferAlgorithm |
|
25 |
extends |
|
26 |
GeoAlgorithm { |
|
27 |
|
|
28 |
|
|
29 |
public static final String RESULT = "RESULT"; |
|
30 |
public static final String NOTROUNDED = "NOTROUNDED"; |
|
31 |
public static final String LAYER = "LAYER"; |
|
32 |
public static final String DISTANCES = "DISTANCES"; |
|
33 |
|
|
34 |
private IVectorLayer m_Output; |
|
35 |
private boolean m_bRounded; |
|
36 |
private int m_iNumProcessed = 0; |
|
37 |
private Double[] m_Distances; |
|
38 |
|
|
39 |
|
|
40 |
@Override |
|
41 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
|
42 |
|
|
43 |
final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
|
44 |
if (!m_bIsAutoExtent) { |
|
45 |
layerIn.addFilter(new BoundingBoxFilter(m_AnalysisExtent)); |
|
46 |
} |
|
47 |
|
|
48 |
final FixedTableModel distances = (FixedTableModel) m_Parameters.getParameterValueAsObject(DISTANCES); |
|
49 |
final ArrayList<Double> distancesList = new ArrayList<Double>(); |
|
50 |
for (int i = 0; i < distances.getRowCount(); i++) { |
|
51 |
try { |
|
52 |
final double dDistance = Double.parseDouble(distances.getValueAt(i, 0).toString()); |
|
53 |
if (dDistance > 0) { |
|
54 |
distancesList.add(new Double(dDistance)); |
|
55 |
} |
|
56 |
} |
|
57 |
catch (final Exception e) { |
|
58 |
//ignore wrong values in table |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
if (distancesList.size() == 0) { |
|
63 |
throw new GeoAlgorithmExecutionException(Sextante.getText("No_Elements_In_Distance_List")); |
|
64 |
} |
|
65 |
|
|
66 |
m_Distances = distancesList.toArray(new Double[0]); |
|
67 |
Arrays.sort(m_Distances); |
|
68 |
|
|
69 |
m_bRounded = !m_Parameters.getParameterValueAsBoolean(NOTROUNDED); |
|
70 |
|
|
71 |
final int iFieldCount = layerIn.getFieldCount(); |
|
72 |
Class types[]; |
|
73 |
String[] sFieldNames; |
|
74 |
|
|
75 |
types = new Class[iFieldCount + 2]; |
|
76 |
types[0] = Integer.class; |
|
77 |
types[1] = Double.class; |
|
78 |
sFieldNames = new String[iFieldCount + 2]; |
|
79 |
sFieldNames[0] = "ID"; |
|
80 |
sFieldNames[1] = "DIST"; |
|
81 |
for (int i = 0; i < iFieldCount; i++) { |
|
82 |
sFieldNames[i + 2] = checkAttrName(layerIn.getFieldName(i), i + 2, sFieldNames); |
|
83 |
types[i + 2] = layerIn.getFieldType(i); |
|
84 |
} |
|
85 |
|
|
86 |
m_Output = getNewVectorLayer(RESULT, "Buffer", IVectorLayer.SHAPE_TYPE_POLYGON, types, sFieldNames); |
|
87 |
|
|
88 |
int i = 0; |
|
89 |
final int iTotal = layerIn.getShapesCount(); |
|
90 |
final IFeatureIterator iter = layerIn.iterator(); |
|
91 |
while (iter.hasNext() && setProgress(i, iTotal)) { |
|
92 |
final IFeature feature = iter.next(); |
|
93 |
computeBuffer(feature.getGeometry(), feature.getRecord().getValues()); |
|
94 |
i++; |
|
95 |
} |
|
96 |
iter.close(); |
|
97 |
|
|
98 |
return !m_Task.isCanceled(); |
|
99 |
|
|
100 |
} |
|
101 |
|
|
102 |
|
|
103 |
@Override |
|
104 |
public void defineCharacteristics() { |
|
105 |
|
|
106 |
setName(Sextante.getText("Multiple_buffer")); |
|
107 |
setGroup(Sextante.getText("Buffers")); |
|
108 |
setUserCanDefineAnalysisExtent(true); |
|
109 |
|
|
110 |
try { |
|
111 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Input_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
|
112 |
m_Parameters.addFixedTable(DISTANCES, Sextante.getText("Distances_table"), |
|
113 |
new String[] { Sextante.getText("Distance") }, 3, false); |
|
114 |
m_Parameters.addBoolean(NOTROUNDED, Sextante.getText("Do_not_round_resulting_polygons"), false); |
|
115 |
|
|
116 |
addOutputVectorLayer(RESULT, Sextante.getText("Buffer"), OutputVectorLayer.SHAPE_TYPE_POLYGON); |
|
117 |
} |
|
118 |
catch (final RepeatedParameterNameException e) { |
|
119 |
Sextante.addErrorToLog(e); |
|
120 |
} |
|
121 |
|
|
122 |
} |
|
123 |
|
|
124 |
|
|
125 |
public void computeBuffer(final Geometry originalGeometry, |
|
126 |
final Object[] record) { |
|
127 |
|
|
128 |
Geometry solution = null; |
|
129 |
final Geometry inputParam = originalGeometry; |
|
130 |
|
|
131 |
int cap = BufferOp.CAP_ROUND; |
|
132 |
if (!m_bRounded) { |
|
133 |
cap = BufferOp.CAP_SQUARE; |
|
134 |
} |
|
135 |
|
|
136 |
Geometry previousExteriorRing = null; |
|
137 |
for (int i = 0; i < m_Distances.length; i++) { |
|
138 |
final double dDistRing = m_Distances[i].doubleValue(); |
|
139 |
final BufferOp bufOp = new BufferOp(inputParam); |
|
140 |
bufOp.setEndCapStyle(cap); |
|
141 |
final Geometry newGeometry = bufOp.getResultGeometry(dDistRing); |
|
142 |
if (previousExteriorRing != null) { |
|
143 |
solution = newGeometry.difference(previousExteriorRing); |
|
144 |
} |
|
145 |
else { |
|
146 |
solution = newGeometry; |
|
147 |
} |
|
148 |
m_iNumProcessed++; |
|
149 |
addFeature(solution, dDistRing, record); |
|
150 |
previousExteriorRing = newGeometry; |
|
151 |
} |
|
152 |
|
|
153 |
} |
|
154 |
|
|
155 |
|
|
156 |
protected void addFeature(final Geometry geom, |
|
157 |
final double dDistance, |
|
158 |
final Object[] record) { |
|
159 |
|
|
160 |
final Object[] values = new Object[2 + record.length]; |
|
161 |
values[0] = new Long(m_iNumProcessed); |
|
162 |
values[1] = new Double(dDistance); |
|
163 |
for (int i = 0; i < record.length; i++) { |
|
164 |
values[i + 2] = record[i]; |
|
165 |
} |
|
166 |
m_Output.addFeature(geom, values); |
|
167 |
|
|
168 |
} |
|
169 |
|
|
170 |
} |
Also available in: Unified diff