這篇是轉載的平常心博客,原地址見: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:
- <LayerqueryableLayerqueryable="1">
- <Name>spearfish</Name>
- <Title>spearfish</Title>
- <Abstract>Layer-Group type layer: spearfish</Abstract>
- <CRS>EPSG:26713</CRS>
- <EX_GeographicBoundingBox>
- <westBoundLongitude>-103.87791475407893</westBoundLongitude>
- <eastBoundLongitude>-103.62278893469492</eastBoundLongitude>
- <southBoundLatitude>44.37246687108142</southBoundLatitude>
- <northBoundLatitude>44.50235105543566</northBoundLatitude>
- </EX_GeographicBoundingBox>
- <BoundingBoxCRSBoundingBoxCRS="EPSG:26713"minx="589425.9342365642"miny="4913959.224611808"maxx="609518.6719560538"maxy="4928082.949945881"/>
- </Layer>
- <LayerqueryableLayerqueryable="1">
- <Name>tasmania</Name>
- <Title>tasmania</Title>
- <Abstract>Layer-Group type layer: tasmania</Abstract>
- <CRS>EPSG:4326</CRS>
- <EX_GeographicBoundingBox>
- <westBoundLongitude>143.83482400000003</westBoundLongitude>
- <eastBoundLongitude>148.47914100000003</eastBoundLongitude>
- <southBoundLatitude>-43.648056</southBoundLatitude>
- <northBoundLatitude>-39.573891</northBoundLatitude>
- </EX_GeographicBoundingBox>
- <BoundingBoxCRSBoundingBoxCRS="EPSG:4326"minx="-43.648056"miny="143.83482400000003"maxx="-39.573891"maxy="148.47914100000003"/>
- </Layer>
- <LayerqueryableLayerqueryable="1">
- <Name>tiger-ny</Name>
- <Title>tiger-ny</Title>
- <Abstract>Layer-Group type layer: tiger-ny</Abstract>
- <CRS>EPSG:4326</CRS>
- <EX_GeographicBoundingBox>
- <westBoundLongitude>-74.047185</westBoundLongitude>
- <eastBoundLongitude>-73.907005</eastBoundLongitude>
- <southBoundLatitude>40.679648</southBoundLatitude>
- <northBoundLatitude>40.882078</northBoundLatitude>
- </EX_GeographicBoundingBox>
- <BoundingBoxCRSBoundingBoxCRS="EPSG:4326"minx="40.679648"miny="-74.047185"maxx="40.882078"maxy="-73.907005"/>
- </Layer>
- <LayerqueryableLayerqueryable="1">
- <Name>省界_region</Name>
- <Title>省界_region</Title>
- <Abstract/>
- <KeywordList>
- <Keyword>features</Keyword>
- <Keyword>省界_region</Keyword>
- </KeywordList>
- <CRS>EPSG:4326</CRS>
- <CRS>CRS:84</CRS>
- <EX_GeographicBoundingBox>
- <westBoundLongitude>73.441277</westBoundLongitude>
- <eastBoundLongitude>135.08693</eastBoundLongitude>
- <southBoundLatitude>18.159829</southBoundLatitude>
- <northBoundLatitude>53.561771</northBoundLatitude>
- </EX_GeographicBoundingBox>
- <BoundingBoxCRSBoundingBoxCRS="CRS:84"minx="73.441277"miny="18.159829"maxx="135.08693"maxy="53.561771"/>
- <BoundingBoxCRSBoundingBoxCRS="EPSG:4326"minx="18.159829"miny="73.441277"maxx="53.561771"maxy="135.08693"/>
- <Style>
- <Name>polygon_x</Name>
- <Title>Default Polygon</Title>
- <Abstract>A sample style that draws a polygon</Abstract>
- <LegendURLwidthLegendURLwidth="20"height="20">
- <Format>image/png</Format>
- <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"/>
- </LegendURL>
- </Style>
- </Layer>
里面的每一個Layer節點都是一個WMS圖層。
2、例子
- package gov.nasa.worldwindx.v5cn;
- import gov.nasa.worldwind.avlist.AVKey;
- import gov.nasa.worldwind.avlist.AVList;
- import gov.nasa.worldwind.avlist.AVListImpl;
- import gov.nasa.worldwind.ogc.wms.WMSCapabilities;
- import gov.nasa.worldwind.wms.WMSTiledImageLayer;
- import gov.nasa.worldwindx.examples.ApplicationTemplate;
- import java.net.URI;
- import java.net.URISyntaxException;
- public class WMSLayerTest extends ApplicationTemplate {
- public static class AppFrame extends ApplicationTemplate.AppFrame{
- private static final long serialVersionUID = 1L;
- public AppFrame(){
- try {
- //請求地圖的URL
- String uri = "http://127.0.0.1:8080/geoserver/PostGIS/wms";
- WMSCapabilities caps;
- URI serverURI = new URI(uri);
- //獲得WMSCapabilities對象
- caps = WMSCapabilities.retrieve(serverURI);
- //解析WMSCapabilities數據
- caps.parse();
- AVList params = new AVListImpl();
- //圖層的名稱
- params.setValue(AVKey.LAYER_NAMES, "planet_osm_line");
- //地圖服務的協議,這里是OGC:WMS
- params.setValue(AVKey.SERVICE_NAME, "OGC:WMS");
- //獲得地圖的uri,也就是上面定義的uri
- params.setValue(AVKey.GET_MAP_URL, uri);
- //在本地緩存文件的名稱
- params.setValue(AVKey.DATA_CACHE_NAME, "planet_osm_line");
- params.setValue(AVKey.TILE_URL_BUILDER, new WMSTiledImageLayer.URLBuilder(params));
- WMSTiledImageLayer imageLayer = new WMSTiledImageLayer(caps,params);
- //圖層名稱
- imageLayer.setName("planet_osm_line");
- imageLayer.setEnabled(true);
- //圖層的透明度
- imageLayer.setOpacity(1);
- //圖層的最大顯示高度
- imageLayer.setMaxActiveAltitude(33500000);
- getWwd().getModel().getLayers().add(imageLayer);
- getLayerPanel().update(getWwd());
- } catch (URISyntaxException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- ApplicationTemplate.start("WMS圖層", WMSLayerTest.AppFrame.class);
- }
- }
請求的URL就是GeoServer中點擊【OpenLayers】中的路徑:
圖層名稱就是定義發布服務是的名稱,可以在GeoServer中找到。
