root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / clipByRectangle / ClipByRectangleAlgorithm.java @ 59
History | View | Annotate | Download (4.02 KB)
1 |
package es.unex.sextante.vectorTools.clipByRectangle; |
---|---|
2 |
|
3 |
import com.vividsolutions.jts.geom.Geometry; |
4 |
import com.vividsolutions.jts.geom.prep.PreparedGeometry; |
5 |
import com.vividsolutions.jts.geom.prep.PreparedGeometryFactory; |
6 |
import com.vividsolutions.jts.index.strtree.STRtree; |
7 |
|
8 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
9 |
import es.unex.sextante.core.GeoAlgorithm; |
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.GeoAlgorithmExecutionException; |
15 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
16 |
import es.unex.sextante.vectorTools.clip.ClipAlgorithm; |
17 |
|
18 |
// Author Nacho Varela (Based on ClipAlgorithm)
|
19 |
//TODO Checkbox to choose if get whole geometries that touch the rectangle or clip them
|
20 |
//TODO Enable select a full extent of other layer or select by clicking
|
21 |
public class ClipByRectangleAlgorithm |
22 |
extends
|
23 |
GeoAlgorithm { |
24 |
|
25 |
public static final String LAYER = "LAYER"; |
26 |
public static final String YMAX = "YMAX"; |
27 |
public static final String XMAX = "XMAX"; |
28 |
public static final String YMIN = "YMIN"; |
29 |
public static final String XMIN = "XMIN"; |
30 |
public static final String RESULT = "RESULT"; |
31 |
|
32 |
private IVectorLayer m_Output;
|
33 |
|
34 |
|
35 |
@Override
|
36 |
public void defineCharacteristics() { |
37 |
|
38 |
setName(Sextante.getText("Clip_by_rectangle"));
|
39 |
setGroup(Sextante.getText("Tools_for_vector_layers"));
|
40 |
setUserCanDefineAnalysisExtent(true);
|
41 |
|
42 |
try {
|
43 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Layer_to_clip"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY,
|
44 |
true);
|
45 |
/*m_Parameters.addNumericalValue(XMIN, "X Min", -100, AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
|
46 |
m_Parameters.addNumericalValue(XMAX, "X Max", 100, AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
|
47 |
m_Parameters.addNumericalValue(YMIN, "Y Min", -100, AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
|
48 |
m_Parameters.addNumericalValue(YMAX, "Y Max", 100, AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);*/
|
49 |
|
50 |
addOutputVectorLayer(RESULT, Sextante.getText("Clipped_Layer"));
|
51 |
} |
52 |
catch (final RepeatedParameterNameException e) { |
53 |
Sextante.addErrorToLog(e); |
54 |
} |
55 |
|
56 |
} |
57 |
|
58 |
|
59 |
@Override
|
60 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
61 |
|
62 |
final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(LAYER);
|
63 |
|
64 |
/*final double dXMin = m_Parameters.getParameterValueAsDouble(XMIN);
|
65 |
final double dXMax = m_Parameters.getParameterValueAsDouble(XMAX);
|
66 |
final double dYMin = m_Parameters.getParameterValueAsDouble(YMIN);
|
67 |
final double dYMax = m_Parameters.getParameterValueAsDouble(YMAX);*/
|
68 |
|
69 |
final STRtree tree = buildClipTree();//dXMin, dXMax, dYMin, dYMax); |
70 |
|
71 |
m_Output = getNewVectorLayer(RESULT, Sextante.getText("Clipped_Layer"), layerIn.getShapeType(), layerIn.getFieldTypes(),
|
72 |
layerIn.getFieldNames()); |
73 |
|
74 |
final IFeatureIterator iter = layerIn.iterator();
|
75 |
|
76 |
int i = 0; |
77 |
final int iShapeCount = layerIn.getShapesCount(); |
78 |
final ClipAlgorithm alg = new ClipAlgorithm(); |
79 |
while (iter.hasNext() && setProgress(i, iShapeCount)) {
|
80 |
final IFeature feature = iter.next();
|
81 |
final Geometry g = alg.clipGeometry(feature.getGeometry(), tree);
|
82 |
if (g != null) { |
83 |
m_Output.addFeature(g, feature.getRecord().getValues()); |
84 |
} |
85 |
i++; |
86 |
} |
87 |
iter.close(); |
88 |
|
89 |
return !m_Task.isCanceled();
|
90 |
|
91 |
} |
92 |
|
93 |
|
94 |
private STRtree buildClipTree() {
|
95 |
|
96 |
final STRtree tree = new STRtree(); |
97 |
|
98 |
final Geometry geom = m_AnalysisExtent.getAsJTSGeometry();
|
99 |
|
100 |
final PreparedGeometry pg = PreparedGeometryFactory.prepare(geom);
|
101 |
tree.insert(pg.getGeometry().getEnvelopeInternal(), pg); |
102 |
|
103 |
return tree;
|
104 |
|
105 |
} |
106 |
|
107 |
} |