World Wind Java開發之十三——加載Geoserver發布的WMS服務(轉)


這篇是轉載的平常心博客,原地址見:http://www.v5cn.cn/?p=171

 

1、WMSTiledImageLayer類說明

 

 

一個WMSTiledImageLayer類對象只能對應一個WMS發布的服務圖層,使用WMS服務時需要使用到WMS元數據描述類WMSCapabilities對象,我們使用WMSCapabilites類的靜態方法retrieve來獲得它的對象。WMSCapabilites對象可以包含WMS服務中所以的圖層和圖層樣式,我們通過把需要顯示的圖層名稱保存到AVList對象中,然后通過WMSCapabilites對象和AVList來創建WMSTiledImageLayer對象。我們可以通過一個請求地址來查看WMS服務所包含的元數據。例如:http://127.0.0.1:8080/geoserver/PostGIS/wms?request=GetGapabilities&Service=WMS 問號后面的地址是固定的,可以但看如下的XML:

 

[html]  view plain  copy
 
 print?在CODE上查看代碼片派生到我的代碼片
  1. <LayerqueryableLayerqueryable="1">  
  2. <Name>spearfish</Name>  
  3. <Title>spearfish</Title>  
  4. <Abstract>Layer-Group type layer: spearfish</Abstract>  
  5. <CRS>EPSG:26713</CRS>  
  6. <EX_GeographicBoundingBox>  
  7. <westBoundLongitude>-103.87791475407893</westBoundLongitude>  
  8. <eastBoundLongitude>-103.62278893469492</eastBoundLongitude>  
  9. <southBoundLatitude>44.37246687108142</southBoundLatitude>  
  10. <northBoundLatitude>44.50235105543566</northBoundLatitude>  
  11. </EX_GeographicBoundingBox>  
  12. <BoundingBoxCRSBoundingBoxCRS="EPSG:26713"minx="589425.9342365642"miny="4913959.224611808"maxx="609518.6719560538"maxy="4928082.949945881"/>  
  13. </Layer>  
  14. <LayerqueryableLayerqueryable="1">  
  15. <Name>tasmania</Name>  
  16. <Title>tasmania</Title>  
  17. <Abstract>Layer-Group type layer: tasmania</Abstract>  
  18. <CRS>EPSG:4326</CRS>  
  19. <EX_GeographicBoundingBox>  
  20. <westBoundLongitude>143.83482400000003</westBoundLongitude>  
  21. <eastBoundLongitude>148.47914100000003</eastBoundLongitude>  
  22. <southBoundLatitude>-43.648056</southBoundLatitude>  
  23. <northBoundLatitude>-39.573891</northBoundLatitude>  
  24. </EX_GeographicBoundingBox>  
  25. <BoundingBoxCRSBoundingBoxCRS="EPSG:4326"minx="-43.648056"miny="143.83482400000003"maxx="-39.573891"maxy="148.47914100000003"/>  
  26. </Layer>  
  27. <LayerqueryableLayerqueryable="1">  
  28. <Name>tiger-ny</Name>  
  29. <Title>tiger-ny</Title>  
  30. <Abstract>Layer-Group type layer: tiger-ny</Abstract>  
  31. <CRS>EPSG:4326</CRS>  
  32. <EX_GeographicBoundingBox>  
  33. <westBoundLongitude>-74.047185</westBoundLongitude>  
  34. <eastBoundLongitude>-73.907005</eastBoundLongitude>  
  35. <southBoundLatitude>40.679648</southBoundLatitude>  
  36. <northBoundLatitude>40.882078</northBoundLatitude>  
  37. </EX_GeographicBoundingBox>  
  38. <BoundingBoxCRSBoundingBoxCRS="EPSG:4326"minx="40.679648"miny="-74.047185"maxx="40.882078"maxy="-73.907005"/>  
  39. </Layer>  
  40. <LayerqueryableLayerqueryable="1">  
  41. <Name>省界_region</Name>  
  42. <Title>省界_region</Title>  
  43. <Abstract/>  
  44. <KeywordList>  
  45. <Keyword>features</Keyword>  
  46. <Keyword>省界_region</Keyword>  
  47. </KeywordList>  
  48. <CRS>EPSG:4326</CRS>  
  49. <CRS>CRS:84</CRS>  
  50. <EX_GeographicBoundingBox>  
  51. <westBoundLongitude>73.441277</westBoundLongitude>  
  52. <eastBoundLongitude>135.08693</eastBoundLongitude>  
  53. <southBoundLatitude>18.159829</southBoundLatitude>  
  54. <northBoundLatitude>53.561771</northBoundLatitude>  
  55. </EX_GeographicBoundingBox>  
  56. <BoundingBoxCRSBoundingBoxCRS="CRS:84"minx="73.441277"miny="18.159829"maxx="135.08693"maxy="53.561771"/>  
  57. <BoundingBoxCRSBoundingBoxCRS="EPSG:4326"minx="18.159829"miny="73.441277"maxx="53.561771"maxy="135.08693"/>  
  58. <Style>  
  59. <Name>polygon_x</Name>  
  60. <Title>Default Polygon</Title>  
  61. <Abstract>A sample style that draws a polygon</Abstract>  
  62. <LegendURLwidthLegendURLwidth="20"height="20">  
  63. <Format>image/png</Format>  
  64. <OnlineResourcexmlns:xlinkOnlineResourcexmlns:xlink="http://www.w3.org/1999/xlink"xlink:type="simple"xlink:href="http://localhost:8080/geoserver/cite/ows?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=%3F%3F_region"/>  
  65. </LegendURL>  
  66. </Style>  
  67. </Layer>  

 

里面的每一個Layer節點都是一個WMS圖層。

2、例子

[java]  view plain  copy
 
 print?在CODE上查看代碼片派生到我的代碼片
  1. package gov.nasa.worldwindx.v5cn;  
  2.    
  3. import gov.nasa.worldwind.avlist.AVKey;  
  4. import gov.nasa.worldwind.avlist.AVList;  
  5. import gov.nasa.worldwind.avlist.AVListImpl;  
  6. import gov.nasa.worldwind.ogc.wms.WMSCapabilities;  
  7. import gov.nasa.worldwind.wms.WMSTiledImageLayer;  
  8. import gov.nasa.worldwindx.examples.ApplicationTemplate;  
  9.    
  10. import java.net.URI;  
  11. import java.net.URISyntaxException;  
  12.    
  13. public class WMSLayerTest extends ApplicationTemplate {  
  14.     public static class AppFrame extends ApplicationTemplate.AppFrame{  
  15.    
  16.         private static final long serialVersionUID = 1L;  
  17.    
  18.         public AppFrame(){  
  19.             try {  
  20.                 //請求地圖的URL  
  21.                 String uri = "http://127.0.0.1:8080/geoserver/PostGIS/wms";  
  22.                 WMSCapabilities caps;  
  23.                 URI serverURI = new URI(uri);  
  24.    
  25.                 //獲得WMSCapabilities對象  
  26.                 caps = WMSCapabilities.retrieve(serverURI);  
  27.                 //解析WMSCapabilities數據  
  28.                 caps.parse();  
  29.    
  30.                 AVList params = new AVListImpl();  
  31.    
  32.                 //圖層的名稱  
  33.                 params.setValue(AVKey.LAYER_NAMES, "planet_osm_line");  
  34.                 //地圖服務的協議,這里是OGC:WMS  
  35.                 params.setValue(AVKey.SERVICE_NAME, "OGC:WMS");  
  36.                 //獲得地圖的uri,也就是上面定義的uri  
  37.                 params.setValue(AVKey.GET_MAP_URL, uri);  
  38.                 //在本地緩存文件的名稱  
  39.                 params.setValue(AVKey.DATA_CACHE_NAME, "planet_osm_line");  
  40.                 params.setValue(AVKey.TILE_URL_BUILDER, new WMSTiledImageLayer.URLBuilder(params));  
  41.    
  42.                 WMSTiledImageLayer imageLayer = new WMSTiledImageLayer(caps,params);  
  43.                 //圖層名稱  
  44.                 imageLayer.setName("planet_osm_line");  
  45.                 imageLayer.setEnabled(true);  
  46.                 //圖層的透明度  
  47.                 imageLayer.setOpacity(1);  
  48.                 //圖層的最大顯示高度  
  49.                 imageLayer.setMaxActiveAltitude(33500000);  
  50.                 getWwd().getModel().getLayers().add(imageLayer);  
  51.                 getLayerPanel().update(getWwd());  
  52.    
  53.             } catch (URISyntaxException e) {  
  54.                 e.printStackTrace();  
  55.             } catch (Exception e) {  
  56.                 e.printStackTrace();  
  57.             }  
  58.         }  
  59.    
  60.     }  
  61. public static void main(String[] args) {  
  62.         ApplicationTemplate.start("WMS圖層", WMSLayerTest.AppFrame.class);  
  63.     }  
  64. }  

請求的URL就是GeoServer中點擊【OpenLayers】中的路徑:


 

圖層名稱就是定義發布服務是的名稱,可以在GeoServer中找到。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM