Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClientOld / MapClient.java @ 3321

History | View | Annotate | Download (5.88 KB)

1
package org.gvsig.remoteClientOld;
2

    
3
import java.awt.Image;
4
import java.awt.image.BufferedImage;
5
import java.io.ByteArrayInputStream;
6
import java.io.IOException;
7
import java.net.URL;
8
import java.util.ArrayList;
9

    
10
import javax.imageio.ImageIO;
11

    
12
import org.gvsig.remoteClientOld.descriptor.Capabilities;
13
import org.gvsig.remoteClientOld.exception.ServerOutOfOrderException;
14
import org.gvsig.remoteClientOld.exception.UnsupportedVersionException;
15
import org.gvsig.remoteClientOld.exception.WMSException;
16
import org.gvsig.remoteClientOld.request.GetCapabilitiesRequest;
17
import org.gvsig.remoteClientOld.request.GetFeatureInfoRequest;
18
import org.gvsig.remoteClientOld.request.GetMapRequest;
19
import org.gvsig.remoteClientOld.utils.LogManager;
20
import org.gvsig.remoteClientOld.utils.Logger;
21
import org.gvsig.remoteClientOld.wms.WMSAdapter;
22
import org.gvsig.remoteClientOld.wms.WMSAdapterFactory;
23

    
24
import com.iver.cit.gvsig.fmap.DriverException;
25

    
26
public class MapClient extends RemoteClient implements IMapClient 
27
{
28
        protected MapAdapter adapter;
29
        protected Logger m_log;
30
        private Capabilities theCapabilities;
31
        
32
        
33
        public MapClient()
34
        {
35
                m_log = LogManager.getLogManager().getLogger(getClass().getName());        
36
        }
37
        
38
        
39
        public String getName()
40
        {
41
                return "WMSClient"; 
42
        }
43
        
44
        
45
        /**
46
         * Sets the server that we want to connect to.
47
         * Establece el servidor al que se quiere conectar.
48
         * @param host
49
         */
50
        public void setHost(String host)
51
        {
52
                try
53
                {
54
                        if (adapter == null)
55
                        {
56
                                adapter = WMSAdapterFactory.getDriver(new URL(host));
57
                        }
58
                        adapter.setHost(host);
59
                        adapter.setServiceName("WMS");
60
                }
61
                catch(Exception e)
62
                {
63
                        m_log.error("WMSClient.setHost(). " + e);
64
                }
65
        }
66
        
67
        /**
68
         * Sends the GetCapabilities operation to the server.
69
         * Env?a la operaci?n GetCapabilities al servidor.
70
         * @throws ServerOutOfOrderException 
71
         */
72
        public Capabilities getCapabilities() 
73
                throws ServerOutOfOrderException, IOException, UnsupportedVersionException 
74
        {                
75
                theCapabilities = adapter.getCapabilities();
76
                return theCapabilities;
77
        }
78

    
79
        public Image getMap(GetMapRequest request) 
80
        throws IOException, NoSuchFieldException, WMSException 
81
        {        
82
                BufferedImage image = new BufferedImage(request.getWidth(),
83
                                request.getHeight(), BufferedImage.TYPE_INT_ARGB);
84

    
85
                try {
86
                        byte[] res = adapter.getMap(request);
87
                        ByteArrayInputStream inbytes = new ByteArrayInputStream(res);
88
                        image = ImageIO.read(inbytes);
89

    
90
                        return image;
91
                } catch (Exception e) {
92
                        return null;
93
                }
94
        }
95
        
96
        public void getFeatureInfo(GetFeatureInfoRequest request) 
97
        throws IOException, NoSuchFieldException, WMSException 
98
        {
99
                try
100
                {
101
                        adapter.getFeatureInfo(request);
102
                }
103
                catch(Exception e){}
104
        }
105
        
106
        /**
107
         * Returns the server's name. The string is extracted from the GetCapabilities
108
         * response.
109
         * Devuelve el nombre del servidor indicado por ?ste.
110
         * @return String
111
         */
112
        public String getServerName()
113
        {
114
                try
115
                {
116
                        if (theCapabilities == null)
117
                        {
118
                                theCapabilities = getCapabilities();
119
                        }
120
                        return theCapabilities.getServiceInformation().getName();                        
121
                }
122
                catch(Exception e)
123
                {
124
                        m_log.error("Error reading ServerName from Capabilities. " + e);
125
                        return "";
126
                }
127
        }
128
                
129
        /**
130
         * Returns a string containing the server's WMS version number.
131
         * Devuelve el n?mero de versi?n WMS del servidor
132
         * @return String
133
         */
134
        public String getVersion()
135
        {
136
                try
137
                {
138
                        if (theCapabilities == null)
139
                        {
140
                                theCapabilities = getCapabilities();
141
                        }
142
                        return theCapabilities.getServiceInformation().getVersion();                        
143
                }
144
                catch(Exception e)
145
                {
146
                        m_log.error("Error reading version from Capabilities. " + e);
147
                        return "";
148
                }        
149
        }
150
        
151
        /**
152
         * Returns name and description of the server. It is supposed to be used
153
         * as the source of the abstract field in your application's interface.
154
         * Devuelve nombre y descripci?n (abstract) del servidor.
155
         * @return String
156
         */
157
        public String getDescription()
158
        {
159
                try
160
                {
161
                        if (theCapabilities == null)
162
                        {
163
                                theCapabilities = getCapabilities();
164
                        }
165
                        String name= theCapabilities.getServiceInformation().getName();
166
                        String title = theCapabilities.getServiceInformation().getTitle();
167
                        String abstr = theCapabilities.getServiceInformation().getAbstract();
168
                        
169
                        String description = name + "\n\n" + title + "\n" + abstr;
170
                        if ( description.trim().length() == 0) return "No description";
171
                        else return description;
172
                }
173
                catch(Exception e)
174
                {
175
                        m_log.error("Error reading description from Capabilities. " + e);
176
                        return "";
177
                }        
178
        }                
179
        
180
        
181
        /**
182
         * Returns a list containing the formats supported by the map
183
         * @return ArrayList
184
         */
185
        public ArrayList getMapFormats(){
186
                ArrayList formats = null;
187
                try
188
                {
189
                        if (theCapabilities == null)
190
                        {
191
                                theCapabilities = getCapabilities();
192
                        }                        
193
                        formats = theCapabilities.getServiceInformation().mapRequestInfo.formats;
194
                        //return null;//wms.getCapabilities().getFormats();
195
                }
196
                catch(Exception e){}
197
                
198
                return formats;
199
        }
200
        
201
        public String[] getLayerNames(){
202
                String[] names = null;                
203
                try
204
                {
205
                        if (theCapabilities == null)
206
                        {
207
                                theCapabilities = getCapabilities();
208
                        }                        
209
                        names = theCapabilities.getMapComposition().getLayerNames(); 
210
                }
211
                catch(Exception e){}
212
                return names;
213
        }
214
        
215
        /**
216
         * Returns the number of layers at the server.
217
         * Devuelve el numero de layers en el servidor
218
         * @return int
219
         */
220
        public int getNumOfLayers(){
221
                return getLayerNames().length;
222
        }
223

    
224
        
225
        /**
226
         * Returns an ArrayList containing a set of Strings with the layer's SRSs.
227
         *          
228
         * @param layer name
229
         * @return ArrayList
230
         */
231
        public ArrayList getSRSs(String layerName) 
232
        {        
233
                //TODO: get all the SRS from a Layer.
234
                return null;
235
        }        
236
        
237
        public void close()
238
        {}
239
        
240
        public void connect() throws IOException, DriverException 
241
        {
242
                try {
243
                        if ((adapter.getHost() != null) && (adapter.getHost().trim().length() > 0))
244
                        {
245
                                getCapabilities();                        
246
                                adapter.setConnected(true);
247
                        }
248
                        else
249
                        {
250
                                m_log.error("No host specified. Must specify a host to connect to");
251
                        }
252
                } catch (Exception e) {
253
                        throw new DriverException(e);
254
                }
255
        }
256
}