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 / DefaultInputFactory.java @ 1055

History | View | Annotate | Download (8.56 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.vectorial.FLyrVect;
42
import org.gvsig.geoprocess.lib.sextante.dataObjects.FLyrRasterIRasterLayer;
43
import org.gvsig.geoprocess.lib.sextante.dataObjects.FileTools;
44
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
45
import org.gvsig.geoprocess.lib.sextante.dataObjects.TableDocumentITable;
46

    
47
import es.unex.sextante.core.AbstractInputFactory;
48
import es.unex.sextante.core.NamedExtent;
49
import es.unex.sextante.core.Sextante;
50
import es.unex.sextante.dataObjects.I3DRasterLayer;
51
import es.unex.sextante.dataObjects.IDataObject;
52
import java.util.Iterator;
53
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer;
54

    
55
public class DefaultInputFactory extends AbstractInputFactory {
56

    
57
    public void createDataObjects() {
58

    
59
        final List<IDataObject> list = new ArrayList<IDataObject>();
60

    
61
        try {
62
            final Project project =
63
                ((ProjectExtension) PluginServices
64
                    .getExtension(ProjectExtension.class)).getProject();
65
            final List<Document> views = project.getDocuments(ViewManager.TYPENAME);
66
            for (int i = 0; i < views.size(); i++) {
67
                try {
68
                    final DefaultViewDocument view = (DefaultViewDocument) views.get(i);
69
                    final FLayers layers = view.getMapContext().getLayers();
70
                    Iterator<FLayer> iter = layers.iterator();
71
                    while (iter.hasNext()) {
72
                        try {
73
                            final FLayer layer = iter.next();
74
                            if (layer instanceof RasterLayer) {
75
                                final FLyrRasterIRasterLayer rasterLayer =
76
                                    new FLyrRasterIRasterLayer();
77
                                rasterLayer.create((RasterLayer) layer);
78
                                list.add(rasterLayer);
79
                            } else if (layer instanceof FLyrVect) {
80
                                final FlyrVectIVectorLayer vectorLayer = new FlyrVectIVectorLayer();
81
                                vectorLayer.create((FLyrVect) layer);
82
                                list.add(vectorLayer);
83
                            }
84
                        } catch(Throwable th) {
85
                            Sextante.addErrorToLog(th);
86
                        }
87
                    }
88
                } catch(Throwable th) {
89
                    Sextante.addErrorToLog(th);
90
                }
91
            }
92

    
93
            final List<Document> tables = project.getDocuments(TableManager.TYPENAME);
94
            for (int i = 0; i < tables.size(); i++) {
95
                try {
96
                    final TableDocumentITable table = new TableDocumentITable();
97
                    table.create((TableDocument) tables.get(i));
98
                    list.add(table);
99
                } catch(Throwable th) {
100
                    Sextante.addErrorToLog(th);
101
                }
102
            }
103
        } catch(Throwable th) {
104
            Sextante.addErrorToLog(th);
105
        }
106

    
107
        m_Objects = list.toArray(new IDataObject[list.size()]);
108
    }
109

    
110
    public NamedExtent[] getPredefinedExtents() {
111

    
112
        final Project project =
113
            ((ProjectExtension) PluginServices
114
                .getExtension(ProjectExtension.class)).getProject();
115
        final List<Document> views = project.getDocuments(ViewManager.TYPENAME);
116
        final NamedExtent ne[] = new NamedExtent[views.size()];
117
        
118
        for (int i = 0; i < views.size(); i++) {
119
            final DefaultViewDocument view = (DefaultViewDocument) views.get(i);
120
            Envelope envelope = view.getMapContext().getViewPort().getAdjustedEnvelope();
121
            
122
            if (envelope != null) {
123
              double x = envelope.getMinimum(0);
124
              double y = envelope.getMinimum(1);
125
              double width = envelope.getMaximum(0)-x;
126
              double height = envelope.getMaximum(1)-y;
127
              final Rectangle2D extent = new Rectangle2D.Double(x, y, width, height);
128
              final String sName = view.getName();
129
              ne[i] = new NamedExtent(sName, extent);
130
            }
131
        }
132
        return ne;
133
    }
134

    
135
    public String[] getRasterLayerInputExtensions() {
136

    
137
        return FileTools.RASTER_EXT_IN;
138

    
139
    }
140

    
141
    public String[] get3DRasterLayerInputExtensions() {
142

    
143
        return FileTools.RASTER3D_EXT_IN;
144

    
145
    }
146

    
147
    public String[] getVectorLayerInputExtensions() {
148

    
149
        return FileTools.VECTOR_EXT_IN;
150

    
151
    }
152

    
153
    public String[] getTableInputExtensions() {
154

    
155
        return FileTools.TABLE_EXT;
156

    
157
    }
158

    
159
    public IDataObject openDataObjectFromFile(final String sFilename) {
160

    
161
        final Object object = FileTools.open(sFilename);
162

    
163
        if (object == null) {
164
            return null;
165
        } else
166
            if (object instanceof RasterLayer) {
167
                final FLyrRasterIRasterLayer layer = new FLyrRasterIRasterLayer();
168
                layer.create((RasterLayer) object);
169
                return layer;
170
            } else
171
                if (object instanceof FLyrVect) {
172
                    final FlyrVectIVectorLayer layer = new FlyrVectIVectorLayer();
173
                    layer.create((FLyrVect) object);
174
                    return layer;
175
                } else
176
                    if (object instanceof I3DRasterLayer) {
177
                        return (I3DRasterLayer) object;
178
                    } else {
179
                        return null;
180
                    }
181

    
182
    }
183

    
184
    public void close(final String sName) {
185

    
186
        final IDataObject dataObject = this.getInputFromName(sName);
187

    
188
        final Object obj = dataObject.getBaseDataObject();
189
        if (obj instanceof FLayer) {
190
            removeLayer((FLayer) obj);
191
        } else {
192
            final Project project =
193
                ((ProjectExtension) PluginServices
194
                    .getExtension(ProjectExtension.class)).getProject();
195
            project.remove((Document) obj);
196
        }
197

    
198
    }
199

    
200
    private static void removeLayer(final FLayer baseLayer) {
201

    
202
        final Project project =
203
            ((ProjectExtension) PluginServices
204
                .getExtension(ProjectExtension.class)).getProject();
205
        final List<Document> views = project.getDocuments(ViewManager.TYPENAME);
206
        for (int i = 0; i < views.size(); i++) {
207
            final DefaultViewDocument view = (DefaultViewDocument) views.get(i);
208
            final FLayers layers = view.getMapContext().getLayers();
209
            final Iterator<FLayer> iter = layers.iterator();
210
            while (iter.hasNext()) {
211
                final FLayer layer = iter.next();
212
                if (layer.equals(baseLayer)) {
213
                    layers.removeLayer(baseLayer);
214
                    return;
215
                }
216
            }
217

    
218
        }
219

    
220
    }
221

    
222
    @Override
223
    public I3DRasterLayer[] get3DRasterLayers() {
224

    
225
        return new I3DRasterLayer[0];
226

    
227
    }
228

    
229
}