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 @ 352

History | View | Annotate | Download (6.85 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 org.gvsig.fmap.dal.exception.DataException;
27
import org.gvsig.fmap.dal.feature.FeatureStore;
28
import org.gvsig.fmap.dal.feature.FeatureType;
29
import org.gvsig.geoprocess.algorithm.base.core.ScalableUnionOperation;
30
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
31
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
32

    
33
import es.unex.sextante.core.Sextante;
34
import es.unex.sextante.dataObjects.IVectorLayer;
35
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
36
import es.unex.sextante.exceptions.RepeatedParameterNameException;
37
import es.unex.sextante.outputs.OutputVectorLayer;
38

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

    
45
        public static final String  RESULT_POL            = "RESULT_POL";
46
        public static final String  RESULT_LIN            = "RESULT_LIN";
47
        public static final String  RESULT_POINT          = "RESULT_POINT";
48
        public static final String  LAYER                 = "LAYER";
49
        public static final String  CLIP                  = "CLIP";
50
        public static final String  SELECTGEOM_INPUT      = "SELECTGEOM_INPUT";
51
        public static final String  SELECTGEOM_OVERLAY    = "SELECTGEOM_OVERLAY";
52
        
53
        /*
54
         * (non-Javadoc)
55
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
56
         */
57
        public void defineCharacteristics() {
58
        setName(getTranslation("Clip"));
59
        setGroup(getTranslation("basic_vect_algorithms"));
60
        // setGeneratesUserDefinedRasterOutput(false);
61
                
62
                try {
63
                        m_Parameters.addInputVectorLayer(LAYER, 
64
                getTranslation("Input_layer"),
65
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
66
                                                                                                true);
67
                        m_Parameters.addInputVectorLayer(CLIP, 
68
                getTranslation("Clip_layer"),
69
                                                                                                IVectorLayer.SHAPE_TYPE_POLYGON, 
70
                                                                                                true);
71
            m_Parameters.addBoolean(SELECTGEOM_INPUT, 
72
                            getTranslation("Selected_geometries_input_layer_clip"), false);
73
            m_Parameters.addBoolean(SELECTGEOM_OVERLAY, 
74
                            getTranslation("Selected_geometries_overlay_layer_clip"), false);
75
                } catch (RepeatedParameterNameException e) {
76
                        Sextante.addErrorToLog(e);
77
                }
78
        //addOutputVectorLayer(RESULT, getTranslation("Clip"),
79
           // OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
80
        
81
        addOutputVectorLayer(RESULT_POL, getTranslation("Clip_polygon"),
82
                        OutputVectorLayer.SHAPE_TYPE_POLYGON);
83
        addOutputVectorLayer(RESULT_LIN, getTranslation("Clip_line"),
84
                        OutputVectorLayer.SHAPE_TYPE_LINE);
85
        addOutputVectorLayer(RESULT_POINT, getTranslation("Clip_point"),
86
                        OutputVectorLayer.SHAPE_TYPE_POINT);
87
        }
88
        
89
        /*
90
         * (non-Javadoc)
91
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
92
         */
93
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
94
                if(existsOutPutFile(ClipAlgorithm.RESULT_POL, 0) || 
95
                        existsOutPutFile(ClipAlgorithm.RESULT_LIN, 0) ||
96
                        existsOutPutFile(ClipAlgorithm.RESULT_POINT, 0)) {
97
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
98
            }
99
                org.gvsig.fmap.geom.Geometry clippingGeometry = null;
100
                IVectorLayer clip = m_Parameters.getParameterValueAsVectorLayer(CLIP);
101
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
102
                boolean selectedGeomInput = m_Parameters.getParameter(SELECTGEOM_INPUT).getParameterValueAsBoolean();
103
                boolean selectedGeomOverlay = m_Parameters.getParameter(SELECTGEOM_OVERLAY).getParameterValueAsBoolean();
104
                
105
                try {
106
                        clippingGeometry = ScalableUnionOperation.joinLayerGeometries(clip, selectedGeomOverlay);
107
                } catch (Exception e) {
108
                        Sextante.addErrorToLog(e);
109
                        return false;
110
                }
111
                
112
                FeatureStore storeLayer = null;
113
                if(layer instanceof FlyrVectIVectorLayer && clippingGeometry != null)
114
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
115
                else
116
                        return false;
117
                
118
                try {
119
                        FeatureType featureType = storeLayer.getDefaultFeatureType();
120
                        
121
                        if(isPolygon(storeLayer) || isUndefined(storeLayer)) {
122
                                FeatureStore outFeatStore =
123
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON,
124
                                                        getTranslation("Clip_polygon"), RESULT_POL);
125

    
126
                                ClipOperation operation = new ClipOperation(clippingGeometry, this);
127
                                operation.setTaskStatus(getStatus());
128
                                operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, 
129
                                                selectedGeomInput, false, true);
130
                        } else {
131
                                buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON,
132
                                                        getTranslation("Null_polygon"), RESULT_POL);
133
                        }
134
                        
135
                        if(isLine(storeLayer) || isUndefined(storeLayer)) {
136
                                FeatureStore outFeatStore =
137
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_LINE,
138
                                                        getTranslation("Clip_line"), RESULT_LIN);
139

    
140
                                ClipOperation operation = new ClipOperation(clippingGeometry, this);
141
                                operation.setTaskStatus(getStatus());
142
                                operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, 
143
                                                selectedGeomInput, false, true);
144
                        } else {
145
                                buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_LINE,
146
                                                getTranslation("Null_line"), RESULT_LIN);
147
                        }
148
                        
149
                        if(isPoint(storeLayer) || isUndefined(storeLayer)) {
150
                                FeatureStore outFeatStore =
151
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POINT,
152
                                                        getTranslation("Clip_point"), RESULT_POINT);
153

    
154
                                ClipOperation operation = new ClipOperation(clippingGeometry, this);
155
                                operation.setTaskStatus(getStatus());
156
                                operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, 
157
                                                selectedGeomInput, false, true);
158
                        } else {
159
                                buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POINT,
160
                                                getTranslation("Null_point"), RESULT_POINT);
161
                        }
162
                } catch (DataException e) {
163
                        Sextante.addErrorToLog(e);
164
                        return false;
165
                }
166
                
167
                return true;
168
        }
169
        
170
}