Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.app / org.gvsig.geoprocess.app.mainplugin / src / main / java / org / gvsig / geoprocess / sextante / AbstractSextanteGeoProcess.java @ 191

History | View | Annotate | Download (8.7 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22
package org.gvsig.geoprocess.sextante;
23

    
24
import java.util.Iterator;
25

    
26
import es.unex.sextante.core.GeoAlgorithm;
27
import es.unex.sextante.core.Sextante;
28
import es.unex.sextante.dataObjects.IVectorLayer;
29
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
30
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
31

    
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.exception.ReadException;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureType;
37
import org.gvsig.fmap.geom.Geometry;
38
import org.gvsig.geoprocess.GeoProcess;
39
import org.gvsig.geoprocess.core.gvVectorLayer;
40

    
41
/**
42
 * Base implementation for Sextante based {@link GeoProcess} objects.
43
 * 
44
 * @author gvSIG Team
45
 * @version $Id$
46
 */
47
public abstract class AbstractSextanteGeoProcess extends GeoAlgorithm implements
48
    GeoProcess {
49

    
50
    protected String[] attrNames = null;
51
    private int nSteps = 0;
52

    
53
    /**
54
     * Builds the output FeatureStore
55
     * 
56
     * @param featureType
57
     * @return FeatureStore
58
     */
59
    protected FeatureStore buildOutPutStore(FeatureType featureType,
60
        int shapeType, String sextanteLayerName, String sextanteLayerLabel) {
61

    
62
        Class<?>[] types = null;
63
        if (featureType.getDefaultGeometryAttribute() != null) {
64
            // Tiene campo GEOMETRY.
65
            // Hay que quitarlo
66
            attrNames = new String[featureType.size() - 1];
67
            types = new Class[featureType.size() - 1];
68
        } else {
69
            attrNames = new String[featureType.size()];
70
            types = new Class[featureType.size()];
71
        }
72

    
73
        int i = 0;
74
        @SuppressWarnings("unchecked")
75
        Iterator<FeatureAttributeDescriptor> it = featureType.iterator();
76
        while (it.hasNext()) {
77
            FeatureAttributeDescriptor attribute = it.next();
78
            if (attribute.getName().compareTo("GEOMETRY") != 0) {
79
                attrNames[i] = attribute.getName();
80
                types[i] = attribute.getObjectClass();
81
                i++;
82
            }
83
        }
84

    
85
        try {
86
            IVectorLayer output =
87
                getNewVectorLayer(sextanteLayerLabel, sextanteLayerName,
88
                    shapeType, types, attrNames);
89
            return ((gvVectorLayer) output).getFeatureStore();
90
        } catch (UnsupportedOutputChannelException e) {
91
            Sextante.addErrorToLog(e);
92
        } catch (GeoAlgorithmExecutionException e) {
93
            Sextante.addErrorToLog(e);
94
        }
95
        return null;
96
    }
97

    
98
    /**
99
     * Builds the output FeatureStore
100
     * 
101
     * @param featureType
102
     * @return FeatureStore
103
     */
104
    protected FeatureStore buildOutPutStoreFromUnion(FeatureType featureType1,
105
        FeatureType featureType2, int shapeType, String sextanteLayerName,
106
        String sextanteLayerLabel) {
107
        return buildOutPutStoreFromUnion(featureType1, featureType2, shapeType,
108
            sextanteLayerName, sextanteLayerLabel, null, null);
109
    }
110

    
111
    /**
112
     * Builds the output FeatureStore
113
     * 
114
     * @param featureType
115
     * @return FeatureStore
116
     */
117
    protected FeatureStore buildOutPutStoreFromUnion(FeatureType featureType1,
118
        FeatureType featureType2, int shapeType, String sextanteLayerName,
119
        String sextanteLayerLabel, String newField, Class<?> newFieldType) {
120
        int sizeAux = 0;
121
        if (newField != null && newFieldType != null)
122
            sizeAux = 1;
123

    
124
        Class<?>[] types = null;
125
        // Tiene campo GEOMETRY. Hay que quitarlo
126
        if (featureType1.getDefaultGeometryAttribute() != null) {
127
            attrNames =
128
                new String[featureType1.size() + featureType2.size() - 2
129
                    + sizeAux];
130
            types =
131
                new Class[featureType1.size() + featureType2.size() - 2
132
                    + sizeAux];
133
        } else {
134
            attrNames =
135
                new String[featureType1.size() + featureType2.size() + sizeAux];
136
            types =
137
                new Class[featureType1.size() + featureType2.size() + sizeAux];
138
        }
139

    
140
        int i = 0;
141
        @SuppressWarnings("unchecked")
142
        Iterator<FeatureAttributeDescriptor> it = featureType1.iterator();
143
        while (it.hasNext()) {
144
            FeatureAttributeDescriptor attribute =
145
                (FeatureAttributeDescriptor) it.next();
146
            if (attribute.getName().compareTo("GEOMETRY") != 0) {
147
                attrNames[i] = attribute.getName();
148
                types[i] = attribute.getObjectClass();
149
                i++;
150
            }
151
        }
152

    
153
        @SuppressWarnings("unchecked")
154
        Iterator<FeatureAttributeDescriptor> it2 = featureType2.iterator();
155
        while (it2.hasNext()) {
156
            FeatureAttributeDescriptor attribute = it2.next();
157
            if (attribute.getName().compareTo("GEOMETRY") != 0) {
158
                String attrName =
159
                    checkAttrName(attribute.getName(), featureType1.size() - 1);
160
                attrNames[i] = attrName;
161
                types[i] = attribute.getObjectClass();
162
                i++;
163
            }
164
        }
165

    
166
        if (newField != null && newFieldType != null) {
167
            attrNames[attrNames.length - 1] = newField;
168
            types[types.length - 1] = newFieldType;
169
        }
170

    
171
        try {
172
            IVectorLayer output =
173
                getNewVectorLayer(sextanteLayerLabel, sextanteLayerName,
174
                    shapeType, types, attrNames);
175
            return ((gvVectorLayer) output).getFeatureStore();
176
        } catch (UnsupportedOutputChannelException e) {
177
            Sextante.addErrorToLog(e);
178
        } catch (GeoAlgorithmExecutionException e) {
179
            Sextante.addErrorToLog(e);
180
        }
181
        return null;
182
    }
183

    
184
    /**
185
     * Changes the attribute name if this name is in the list.
186
     * 
187
     * @param name
188
     * @return
189
     */
190
    protected String checkAttrName(String name, int size) {
191
        String newName = name;
192
        for (int i = 0; i < size; i++)
193
            if (attrNames[i].compareTo(newName) == 0)
194
                newName = newName + "I";
195
        return newName;
196
    }
197

    
198
    /**
199
     * Gets the shape type of the selected feature store
200
     * 
201
     * @param FeatureStore
202
     *            source
203
     * @return shape type
204
     * @throws ReadException
205
     */
206
    protected int getShapeType(FeatureStore storeLayer1) throws ReadException {
207
        FeatureType featureType;
208
        try {
209
            featureType = storeLayer1.getDefaultFeatureType();
210
        } catch (DataException e) {
211
            throw new ReadException(storeLayer1.getName(), e);
212
        }
213
        int indexGeom = featureType.getDefaultGeometryAttributeIndex();
214
        return featureType.getAttributeDescriptor(indexGeom).getGeomType()
215
            .getType();
216
    }
217

    
218
    /**
219
     * Returns true if it is a point layer
220
     * 
221
     * @param store
222
     * @return
223
     * @throws ReadException
224
     */
225
    protected boolean isPoint(FeatureStore store) throws ReadException {
226
        return (getShapeType(store) == Geometry.TYPES.POINT || getShapeType(store) == Geometry.TYPES.MULTIPOINT);
227
    }
228

    
229
    /**
230
     * Returns true if it is a polygon layer
231
     * 
232
     * @param store
233
     * @return
234
     * @throws ReadException
235
     */
236
    protected boolean isPolygon(FeatureStore store) throws ReadException {
237
        return (getShapeType(store) == Geometry.TYPES.SURFACE || getShapeType(store) == Geometry.TYPES.MULTISURFACE);
238
    }
239

    
240
    /**
241
     * Returns true if it is a line layer
242
     * 
243
     * @param store
244
     * @return
245
     * @throws ReadException
246
     */
247
    protected boolean isLine(FeatureStore store) throws ReadException {
248
        return (getShapeType(store) == Geometry.TYPES.CURVE || getShapeType(store) == Geometry.TYPES.MULTICURVE);
249
    }
250

    
251
    public void setProgress(int iStep) {
252
        super.setProgress(iStep, nSteps);
253
    }
254

    
255
    public void setTotalNumberOfSteps(int nSteps) {
256
        this.nSteps = nSteps;
257
    }
258

    
259
}