gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / core / DefaultOutputFactory.java @ 626
History | View | Annotate | Download (9.09 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.geoprocess.lib.sextante.dataObjects.BufferWriteOnlyIRasterLayer; |
38 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FLyrRasterIRasterLayer; |
39 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FileTools; |
40 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
41 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.TableDocumentITable; |
42 |
import org.gvsig.geoprocess.lib.sextante.outputs.CompositeSourceIOutputChannel; |
43 |
|
44 |
import es.unex.sextante.core.AnalysisExtent; |
45 |
import es.unex.sextante.core.ITaskMonitor; |
46 |
import es.unex.sextante.core.OutputFactory; |
47 |
import es.unex.sextante.dataObjects.IRasterLayer; |
48 |
import es.unex.sextante.dataObjects.ITable; |
49 |
import es.unex.sextante.dataObjects.IVectorLayer; |
50 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
51 |
import es.unex.sextante.gui.core.DefaultTaskMonitor; |
52 |
import es.unex.sextante.outputs.FileOutputChannel; |
53 |
import es.unex.sextante.outputs.IOutputChannel; |
54 |
|
55 |
/**
|
56 |
* An OutputFactory based on the gvSIG data model. Supports only file-based
|
57 |
* outputs.
|
58 |
*
|
59 |
* @author volaya
|
60 |
*
|
61 |
*/
|
62 |
public class DefaultOutputFactory extends OutputFactory { |
63 |
|
64 |
@Override
|
65 |
public IVectorLayer getNewVectorLayer(final String name, |
66 |
final int shapeType, final Class[] types, final String[] fields, |
67 |
final IOutputChannel channel, final Object crs) |
68 |
throws UnsupportedOutputChannelException {
|
69 |
|
70 |
final int[] sizes = getSizesFromTypeClass(types); |
71 |
return getNewVectorLayer(name, shapeType, types, fields, channel, crs,
|
72 |
sizes); |
73 |
|
74 |
} |
75 |
|
76 |
private int[] getSizesFromTypeClass(final Class[] types) { |
77 |
final int[] sizes = new int[types.length]; |
78 |
for (int i = 0; i < sizes.length; i++) { |
79 |
sizes[i] = getSizeFromTypeClass(types[i]); |
80 |
} |
81 |
return sizes;
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
public IVectorLayer getNewVectorLayer(final String sName, |
86 |
final int iShapeType, final Class[] types, final String[] sFields, |
87 |
final IOutputChannel channel, final Object crs, int[] fieldSize) |
88 |
throws UnsupportedOutputChannelException {
|
89 |
|
90 |
if( fieldSize==null ) { |
91 |
fieldSize = getSizesFromTypeClass(types); |
92 |
} |
93 |
if (channel != null) { |
94 |
if (channel instanceof CompositeSourceIOutputChannel) { |
95 |
FlyrVectIVectorLayer layer = new FlyrVectIVectorLayer();
|
96 |
Object inputParams =
|
97 |
((CompositeSourceIOutputChannel) channel).getParameters(); |
98 |
layer.create(sName, inputParams, iShapeType, types, sFields, |
99 |
crs, fieldSize); |
100 |
return layer;
|
101 |
|
102 |
} else
|
103 |
if (channel instanceof FileOutputChannel) { |
104 |
FlyrVectIVectorLayer layer = new FlyrVectIVectorLayer();
|
105 |
String fileName =
|
106 |
((FileOutputChannel) channel).getFilename(); |
107 |
layer.create(sName, fileName, iShapeType, types, sFields, |
108 |
crs, fieldSize); |
109 |
return layer;
|
110 |
} |
111 |
} |
112 |
|
113 |
|
114 |
throw new UnsupportedOutputChannelException(); |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public IRasterLayer getNewRasterLayer(final String sName, |
119 |
final int iDataType, final AnalysisExtent extent, final int iBands, |
120 |
final IOutputChannel channel, final Object crs) |
121 |
throws UnsupportedOutputChannelException {
|
122 |
|
123 |
if (channel instanceof FileOutputChannel) { |
124 |
final String sFilename = |
125 |
((FileOutputChannel) channel).getFilename(); |
126 |
final BufferWriteOnlyIRasterLayer layer = new BufferWriteOnlyIRasterLayer(); |
127 |
layer.create(sName, sFilename, extent, iDataType, iBands, crs); |
128 |
return layer;
|
129 |
} else {
|
130 |
throw new UnsupportedOutputChannelException(); |
131 |
} |
132 |
|
133 |
} |
134 |
|
135 |
public IRasterLayer getNewEmptyRORasterLayer(final String sName, |
136 |
final int iDataType, final AnalysisExtent extent, final int iBands, |
137 |
final IOutputChannel channel, final Object crs) |
138 |
throws UnsupportedOutputChannelException {
|
139 |
|
140 |
if (channel instanceof FileOutputChannel) { |
141 |
final String sFilename = |
142 |
((FileOutputChannel) channel).getFilename(); |
143 |
final FLyrRasterIRasterLayer layer = new FLyrRasterIRasterLayer(sFilename); |
144 |
return layer;
|
145 |
} else {
|
146 |
throw new UnsupportedOutputChannelException(); |
147 |
} |
148 |
|
149 |
} |
150 |
|
151 |
@Override
|
152 |
public ITable getNewTable(final String sName, final Class types[], |
153 |
final String[] sFields, final IOutputChannel channel) |
154 |
throws UnsupportedOutputChannelException {
|
155 |
|
156 |
if (channel instanceof FileOutputChannel) { |
157 |
final String sFilename = |
158 |
((FileOutputChannel) channel).getFilename(); |
159 |
final TableDocumentITable table = new TableDocumentITable(); |
160 |
table.create(sName, sFilename, types, sFields); |
161 |
return table;
|
162 |
} else {
|
163 |
throw new UnsupportedOutputChannelException(); |
164 |
} |
165 |
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
public String getTempFolder() { |
170 |
|
171 |
return Utilities.createTempDirectory(); |
172 |
|
173 |
} |
174 |
|
175 |
@Override
|
176 |
public String[] getRasterLayerOutputExtensions() { |
177 |
|
178 |
return FileTools.RASTER_EXT_IN;
|
179 |
|
180 |
} |
181 |
|
182 |
@Override
|
183 |
public String[] getVectorLayerOutputExtensions() { |
184 |
|
185 |
return new String[] { "shp", "dxf" }; |
186 |
|
187 |
} |
188 |
|
189 |
@Override
|
190 |
public String[] getTableOutputExtensions() { |
191 |
|
192 |
return new String[] { "dbf" }; |
193 |
|
194 |
} |
195 |
|
196 |
public void addMessage(final String s) { |
197 |
|
198 |
NotificationManager.addInfo(s, null);
|
199 |
|
200 |
} |
201 |
|
202 |
@Override
|
203 |
public ITaskMonitor getTaskMonitor(final String sTitle, |
204 |
final boolean bDeterminate, final JDialog parent) { |
205 |
|
206 |
return new DefaultTaskMonitor(sTitle, bDeterminate, parent); |
207 |
|
208 |
} |
209 |
|
210 |
@Override
|
211 |
public Object getDefaultCRS() { |
212 |
//Uses the active view
|
213 |
IWindow window = PluginServices.getMDIManager().getActiveWindow(); |
214 |
|
215 |
//If there is not active view then it looks for a view
|
216 |
if(window == null || !(window instanceof AbstractViewPanel)) { |
217 |
IWindow[] windowList = PluginServices.getMDIManager().getAllWindows();
|
218 |
for (int i = 0; i < windowList.length; i++) { |
219 |
if(windowList[i] instanceof AbstractViewPanel) { |
220 |
window = windowList[i]; |
221 |
break;
|
222 |
} |
223 |
} |
224 |
} |
225 |
|
226 |
if(window != null && window instanceof AbstractViewPanel) { |
227 |
return ((AbstractViewPanel)window).getMapControl().getProjection();
|
228 |
} else {
|
229 |
return ((ProjectPreferences) ApplicationLocator.getManager()
|
230 |
.getPreferences("project")).getDefaultProjection();
|
231 |
} |
232 |
|
233 |
} |
234 |
|
235 |
private int getSizeFromTypeClass(final Class<?> type) { |
236 |
|
237 |
if (type.equals(Integer.class)) { |
238 |
return 10; |
239 |
} else
|
240 |
if (type.equals(Long.class)) { |
241 |
return 20; |
242 |
} else
|
243 |
if (type.equals(Double.class)) { |
244 |
return 20; |
245 |
} else
|
246 |
if (type.equals(Float.class)) { |
247 |
return 15; |
248 |
} else
|
249 |
if (type.equals(String.class)) { |
250 |
return 80; |
251 |
} else
|
252 |
if (type.equals(Date.class)) { |
253 |
return 10; |
254 |
} else
|
255 |
if (type.equals(Boolean.class)) { |
256 |
return 1; |
257 |
} else {
|
258 |
return 20; |
259 |
} |
260 |
|
261 |
} |
262 |
|
263 |
} |