Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / branches / refactor-2018 / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / core / DefaultOutputFactory.java @ 1058

History | View | Annotate | Download (10.4 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.lib.sextante.core;
25

    
26
import java.util.Date;
27

    
28
import javax.swing.JDialog;
29

    
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.Utilities;
32
import org.gvsig.andami.messages.NotificationManager;
33
import org.gvsig.andami.ui.mdiManager.IWindow;
34
import org.gvsig.app.ApplicationLocator;
35
import org.gvsig.app.project.ProjectPreferences;
36
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
37
import org.gvsig.fmap.geom.Geometry;
38
import org.gvsig.geoprocess.lib.sextante.dataObjects.BufferWriteOnlyIRasterLayer;
39
import org.gvsig.geoprocess.lib.sextante.dataObjects.FLyrRasterIRasterLayer;
40
import org.gvsig.geoprocess.lib.sextante.dataObjects.FileTools;
41
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
42
import org.gvsig.geoprocess.lib.sextante.dataObjects.TableDocumentITable;
43
import org.gvsig.geoprocess.lib.sextante.outputs.CompositeSourceIOutputChannel;
44

    
45
import es.unex.sextante.core.AnalysisExtent;
46
import es.unex.sextante.core.ITaskMonitor;
47
import es.unex.sextante.core.OutputFactory;
48
import es.unex.sextante.core.Sextante;
49
import es.unex.sextante.dataObjects.IRasterLayer;
50
import es.unex.sextante.dataObjects.ITable;
51
import es.unex.sextante.dataObjects.IVectorLayer;
52
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
53
import es.unex.sextante.gui.core.DefaultTaskMonitor;
54
import es.unex.sextante.outputs.FileOutputChannel;
55
import es.unex.sextante.outputs.IOutputChannel;
56
import es.unex.sextante.outputs.NullOutputChannel;
57
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
58

    
59
/**
60
 * An OutputFactory based on the gvSIG data model. Supports only file-based
61
 * outputs.
62
 *
63
 * @author volaya
64
 *
65
 */
66
public class DefaultOutputFactory extends OutputFactory {
67

    
68
    @Override
69
    public IVectorLayer getNewVectorLayer(final String name,
70
        final int shapeType, final Class[] types, final String[] fields,
71
        final IOutputChannel channel, final Object crs, final int subtype)
72
        throws UnsupportedOutputChannelException {
73

    
74
        final int[] sizes = getSizesFromTypeClass(types);
75
        return getNewVectorLayer(name, shapeType, types, fields, channel, crs,
76
            sizes, subtype);
77

    
78
    }
79
    @Override
80
    public IVectorLayer getNewVectorLayer(final String name,
81
        final int shapeType, final Class[] types, final String[] fields,
82
        final IOutputChannel channel, final Object crs)
83
        throws UnsupportedOutputChannelException {
84

    
85
        final int[] sizes = getSizesFromTypeClass(types);
86
        return getNewVectorLayer(name, shapeType, types, fields, channel, crs,
87
            sizes, Geometry.SUBTYPES.UNKNOWN);
88

    
89
    }
90

    
91
    private int[] getSizesFromTypeClass(final Class[] types) {
92
        final int[] sizes = new int[types.length];
93
        for (int i = 0; i < sizes.length; i++) {
94
            sizes[i] = getSizeFromTypeClass(types[i]);
95
        }
96
        return sizes;
97
    }
98

    
99
    @Override
100
    public IVectorLayer getNewVectorLayer(final String sName,
101
        final int iShapeType, final Class[] types, final String[] sFields,
102
        final IOutputChannel channel, final Object crs, int[] fieldSize)
103
        throws UnsupportedOutputChannelException {
104
        return getNewVectorLayer(sName, iShapeType, types, sFields,
105
        channel, crs, fieldSize, Geometry.SUBTYPES.UNKNOWN);
106

    
107
    }
108

    
109
    @Override
110
    public IVectorLayer getNewVectorLayer(final String sName,
111
        final int iShapeType, final Class[] types, final String[] sFields,
112
        final IOutputChannel channel, final Object crs, int[] fieldSize, int subtype)
113
        throws UnsupportedOutputChannelException {
114

    
115
        if( fieldSize==null ) {
116
            fieldSize = getSizesFromTypeClass(types);
117
        }
118
        if (channel != null) {
119
            if (channel instanceof CompositeSourceIOutputChannel) {
120
                FlyrVectIVectorLayer layer = new FlyrVectIVectorLayer();
121
                Object inputParams =
122
                    ((CompositeSourceIOutputChannel) channel).getParameters();
123
                layer.create(sName, inputParams, iShapeType, types, sFields,
124
                    crs, fieldSize, subtype);
125
                return layer;
126

    
127
            } else if (channel instanceof FileOutputChannel) {
128
                FlyrVectIVectorLayer layer = new FlyrVectIVectorLayer();
129
                String fileName = ((FileOutputChannel) channel).getFilename();
130
                layer.create(sName, fileName, iShapeType, types, sFields, crs,
131
                    fieldSize, subtype);
132
                return layer;
133
            } else if (channel instanceof NullOutputChannel) {
134
                return null;
135
            }
136
        }
137

    
138
        throw new UnsupportedOutputChannelException();
139
    }
140

    
141
    @Override
142
    public IRasterLayer getNewRasterLayer(final String sName,
143
        final int iDataType, final AnalysisExtent extent, final int iBands,
144
        final IOutputChannel channel, final Object crs)
145
        throws UnsupportedOutputChannelException {
146

    
147
        if (channel instanceof FileOutputChannel) {
148
            final String sFilename =
149
                ((FileOutputChannel) channel).getFilename();
150
            final BufferWriteOnlyIRasterLayer layer = new BufferWriteOnlyIRasterLayer();
151
            try {
152
                layer.create(sName, sFilename, extent, iDataType, iBands, crs);
153
            } catch (BufferException e) {
154
                Sextante.addErrorToLog(e);            
155
            }
156
            return layer;
157
        } else {
158
            throw new UnsupportedOutputChannelException();
159
        }
160

    
161
    }
162

    
163
    public IRasterLayer getNewEmptyRORasterLayer(final String sName,
164
            final int iDataType, final AnalysisExtent extent, final int iBands,
165
            final IOutputChannel channel, final Object crs)
166
            throws UnsupportedOutputChannelException {
167

    
168
            if (channel instanceof FileOutputChannel) {
169
                final String sFilename =
170
                    ((FileOutputChannel) channel).getFilename();
171
                final FLyrRasterIRasterLayer layer = new FLyrRasterIRasterLayer(sFilename);
172
                return layer;
173
            } else {
174
                throw new UnsupportedOutputChannelException();
175
            }
176

    
177
        }
178

    
179
    @Override
180
    public ITable getNewTable(final String sName, final Class types[],
181
        final String[] sFields, final IOutputChannel channel)
182
        throws UnsupportedOutputChannelException {
183

    
184
        if (channel instanceof FileOutputChannel) {
185
            final String sFilename =
186
                ((FileOutputChannel) channel).getFilename();
187
            final TableDocumentITable table = new TableDocumentITable();
188
            table.create(sName, sFilename, types, sFields);
189
            return table;
190
        } else {
191
            throw new UnsupportedOutputChannelException();
192
        }
193

    
194
    }
195

    
196
    @Override
197
    public String getTempFolder() {
198

    
199
        return Utilities.createTempDirectory();
200

    
201
    }
202

    
203
    @Override
204
    public String[] getRasterLayerOutputExtensions() {
205

    
206
        return FileTools.RASTER_EXT_IN;
207

    
208
    }
209

    
210
    @Override
211
    public String[] getVectorLayerOutputExtensions() {
212

    
213
        return new String[] { "shp", "dxf" };
214

    
215
    }
216

    
217
    @Override
218
    public String[] getTableOutputExtensions() {
219

    
220
        return new String[] { "dbf" };
221

    
222
    }
223

    
224
    public void addMessage(final String s) {
225

    
226
        NotificationManager.addInfo(s, null);
227

    
228
    }
229

    
230
    @Override
231
    public ITaskMonitor getTaskMonitor(final String sTitle,
232
        final boolean bDeterminate, final JDialog parent) {
233

    
234
        return new DefaultTaskMonitor(sTitle, bDeterminate, parent);
235

    
236
    }
237

    
238
    @Override
239
    public Object getDefaultCRS() {
240
            //Uses the active view
241
            IWindow window = PluginServices.getMDIManager().getActiveWindow();
242

    
243
            //If there is not active view then it looks for a view
244
            if(window == null || !(window instanceof AbstractViewPanel)) {
245
                    IWindow[] windowList = PluginServices.getMDIManager().getAllWindows();
246
                for (IWindow windowList1 : windowList) {
247
                    if (windowList1 instanceof AbstractViewPanel) {
248
                        window = windowList1;
249
                        break;
250
                    }
251
                }
252
            }
253

    
254
            if(window != null && window instanceof AbstractViewPanel) {
255
                    return ((AbstractViewPanel)window).getMapControl().getProjection();
256
            } else {
257
                    return ((ProjectPreferences) ApplicationLocator.getManager()
258
                                    .getPreferences("project")).getDefaultProjection();
259
            }
260

    
261
    }
262

    
263
    private int getSizeFromTypeClass(final Class<?> type) {
264

    
265
        if (type.equals(Integer.class)) {
266
            return 10;
267
        } else
268
            if (type.equals(Long.class)) {
269
                return 20;
270
            } else
271
                if (type.equals(Double.class)) {
272
                    return 20;
273
                } else
274
                    if (type.equals(Float.class)) {
275
                        return 15;
276
                    } else
277
                        if (type.equals(String.class)) {
278
                            return 80;
279
                        } else
280
                            if (type.equals(Date.class)) {
281
                                return 10;
282
                            } else
283
                                if (type.equals(Boolean.class)) {
284
                                    return 1;
285
                                } else {
286
                                    return 20;
287
                                }
288

    
289
    }
290

    
291
}