Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.convexhull / src / main / java / org / gvsig / sextante / app / algorithm / convexhull / ConvexHullAlgorithm.java @ 46

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

    
23
import java.util.Iterator;
24
import java.util.List;
25

    
26
import org.gvsig.fmap.dal.DataSet;
27
import org.gvsig.fmap.dal.exception.DataException;
28
import org.gvsig.fmap.dal.feature.DisposableIterator;
29
import org.gvsig.fmap.dal.feature.Feature;
30
import org.gvsig.fmap.dal.feature.FeatureSet;
31
import org.gvsig.fmap.dal.feature.FeatureStore;
32
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection;
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.sextante.app.algorithm.base.util.GeometryUtil;
35
import org.gvsig.sextante.app.extension.core.gvVectorLayer;
36

    
37
import es.unex.sextante.core.GeoAlgorithm;
38
import es.unex.sextante.core.Sextante;
39
import es.unex.sextante.dataObjects.IVectorLayer;
40
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
41
import es.unex.sextante.exceptions.RepeatedParameterNameException;
42
import es.unex.sextante.outputs.OutputVectorLayer;
43

    
44
/**
45
 * Convex Hull Algorithm
46
 * @author Nacho Brodin (nachobrodin@gmail.com)
47
 */
48
public class ConvexHullAlgorithm extends GeoAlgorithm {
49
        public static final String  LAYER     = "LAYER";
50
        public static final String  RESULT    = "RESULT";
51
        public static final String  CHECK     = "CHECK";
52
        
53
        public void defineCharacteristics(){
54
                setName(Sextante.getText("Convex Hull"));
55
                setGroup(Sextante.getText("gvSIG_Algorithms"));
56
                setGeneratesUserDefinedRasterOutput(false);
57
                try {
58
                        m_Parameters.addInputVectorLayer(LAYER, 
59
                                                                                                Sextante.getText("Input_layer"), 
60
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
61
                                                                                                true);
62
                        addOutputVectorLayer(RESULT,
63
                                                                        Sextante.getText("ConvexHull"),
64
                                                                        OutputVectorLayer.SHAPE_TYPE_POLYGON);
65
                        m_Parameters.addBoolean(CHECK, Sextante.getText("Selected_geometries"), false);
66
                } catch (RepeatedParameterNameException e) {
67
                        Sextante.addErrorToLog(e);
68
                }
69
        }
70
        
71
        /*
72
         * (non-Javadoc)
73
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
74
         */
75
        @SuppressWarnings("unchecked")
76
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
77
                IVectorLayer input = m_Parameters.getParameterValueAsVectorLayer(LAYER);
78
                boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
79
                
80
                FeatureStore store = null;
81
                
82
                if(input instanceof gvVectorLayer)
83
                        store = ((gvVectorLayer)input).getFeatureStore();
84
                else
85
                        return false;
86
                
87
                ConvexHullOperation convexHullOperation = new ConvexHullOperation();
88
                
89
                FeatureSet features = null;
90
                try {
91
                        features = store.getFeatureSet();
92
                        
93
                        DisposableIterator it = null;
94
                        if(selectedGeom) {
95
                                DataSet ds = store.getSelection();
96
                                it = ((DefaultFeatureSelection)ds).iterator();
97
                                //if(!it.hasNext())Mensaje de no hay geometrias seleccionadas
98
                        } else
99
                                it = features.iterator();
100
                        
101
                        while( it.hasNext() ) {
102
                                Feature feature = (Feature)it.next();
103
                                List geomList = feature.getGeometries();
104
                                if(geomList == null) {
105
                                        Geometry geom = feature.getDefaultGeometry();
106
                                        if(geom != null)
107
                                                convexHullOperation.invoke(geom);
108
                                        continue;
109
                                }
110
                                Iterator<Geometry> itGeom = geomList.iterator();
111
                                while(itGeom.hasNext()) {
112
                                        Geometry g = itGeom.next();
113
                                        convexHullOperation.invoke(g);
114
                                }
115
                        }
116
                        Geometry g = convexHullOperation.getGeometry();
117
                        if(g == null)
118
                                return false;
119
                        String[] sNames = {"ID"};
120
                        Class [] types = {Integer.class};
121
                        IVectorLayer output = getNewVectorLayer(RESULT,
122
                                                                                                        Sextante.getText("ConvexHull"),
123
                                                                                                        OutputVectorLayer.SHAPE_TYPE_POLYGON, types, sNames);
124
                        
125
                        com.vividsolutions.jts.geom.Geometry jtsGeom = GeometryUtil.geomToJTS(g);
126
                        
127
                        output.addFeature(jtsGeom, new Object[]{new Integer(0)});
128
                        it.dispose();
129
                } catch (DataException e) {
130
                        Sextante.addErrorToLog(e);
131
                }
132
                return !m_Task.isCanceled();
133
        }
134
}