Revision 5393 branches/MULTITHREADING_DEVELOPMENT/extensions/extWMS/src/com/iver/cit/gvsig/fmap/drivers/wms/FMapWMSDriver.java
FMapWMSDriver.java | ||
---|---|---|
52 | 52 |
import java.util.Vector; |
53 | 53 |
|
54 | 54 |
import org.gvsig.remoteClient.exceptions.ServerErrorException; |
55 |
import org.gvsig.remoteClient.taskplanning.ICancellable; |
|
55 | 56 |
import org.gvsig.remoteClient.taskplanning.retrieving.RetrieveEvent; |
56 | 57 |
import org.gvsig.remoteClient.wms.WMSClient; |
57 | 58 |
import org.gvsig.remoteClient.wms.WMSDimension; |
... | ... | |
63 | 64 |
|
64 | 65 |
import com.iver.cit.gvsig.fmap.drivers.UnsupportedVersionException; |
65 | 66 |
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode; |
67 |
import com.iver.cit.gvsig.gui.wizards.WMSWizardData; |
|
68 |
|
|
66 | 69 |
/** |
67 | 70 |
* Driver WMS. |
68 |
* |
|
71 |
*
|
|
69 | 72 |
* @author Jaume Dominguez Faus |
70 | 73 |
*/ |
71 | 74 |
public class FMapWMSDriver implements WMSEventListener { |
72 |
|
|
73 |
|
|
75 |
|
|
74 | 76 |
protected WMSClient client; |
75 |
private WMSLayerNode fmapRootLayer; |
|
76 |
private TreeMap layers = new TreeMap(); |
|
77 |
private Vector listeners = new Vector(); |
|
78 |
|
|
79 |
|
|
80 |
public void newEvent(int idRequest, RetrieveEvent event) { |
|
81 |
int eventType = event.getType(); |
|
82 |
if (idRequest == WMSOperations.GET_CAPABILITIES) { |
|
83 |
|
|
84 |
if (eventType == WMSOperations.REQUEST_FINISHED) { |
|
85 |
WMSLayer clientRoot = client.getLayersRoot(); |
|
86 |
fmapRootLayer = parseTree(clientRoot, null); |
|
87 |
|
|
88 |
} |
|
89 |
for (int i = 0; i < listeners.size(); i++) { |
|
90 |
WMSOperations l = (WMSOperations) listeners.get(i); |
|
91 |
l.getCapabilities(eventType, event.getMessage()); |
|
92 |
} |
|
93 |
} else if (idRequest == WMSOperations.GET_MAP) { |
|
94 |
String fileName = event.getFileName(); |
|
95 |
for (int i = 0; i < listeners.size(); i++) { |
|
96 |
WMSOperations l = (WMSOperations) listeners.get(i); |
|
97 |
l.getMap(eventType, new File(fileName), event.getMessage()); |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
77 |
|
|
78 |
private WMSLayerNode fmapRootLayer; |
|
79 |
|
|
80 |
private TreeMap layers = new TreeMap(); |
|
81 |
|
|
82 |
private Vector listeners = new Vector(); |
|
83 |
|
|
84 |
private WMSOperations wizardDataListener; |
|
85 |
|
|
86 |
public void newEvent(int idRequest, RetrieveEvent event) { |
|
87 |
int eventType = event.getType(); |
|
88 |
if (idRequest == WMSOperations.GET_CAPABILITIES) { |
|
89 |
|
|
90 |
if (eventType == WMSOperations.REQUEST_FINISHED) { |
|
91 |
WMSLayer clientRoot = client.getLayersRoot(); |
|
92 |
fmapRootLayer = parseTree(clientRoot, null); |
|
93 |
} |
|
94 |
wizardDataListener.getCapabilities(eventType, event.getMessage()); |
|
95 |
|
|
96 |
} else if (idRequest == WMSOperations.GET_MAP) { |
|
97 |
String fileName = event.getFileName(); |
|
98 |
for (int i = 0; i < listeners.size(); i++) { |
|
99 |
WMSOperations l = (WMSOperations) listeners.get(i); |
|
100 |
l.getMap(eventType, new File(fileName), event.getMessage()); |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
102 | 105 |
protected FMapWMSDriver(URL host) { |
103 | 106 |
try { |
104 | 107 |
client = new WMSClient(host.toString(), this); |
... | ... | |
110 | 113 |
} |
111 | 114 |
|
112 | 115 |
/* |
113 |
* (non-Javadoc) |
|
116 |
* (non-Javadoc) |
|
117 |
* |
|
114 | 118 |
* @see com.iver.cit.gvsig.fmap.drivers.WMSDriver#getMap(org.gvsig.remoteClient.wms.WMSStatus) |
115 | 119 |
*/ |
116 |
public void getMap(WMSStatus status) throws WMSException { |
|
117 |
try { |
|
118 |
client.getMap(status); |
|
119 |
} catch (org.gvsig.remoteClient.exceptions.WMSException e) { |
|
120 |
throw new WMSException(e.getMessage()); |
|
121 |
} catch (ServerErrorException e) { |
|
122 |
throw new WMSException("WMS Unexpected server error."+e.getMessage()); |
|
123 |
} |
|
124 |
} |
|
125 |
|
|
126 |
/** |
|
127 |
* Establishes the connection. |
|
128 |
* @param override, if true the previous downloaded data will be overridden |
|
129 |
* @return <b>true</b> if the connection was successful, or <b>false</b> if it was no possible |
|
130 |
* to establish the connection for any reason such as version negotiation. |
|
131 |
*/ |
|
132 |
public boolean connect(boolean override) { |
|
133 |
return client.connect(override); |
|
134 |
} |
|
120 |
public void getMap(WMSStatus status, ICancellable cancel) |
|
121 |
throws WMSException { |
|
122 |
client.getMap(status, cancel); |
|
123 |
} |
|
135 | 124 |
|
136 |
public boolean connect() { |
|
137 |
return client.connect(false); |
|
138 |
} |
|
125 |
/** |
|
126 |
* Establishes the connection. |
|
127 |
* |
|
128 |
* @param override, |
|
129 |
* if true the previous downloaded data will be overridden |
|
130 |
* @return <b>true</b> if the connection was successful, or <b>false</b> |
|
131 |
* if it was no possible to establish the connection for any reason |
|
132 |
* such as version negotiation. |
|
133 |
*/ |
|
134 |
public boolean connect(boolean override) { |
|
139 | 135 |
|
140 |
/** |
|
141 |
* @return the version of this client. |
|
142 |
*/ |
|
143 |
public String getVersion() { |
|
144 |
return client.getVersion(); |
|
145 |
} |
|
136 |
return client.connect(override); |
|
137 |
} |
|
146 | 138 |
|
147 |
/** |
|
148 |
* @return The title of the service offered by the WMS server. |
|
149 |
*/ |
|
150 |
public String getServiceTitle() { |
|
151 |
return client.getServiceInformation().title; |
|
152 |
} |
|
139 |
public boolean connect() { |
|
140 |
return client.connect(false); |
|
141 |
} |
|
153 | 142 |
|
154 |
/** |
|
155 |
* Returns a Hash table containing the values for each online resource. |
|
156 |
* Using as key a String with name of the WMS request and the value returned |
|
157 |
* by the hash is another string containing the corresponding Url |
|
158 |
* @return HashTable |
|
159 |
*/ |
|
160 |
public Hashtable getOnlineResources() { |
|
161 |
Hashtable onlineResources = new Hashtable(); |
|
162 |
ServiceInformation si = client.getServiceInformation(); |
|
163 |
Iterator it = si.operations.keySet().iterator(); |
|
164 |
while (it.hasNext()) { |
|
165 |
String key = (String) it.next(); |
|
166 |
String val = (String) si.operations.get(key); |
|
167 |
if (val==null && (si.online_resource!=null || si.online_resource!= "")) |
|
168 |
val = si.online_resource; |
|
169 |
if (val!=null) { |
|
170 |
onlineResources.put(key, val); |
|
171 |
} |
|
172 |
} |
|
173 |
return onlineResources; |
|
174 |
} |
|
175 |
/** |
|
176 |
* @return <b>Vector</b> containing strings with the formats supported by this server. |
|
177 |
*/ |
|
178 |
public Vector getFormats() { |
|
179 |
return client.getFormats(); |
|
180 |
} |
|
143 |
/** |
|
144 |
* @return the version of this client. |
|
145 |
*/ |
|
146 |
public String getVersion() { |
|
147 |
return client.getVersion(); |
|
148 |
} |
|
181 | 149 |
|
182 |
/** |
|
183 |
* @return <b>Boolean</b> returns if the WMS is queryable (suppports GetFeatureInfo) |
|
184 |
*/ |
|
185 |
public boolean isQueryable() { |
|
186 |
return client.isQueryable(); |
|
187 |
} |
|
188 |
|
|
189 |
/** |
|
190 |
* @return A tree containing the info of all layers available on this server. |
|
191 |
*/ |
|
192 |
public WMSLayerNode getLayersTree() { |
|
193 |
return fmapRootLayer; |
|
194 |
} |
|
150 |
/** |
|
151 |
* @return The title of the service offered by the WMS server. |
|
152 |
*/ |
|
153 |
public String getServiceTitle() { |
|
154 |
return client.getServiceInformation().title; |
|
155 |
} |
|
195 | 156 |
|
196 |
/** |
|
197 |
* Parses the client's layer node and translates it to a fmap's layer node |
|
198 |
* @param WMSLayer |
|
199 |
* @return WMSLayerNode |
|
200 |
*/ |
|
201 |
private WMSLayerNode parseTree(WMSLayer node, WMSLayerNode parentNode) { |
|
202 |
|
|
203 |
WMSLayerNode myNode = new WMSLayerNode(); |
|
204 |
|
|
205 |
// Name |
|
206 |
myNode.setName(node.getName()); |
|
207 |
|
|
208 |
// Title |
|
209 |
myNode.setTitle(node.getTitle()); |
|
210 |
|
|
211 |
// Transparency |
|
212 |
myNode.setTransparency(node.hasTransparency()); |
|
213 |
|
|
214 |
// SRS |
|
215 |
myNode.setSrs(node.getAllSrs()); |
|
216 |
|
|
217 |
// Queryable |
|
218 |
myNode.setQueryable(node.isQueryable() && client.getServiceInformation().isQueryable()); |
|
219 |
|
|
220 |
// Parent layer |
|
221 |
myNode.setParent(parentNode); |
|
222 |
|
|
223 |
// Abstract |
|
224 |
myNode.setAbstract(node.getAbstract()); |
|
225 |
|
|
226 |
// Fixed Size |
|
227 |
myNode.setFixedSize(node.getfixedWidth(), node.getfixedHeight()); |
|
228 |
|
|
229 |
// LatLonBox |
|
230 |
if (node.getLatLonBox()!=null) |
|
231 |
myNode.setLatLonBox(node.getLatLonBox().toString()); |
|
232 |
|
|
233 |
// Styles |
|
234 |
ArrayList styles = node.getStyles(); |
|
235 |
for (int i = 0; i < styles.size(); i++) { |
|
236 |
WMSStyle style = (WMSStyle) styles.get(i); |
|
237 |
myNode.addStyle(style.getName(), style.getTitle(), style.getAbstract()); |
|
238 |
} |
|
239 |
|
|
240 |
// Dimensions |
|
241 |
ArrayList dimensions = node.getDimensions(); |
|
242 |
for (int i = 0; i < dimensions.size(); i++) { |
|
243 |
WMSDimension d = (WMSDimension) dimensions.get(i); |
|
244 |
myNode.addDimension(d.getName(), d.getUnits(), d.getUnitSymbol(), d.getDimensionExpression()); |
|
245 |
} |
|
246 |
|
|
247 |
// Childs |
|
248 |
int children = node.getChildren().size(); |
|
249 |
myNode.setChildren(new ArrayList()); |
|
250 |
for (int i = 0; i < children; i++) { |
|
251 |
myNode.getChildren().add(parseTree((WMSLayer)node.getChildren().get(i), myNode)); |
|
252 |
} |
|
253 |
|
|
254 |
|
|
255 |
if (myNode.getName()!=null) |
|
256 |
layers.put(myNode.getName(), myNode); |
|
257 |
|
|
258 |
return myNode; |
|
259 |
} |
|
157 |
/** |
|
158 |
* Returns a Hash table containing the values for each online resource. |
|
159 |
* Using as key a String with name of the WMS request and the value returned |
|
160 |
* by the hash is another string containing the corresponding Url |
|
161 |
* |
|
162 |
* @return HashTable |
|
163 |
*/ |
|
164 |
public Hashtable getOnlineResources() { |
|
165 |
Hashtable onlineResources = new Hashtable(); |
|
166 |
ServiceInformation si = client.getServiceInformation(); |
|
167 |
Iterator it = si.operations.keySet().iterator(); |
|
168 |
while (it.hasNext()) { |
|
169 |
String key = (String) it.next(); |
|
170 |
String val = (String) si.operations.get(key); |
|
171 |
if (val == null |
|
172 |
&& (si.online_resource != null || si.online_resource != "")) |
|
173 |
val = si.online_resource; |
|
174 |
if (val != null) { |
|
175 |
onlineResources.put(key, val); |
|
176 |
} |
|
177 |
} |
|
178 |
return onlineResources; |
|
179 |
} |
|
260 | 180 |
|
261 |
/** |
|
262 |
* @return |
|
263 |
*/ |
|
264 |
public String getAbstract() { |
|
265 |
return client.getServiceInformation().abstr; |
|
266 |
} |
|
181 |
/** |
|
182 |
* @return <b>Vector</b> containing strings with the formats supported by |
|
183 |
* this server. |
|
184 |
*/ |
|
185 |
public Vector getFormats() { |
|
186 |
return client.getFormats(); |
|
187 |
} |
|
267 | 188 |
|
268 |
/** |
|
269 |
* @param layerName |
|
270 |
* @param srs |
|
271 |
* @return |
|
272 |
*/ |
|
273 |
public Rectangle2D getLayersExtent(String[] layerName, String srs) { |
|
274 |
return client.getLayersExtent(layerName, srs); |
|
275 |
} |
|
189 |
/** |
|
190 |
* @return <b>Boolean</b> returns if the WMS is queryable (suppports |
|
191 |
* GetFeatureInfo) |
|
192 |
*/ |
|
193 |
public boolean isQueryable() { |
|
194 |
return client.isQueryable(); |
|
195 |
} |
|
276 | 196 |
|
277 |
/** |
|
278 |
* @param string |
|
279 |
* @return |
|
280 |
*/ |
|
281 |
public WMSLayerNode getLayer(String layerName) { |
|
282 |
if (getLayers().get(layerName) != null) |
|
283 |
{ |
|
284 |
return (WMSLayerNode)layers.get(layerName); |
|
285 |
} |
|
286 |
return null; |
|
287 |
} |
|
197 |
/** |
|
198 |
* @return A tree containing the info of all layers available on this |
|
199 |
* server. |
|
200 |
*/ |
|
201 |
public WMSLayerNode getLayersTree() { |
|
202 |
return fmapRootLayer; |
|
203 |
} |
|
288 | 204 |
|
289 |
/** |
|
290 |
* @return |
|
291 |
*/ |
|
292 |
private TreeMap getLayers() { |
|
293 |
if (fmapRootLayer == null){ |
|
294 |
fmapRootLayer = getLayersTree(); |
|
295 |
} |
|
296 |
return layers; |
|
297 |
} |
|
205 |
/** |
|
206 |
* Parses the client's layer node and translates it to a fmap's layer node |
|
207 |
* |
|
208 |
* @param WMSLayer |
|
209 |
* @return WMSLayerNode |
|
210 |
*/ |
|
211 |
private WMSLayerNode parseTree(WMSLayer node, WMSLayerNode parentNode) { |
|
298 | 212 |
|
299 |
/** |
|
300 |
* @param wmsStatus |
|
301 |
* @param i |
|
302 |
* @param j |
|
303 |
* @param max_value |
|
304 |
* @return |
|
305 |
* @throws WMSException |
|
306 |
*/ |
|
307 |
public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, int max_value) throws WMSException { |
|
308 |
try { |
|
309 |
return client.getFeatureInfo(_wmsStatus, i, j, max_value); |
|
310 |
} catch (org.gvsig.remoteClient.exceptions.WMSException e) { |
|
311 |
throw new WMSException(); |
|
312 |
} |
|
313 |
} |
|
314 |
|
|
315 |
public String getHost(){ |
|
316 |
return client.getHost(); |
|
317 |
} |
|
318 |
|
|
319 |
public void addListener(WMSOperations listener) { |
|
320 |
if (!listeners.contains(listener)) { |
|
321 |
System.out.println("Listener afegit al driver"); |
|
322 |
listeners.add(listener); |
|
323 |
} |
|
324 |
} |
|
325 |
|
|
213 |
WMSLayerNode myNode = new WMSLayerNode(); |
|
214 |
|
|
215 |
// Name |
|
216 |
myNode.setName(node.getName()); |
|
217 |
|
|
218 |
// Title |
|
219 |
myNode.setTitle(node.getTitle()); |
|
220 |
|
|
221 |
// Transparency |
|
222 |
myNode.setTransparency(node.hasTransparency()); |
|
223 |
|
|
224 |
// SRS |
|
225 |
myNode.setSrs(node.getAllSrs()); |
|
226 |
|
|
227 |
// Queryable |
|
228 |
myNode.setQueryable(node.isQueryable() |
|
229 |
&& client.getServiceInformation().isQueryable()); |
|
230 |
|
|
231 |
// Parent layer |
|
232 |
myNode.setParent(parentNode); |
|
233 |
|
|
234 |
// Abstract |
|
235 |
myNode.setAbstract(node.getAbstract()); |
|
236 |
|
|
237 |
// Fixed Size |
|
238 |
myNode.setFixedSize(node.getfixedWidth(), node.getfixedHeight()); |
|
239 |
|
|
240 |
// LatLonBox |
|
241 |
if (node.getLatLonBox() != null) |
|
242 |
myNode.setLatLonBox(node.getLatLonBox().toString()); |
|
243 |
|
|
244 |
// Styles |
|
245 |
ArrayList styles = node.getStyles(); |
|
246 |
for (int i = 0; i < styles.size(); i++) { |
|
247 |
WMSStyle style = (WMSStyle) styles.get(i); |
|
248 |
myNode.addStyle(style.getName(), style.getTitle(), style |
|
249 |
.getAbstract()); |
|
250 |
} |
|
251 |
|
|
252 |
// Dimensions |
|
253 |
ArrayList dimensions = node.getDimensions(); |
|
254 |
for (int i = 0; i < dimensions.size(); i++) { |
|
255 |
WMSDimension d = (WMSDimension) dimensions.get(i); |
|
256 |
myNode.addDimension(d.getName(), d.getUnits(), d.getUnitSymbol(), d |
|
257 |
.getDimensionExpression()); |
|
258 |
} |
|
259 |
|
|
260 |
// Childs |
|
261 |
int children = node.getChildren().size(); |
|
262 |
myNode.setChildren(new ArrayList()); |
|
263 |
for (int i = 0; i < children; i++) { |
|
264 |
myNode.getChildren().add( |
|
265 |
parseTree((WMSLayer) node.getChildren().get(i), myNode)); |
|
266 |
} |
|
267 |
|
|
268 |
if (myNode.getName() != null) |
|
269 |
layers.put(myNode.getName(), myNode); |
|
270 |
|
|
271 |
return myNode; |
|
272 |
} |
|
273 |
|
|
274 |
/** |
|
275 |
* @return |
|
276 |
*/ |
|
277 |
public String getAbstract() { |
|
278 |
return client.getServiceInformation().abstr; |
|
279 |
} |
|
280 |
|
|
281 |
/** |
|
282 |
* @param layerName |
|
283 |
* @param srs |
|
284 |
* @return |
|
285 |
*/ |
|
286 |
public Rectangle2D getLayersExtent(String[] layerName, String srs) { |
|
287 |
return client.getLayersExtent(layerName, srs); |
|
288 |
} |
|
289 |
|
|
290 |
/** |
|
291 |
* @param string |
|
292 |
* @return |
|
293 |
*/ |
|
294 |
public WMSLayerNode getLayer(String layerName) { |
|
295 |
if (getLayers().get(layerName) != null) { |
|
296 |
return (WMSLayerNode) layers.get(layerName); |
|
297 |
} |
|
298 |
return null; |
|
299 |
} |
|
300 |
|
|
301 |
/** |
|
302 |
* @return |
|
303 |
*/ |
|
304 |
private TreeMap getLayers() { |
|
305 |
if (fmapRootLayer == null) { |
|
306 |
fmapRootLayer = getLayersTree(); |
|
307 |
} |
|
308 |
return layers; |
|
309 |
} |
|
310 |
|
|
311 |
/** |
|
312 |
* @param wmsStatus |
|
313 |
* @param i |
|
314 |
* @param j |
|
315 |
* @param max_value |
|
316 |
* @return |
|
317 |
* @throws WMSException |
|
318 |
*/ |
|
319 |
public String getFeatureInfo(WMSStatus _wmsStatus, int i, int j, |
|
320 |
int max_value) throws WMSException { |
|
321 |
try { |
|
322 |
return client.getFeatureInfo(_wmsStatus, i, j, max_value); |
|
323 |
} catch (org.gvsig.remoteClient.exceptions.WMSException e) { |
|
324 |
throw new WMSException(); |
|
325 |
} |
|
326 |
} |
|
327 |
|
|
328 |
public String getHost() { |
|
329 |
return client.getHost(); |
|
330 |
} |
|
331 |
|
|
332 |
public void addListener(WMSOperations listener) { |
|
333 |
if (!listeners.contains(listener)) { |
|
334 |
listeners.add(listener); |
|
335 |
} |
|
336 |
} |
|
337 |
|
|
326 | 338 |
public void cancel(int opCode) { |
327 |
client.cancel(WMSOperations.GET_CAPABILITIES);
|
|
339 |
client.cancel(opCode);
|
|
328 | 340 |
} |
329 |
|
|
341 |
|
|
342 |
public void setWizardDataListener(WMSOperations wizard) { |
|
343 |
wizardDataListener = wizard; |
|
344 |
} |
|
345 |
|
|
330 | 346 |
} |
331 |
|
Also available in: Unified diff