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 | 237 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | 245 | cordinyana | * Copyright (C) 2007-2012 gvSIG Association.
|
5 | 237 | cordinyana | *
|
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 | 245 | cordinyana | *
|
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 | 237 | cordinyana | */
|
24 | 220 | cordinyana | package org.gvsig.geoprocess.lib.sextante.core; |
25 | |||
26 | import java.util.Date; |
||
27 | |||
28 | import javax.swing.JDialog; |
||
29 | |||
30 | 476 | nbrodin | import org.gvsig.andami.PluginServices; |
31 | 403 | nbrodin | import org.gvsig.andami.Utilities; |
32 | import org.gvsig.andami.messages.NotificationManager; |
||
33 | 476 | nbrodin | import org.gvsig.andami.ui.mdiManager.IWindow; |
34 | 403 | nbrodin | import org.gvsig.app.ApplicationLocator; |
35 | import org.gvsig.app.project.ProjectPreferences; |
||
36 | 476 | nbrodin | import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
37 | 854 | fdiaz | import org.gvsig.fmap.geom.Geometry; |
38 | 403 | nbrodin | 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 | 220 | cordinyana | import es.unex.sextante.core.AnalysisExtent; |
46 | import es.unex.sextante.core.ITaskMonitor; |
||
47 | import es.unex.sextante.core.OutputFactory; |
||
48 | 1058 | omartinez | import es.unex.sextante.core.Sextante; |
49 | 220 | cordinyana | 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 | 651 | fdiaz | import es.unex.sextante.outputs.NullOutputChannel; |
57 | 1055 | omartinez | import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
58 | 220 | cordinyana | |
59 | /**
|
||
60 | * An OutputFactory based on the gvSIG data model. Supports only file-based
|
||
61 | * outputs.
|
||
62 | 651 | fdiaz | *
|
63 | 220 | cordinyana | * @author volaya
|
64 | 651 | fdiaz | *
|
65 | 220 | cordinyana | */
|
66 | 225 | cordinyana | public class DefaultOutputFactory extends OutputFactory { |
67 | 220 | cordinyana | |
68 | 1055 | omartinez | @Override
|
69 | 854 | fdiaz | 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 | 220 | cordinyana | @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 | 626 | jjdelcerro | final int[] sizes = getSizesFromTypeClass(types); |
86 | return getNewVectorLayer(name, shapeType, types, fields, channel, crs,
|
||
87 | 854 | fdiaz | sizes, Geometry.SUBTYPES.UNKNOWN); |
88 | 626 | jjdelcerro | |
89 | } |
||
90 | 651 | fdiaz | |
91 | 626 | jjdelcerro | private int[] getSizesFromTypeClass(final Class[] types) { |
92 | 220 | cordinyana | final int[] sizes = new int[types.length]; |
93 | for (int i = 0; i < sizes.length; i++) { |
||
94 | sizes[i] = getSizeFromTypeClass(types[i]); |
||
95 | } |
||
96 | 626 | jjdelcerro | return sizes;
|
97 | 220 | cordinyana | } |
98 | |||
99 | @Override
|
||
100 | public IVectorLayer getNewVectorLayer(final String sName, |
||
101 | final int iShapeType, final Class[] types, final String[] sFields, |
||
102 | 626 | jjdelcerro | final IOutputChannel channel, final Object crs, int[] fieldSize) |
103 | 220 | cordinyana | throws UnsupportedOutputChannelException {
|
104 | 854 | fdiaz | return getNewVectorLayer(sName, iShapeType, types, sFields,
|
105 | channel, crs, fieldSize, Geometry.SUBTYPES.UNKNOWN); |
||
106 | 220 | cordinyana | |
107 | 854 | fdiaz | } |
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 | 626 | jjdelcerro | if( fieldSize==null ) { |
116 | fieldSize = getSizesFromTypeClass(types); |
||
117 | } |
||
118 | 220 | cordinyana | if (channel != null) { |
119 | 225 | cordinyana | if (channel instanceof CompositeSourceIOutputChannel) { |
120 | FlyrVectIVectorLayer layer = new FlyrVectIVectorLayer();
|
||
121 | 220 | cordinyana | Object inputParams =
|
122 | 225 | cordinyana | ((CompositeSourceIOutputChannel) channel).getParameters(); |
123 | 220 | cordinyana | layer.create(sName, inputParams, iShapeType, types, sFields, |
124 | 854 | fdiaz | crs, fieldSize, subtype); |
125 | 220 | cordinyana | return layer;
|
126 | |||
127 | 651 | fdiaz | } 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 | 854 | fdiaz | fieldSize, subtype); |
132 | 651 | fdiaz | return layer;
|
133 | } else if (channel instanceof NullOutputChannel) { |
||
134 | return null; |
||
135 | } |
||
136 | 220 | cordinyana | } |
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 | 225 | cordinyana | final BufferWriteOnlyIRasterLayer layer = new BufferWriteOnlyIRasterLayer(); |
151 | 1055 | omartinez | try {
|
152 | layer.create(sName, sFilename, extent, iDataType, iBands, crs); |
||
153 | 1058 | omartinez | } catch (BufferException e) {
|
154 | Sextante.addErrorToLog(e); |
||
155 | 1055 | omartinez | } |
156 | 220 | cordinyana | return layer;
|
157 | } else {
|
||
158 | throw new UnsupportedOutputChannelException(); |
||
159 | } |
||
160 | |||
161 | } |
||
162 | 651 | fdiaz | |
163 | 403 | nbrodin | 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 | 220 | cordinyana | |
168 | 403 | nbrodin | 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 | 220 | cordinyana | @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 | 225 | cordinyana | final TableDocumentITable table = new TableDocumentITable(); |
188 | 220 | cordinyana | 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 | 476 | nbrodin | //Uses the active view
|
241 | IWindow window = PluginServices.getMDIManager().getActiveWindow(); |
||
242 | 651 | fdiaz | |
243 | 476 | nbrodin | //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 | 1055 | omartinez | for (IWindow windowList1 : windowList) {
|
247 | if (windowList1 instanceof AbstractViewPanel) { |
||
248 | window = windowList1; |
||
249 | break;
|
||
250 | } |
||
251 | } |
||
252 | 476 | nbrodin | } |
253 | 651 | fdiaz | |
254 | 476 | nbrodin | 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 | 220 | cordinyana | |
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 | } |