Statistics
| Revision:

gvsig-3d / 2.1 / trunk / org.gvsig.view3d / org.gvsig.view3d.swing / org.gvsig.view3d.swing.impl / src / main / java / org / gvsig / view3d / swing / impl / data / DefaultTiledImageLayer.java @ 474

History | View | Annotate | Download (8.7 KB)

1 446 llmarques
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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
25
package org.gvsig.view3d.swing.impl.data;
26
27 457 llmarques
import gov.nasa.worldwind.avlist.AVKey;
28 446 llmarques
import gov.nasa.worldwind.avlist.AVList;
29 457 llmarques
import gov.nasa.worldwind.data.RasterServer;
30 461 llmarques
import gov.nasa.worldwind.geom.Angle;
31
import gov.nasa.worldwind.geom.Extent;
32
import gov.nasa.worldwind.geom.Sector;
33 446 llmarques
import gov.nasa.worldwind.layers.BasicTiledImageLayer;
34 461 llmarques
import gov.nasa.worldwind.layers.TextureTile;
35
import gov.nasa.worldwind.render.DrawContext;
36 457 llmarques
import gov.nasa.worldwind.retrieve.RetrievalPostProcessor;
37
import gov.nasa.worldwind.retrieve.Retriever;
38
import gov.nasa.worldwind.retrieve.RetrieverFactory;
39 461 llmarques
import gov.nasa.worldwind.util.Logging;
40 457 llmarques
import gov.nasa.worldwind.util.WWUtil;
41 446 llmarques
42 472 llmarques
import org.gvsig.fmap.mapcontext.layers.FLayer;
43
import org.gvsig.view3d.swing.api.MapControl3D;
44
import org.gvsig.view3d.swing.impl.DefaultMapControl3D;
45 457 llmarques
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47 446 llmarques
import org.w3c.dom.Element;
48 457 llmarques
49 446 llmarques
/**
50 474 llmarques
 * Default implementation of {@link BasicTiledImageLayer}. This class represents
51
 * a tiled raster layer. Manages creation of tiles, visibility of tiles, levels
52
 * of details, resolution...
53
 *
54 446 llmarques
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
55
 */
56 472 llmarques
public class DefaultTiledImageLayer extends BasicTiledImageLayer {
57 446 llmarques
58 457 llmarques
    private static final Logger LOG = LoggerFactory
59 472 llmarques
        .getLogger(DefaultTiledImageLayer.class);
60 457 llmarques
61 446 llmarques
    /**
62 474 llmarques
     * Default constructor from parameters.
63
     *
64 446 llmarques
     * @param params
65 474 llmarques
     *            parameters of {@link DefaultTiledImageLayer}
66 446 llmarques
     */
67 472 llmarques
    public DefaultTiledImageLayer(AVList params) {
68 446 llmarques
69
        super(params);
70 457 llmarques
71
        this.createRasterServer(params);
72 446 llmarques
    }
73
74
    /**
75 474 llmarques
     * Default constructor form {@link Element}. Requerided parameters:
76
     *
77
     * <ul>
78
     * <li>AVKey.DATASET_NAME</li>
79
     * <li>AVKey.DATA_CACHE_NAME</li>
80
     * <li>DefaultMapControl3D.GVSIG_MAPCONTROL3D</li>
81
     * <li>DefaultMapControl3D.GVSIG_LAYER</li>
82
     * </ul>
83
     *
84 446 llmarques
     * @param domElement
85 474 llmarques
     *            element that contains parameters values.
86 446 llmarques
     * @param params
87 474 llmarques
     *            Additional parameters. If there are some parameter not defined
88
     *            at {@link Element}, it is getted from additional parameters.
89 446 llmarques
     */
90 472 llmarques
    public DefaultTiledImageLayer(Element domElement, AVList params) {
91 446 llmarques
        super(domElement, params);
92 457 llmarques
93
        this.createRasterServer(params != null ? params : (AVList) this
94
            .getValue(AVKey.CONSTRUCTION_PARAMETERS));
95 446 llmarques
    }
96
97 457 llmarques
    private void createRasterServer(AVList params) {
98 474 llmarques
99 457 llmarques
        if (params == null) {
100
            LOG.error("Can not create raster server, parameters is null");
101
            throw new IllegalArgumentException();
102
        }
103
104
        if (this.getDataFileStore() == null) {
105
            LOG.error("Can not create raster server, file store is null");
106
            throw new IllegalArgumentException();
107
        }
108
109
        String datasetName = params.getStringValue(AVKey.DATASET_NAME);
110
        if (WWUtil.isEmpty(datasetName)) {
111
            LOG.error(
112
                "Can not create raster server, missing required parameter: {}",
113
                AVKey.DATASET_NAME);
114
            throw new IllegalArgumentException();
115
        }
116
117
        String dataCacheName = params.getStringValue(AVKey.DATA_CACHE_NAME);
118
        if (WWUtil.isEmpty(dataCacheName)) {
119
            LOG.error(
120
                "Can not create raster server, missing required parameter: {}",
121
                AVKey.DATA_CACHE_NAME);
122
            throw new IllegalArgumentException();
123
        }
124
125 472 llmarques
        MapControl3D mapControl3D =
126 474 llmarques
            (MapControl3D) params
127
                .getValue(DefaultMapControl3D.GVSIG_MAPCONTROL3D);
128 457 llmarques
129 472 llmarques
        if (mapControl3D == null) {
130 457 llmarques
            LOG.error(
131
                "Can not create raster server, missing required parameter: {}",
132 472 llmarques
                DefaultMapControl3D.GVSIG_MAPCONTROL3D);
133 457 llmarques
            throw new IllegalArgumentException();
134
        }
135
136 474 llmarques
        FLayer layer =
137
            (FLayer) params.getValue(DefaultMapControl3D.GVSIG_LAYER);
138 457 llmarques
139
        if (layer == null) {
140
            LOG.error(
141
                "Can not create raster server, missing required parameter: {}",
142
                "GVSIG_LAYER");
143
            throw new IllegalArgumentException();
144
        }
145
146
        final AVList rasterServerParams = params.copy();
147
148
        rasterServerParams.setValue(AVKey.FILE_STORE, this.getDataFileStore());
149
150
        RetrieverFactory retrieverFactory = new RetrieverFactory() {
151
152
            final protected RasterServer rasterServer =
153
                new DefaultRasterServer(rasterServerParams);
154
155
            public Retriever createRetriever(AVList tileParams,
156
                RetrievalPostProcessor postProcessor) {
157
158
                DefaultRasterServerRetriever retriever =
159
                    new DefaultRasterServerRetriever(tileParams,
160
                        this.rasterServer, postProcessor);
161
162
                // copy only values that do not exist in destination AVList
163
                // from rasterServerParams (source) to retriever (destination)
164
165
                String[] keysToCopy =
166
                    new String[] { AVKey.DATASET_NAME, AVKey.DISPLAY_NAME,
167
                        AVKey.FILE_STORE, AVKey.IMAGE_FORMAT,
168
                        AVKey.FORMAT_SUFFIX };
169
170
                WWUtil.copyValues(rasterServerParams, retriever, keysToCopy,
171
                    false);
172
173
                return retriever;
174
            }
175
        };
176
177
        params.setValue(AVKey.RETRIEVER_FACTORY_LOCAL, retrieverFactory);
178
        this.setValue(AVKey.RETRIEVER_FACTORY_LOCAL, retrieverFactory);
179
180
    }
181 474 llmarques
182 461 llmarques
    protected boolean isTileVisible(DrawContext dc, TextureTile tile) {
183 474 llmarques
184
        if (tile.getSector().isWithinLatLonLimits()) {
185 461 llmarques
            return tile.getExtent(dc).intersects(
186
                dc.getView().getFrustumInModelCoordinates())
187
                && (dc.getVisibleSector() == null || dc.getVisibleSector()
188 474 llmarques
                    .intersects(tile.getSector()));
189 461 llmarques
        } else {
190 474 llmarques
191 461 llmarques
            // Try to fix extend. This is necessary to avoid problems getting if
192
            // some tile is visible or not when the tile has corrupt sector like
193 462 llmarques
            // 180.0000001 or -90.000000004561
194 461 llmarques
            Extent extend = this.fixExtend(tile, dc);
195
            return extend.intersects(dc.getView()
196
                .getFrustumInModelCoordinates())
197
                && (dc.getVisibleSector() == null || dc.getVisibleSector()
198
                    .intersects(tile.getSector()));
199
        }
200
    }
201
202
    private Extent fixExtend(TextureTile tile, DrawContext dc) {
203 474 llmarques
204 461 llmarques
        if (dc == null) {
205
            String msg = Logging.getMessage("nullValue.DrawContextIsNull");
206
            Logging.logger().severe(msg);
207
            throw new IllegalArgumentException(msg);
208
        }
209 474 llmarques
210
        if (tile.getSector().isWithinLatLonLimits()) {
211 461 llmarques
            return Sector.computeBoundingBox(dc.getGlobe(),
212
                dc.getVerticalExaggeration(), tile.getSector());
213
        } else {
214 474 llmarques
215 461 llmarques
            double[] degreesArray = tile.getSector().asDegreesArray();
216 474 llmarques
217 461 llmarques
            double minLatitude =
218
                degreesArray[0] < Angle.NEG90.degrees ? Angle.NEG90.degrees
219
                    : degreesArray[0];
220
            double maxLatitude =
221
                degreesArray[1] > Angle.POS90.degrees ? Angle.POS90.degrees
222
                    : degreesArray[1];
223
            double minLongitude =
224
                degreesArray[2] < Angle.NEG180.degrees ? Angle.NEG180.degrees
225
                    : degreesArray[2];
226
            double maxLongitude =
227
                degreesArray[3] > Angle.POS180.degrees ? Angle.POS180.degrees
228
                    : degreesArray[3];
229 474 llmarques
230 461 llmarques
            Sector adjustedSector =
231
                new Sector(Angle.fromDegrees(minLatitude),
232
                    Angle.fromDegrees(maxLatitude),
233
                    Angle.fromDegrees(minLongitude),
234
                    Angle.fromDegrees(maxLongitude));
235 474 llmarques
236 461 llmarques
            return Sector.computeBoundingBox(dc.getGlobe(),
237
                dc.getVerticalExaggeration(), adjustedSector);
238
        }
239
    }
240 446 llmarques
}