Statistics
| Revision:

root / trunk / libraries / libWMSv0 / src / com / iver / wmsclient / wms_1_1_0 / WMS_1_1_0.java @ 189

History | View | Annotate | Download (11.6 KB)

1
package com.iver.wmsclient.wms_1_1_0;
2

    
3
import com.iver.utiles.StringUtilities;
4

    
5
import com.iver.wmsclient.Capabilities;
6
import com.iver.wmsclient.FeatureInfoQuery;
7
import com.iver.wmsclient.MapQuery;
8
import com.iver.wmsclient.UnsupportedVersionException;
9
import com.iver.wmsclient.WMSException;
10
import com.iver.wmsclient.WMSUtilities;
11
import com.iver.wmsclient.WMSVersionClient;
12
import com.iver.wmsclient.wms_1_1_0.capabilities.BoundingBox;
13
import com.iver.wmsclient.wms_1_1_0.capabilities.DCPType;
14
import com.iver.wmsclient.wms_1_1_0.capabilities.Format;
15
import com.iver.wmsclient.wms_1_1_0.capabilities.GetFeatureInfo;
16
import com.iver.wmsclient.wms_1_1_0.capabilities.LatLonBoundingBox;
17
import com.iver.wmsclient.wms_1_1_0.capabilities.Layer;
18
import com.iver.wmsclient.wms_1_1_0.capabilities.WMT_MS_Capabilities;
19
import com.iver.wmsclient.wms_1_1_0.exception.ServiceException;
20
import com.iver.wmsclient.wms_1_1_0.exception.ServiceExceptionReport;
21

    
22
import org.apache.log4j.Logger;
23

    
24
import org.exolab.castor.xml.MarshalException;
25
import org.exolab.castor.xml.Unmarshaller;
26
import org.exolab.castor.xml.ValidationException;
27

    
28
import java.awt.geom.Rectangle2D;
29

    
30
import java.io.ByteArrayInputStream;
31
import java.io.ByteArrayOutputStream;
32
import java.io.IOException;
33
import java.io.InputStream;
34
import java.io.InputStreamReader;
35

    
36
import java.net.URL;
37

    
38
import java.util.ArrayList;
39

    
40

    
41
/**
42
 * Cliente para la versi?n 1.1.0
43
 *
44
 * @author Fernando Gonz?lez Cort?s
45
 */
46
public class WMS_1_1_0 implements WMSVersionClient {
47
    private static Logger logger = Logger.getLogger(WMS_1_1_0.class.getName());
48
    private String[] mapURLs;
49
    private String[] infoURLs;
50
    private WMT_MS_Capabilities root;
51

    
52
    /**
53
     * @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#getCapabilities(java.net.URL)
54
     */
55
    public Capabilities getCapabilities(URL host)
56
        throws UnsupportedVersionException, IllegalStateException, IOException {
57
        InputStream in = null;
58
        String response = null;
59

    
60
        try {
61
            logger.debug("Obteniendo capabilities...");
62

    
63
            String hostString = host.toString();
64

    
65
            if (!hostString.endsWith("?")) {
66
                hostString += "?";
67
            }
68

    
69
            String request = getQueryHeader() + "SERVICE=WMS&request=getCapabilities";
70

    
71
            //String request = "REQUEST=GetCapabilities&SERVICE=wms";
72
            URL query = new URL(hostString + request);
73
            in = query.openStream();
74

    
75
            ByteArrayOutputStream outbytes = new ByteArrayOutputStream();
76
            WMSUtilities.serializar(in, outbytes);
77

    
78
            response = new String(outbytes.toByteArray());
79
            logger.debug(response);
80

    
81
            logger.debug("Parseando la respuesta...");
82

    
83
            try {
84
                Unmarshaller umn = new Unmarshaller(WMT_MS_Capabilities.class);
85
                umn.setIgnoreExtraElements(true);
86
                umn.setIgnoreExtraAttributes(true);
87
                root = (WMT_MS_Capabilities) umn.unmarshal(new InputStreamReader(
88
                            new ByteArrayInputStream(WMSUtilities.eliminarDTD(
89
                                    outbytes.toByteArray(),
90
                                    "WMT_MS_Capabilities"))));
91
            } catch (MarshalException e) {
92
                Capabilities c = new Capabilities();
93
                c.setVersion(null);
94

    
95
                return c;
96
            }
97

    
98
            //                        LocalConfiguration.getInstance().getProperties().setProperty("org.exolab.castor.xml.strictelements", "false");
99

    
100
            /*                        WMT_MS_Capabilities root = (WMT_MS_Capabilities) WMT_MS_Capabilities.unmarshal(new InputStreamReader(
101
               new ByteArrayInputStream(WMSUtilities.eliminarDTD(
102
                               outbytes.toByteArray()))));
103
             */
104
            DCPType[] DCPs = root.getCapability().getRequest().getGetMap()
105
                                 .getDCPType();
106
            mapURLs = new String[DCPs.length];
107

    
108
            for (int i = 0; i < DCPs.length; i++) {
109
                mapURLs[i] = DCPs[i].getHTTP().getGet().getOnlineResource()
110
                                    .getHref();
111

    
112
                if (!mapURLs[i].endsWith("?")) {
113
                    mapURLs[i] = mapURLs[i] + "?";
114
                }
115
            }
116

    
117
            GetFeatureInfo fi = root.getCapability().getRequest()
118
                                    .getGetFeatureInfo();
119

    
120
            if (fi != null) {
121
                DCPs = fi.getDCPType();
122
                infoURLs = new String[DCPs.length];
123

    
124
                for (int i = 0; i < DCPs.length; i++) {
125
                    infoURLs[i] = DCPs[i].getHTTP().getGet().getOnlineResource()
126
                                         .getHref();
127

    
128
                    if (!infoURLs[i].endsWith("?")) {
129
                        infoURLs[i] = infoURLs[i] + "?";
130
                    }
131
                }
132
            }
133

    
134
            Capabilities ret = new Capabilities();
135
            ret.setRoot(root);
136
            ret.setVersion(root.getVersion());
137

    
138
            return ret;
139
        } catch (IOException e) {
140
            logger.debug(e);
141
            throw e;
142
        } catch (Exception e) {
143
            logger.error("Error", e);
144

    
145
            Capabilities ret = new Capabilities();
146

    
147
            if (response == null) {
148
                ret.setRoot(null);
149
                ret.setVersion(null);
150

    
151
                return ret;
152
            } else {
153
                ret.setRoot(null);
154
                ret.setVersion(StringUtilities.substringDelimited(response,
155
                        "version=\"", "\"", response.indexOf("bilities")));
156

    
157
                return ret;
158
            }
159
        } finally {
160
            if (in != null) {
161
                in.close();
162
            }
163
        }
164
    }
165

    
166
    /**
167
     * @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#doMapQuery(com.iver.cit.gvsig.fmap.wms.wmsclient.MapQuery)
168
     */
169
    public byte[] doMapQuery(MapQuery queryInfo)
170
        throws IOException, NoSuchFieldException, ValidationException, 
171
            WMSException {
172
        throw new RuntimeException("no debio llegar aqu?");
173
    }
174

    
175
    /**
176
     * @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#getVersion()
177
     */
178
    public String getVersion() {
179
        return "1.1.0";
180
    }
181

    
182
    /**
183
     * @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#getBoundingBox(javax.swing.tree.TreePath,
184
     *      java.lang.String)
185
     */
186
    public Rectangle2D getBoundingBox(String[] t, String srs) {
187
        Layer[] layer = getLayerByPath(t);
188
        BoundingBox[] bbox = getBoundingBox(layer);
189

    
190
        for (int i = 0; i < bbox.length; i++) {
191
            if (bbox[i].getSRS().equals(srs)) {
192
                return new Rectangle2D.Double(Double.parseDouble(
193
                        bbox[i].getMinx()),
194
                    Double.parseDouble(bbox[i].getMiny()),
195
                    Double.parseDouble(bbox[i].getMaxx())- Double.parseDouble(
196
                            bbox[i].getMinx()),
197
                    Double.parseDouble(bbox[i].getMaxy()) - Double.parseDouble(bbox[i].getMiny()));
198
            }
199
        }
200

    
201
        LatLonBoundingBox[] llbbox = getLatLonBoundingBox(layer);
202

    
203
        for (int i = 0; i < llbbox.length; i++) {
204
            return new Rectangle2D.Double(Double.parseDouble(
205
                    llbbox[i].getMinx()),
206
                Double.parseDouble(llbbox[i].getMiny()),
207
                Double.parseDouble(llbbox[i].getMaxx()) - Double.parseDouble(
208
                        llbbox[i].getMinx()),
209
                Double.parseDouble(llbbox[i].getMaxy()) - Double.parseDouble(llbbox[i].getMiny()));
210
        }
211

    
212
        return null;
213
    }
214

    
215
    /**
216
     * Dado un array de layers devuelve el array de boundingbox de  todos ellos
217
     *
218
     * @param layer Array de layers
219
     *
220
     * @return array de boundingBox
221
     */
222
    private BoundingBox[] getBoundingBox(Layer[] layer) {
223
        ArrayList globalBbox = new ArrayList();
224

    
225
        for (int i = 0; i < layer.length; i++) {
226
            Layer l = (Layer) layer[i];
227
            BoundingBox[] bboxes = l.getBoundingBox();
228

    
229
            if (bboxes != null) {
230
                for (int j = 0; j < bboxes.length; j++) {
231
                    globalBbox.add(bboxes[j]);
232
                }
233
            }
234
        }
235

    
236
        return (BoundingBox[]) globalBbox.toArray(new BoundingBox[0]);
237
    }
238

    
239
    /**
240
     * Dado un array de layers devuelve el array de boundingbox de  todos ellos
241
     *
242
     * @param layer Array de layers
243
     *
244
     * @return array de boundingBox
245
     */
246
    private LatLonBoundingBox[] getLatLonBoundingBox(Layer[] layer) {
247
        ArrayList globalBbox = new ArrayList();
248

    
249
        for (int i = 0; i < layer.length; i++) {
250
            Layer l = (Layer) layer[i];
251
            LatLonBoundingBox bbox = l.getLatLonBoundingBox();
252

    
253
            if (bbox != null) {
254
                globalBbox.add(bbox);
255
            }
256
        }
257

    
258
        return (LatLonBoundingBox[]) globalBbox.toArray(new LatLonBoundingBox[0]);
259
    }
260

    
261
    /**
262
     * Obtiene dado un TreePath con las capas devuelve un array con los objetos
263
     * Layer desde el inicio al final del path
264
     *
265
     * @param t TreePath
266
     *
267
     * @return Array de layers
268
     */
269
    private Layer[] getLayerByPath(String[] t) {
270
        ArrayList ret = new ArrayList();
271
        Layer raiz = root.getCapability().getLayer();
272
        ret.add(raiz);
273

    
274
        for (int i = 1; i < t.length; i++) {
275
            for (int j = 0; j < raiz.getLayerCount(); j++) {
276
                    if (raiz.getLayer(j).getTitle() == null) continue;
277
                if (raiz.getLayer(j).getTitle().getContent().equals(t[i])) {
278
                    raiz = raiz.getLayer(j);
279
                    ret.add(raiz);
280

    
281
                    break;
282
                }
283
            }
284
        }
285

    
286
        return (Layer[]) ret.toArray(new Layer[0]);
287
    }
288

    
289
    /**
290
     * @see com.iver.cit.gvsig.fmap.wms.wmsclient.WMSClient#doFeatureInfo(com.iver.cit.gvsig.fmap.wms.wmsclient.FeatureInfoQuery)
291
     */
292
    public byte[] doFeatureInfo(FeatureInfoQuery query)
293
        throws ValidationException, IOException, WMSException, 
294
            NoSuchFieldException {
295
        throw new RuntimeException("no debio llegar aqu?");
296
    }
297

    
298
    /**
299
     * @see com.iver.cit.gvsig.fmap.wms.WMSVersionClient#getMapURLs()
300
     */
301
    public String[] getMapURLs() {
302
        return mapURLs;
303
    }
304

    
305
    /**
306
     * @see com.iver.cit.gvsig.fmap.wms.WMSVersionClient#parseException(byte[])
307
     */
308
    public String parseException(byte[] bytes) {
309
        try {
310
            String error = "";
311

    
312
            ServiceExceptionReport ex = (ServiceExceptionReport) ServiceExceptionReport.unmarshal(new InputStreamReader(
313
                        new ByteArrayInputStream(bytes)));
314
            ServiceException[] exs = ex.getServiceException();
315

    
316
            for (int i = 0; i < exs.length; i++) {
317
                if (exs[i].getCode() != null) {
318
                    error = error + exs[i].getCode() + ": ";
319
                }
320

    
321
                error += (exs[i].getContent() + "\n");
322
            }
323

    
324
            return error;
325
        } catch (Throwable e) {
326
            return null;
327
        }
328
    }
329

    
330
    /**
331
     * @see com.iver.cit.gvsig.fmap.wms.WMSVersionClient#getInfoURLs()
332
     */
333
    public String[] getInfoURLs() {
334
        return infoURLs;
335
    }
336

    
337
        /**
338
         * @see com.iver.wmsclient.WMSClient#getInfoFormats()
339
         */
340
        public String[] getInfoFormats() {
341
                Format[] formats = root.getCapability().getRequest().getGetFeatureInfo().getFormat();
342
                String[] ret = new String[formats.length];
343
                for (int i = 0; i < formats.length; i++) {
344
                        ret[i] = formats[i].getContent();                        
345
                }
346
                
347
                return ret;
348
        }
349

    
350
        /**
351
         * @see com.iver.wmsclient.WMSVersionClient#getQueryHeader()
352
         */
353
        public String getQueryHeader() {
354
                return "VERSION=1.1.0&";
355
        }
356

    
357
        /**
358
         * @see com.iver.wmsclient.WMSClient#createQuery()
359
         */
360
        public MapQuery createQuery() {
361
        throw new RuntimeException("No debi? llegar nunca a esta linea");
362
        }
363
}