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 @ 1055

History | View | Annotate | Download (10.5 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.dataObjects.IRasterLayer;
49
import es.unex.sextante.dataObjects.ITable;
50
import es.unex.sextante.dataObjects.IVectorLayer;
51
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
52
import es.unex.sextante.gui.core.DefaultTaskMonitor;
53
import es.unex.sextante.outputs.FileOutputChannel;
54
import es.unex.sextante.outputs.IOutputChannel;
55
import es.unex.sextante.outputs.NullOutputChannel;
56
import java.util.logging.Level;
57
import java.util.logging.Logger;
58
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException;
59

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

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

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

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

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

    
90
    }
91

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

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

    
108
    }
109

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

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

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

    
139
        throw new UnsupportedOutputChannelException();
140
    }
141

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

    
148
        if (channel instanceof FileOutputChannel) {
149
            final String sFilename =
150
                ((FileOutputChannel) channel).getFilename();
151
            final BufferWriteOnlyIRasterLayer layer = new BufferWriteOnlyIRasterLayer();
152
            try {
153
                layer.create(sName, sFilename, extent, iDataType, iBands, crs);
154
            } catch (BufferException ex) {
155
                Logger.getLogger(DefaultOutputFactory.class.getName()).log(Level.SEVERE, null, ex);
156
            }
157
            return layer;
158
        } else {
159
            throw new UnsupportedOutputChannelException();
160
        }
161

    
162
    }
163

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

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

    
178
        }
179

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

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

    
195
    }
196

    
197
    @Override
198
    public String getTempFolder() {
199

    
200
        return Utilities.createTempDirectory();
201

    
202
    }
203

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

    
207
        return FileTools.RASTER_EXT_IN;
208

    
209
    }
210

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

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

    
216
    }
217

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

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

    
223
    }
224

    
225
    public void addMessage(final String s) {
226

    
227
        NotificationManager.addInfo(s, null);
228

    
229
    }
230

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

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

    
237
    }
238

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

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

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

    
262
    }
263

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

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

    
290
    }
291

    
292
}