Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.difference / src / main / java / org / gvsig / geoprocess / algorithm / difference / DifferenceAlgorithm.java @ 335

History | View | Annotate | Download (6.61 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.difference;
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.GeometryOperation;
31
import org.gvsig.geoprocess.algorithm.base.core.ScalableUnionOperation;
32
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
33
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
34

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

    
41
/**
42
 * Difference algorithm
43
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
44
 */
45
public class DifferenceAlgorithm extends AbstractSextanteGeoProcess {
46
        public static final String  RESULT_POL        = "RESULT_POL";
47
        public static final String  RESULT_POINT      = "RESULT_POINT";
48
        public static final String  RESULT_LINE       = "RESULT_LINE";
49
        public static final String  LAYER             = "LAYER";
50
        public static final String  DIF               = "DIF";
51
        public static final String  CHECK_INPUT       = "CHECK_INPUT";
52
        public static final String  CHECK_OUTPUT      = "CHECK_OUTPUT";
53

    
54
        public void defineCharacteristics(){
55
        setName(getTranslation("Difference"));
56
        setGroup(getTranslation("basic_vect_algorithms"));
57
        // setGeneratesUserDefinedRasterOutput(false);
58
                
59
                try {
60
                        m_Parameters.addInputVectorLayer(LAYER, getTranslation("Input_layer"),
61
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
62
                                                                                                true);
63
                        m_Parameters.addInputVectorLayer(DIF, getTranslation("Overlays_layer"),
64
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
65
                                                                                                true);
66
            m_Parameters.addBoolean(CHECK_INPUT, 
67
                            getTranslation("Selected_geometries_input_layer"), false);
68
            m_Parameters.addBoolean(CHECK_OUTPUT, 
69
                            getTranslation("Selected_geometries_input_layer"), false);
70
                } catch (RepeatedParameterNameException e) {
71
                        Sextante.addErrorToLog(e);
72
                }
73
                
74
                addOutputVectorLayer(RESULT_POL, getTranslation("Difference_polygon"),
75
                                OutputVectorLayer.SHAPE_TYPE_POLYGON);
76
                addOutputVectorLayer(RESULT_LINE, getTranslation("Difference_line"),
77
                                OutputVectorLayer.SHAPE_TYPE_LINE);
78
                addOutputVectorLayer(RESULT_POINT, getTranslation("Difference_point"),
79
                                OutputVectorLayer.SHAPE_TYPE_POINT);
80
        }
81
        
82
        /*
83
         * (non-Javadoc)
84
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
85
         */
86
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
87
                if(existsOutPutFile(RESULT_POL, 0) || 
88
                        existsOutPutFile(RESULT_LINE, 0) ||
89
                        existsOutPutFile(RESULT_POINT, 0)) {
90
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
91
            }
92
                org.gvsig.fmap.geom.Geometry overlayGeometry = null;
93
                IVectorLayer dif = m_Parameters.getParameterValueAsVectorLayer(DIF);
94
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
95
                boolean selectedGeomInput = m_Parameters.getParameter(CHECK_INPUT).getParameterValueAsBoolean();
96
                boolean selectedGeomOutput = m_Parameters.getParameter(CHECK_OUTPUT).getParameterValueAsBoolean();
97
                
98
                try {
99
                        overlayGeometry = ScalableUnionOperation.joinLayerGeometries(dif, selectedGeomOutput);
100
                } catch (Exception e) {
101
                        Sextante.addErrorToLog(e);
102
                        return false;
103
                }
104
                
105
                FeatureStore storeLayer = null;
106
                if(layer instanceof FlyrVectIVectorLayer)
107
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
108
                else
109
                        return false;
110
                
111
                try {
112
                        FeatureSet features = null;
113
                        features = storeLayer.getFeatureSet();
114
                        FeatureType featureType = features.getDefaultFeatureType();
115
                        
116
                        GeometryOperation operation = new DifferenceOperation(overlayGeometry, this);
117
            operation.setTaskStatus(getStatus());
118
            
119
            if (isPolygon(storeLayer) || isUndefined(storeLayer)) {
120
                                FeatureStore outFeatStore =
121
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON,
122
                                                        getTranslation("Difference_polygon"), RESULT_POL);
123

    
124
                                operation.computesGeometryOperation(storeLayer, outFeatStore, 
125
                                                attrNames, selectedGeomInput, selectedGeomOutput, true);
126
                        } else {
127
                                buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON,
128
                                                getTranslation("Null_polygon"), RESULT_POL);
129
                        }
130
            
131
            if (isLine(storeLayer) || isUndefined(storeLayer)) {
132
                                FeatureStore outFeatStore =
133
                                        buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_LINE,
134
                                                        getTranslation("Difference_line"), RESULT_LINE);
135

    
136
                                operation.computesGeometryOperation(storeLayer, outFeatStore, 
137
                                                attrNames, selectedGeomInput, selectedGeomOutput, true);
138
                        } else {
139
                                buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_LINE,
140
                                                getTranslation("Null_line"), RESULT_LINE);
141
                        }
142
                        
143
                        if (isPoint(storeLayer) || isUndefined(storeLayer)) {
144
                                FeatureStore outFeatStore =
145
                                        buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_POINT,
146
                                                        getTranslation("Difference_point"), RESULT_POINT);
147

    
148
                                operation.computesGeometryOperation(storeLayer, outFeatStore, 
149
                                                attrNames, selectedGeomInput, selectedGeomOutput, true);
150
                        } else {
151
                                buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_POINT,
152
                                                getTranslation("Null_point"), RESULT_POINT);
153
                        }
154
            
155
                } catch (DataException e) {
156
                        Sextante.addErrorToLog(e);
157
                        return false;
158
                }
159
                if(getTaskMonitor().isCanceled())
160
                        return false;
161
                return true;
162
        }
163

    
164
}