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 / DefaultInputFactory.java @ 1030
History | View | Annotate | Download (8.16 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.awt.geom.Rectangle2D; |
27 |
import java.util.ArrayList; |
28 |
import java.util.List; |
29 |
|
30 |
import org.gvsig.andami.PluginServices; |
31 |
import org.gvsig.app.extension.ProjectExtension; |
32 |
import org.gvsig.app.project.Project; |
33 |
import org.gvsig.app.project.documents.Document; |
34 |
import org.gvsig.app.project.documents.table.TableDocument; |
35 |
import org.gvsig.app.project.documents.table.TableManager; |
36 |
import org.gvsig.app.project.documents.view.DefaultViewDocument; |
37 |
import org.gvsig.app.project.documents.view.ViewManager; |
38 |
import org.gvsig.fmap.geom.primitive.Envelope; |
39 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
40 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
41 |
import org.gvsig.fmap.mapcontext.layers.LayersIterator; |
42 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
43 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FLyrRasterIRasterLayer; |
44 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FileTools; |
45 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
46 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.TableDocumentITable; |
47 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
48 |
|
49 |
import es.unex.sextante.core.AbstractInputFactory; |
50 |
import es.unex.sextante.core.NamedExtent; |
51 |
import es.unex.sextante.core.Sextante; |
52 |
import es.unex.sextante.dataObjects.I3DRasterLayer; |
53 |
import es.unex.sextante.dataObjects.IDataObject; |
54 |
|
55 |
public class DefaultInputFactory extends AbstractInputFactory { |
56 |
|
57 |
public void createDataObjects() { |
58 |
|
59 |
final List<IDataObject> list = new ArrayList<IDataObject>(); |
60 |
|
61 |
final Project project =
|
62 |
((ProjectExtension) PluginServices |
63 |
.getExtension(ProjectExtension.class)).getProject(); |
64 |
final List<Document> views = project.getDocuments(ViewManager.TYPENAME); |
65 |
for (int i = 0; i < views.size(); i++) { |
66 |
final DefaultViewDocument view = (DefaultViewDocument) views.get(i);
|
67 |
final FLayers layers = view.getMapContext().getLayers();
|
68 |
final LayersIterator iter = new LayersIterator(layers); |
69 |
while (iter.hasNext()) {
|
70 |
try {
|
71 |
final FLayer layer = iter.nextLayer();
|
72 |
if (layer instanceof FLyrRaster) { |
73 |
final FLyrRasterIRasterLayer rasterLayer =
|
74 |
new FLyrRasterIRasterLayer();
|
75 |
rasterLayer.create((FLyrRaster) layer); |
76 |
list.add(rasterLayer); |
77 |
} else if (layer instanceof FLyrVect) { |
78 |
final FlyrVectIVectorLayer vectorLayer = new FlyrVectIVectorLayer(); |
79 |
vectorLayer.create((FLyrVect) layer); |
80 |
list.add(vectorLayer); |
81 |
} |
82 |
} catch(Throwable th) { |
83 |
Sextante.addErrorToLog(th); |
84 |
} |
85 |
} |
86 |
} |
87 |
|
88 |
final List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
89 |
for (int i = 0; i < tables.size(); i++) { |
90 |
try {
|
91 |
final TableDocumentITable table = new TableDocumentITable(); |
92 |
table.create((TableDocument) tables.get(i)); |
93 |
list.add(table); |
94 |
} catch(Throwable th) { |
95 |
Sextante.addErrorToLog(th); |
96 |
} |
97 |
} |
98 |
|
99 |
m_Objects = list.toArray(new IDataObject[list.size()]);
|
100 |
} |
101 |
|
102 |
public NamedExtent[] getPredefinedExtents() { |
103 |
|
104 |
final Project project =
|
105 |
((ProjectExtension) PluginServices |
106 |
.getExtension(ProjectExtension.class)).getProject(); |
107 |
final List<Document> views = project.getDocuments(ViewManager.TYPENAME); |
108 |
final NamedExtent ne[] = new NamedExtent[views.size()]; |
109 |
|
110 |
for (int i = 0; i < views.size(); i++) { |
111 |
final DefaultViewDocument view = (DefaultViewDocument) views.get(i);
|
112 |
Envelope envelope = view.getMapContext().getViewPort().getAdjustedEnvelope(); |
113 |
|
114 |
if (envelope != null) { |
115 |
double x = envelope.getMinimum(0); |
116 |
double y = envelope.getMinimum(1); |
117 |
double width = envelope.getMaximum(0)-x; |
118 |
double height = envelope.getMaximum(1)-y; |
119 |
final Rectangle2D extent = new Rectangle2D.Double(x, y, width, height); |
120 |
final String sName = view.getName(); |
121 |
ne[i] = new NamedExtent(sName, extent);
|
122 |
} |
123 |
} |
124 |
return ne;
|
125 |
} |
126 |
|
127 |
public String[] getRasterLayerInputExtensions() { |
128 |
|
129 |
return FileTools.RASTER_EXT_IN;
|
130 |
|
131 |
} |
132 |
|
133 |
public String[] get3DRasterLayerInputExtensions() { |
134 |
|
135 |
return FileTools.RASTER3D_EXT_IN;
|
136 |
|
137 |
} |
138 |
|
139 |
public String[] getVectorLayerInputExtensions() { |
140 |
|
141 |
return FileTools.VECTOR_EXT_IN;
|
142 |
|
143 |
} |
144 |
|
145 |
public String[] getTableInputExtensions() { |
146 |
|
147 |
return FileTools.TABLE_EXT;
|
148 |
|
149 |
} |
150 |
|
151 |
public IDataObject openDataObjectFromFile(final String sFilename) { |
152 |
|
153 |
final Object object = FileTools.open(sFilename); |
154 |
|
155 |
if (object == null) { |
156 |
return null; |
157 |
} else
|
158 |
if (object instanceof FLyrRaster) { |
159 |
final FLyrRasterIRasterLayer layer = new FLyrRasterIRasterLayer(); |
160 |
layer.create((FLyrRaster) object); |
161 |
return layer;
|
162 |
} else
|
163 |
if (object instanceof FLyrVect) { |
164 |
final FlyrVectIVectorLayer layer = new FlyrVectIVectorLayer(); |
165 |
layer.create((FLyrVect) object); |
166 |
return layer;
|
167 |
} else
|
168 |
if (object instanceof I3DRasterLayer) { |
169 |
return (I3DRasterLayer) object;
|
170 |
} else {
|
171 |
return null; |
172 |
} |
173 |
|
174 |
} |
175 |
|
176 |
public void close(final String sName) { |
177 |
|
178 |
final IDataObject dataObject = this.getInputFromName(sName); |
179 |
|
180 |
final Object obj = dataObject.getBaseDataObject(); |
181 |
if (obj instanceof FLayer) { |
182 |
removeLayer((FLayer) obj); |
183 |
} else {
|
184 |
final Project project =
|
185 |
((ProjectExtension) PluginServices |
186 |
.getExtension(ProjectExtension.class)).getProject(); |
187 |
project.remove((Document) obj);
|
188 |
} |
189 |
|
190 |
} |
191 |
|
192 |
private static void removeLayer(final FLayer baseLayer) { |
193 |
|
194 |
final Project project =
|
195 |
((ProjectExtension) PluginServices |
196 |
.getExtension(ProjectExtension.class)).getProject(); |
197 |
final List<Document> views = project.getDocuments(ViewManager.TYPENAME); |
198 |
for (int i = 0; i < views.size(); i++) { |
199 |
final DefaultViewDocument view = (DefaultViewDocument) views.get(i);
|
200 |
final FLayers layers = view.getMapContext().getLayers();
|
201 |
final LayersIterator iter = new LayersIterator(layers); |
202 |
while (iter.hasNext()) {
|
203 |
final FLayer layer = iter.nextLayer();
|
204 |
if (layer.equals(baseLayer)) {
|
205 |
layers.removeLayer(baseLayer); |
206 |
return;
|
207 |
} |
208 |
} |
209 |
|
210 |
} |
211 |
|
212 |
} |
213 |
|
214 |
@Override
|
215 |
public I3DRasterLayer[] get3DRasterLayers() { |
216 |
|
217 |
return new I3DRasterLayer[0]; |
218 |
|
219 |
} |
220 |
|
221 |
} |