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

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

    
34
import es.unex.sextante.core.Sextante;
35
import es.unex.sextante.dataObjects.IVectorLayer;
36
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
37
import es.unex.sextante.exceptions.RepeatedParameterNameException;
38
import es.unex.sextante.outputs.OutputVectorLayer;
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_POL      = "RESULT_POL";
47
        public static final String  RESULT_LIN      = "RESULT_LIN";
48
        public static final String  RESULT_POINT    = "RESULT_POINT";
49
        public static final String  LAYER           = "LAYER";
50
        public static final String  CLIP            = "CLIP";
51
        public static final String  CHECK           = "CHECK";
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(CHECK,
72
                getTranslation("Selected_geometries_input_layer"), false);
73
                } catch (RepeatedParameterNameException e) {
74
                        Sextante.addErrorToLog(e);
75
                }
76
        //addOutputVectorLayer(RESULT, getTranslation("Clip"),
77
           // OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
78
        
79
        addOutputVectorLayer(RESULT_POL, getTranslation("Clip_polygon"),
80
                        OutputVectorLayer.SHAPE_TYPE_POLYGON);
81
        addOutputVectorLayer(RESULT_LIN, getTranslation("Clip_line"),
82
                        OutputVectorLayer.SHAPE_TYPE_LINE);
83
        addOutputVectorLayer(RESULT_POINT, getTranslation("Clip_point"),
84
                        OutputVectorLayer.SHAPE_TYPE_POINT);
85
        }
86
        
87
        /*
88
         * (non-Javadoc)
89
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
90
         */
91
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
92
                if(existsOutPutFile(ClipAlgorithm.RESULT_POL, 0) || 
93
                        existsOutPutFile(ClipAlgorithm.RESULT_LIN, 0) ||
94
                        existsOutPutFile(ClipAlgorithm.RESULT_POINT, 0)) {
95
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
96
            }
97
                org.gvsig.fmap.geom.Geometry clippingGeometry = null;
98
                IVectorLayer clip = m_Parameters.getParameterValueAsVectorLayer(CLIP);
99
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
100
                boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
101
                
102
                try {
103
                        clippingGeometry = ScalableUnionOperation.joinLayerGeometries(clip, false);
104
                } catch (Exception e) {
105
                        Sextante.addErrorToLog(e);
106
                        return false;
107
                }
108
                
109
                FeatureStore storeLayer = null;
110
                if(layer instanceof FlyrVectIVectorLayer && clippingGeometry != null)
111
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
112
                else
113
                        return false;
114
                
115
                try {
116
                        FeatureSet features = null;
117
                        features = storeLayer.getFeatureSet();
118
                        FeatureType featureType = features.getDefaultFeatureType();
119
                        
120
                        if(isPolygon(storeLayer) || isUndefined(storeLayer)) {
121
                                FeatureStore outFeatStore =
122
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON,
123
                                                        getTranslation("Clip_polygon"), RESULT_POL);
124

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

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

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