Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.clip / src / main / java / org / gvsig / geoprocess / algorithm / clip / ClipAlgorithm.java @ 245

History | View | Annotate | Download (4.42 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.clip;
25

    
26
import es.unex.sextante.core.Sextante;
27
import es.unex.sextante.dataObjects.IVectorLayer;
28
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
29
import es.unex.sextante.exceptions.RepeatedParameterNameException;
30
import es.unex.sextante.outputs.OutputVectorLayer;
31

    
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.FeatureSet;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.geoprocess.algorithm.base.core.ScalableUnionOperation;
37
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
38
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
39

    
40
/**
41
 * Clip algorithm
42
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
43
 */
44
public class ClipAlgorithm extends AbstractSextanteGeoProcess {
45

    
46
        public static final String  RESULT    = "RESULT";
47
        public static final String  LAYER     = "LAYER";
48
        public static final String  CLIP      = "CLIP";
49
        public static final String  CHECK     = "CHECK";
50
        
51
        /*
52
         * (non-Javadoc)
53
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
54
         */
55
        public void defineCharacteristics(){
56
        setName(getTranslation("Clip"));
57
        setGroup(getTranslation("basic_vect_algorithms"));
58
        // setGeneratesUserDefinedRasterOutput(false);
59
                
60
                try {
61
                        m_Parameters.addInputVectorLayer(LAYER, 
62
                getTranslation("Input_layer"),
63
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
64
                                                                                                true);
65
                        m_Parameters.addInputVectorLayer(CLIP, 
66
                getTranslation("Clip_layer"),
67
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
68
                                                                                                true);
69
            m_Parameters.addBoolean(CHECK,
70
                getTranslation("Selected_geometries"), false);
71
                } catch (RepeatedParameterNameException e) {
72
                        Sextante.addErrorToLog(e);
73
                }
74
        addOutputVectorLayer(RESULT, getTranslation("Clip"),
75
            OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
76
        }
77
        
78
        /*
79
         * (non-Javadoc)
80
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
81
         */
82
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
83
                org.gvsig.fmap.geom.Geometry clippingGeometry = null;
84
                IVectorLayer clip = m_Parameters.getParameterValueAsVectorLayer(CLIP);
85
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
86
                boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
87
                
88
                try {
89
                        clippingGeometry = ScalableUnionOperation.joinLayerGeometries(clip);
90
                } catch (Exception e) {
91
                        Sextante.addErrorToLog(e);
92
                        return false;
93
                }
94
                
95
                FeatureStore storeLayer = null;
96
                if(layer instanceof FlyrVectIVectorLayer && clippingGeometry != null)
97
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
98
                else
99
                        return false;
100
                
101
                try {
102
                        FeatureSet features = null;
103
                        features = storeLayer.getFeatureSet();
104
                        FeatureType featureType = features.getDefaultFeatureType();
105
            FeatureStore outFeatStore =
106
                buildOutPutStore(featureType, layer.getShapeType(),
107
                    getTranslation("Clip"), RESULT);
108
                        
109
                        ClipOperation operation = new ClipOperation(clippingGeometry);
110
            operation.setTaskStatus(getStatus());
111
                        operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
112
                } catch (DataException e) {
113
                        Sextante.addErrorToLog(e);
114
                        return false;
115
                }
116
                
117
                return true;
118
        }
119
        
120
}