Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / boundingbox / BoundingBoxAlgorithm.java @ 202

History | View | Annotate | Download (2.94 KB)

1

    
2

    
3
package es.unex.sextante.vectorTools.boundingbox;
4

    
5
import com.vividsolutions.jts.geom.Geometry;
6

    
7
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
8
import es.unex.sextante.core.GeoAlgorithm;
9
import es.unex.sextante.core.Sextante;
10
import es.unex.sextante.dataObjects.IFeature;
11
import es.unex.sextante.dataObjects.IFeatureIterator;
12
import es.unex.sextante.dataObjects.IVectorLayer;
13
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter;
14
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
15
import es.unex.sextante.exceptions.RepeatedParameterNameException;
16
import es.unex.sextante.outputs.OutputVectorLayer;
17

    
18
import org.gvsig.tools.exception.BaseException;
19

    
20

    
21
public class BoundingBoxAlgorithm
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 boolean processAlgorithm() throws GeoAlgorithmExecutionException {
31

    
32
      int i = 0;
33

    
34
      final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(LAYER);
35
      if (!m_bIsAutoExtent) {
36
         layerIn.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
37
      }
38

    
39
      int subtype;
40
    try {
41
        subtype = layerIn.getSubType();
42
    } catch (BaseException e) {
43
        subtype = org.gvsig.fmap.geom.Geometry.SUBTYPES.UNKNOWN;
44
    }
45
      IVectorLayer result;
46
        result = getNewVectorLayer(RESULT, Sextante.getText("Bounding Box"), IVectorLayer.SHAPE_TYPE_POLYGON,
47
                   layerIn.getFieldTypes(), layerIn.getFieldNames(), subtype);
48

    
49
      final IFeatureIterator iter = layerIn.iterator();
50
      final int iTotal = layerIn.getShapesCount();
51
      while (iter.hasNext() && setProgress(i, iTotal)) {
52
         final IFeature feature = iter.next();
53
         final Geometry bbox = getBBox(feature.getGeometry());
54
         result.addFeature(bbox, feature.getRecord().getValues());
55
         setProgress(i, iTotal);
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("Bounding_Box"));
68
      setGroup(Sextante.getText("Tools_for_vector_layers"));
69
      setUserCanDefineAnalysisExtent(true);
70

    
71
      try {
72
            m_Parameters.addInputVectorLayer(
73
                LAYER,
74
                Sextante.getText("Layer"),
75
                new AdditionalInfoVectorLayer(
76
                    true,
77
                    AdditionalInfoVectorLayer.MASK_SHAPE_TYPE_LINE | AdditionalInfoVectorLayer.MASK_SHAPE_TYPE_POLYGON
78
                )
79
            );
80
         addOutputVectorLayer(RESULT, Sextante.getText("Bounding_Box"), OutputVectorLayer.SHAPE_TYPE_POLYGON);
81
      }
82
      catch (final RepeatedParameterNameException e) {
83
         Sextante.addErrorToLog(e);
84
      }
85

    
86
   }
87

    
88

    
89
   private Geometry getBBox(final Geometry geometry) {
90
      return geometry.getEnvelope();
91
   }
92

    
93
}