Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.buffer / src / main / java / org / gvsig / sextante / app / algorithm / buffer / BufferAlgorithm.java @ 43

History | View | Annotate | Download (6.7 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.buffer;
22

    
23
import org.gvsig.fmap.dal.exception.DataException;
24
import org.gvsig.fmap.dal.feature.FeatureSet;
25
import org.gvsig.fmap.dal.feature.FeatureStore;
26
import org.gvsig.fmap.dal.feature.FeatureType;
27
import org.gvsig.sextante.app.algorithm.base.panel.AlgorithmOutputPanel;
28
import org.gvsig.sextante.app.extension.core.gvGeoAlgorithm;
29
import org.gvsig.sextante.app.extension.core.gvVectorLayer;
30

    
31
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
32
import es.unex.sextante.core.Sextante;
33
import es.unex.sextante.dataObjects.IVectorLayer;
34
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
35
import es.unex.sextante.exceptions.OptionalParentParameterException;
36
import es.unex.sextante.exceptions.RepeatedParameterNameException;
37
import es.unex.sextante.exceptions.UndefinedParentParameterNameException;
38
import es.unex.sextante.outputs.OutputVectorLayer;
39

    
40
/**
41
 * Geoprocess that computes a buffer area around each feature's geometry of the
42
 * input layer. <br>
43
 * All the points interior to this buffer area has to be at a distance inferior
44
 * to "buffer distance" to the original geometry. This buffer distance could be
45
 * constant, or it could be a function of the value of a feature attribute.<br>
46
 * 
47
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
48
 */
49
public class BufferAlgorithm extends gvGeoAlgorithm {
50
        public static final String  RESULT            = "RESULT";
51
        public static final String  LAYER             = "LAYER";
52
        public static final String  SELECTED_GEOM     = "SELECTED_GEOM";
53
        public static final String  DISTANCE          = "DISTANCE";
54
        public static final String  FIELD             = "FIELD";
55
        public static final String  DISSOLVE          = "DISSOLVE";
56
        public static final String  ROUND_BORDER      = "ROUND_BORDER";
57
        public static final String  AREA              = "AREA";
58
        public static final String  RING_NUMBER       = "RING_NUMBER";
59
        
60
        /*
61
         * (non-Javadoc)
62
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
63
         */
64
        public void defineCharacteristics(){
65
                setName(Sextante.getText( "Buffer"));
66
                setGroup(Sextante.getText( "gvSIG_Algorithms"));
67
                setGeneratesUserDefinedRasterOutput(false);
68
                String [] sOptions = {Sextante.getText( "Out"),
69
                                                                Sextante.getText( "In"),
70
                                                                Sextante.getText( "InAndOut")};
71
                try {
72
                        m_Parameters.addInputVectorLayer(LAYER, 
73
                                                                                                Sextante.getText( "Input_layer"), 
74
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
75
                                                                                                true);
76
                        m_Parameters.addBoolean(SELECTED_GEOM, Sextante.getText("Selected_geometries"), false);
77
                        m_Parameters.addNumericalValue(DISTANCE, Sextante.getText("Distance"), 0, AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
78
                        m_Parameters.addTableField(FIELD, Sextante.getText("Field"), "LAYER");
79
                        m_Parameters.addBoolean(DISSOLVE, Sextante.getText("Dissolve_entities"), false);
80
                        m_Parameters.addBoolean(ROUND_BORDER, Sextante.getText("Round_border"), true);
81
                        m_Parameters.addSelection(AREA, Sextante.getText("Builds_influence_area"), sOptions);
82
                        m_Parameters.addSelection(RING_NUMBER, Sextante.getText("Number_of_rings"), new String[]{"1", "2", "3"});
83
                        setExternalParameters((Object)new AlgorithmOutputPanel());
84
                } catch (RepeatedParameterNameException e) {
85
                        Sextante.addErrorToLog(e);
86
                } catch (UndefinedParentParameterNameException e) {
87
                        Sextante.addErrorToLog(e);
88
                } catch (OptionalParentParameterException e) {
89
                        Sextante.addErrorToLog(e);
90
                }
91
                addOutputVectorLayer(RESULT,
92
                                                                Sextante.getText( "Difference"),
93
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
94
        }
95
        
96
        /*
97
         * (non-Javadoc)
98
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
99
         */
100
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
101
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
102
                boolean selectedGeom = m_Parameters.getParameter(SELECTED_GEOM).getParameterValueAsBoolean();
103
                double dist = m_Parameters.getParameter(DISTANCE).getParameterValueAsDouble();
104
                int attributePosition = m_Parameters.getParameterValueAsInt(FIELD);
105
                boolean dissolve = m_Parameters.getParameter(DISSOLVE).getParameterValueAsBoolean();
106
                boolean round_border = m_Parameters.getParameter(ROUND_BORDER).getParameterValueAsBoolean();
107
                int inflArea = m_Parameters.getParameterValueAsInt(AREA);
108
                int rings = m_Parameters.getParameterValueAsInt(RING_NUMBER);
109
                
110
                FeatureStore storeLayer = null;
111
                if(layer instanceof gvVectorLayer)
112
                        storeLayer = ((gvVectorLayer)layer).getFeatureStore();
113
                else
114
                        return false;
115
                
116
                try {
117
                        FeatureSet features = null;
118
                        features = storeLayer.getFeatureSet();
119
                        FeatureType featureType = features.getDefaultFeatureType();
120

    
121
                        //Object to compute the distance
122
                        IDistance distance = null;
123
                        if(dist == 0)
124
                                distance = new FieldDistance(attributePosition);
125
                        else 
126
                                distance = new ConstantDistance();
127

    
128
                        //Object to compute the buffer operation
129
                        BufferOperation operation = null;
130
                        switch (inflArea) {
131
                        case 0:
132
                                operation = new OutBufferOperation(distance, layer, dist);
133
                                break;
134
                        case 1:
135
                                operation = new InBufferOperation(distance, layer, dist);
136
                                break;
137
                        case 2:
138
                                operation = new InOutBufferOperation(distance, layer, dist);
139
                                break;
140
                        }
141
                        operation.setTypeOfCap(round_border ? BufferOperation.CAP_ROUND : BufferOperation.CAP_SQUARE);
142
                        operation.setNumberOfRadialBuffers(rings + 1);
143

    
144
                        //Builds the output FeatureStore
145
                        FeatureStore outFeatStore = buildOutPutStore(featureType, org.gvsig.fmap.geom.Geometry.TYPES.SURFACE, Sextante.getText("Buffer"), RESULT);
146

    
147
                        //Computes the operation
148
                        operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
149
                } catch (DataException e) {
150
                        Sextante.addErrorToLog(e);
151
                        return false;
152
                }
153

    
154
                if(dissolve) {
155
                        //TODO: Dissolve operation
156
                }
157

    
158
                return true;
159
        }
160

    
161
}