Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.clip / src / main / java / org / gvsig / sextante / app / algorithm / clip / ClipAlgorithm.java @ 172

History | View | Annotate | Download (4.15 KB)

1
/*
2
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2010 Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 */
21
package org.gvsig.sextante.app.algorithm.clip;
22

    
23
import es.unex.sextante.core.Sextante;
24
import es.unex.sextante.dataObjects.IVectorLayer;
25
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
26
import es.unex.sextante.exceptions.RepeatedParameterNameException;
27
import es.unex.sextante.outputs.OutputVectorLayer;
28

    
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.feature.FeatureSet;
31
import org.gvsig.fmap.dal.feature.FeatureStore;
32
import org.gvsig.fmap.dal.feature.FeatureType;
33
import org.gvsig.geoprocess.core.gvGeoAlgorithm;
34
import org.gvsig.geoprocess.core.gvVectorLayer;
35
import org.gvsig.sextante.app.algorithm.base.core.ScalableUnionOperation;
36

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