Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.dispersepoints / src / main / java / org / gvsig / geoprocess / algorithm / dispersepoints / DispersePointsAlgorithm.java @ 1009

History | View | Annotate | Download (8.46 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2017 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.geoprocess.algorithm.dispersepoints;
24

    
25
import java.util.ArrayList;
26
import java.util.Iterator;
27

    
28
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
29
import es.unex.sextante.core.Sextante;
30
import es.unex.sextante.dataObjects.IVectorLayer;
31
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
32
import es.unex.sextante.exceptions.RepeatedParameterNameException;
33
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
34
import es.unex.sextante.outputs.OutputVectorLayer;
35

    
36
import org.gvsig.fmap.dal.DALLocator;
37
import org.gvsig.fmap.dal.DataManager;
38
import org.gvsig.fmap.dal.DataTypes;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.FeatureReference;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.geom.Geometry;
45
import org.gvsig.fmap.geom.GeometryLocator;
46
import org.gvsig.fmap.geom.GeometryManager;
47
import org.gvsig.fmap.geom.SpatialIndex;
48
import org.gvsig.fmap.geom.SpatialIndexFactory;
49
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
50
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
51
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
52
import org.gvsig.tools.dynobject.DynObject;
53
import org.gvsig.tools.exception.BaseException;
54
import org.gvsig.tools.visitor.VisitCanceledException;
55
import org.gvsig.tools.visitor.Visitor;
56

    
57

    
58
/**
59
 * @author fdiaz
60
 *
61
 */
62
public class DispersePointsAlgorithm extends AbstractSextanteGeoProcess {
63

    
64
    public static final String LAYER = "LAYER";
65
    public static final String RESULT = "RESULT";
66
    public static final String SCATTER_RADIUS = "SCATTER_RADIUS";
67
    public static final String MATCH_DISTANCE = "MATCH_DISTANCE";
68
    public static final String SELECTGEOM_INPUT = "SELECTGEOM_INPUT";
69

    
70

    
71
    /**
72
     *
73
     */
74
    public DispersePointsAlgorithm() {
75
        // TODO Auto-generated constructor stub
76
    }
77

    
78
    public void defineCharacteristics() {
79
        setName(getTranslation("_disperse_points"));
80
        setGroup(getTranslation("basic_vect_algorithms"));
81
        // setGeneratesUserDefinedRasterOutput(false);
82
        try {
83
            m_Parameters.addInputVectorLayer(LAYER,
84
                    getTranslation("input_layer"), IVectorLayer.SHAPE_TYPE_POINT,
85
                    true);
86
            addOutputVectorLayer(RESULT, getTranslation("_disperse_points"),
87
                    OutputVectorLayer.SHAPE_TYPE_POINT);
88
            m_Parameters.addNumericalValue(MATCH_DISTANCE, getTranslation("_match_distance"), 0, AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
89
            m_Parameters.addNumericalValue(SCATTER_RADIUS, getTranslation("_scatter_radius"), 0, AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
90
            m_Parameters.addBoolean(SELECTGEOM_INPUT, getTranslation("Selected_geometries_input_layer"), false);
91
        } catch (RepeatedParameterNameException e) {
92
            Sextante.addErrorToLog(e);
93
        }
94
    }
95

    
96

    
97

    
98
    /* (non-Javadoc)
99
     * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
100
     */
101
    @Override
102
    public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
103
        if(existsOutPutFile(DispersePointsAlgorithm.RESULT, 0)) {
104
            throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
105
        }
106
        IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); //Capa de entrada
107
        double scatterRadius = m_Parameters.getParameter(SCATTER_RADIUS).getParameterValueAsDouble();
108
        double matchDistance = m_Parameters.getParameter(MATCH_DISTANCE).getParameterValueAsDouble();
109
        boolean selectedGeomInput = m_Parameters.getParameter(SELECTGEOM_INPUT).getParameterValueAsBoolean();
110

    
111

    
112
        FlyrVectIVectorLayer inputLayer = null;
113
        if(layer instanceof FlyrVectIVectorLayer) {
114
            inputLayer = ((FlyrVectIVectorLayer)layer); //Capa de entrada
115
        } else {
116
            return false;
117
        }
118

    
119
        DataManager dataManager = DALLocator.getDataManager();
120

    
121
        FeatureStore inputFeatureStore = inputLayer.getFeatureStore();
122

    
123
        //Builds the output and computes the operation
124
        try {
125
            FeatureType featureTypeInputLayer = inputFeatureStore.getDefaultFeatureType(); //Capa de entrada
126

    
127
            GeometryOperation operation = null;
128
            FeatureStore outFeatStore = null;
129

    
130
                outFeatStore =
131
                buildOutPutStore(featureTypeInputLayer, inputLayer.getShapeType(), getTranslation("_disperse_points"),
132
                    RESULT);
133

    
134
                operation =
135
                    new DispersePointsOperation(createIndex(inputLayer.getFeatureStore()), scatterRadius, matchDistance, this);
136

    
137
            operation.setTaskStatus(getStatus());
138
            operation.computesGeometryOperation(inputFeatureStore,
139
                    outFeatStore,
140
                    attrNames,
141
                    selectedGeomInput,
142
                    false,
143
                    true);
144
        } catch (Exception e) {
145
            Sextante.addErrorToLog(e);
146
        }
147

    
148
        return true;
149
    }
150

    
151

    
152
    private SpatialIndex createIndex(FeatureStore store) throws BaseException {
153
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
154

    
155
        SpatialIndexFactory factory = geomManager.getSpatialIndexFactory(geomManager.SPATIALINDEX_DEFAULT_RTREE);
156
        DynObject parameters = factory.createParameters();
157

    
158
        SpatialIndex index = (SpatialIndex) factory.create(parameters, geomManager);
159
        final SpatialIndex wrappedIndex = store.wrapSpatialIndex(index);
160

    
161
        Visitor visitor = new Visitor() {
162

    
163
            @Override
164
            public void visit(Object obj) throws VisitCanceledException, BaseException {
165
                Feature f = (Feature) obj;
166
                Geometry g = f.getDefaultGeometry();
167
                FeatureReference ref = f.getReference();
168
                wrappedIndex.insert(g, ref);
169
            }
170
        };
171
        store.accept(visitor);
172

    
173
        return wrappedIndex;
174
    }
175

    
176
    /**
177
     * Builds the output FeatureStore
178
     * @param featureType
179
     * @return FeatureStore
180
     */
181
    protected FeatureStore buildOutPutStore(FeatureType featureType1,
182
                                            int shapeType,
183
                                            String sextanteLayerName,
184
                                            String sextanteLayerLabel) {
185
        ArrayList<Class> typesList = new ArrayList<Class>();
186
        ArrayList<String> attr = new ArrayList<String>();
187

    
188
        Iterator it = featureType1.iterator();
189
        while( it.hasNext() ) {
190
            FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor)it.next();
191
            if (attribute.getDataType().getType() != DataTypes.GEOMETRY) {
192
                attr.add(attribute.getName());
193
                typesList.add(attribute.getObjectClass());
194
            }
195
        }
196

    
197
        attrNames = new String[attr.size()];
198
        attr.toArray(attrNames);
199
        Class[] types = new Class[typesList.size()];
200
        typesList.toArray(types);
201

    
202
        try {
203
            IVectorLayer output = getNewVectorLayer(sextanteLayerLabel,
204
                                                    sextanteLayerName,
205
                                                    shapeType, types, attrNames);
206
            return ((FlyrVectIVectorLayer)output).getFeatureStore();
207
        } catch (UnsupportedOutputChannelException e) {
208
            Sextante.addErrorToLog(e);
209
        } catch (GeoAlgorithmExecutionException e) {
210
            Sextante.addErrorToLog(e);
211
        }
212
        return null;
213
    }
214
}