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 / RasterTiledImageLayer.java @ 461

History | View | Annotate | Download (8.31 KB)

1
/**
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
import gov.nasa.worldwind.avlist.AVKey;
28
import gov.nasa.worldwind.avlist.AVList;
29
import gov.nasa.worldwind.data.RasterServer;
30
import gov.nasa.worldwind.geom.Angle;
31
import gov.nasa.worldwind.geom.Extent;
32
import gov.nasa.worldwind.geom.Sector;
33
import gov.nasa.worldwind.layers.BasicTiledImageLayer;
34
import gov.nasa.worldwind.layers.TextureTile;
35
import gov.nasa.worldwind.render.DrawContext;
36
import gov.nasa.worldwind.retrieve.RetrievalPostProcessor;
37
import gov.nasa.worldwind.retrieve.Retriever;
38
import gov.nasa.worldwind.retrieve.RetrieverFactory;
39
import gov.nasa.worldwind.util.Logging;
40
import gov.nasa.worldwind.util.WWUtil;
41

    
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44
import org.w3c.dom.Element;
45
import org.gvsig.fmap.mapcontext.MapContext;
46
import org.gvsig.fmap.mapcontext.layers.FLayer;
47
import org.gvsig.view3d.swing.impl.DefaultMapControl3D;
48

    
49
/**
50
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
51
 *
52
 */
53
public class RasterTiledImageLayer extends BasicTiledImageLayer {
54

    
55
    private static final Logger LOG = LoggerFactory
56
        .getLogger(RasterTiledImageLayer.class);
57

    
58
    /**
59
     * @param params
60
     */
61
    public RasterTiledImageLayer(AVList params) {
62

    
63
        super(params);
64

    
65
        this.createRasterServer(params);
66
    }
67

    
68
    /**
69
     * @param domElement
70
     * @param params
71
     */
72
    public RasterTiledImageLayer(Element domElement, AVList params) {
73
        super(domElement, params);
74

    
75
        this.createRasterServer(params != null ? params : (AVList) this
76
            .getValue(AVKey.CONSTRUCTION_PARAMETERS));
77
    }
78

    
79
    private void createRasterServer(AVList params) {
80
        
81
        if (params == null) {
82
            LOG.error("Can not create raster server, parameters is null");
83
            throw new IllegalArgumentException();
84
        }
85

    
86
        if (this.getDataFileStore() == null) {
87
            LOG.error("Can not create raster server, file store is null");
88
            throw new IllegalArgumentException();
89
        }
90

    
91
        String datasetName = params.getStringValue(AVKey.DATASET_NAME);
92
        if (WWUtil.isEmpty(datasetName)) {
93
            LOG.error(
94
                "Can not create raster server, missing required parameter: {}",
95
                AVKey.DATASET_NAME);
96
            throw new IllegalArgumentException();
97
        }
98

    
99
        String dataCacheName = params.getStringValue(AVKey.DATA_CACHE_NAME);
100
        if (WWUtil.isEmpty(dataCacheName)) {
101
            LOG.error(
102
                "Can not create raster server, missing required parameter: {}",
103
                AVKey.DATA_CACHE_NAME);
104
            throw new IllegalArgumentException();
105
        }
106

    
107
        MapContext mapContext =
108
            (MapContext) params.getValue(DefaultMapControl3D.GVSIG_MAPCONTEXT);
109

    
110
        if (mapContext == null) {
111
            LOG.error(
112
                "Can not create raster server, missing required parameter: {}",
113
                DefaultMapControl3D.GVSIG_MAPCONTEXT);
114
            throw new IllegalArgumentException();
115
        }
116

    
117
        FLayer layer = (FLayer) params.getValue(DefaultMapControl3D.GVSIG_LAYER);
118

    
119
        if (layer == null) {
120
            LOG.error(
121
                "Can not create raster server, missing required parameter: {}",
122
                "GVSIG_LAYER");
123
            throw new IllegalArgumentException();
124
        }
125

    
126
        final AVList rasterServerParams = params.copy();
127

    
128
        rasterServerParams.setValue(AVKey.FILE_STORE, this.getDataFileStore());
129

    
130
        RetrieverFactory retrieverFactory = new RetrieverFactory() {
131

    
132
            final protected RasterServer rasterServer =
133
                new DefaultRasterServer(rasterServerParams);
134

    
135
            public Retriever createRetriever(AVList tileParams,
136
                RetrievalPostProcessor postProcessor) {
137

    
138
                DefaultRasterServerRetriever retriever =
139
                    new DefaultRasterServerRetriever(tileParams,
140
                        this.rasterServer, postProcessor);
141

    
142
                // copy only values that do not exist in destination AVList
143
                // from rasterServerParams (source) to retriever (destination)
144

    
145
                String[] keysToCopy =
146
                    new String[] { AVKey.DATASET_NAME, AVKey.DISPLAY_NAME,
147
                        AVKey.FILE_STORE, AVKey.IMAGE_FORMAT,
148
                        AVKey.FORMAT_SUFFIX };
149

    
150
                WWUtil.copyValues(rasterServerParams, retriever, keysToCopy,
151
                    false);
152

    
153
                return retriever;
154
            }
155
        };
156

    
157
        params.setValue(AVKey.RETRIEVER_FACTORY_LOCAL, retrieverFactory);
158
        this.setValue(AVKey.RETRIEVER_FACTORY_LOCAL, retrieverFactory);
159

    
160
    }
161
    
162
    protected boolean isTileVisible(DrawContext dc, TextureTile tile) {
163
        
164
        if(tile.getSector().isWithinLatLonLimits()){
165
            return tile.getExtent(dc).intersects(
166
                dc.getView().getFrustumInModelCoordinates())
167
                && (dc.getVisibleSector() == null || dc.getVisibleSector()
168
                .intersects(tile.getSector()));
169
        } else {
170
            
171
            LOG.warn(
172
                "Tile sector is out of range Latitude {} {} Longitude {} {}. Trying to adjust sector",
173
                new Object[] { tile.getSector().getMinLatitude(),
174
                    tile.getSector().getMaxLatitude(),
175
                    tile.getSector().getMinLongitude(),
176
                    tile.getSector().getMaxLongitude() });
177
            
178
            // Try to fix extend. This is necessary to avoid problems getting if
179
            // some tile is visible or not when the tile has corrupt sector like
180
            // ~180.0000001 or -90.000000004561
181
            Extent extend = this.fixExtend(tile, dc);
182
            return extend.intersects(dc.getView()
183
                .getFrustumInModelCoordinates())
184
                && (dc.getVisibleSector() == null || dc.getVisibleSector()
185
                    .intersects(tile.getSector()));
186
        }
187
    }
188

    
189
    private Extent fixExtend(TextureTile tile, DrawContext dc) {
190
        
191
        if (dc == null) {
192
            String msg = Logging.getMessage("nullValue.DrawContextIsNull");
193
            Logging.logger().severe(msg);
194
            throw new IllegalArgumentException(msg);
195
        }
196
        
197
        if ( tile.getSector().isWithinLatLonLimits()){
198
            return Sector.computeBoundingBox(dc.getGlobe(),
199
                dc.getVerticalExaggeration(), tile.getSector());
200
        } else {
201
            
202
            double[] degreesArray = tile.getSector().asDegreesArray();
203
            
204
            double minLatitude =
205
                degreesArray[0] < Angle.NEG90.degrees ? Angle.NEG90.degrees
206
                    : degreesArray[0];
207
            double maxLatitude =
208
                degreesArray[1] > Angle.POS90.degrees ? Angle.POS90.degrees
209
                    : degreesArray[1];
210
            double minLongitude =
211
                degreesArray[2] < Angle.NEG180.degrees ? Angle.NEG180.degrees
212
                    : degreesArray[2];
213
            double maxLongitude =
214
                degreesArray[3] > Angle.POS180.degrees ? Angle.POS180.degrees
215
                    : degreesArray[3];
216
            
217
            Sector adjustedSector =
218
                new Sector(Angle.fromDegrees(minLatitude),
219
                    Angle.fromDegrees(maxLatitude),
220
                    Angle.fromDegrees(minLongitude),
221
                    Angle.fromDegrees(maxLongitude));
222
            
223
            return Sector.computeBoundingBox(dc.getGlobe(),
224
                dc.getVerticalExaggeration(), adjustedSector);
225
        }
226
    }
227
}