Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.wmts / org.gvsig.wmts.provider / src / main / java / org / gvsig / wmts / provider / tree / DefaultWMTSLayerNode.java @ 8801

History | View | Annotate | Download (6.25 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2018 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.wmts.provider.tree;
24

    
25
import java.net.URL;
26
import java.util.ArrayList;
27
import java.util.Enumeration;
28
import java.util.Iterator;
29
import java.util.List;
30

    
31
import javax.swing.tree.TreeNode;
32

    
33
import org.cresques.cts.IProjection;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
import org.gvsig.fmap.crs.CRSFactory;
38
import org.gvsig.fmap.dal.OpenDataStoreParameters;
39
import org.gvsig.raster.wmts.ogc.struct.WMTSLayer;
40
import org.gvsig.raster.wmts.ogc.struct.WMTSStyle;
41
import org.gvsig.wmts.provider.WMTSRasterProviderParameters;
42

    
43

    
44
/**
45
 * @author fdiaz
46
 *
47
 */
48
public class DefaultWMTSLayerNode extends AbstractWMTSNode implements WMTSLayerNode {
49

    
50
    private static final Logger logger =
51
        LoggerFactory.getLogger(DefaultWMTSLayerNode.class);
52

    
53
    private WMTSLayer source;
54
    private int indexSelectedStyle;
55
    private List<WMTSStyleData> styles;
56
    private List<String> formats;
57
    private List<String> inforFormats;
58
    private List<String> srsList;
59
    private URL service;
60

    
61
    private WMTSRasterProviderParameters params;
62

    
63
    /**
64
     * @param source
65
     * @param parent
66
     *
67
     */
68
    public DefaultWMTSLayerNode(WMTSLayer source, WMTSNode parent, URL service) {
69
        super(parent);
70
        this.source = source;
71
        this.service = service;
72
        this.identifier = source.getIdentifier();
73
        this.title = source.getTitle();
74
        this.styles = new ArrayList<WMTSStyleData>();
75
        List<WMTSStyle> sourceStyles = source.getStyle();
76
        this.indexSelectedStyle = 0;
77
        int i = 0;
78
        for (Iterator<WMTSStyle> iterator = sourceStyles.iterator(); iterator.hasNext();) {
79
            WMTSStyle wmtsStyle = (WMTSStyle) iterator.next();
80
            if(wmtsStyle.isDefault()){
81
                this.indexSelectedStyle = i;
82
            }
83
            styles.add(new DefaultWMTSStyle(wmtsStyle));
84
            i++;
85
        }
86

    
87
        this.formats = source.getFormat();
88
        this.inforFormats = source.getInfoFormat();
89
        this.srsList = source.getSrsList();
90

    
91
    }
92

    
93
    @Override
94
    public OpenDataStoreParameters getParameters() {
95
        if (this.params == null) {
96
            this.params = new WMTSRasterProviderParameters();
97

    
98
            params.setService(this.service);
99
            params.setLayer(source.getTitle());
100

    
101
            @SuppressWarnings("unchecked")
102
            List<WMTSStyle> styles = source.getStyle();
103
            if (styles != null && styles.size() > 0) {
104
                params.setStyle(((WMTSStyle) (styles.get(0))).getIdentifier());
105
            }
106

    
107
            @SuppressWarnings("unchecked")
108
            List<String> formats = source.getFormat();
109
            if (formats != null && formats.size() > 0) {
110
                params.setFormat((String) (formats.get(0)));
111
            }
112

    
113
            @SuppressWarnings("unchecked")
114
            List<String> infoFormats = source.getInfoFormat();
115
            if (infoFormats != null && infoFormats.size() > 0) {
116
                params.setInfoFormat((String) infoFormats.get(0));
117
            }
118

    
119
            @SuppressWarnings("unchecked")
120
            List<String> allSrs = source.getSrsList();
121
            if (allSrs != null && allSrs.size() > 0) {
122
                int i = 0;
123
                while (i < allSrs.size()) {
124
                    String srs = allSrs.get(i);
125
                    if (isValidSrs(srs)) {
126
                        params.setCRS(srs);
127
                        IProjection crs = CRSFactory.getCRS(srs);
128
                        break;
129
                    }
130
                    i++;
131
                }
132
                if (i >= allSrs.size()) {
133
                    throw new IllegalArgumentException("The server '" + service.getHost()
134
                        + "' does not provide any valid srs.'");
135
                }
136
            }
137
        }
138

    
139
        return params;
140
    }
141

    
142
    private boolean isValidSrs(String srs){
143
        try{
144
            IProjection crs=CRSFactory.getCRS(srs);
145
            if(crs!=null){
146
                return true;
147
            }
148
        }catch(Exception e){
149
            logger.info("Can't get crs from: '"+srs+"' code", e);
150
            return false;
151
        }
152
        return false;
153
    }
154

    
155

    
156
    @Override
157
    public TreeNode getChildAt(int childIndex) {
158
        return null;
159
    }
160

    
161
    @Override
162
    public int getChildCount() {
163
        return 0;
164
    }
165

    
166
    @Override
167
    public int getIndex(TreeNode node) {
168
        return -1;
169
    }
170

    
171
    @Override
172
    public boolean getAllowsChildren() {
173
        return false;
174
    }
175

    
176
    @Override
177
    public boolean isLeaf() {
178
        return true;
179
    }
180

    
181
    @Override
182
    public Enumeration children() {
183
        return null;
184
    }
185

    
186
    @Override
187
    public int getSelectedStyleIndex() {
188
        return this.indexSelectedStyle;
189
    }
190

    
191
    @Override
192
    public void setSelectedStyleIndex(int index) {
193
        this.indexSelectedStyle = index;
194
    }
195

    
196
    @Override
197
    public WMTSStyleData getSelectedStyle() {
198
        return styles.get(indexSelectedStyle);
199
    }
200

    
201
    @Override
202
    public void setSelectedStyle(WMTSStyleData selected) {
203
        indexSelectedStyle = styles.indexOf(selected);
204
    }
205

    
206
    @Override
207
    public List<WMTSStyleData> getStyles() {
208
        return styles;
209
    }
210

    
211
    @Override
212
    public List<String> getFormats() {
213
        return formats;
214
    }
215

    
216
    @Override
217
    public List<String> getInfoFormats() {
218
        return inforFormats;
219
    }
220

    
221
    @Override
222
    public List<String> getSrsList() {
223
        return srsList;
224
    }
225
}