World Wind Java开发之十四——添加WMS地图服务资源(转)
数据是GIS的核心,没有数据一切无从谈起,Internet上有很多在线WMS地图服务资源,我们可以好好利用这些数据资源,比如天地图、必应地图、NASA、OGC数据服务等等。
在我们国家常用的还是天地图的地图服务资源,详见:http://blog.3snews.net/space.php?uid=6955280&do=blog&id=67981,这篇博客列举了一些常用的在线地图服务资源,读者可以自行试下。
1、添加天地图地图服务
- public static WMSTiledImageLayer addTianDiTuImage() throws Exception
- {
- // 请求地图的URL
- String uri = "http://www.scgis.net.cn/imap/iMapServer/defaultRest/services/newtianditudom/WMS";
- // WMS
- WMSCapabilities caps;
- URI serverURI;
- serverURI = new URI(uri);
- // 获得WMSCapabilities对象
- caps = WMSCapabilities.retrieve(serverURI);
- // 解析WMSCapabilities数据
- caps.parse();
- // // 输出wms元数据信息
- // System.out.println(caps.getCapabilityInformation().toString());
- // 获取所有图层(这里只有一个,自己用geoserver发布的则可能有很多)
- final List<WMSLayerCapabilities> namedLayerCaps = caps.getNamedLayers();
- String layerName = null;
- for (WMSLayerCapabilities wmsLayerCapabilities : namedLayerCaps)
- {
- layerName = wmsLayerCapabilities.getName();
- }
- AVList params = new AVListImpl();
- // 图层的名称
- params.setValue(AVKey.LAYER_NAMES, layerName);
- // 地图服务的协议,这里是OGC:WMS
- params.setValue(AVKey.SERVICE_NAME, "OGC:WMS");
- // 获得地图的uri,也就是上面定义的uri
- params.setValue(AVKey.GET_MAP_URL, uri);
- // 在本地缓存文件的名称
- params.setValue(AVKey.DATA_CACHE_NAME, layerName);
- params.setValue(AVKey.TILE_URL_BUILDER,
- new WMSTiledImageLayer.URLBuilder(params));
- WMSTiledImageLayer layer = new WMSTiledImageLayer(caps, params);
- return layer;
- }
这里添加天地图影像作为底图,效果还是不错的,看下效果图:


另外,天地图还提供了在线注记底图服务,只需改动上面的地图请求地址即可,看下效果图:

这里加载的效果要比天地图的在线三维球的效果要好,有兴趣的可以去对比下。另外天地图的在线三维体验还是比较差的,只是单纯的浏览,单是这个体验效果还不是很好,毕竟这是政府的东东,不像Google财大气粗,全球几十万台服务器,天地图的商业化发展之路任重道远啊。
2、添加Geoserver发布的地图服务

3、WMS服务器管理面板
examples中有个WMSLayerManager,这个demo实现了wms服务的基本管理,只需根据自己的需要修改源代码即可。这里说下我自己修改源代码的方法,首先将需要修改的java文件在改目录下copy一份,名字自取,这样做的好处是不破坏源代码的完整性,因为其他的demo也可能用到这个java文件,避免了大量的修改,改完只需将src导出jar包即可。如下图所示:

- /*
- Copyright (C) 2001, 2006 United States Government
- as represented by the Administrator of the
- National Aeronautics and Space Administration.
- All Rights Reserved.
- */
- package gov.nasa.worldwindx.examples;
- import gov.nasa.worldwind.*;
- import gov.nasa.worldwind.avlist.*;
- import gov.nasa.worldwind.globes.ElevationModel;
- import gov.nasa.worldwind.layers.*;
- import gov.nasa.worldwind.ogc.wms.*;
- import gov.nasa.worldwind.terrain.CompoundElevationModel;
- import gov.nasa.worldwind.util.WWUtil;
- import gov.nasa.worldwindx.examples.WMSLayersPanel.LayerInfo;
- import javax.swing.*;
- import javax.swing.border.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- import java.util.*;
- import java.util.List;
- /**
- *
- * @项目名称:worldwind-1.5.0
- * @类名称:SmartScopeWMSLayersPanel
- * @类描述: WMS服务图层管理面板
- * @创建人:bluce
- * @创建时间:2015年2月4日 下午5:09:31
- * @修改备注:
- * @版本:
- */
- public class SmartScopeWMSLayersPanel extends JPanel
- {
- /**
- * @Fields serialVersionUID : TODO
- */
- private static final long serialVersionUID = 1L;
- protected static class LayerInfo
- {
- protected WMSCapabilities caps;
- protected AVListImpl params = new AVListImpl();
- protected String getTitle()
- {
- return params.getStringValue(AVKey.DISPLAY_NAME);
- }
- protected String getName()
- {
- return params.getStringValue(AVKey.LAYER_NAMES);
- }
- protected String getAbstract()
- {
- return params.getStringValue(AVKey.LAYER_ABSTRACT);
- }
- }
- // 所有图层元数据信息
- protected String[] servers;
- protected List<WMSLayerCapabilities> namedLayerCaps;
- protected WorldWindow wwd;
- protected URI serverURI;
- protected Dimension size;
- protected Thread loadingThread;
- protected TreeSet<LayerInfo> layerInfos = new TreeSet<LayerInfo>(
- new Comparator<LayerInfo>()
- {
- public int compare(LayerInfo infoA, LayerInfo infoB)
- {
- String nameA = infoA.getName();
- String nameB = infoB.getName();
- return nameA.compareTo(nameB);
- }
- });
- public SmartScopeWMSLayersPanel(String[] servers)
- {
- }
- /**
- *
- * 创建一个新的实例 SmartScopeWMSLayersPanel.
- *
- * @param wwd
- * @param server
- * @param size
- * @throws URISyntaxException
- */
- public SmartScopeWMSLayersPanel(WorldWindow wwd, String[] server,
- Dimension size) throws URISyntaxException
- {
- super(new BorderLayout());
- this.servers = server;
- this.wwd = wwd;
- this.size = size;
- this.setPreferredSize(this.size);
- this.makeProgressPanel();
- // Thread off a retrieval of the server's capabilities document and
- // update of this panel.
- this.loadingThread = new Thread(new Runnable()
- {
- public void run()
- {
- load();
- }
- });
- this.loadingThread.setPriority(Thread.MIN_PRIORITY);
- this.loadingThread.start();
- }
- /**
- *
- * @方法名称: load ;
- * @方法描述: 加载服务 ;
- * @参数 :
- * @返回类型: void ;
- * @创建人:bluce;
- * @创建时间:2015年2月5日 上午9:33:23;
- * @throws
- */
- protected void load()
- {
- WMSCapabilities caps = null;
- try
- {
- for (int i = 0; i < servers.length; i++)
- {
- this.serverURI = new URI(servers[i].trim());
- caps = WMSCapabilities.retrieve(this.serverURI);
- caps.parse();
- // 获取该服务下的所有图层元数据描述
- namedLayerCaps = caps.getNamedLayers();
- if (namedLayerCaps == null) return;
- try
- {
- for (WMSLayerCapabilities lc : namedLayerCaps)
- {
- Set<WMSLayerStyle> styles = lc.getStyles();
- if (styles == null || styles.size() == 0)
- {
- LayerInfo layerInfo = createLayerInfo(caps, lc,
- null);
- SmartScopeWMSLayersPanel.this.layerInfos
- .add(layerInfo);
- }
- else
- {
- for (WMSLayerStyle style : styles)
- {
- LayerInfo layerInfo = createLayerInfo(caps, lc,
- style);
- SmartScopeWMSLayersPanel.this.layerInfos
- .add(layerInfo);
- }
- }
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- return;
- }
- // 在面板上显示所有图层的名称
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- SmartScopeWMSLayersPanel.this.removeAll();
- makeLayerInfosPanel(layerInfos);
- }
- });
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- // Gather up all the named layers and make a world wind layer for each.
- }
- public String getServerDisplayString()
- {
- return this.serverURI.getHost();
- }
- protected LayerInfo createLayerInfo(WMSCapabilities caps,
- WMSLayerCapabilities layerCaps, WMSLayerStyle style)
- {
- // Create the layer info specified by the layer's capabilities entry and
- // the selected style.
- LayerInfo linfo = new LayerInfo();
- linfo.caps = caps;
- linfo.params = new AVListImpl();
- linfo.params.setValue(AVKey.LAYER_NAMES, layerCaps.getName());
- if (style != null) linfo.params.setValue(AVKey.STYLE_NAMES,
- style.getName());
- String abs = layerCaps.getLayerAbstract();
- if (!WWUtil.isEmpty(abs)) linfo.params.setValue(AVKey.LAYER_ABSTRACT,
- abs);
- linfo.params.setValue(AVKey.DISPLAY_NAME, makeTitle(caps, linfo));
- return linfo;
- }
- protected void makeLayerInfosPanel(Collection<LayerInfo> layerInfos)
- {
- // JPanel layersPanel = new JPanel(new GridLayout(0, 1, 0, 4));
- // layersPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
- JPanel layersPanel = new JPanel();
- BoxLayout layout = new BoxLayout(layersPanel, BoxLayout.Y_AXIS);
- layersPanel.setLayout(layout);
- layersPanel.setFont(new Font("宋体", Font.PLAIN, 10));
- // Add the server's layers to the panel.
- for (LayerInfo layerInfo : layerInfos)
- {
- addLayerInfoPanel(layersPanel, SmartScopeWMSLayersPanel.this.wwd,
- layerInfo);
- }
- // Put the name panel in a scroll bar.
- JScrollPane scrollPane = new JScrollPane(layersPanel);
- scrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
- scrollPane.setPreferredSize(size);
- // Add the scroll bar and name panel to a titled panel that will resize
- // with the main window.
- // JPanel westPanel = new JPanel(new GridLayout(0, 1, 0, 10));
- // westPanel.setBorder(new
- // CompoundBorder(BorderFactory.createEmptyBorder(
- // 9, 9, 9, 9), new TitledBorder("Layers")));
- // westPanel.add(scrollPane);
- this.add(scrollPane, BorderLayout.CENTER);
- this.revalidate();
- }
- protected void addLayerInfoPanel(JPanel layersPanel, WorldWindow wwd,
- LayerInfo linfo)
- {
- // Give a layer a button and label and add it to the layer names panel.
- LayerInfoAction action = new LayerInfoAction(linfo, wwd);
- if (linfo.getAbstract() != null)
- {
- action.putValue(Action.SHORT_DESCRIPTION, linfo.getAbstract());
- JCheckBox jcb = new JCheckBox(action);
- jcb.setFont(new Font("宋体", Font.PLAIN, 14));
- jcb.setSelected(false);
- layersPanel.add(jcb);
- }
- }
- protected class LayerInfoAction extends AbstractAction
- {
- protected WorldWindow wwd;
- protected LayerInfo layerInfo;
- protected Object component;
- public LayerInfoAction(LayerInfo linfo, WorldWindow wwd)
- {
- super(linfo.getTitle());
- // Capture info we'll need later to control the layer.
- this.wwd = wwd;
- this.layerInfo = linfo;
- }
- public void actionPerformed(ActionEvent actionEvent)
- {
- // If the layer is selected, add it to the world window's current
- // model, else remove it from the model.
- if (((JCheckBox) actionEvent.getSource()).isSelected())
- {
- if (this.component == null) this.component = createComponent(
- layerInfo.caps, layerInfo.params);
- updateComponent(this.component, true);
- }
- else
- {
- if (this.component != null) updateComponent(this.component,
- false);
- }
- // Tell the world window to update.
- wwd.redraw();
- }
- }
- protected void updateComponent(Object component, boolean enable)
- {
- if (component instanceof Layer)
- {
- Layer layer = (Layer) component;
- LayerList layers = this.wwd.getModel().getLayers();
- layer.setEnabled(enable);
- if (enable)
- {
- if (!layers.contains(layer))
- {
- ApplicationTemplate.insertBeforePlacenames(this.wwd, layer);
- ApplicationTemplate.insertBeforeLayerName(wwd, layer,
- "Landsat");
- this.firePropertyChange("LayersPanelUpdated", null, layer);
- }
- }
- else
- {
- layers.remove(layer);
- this.firePropertyChange("LayersPanelUpdated", layer, null);
- }
- }
- else if (component instanceof ElevationModel)
- {
- ElevationModel model = (ElevationModel) component;
- CompoundElevationModel compoundModel = (CompoundElevationModel) this.wwd
- .getModel().getGlobe().getElevationModel();
- if (enable)
- {
- if (!compoundModel.getElevationModels().contains(model)) compoundModel
- .addElevationModel(model);
- }
- }
- }
- protected static Object createComponent(WMSCapabilities caps, AVList params)
- {
- AVList configParams = params.copy(); // Copy to insulate changes from
- // the caller.
- // Some wms servers are slow, so increase the timeouts and limits used
- // by world wind's retrievers.
- configParams.setValue(AVKey.URL_CONNECT_TIMEOUT, 30000);
- configParams.setValue(AVKey.URL_READ_TIMEOUT, 30000);
- configParams.setValue(AVKey.RETRIEVAL_QUEUE_STALE_REQUEST_LIMIT, 60000);
- try
- {
- String factoryKey = getFactoryKeyForCapabilities(caps);
- Factory factory = (Factory) WorldWind
- .createConfigurationComponent(factoryKey);
- return factory.createFromConfigSource(caps, configParams);
- }
- catch (Exception e)
- {
- // Ignore the exception, and just return null.
- }
- return null;
- }
- protected static String getFactoryKeyForCapabilities(WMSCapabilities caps)
- {
- boolean hasApplicationBilFormat = false;
- Set<String> formats = caps.getImageFormats();
- for (String s : formats)
- {
- if (s.contains("application/bil"))
- {
- hasApplicationBilFormat = true;
- break;
- }
- }
- return hasApplicationBilFormat ? AVKey.ELEVATION_MODEL_FACTORY
- : AVKey.LAYER_FACTORY;
- }
- protected static String makeTitle(WMSCapabilities caps, LayerInfo layerInfo)
- {
- String layerNames = layerInfo.params.getStringValue(AVKey.LAYER_NAMES);
- String styleNames = layerInfo.params.getStringValue(AVKey.STYLE_NAMES);
- String[] lNames = layerNames.split(",");
- String[] sNames = styleNames != null ? styleNames.split(",") : null;
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < lNames.length; i++)
- {
- if (sb.length() > 0) sb.append(", ");
- String layerName = lNames[i];
- WMSLayerCapabilities lc = caps.getLayerByName(layerName);
- String layerTitle = lc.getTitle();
- sb.append(layerTitle != null ? layerTitle : layerName);
- if (sNames == null || sNames.length <= i) continue;
- String styleName = sNames[i];
- WMSLayerStyle style = lc.getStyleByName(styleName);
- if (style == null) continue;
- sb.append(" : ");
- String styleTitle = style.getTitle();
- sb.append(styleTitle != null ? styleTitle : styleName);
- }
- return sb.toString();
- }
- protected void makeProgressPanel()
- {
- // Create the panel holding the progress bar during loading.
- JPanel outerPanel = new JPanel(new BorderLayout());
- outerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
- outerPanel.setPreferredSize(this.size);
- JPanel innerPanel = new JPanel(new BorderLayout());
- innerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
- JProgressBar progressBar = new JProgressBar();
- progressBar.setIndeterminate(true);
- innerPanel.add(progressBar, BorderLayout.CENTER);
- JButton cancelButton = new JButton("Cancel");
- innerPanel.add(cancelButton, BorderLayout.EAST);
- cancelButton.addActionListener(new AbstractAction()
- {
- public void actionPerformed(ActionEvent actionEvent)
- {
- if (loadingThread.isAlive()) loadingThread.interrupt();
- Container c = SmartScopeWMSLayersPanel.this.getParent();
- c.remove(SmartScopeWMSLayersPanel.this);
- }
- });
- outerPanel.add(innerPanel, BorderLayout.NORTH);
- this.add(outerPanel, BorderLayout.CENTER);
- this.revalidate();
- }
- }
World Wind Java开发之十四——添加WMS地图服务资源(转)的更多相关文章
- World Wind Java开发之十五——加载三维模型(转)
之前的一篇博客是关于加载粗三维模型的,见http://blog.csdn.net/giser_whu/article/details/43452703,这个地方还存在着不能加载纹理的问题,一直没呢解决 ...
- Java开发学习(十四)----Spring整合Mybatis及Junit
一.Spring整合Mybatis思路分析 1.1 环境准备 步骤1:准备数据库表 Mybatis是来操作数据库表,所以先创建一个数据库及表 create database spring_db cha ...
- World Wind Java开发之十五——载入三维模型
之前的一篇博客是关于载入粗三维模型的,见http://blog.csdn.net/giser_whu/article/details/43452703,这个地方还存在着不能载入纹理的问题,一直没呢解决 ...
- World Wind Java开发之十二——加载粗制三维模型(ExtrudedPolygon)(转)
ww可以根据DLG图批量生成假三维模型,这对于小区等特征相似的建筑物模型的构建是非常有用的.下面来看如何一步步实现假三维模型的加载: 1.Shp文件的制作 首先在arcmap下数字化几个建筑物,并新建 ...
- World Wind Java开发之十——AnalyticSurface栅格渲染(转)
http://blog.csdn.net/giser_whu/article/details/43017881 1.AnalyticSurfaceDemo ArcGIS下对栅格的各种分级渲染效果是非常 ...
- “全栈2019”Java多线程第二十四章:等待唤醒机制详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第十四章:线程与堆栈详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java异常第十四章:将异常输出到文本文件中
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- “全栈2019”Java第八十四章:接口中嵌套接口详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
随机推荐
- 最大子序列和——HDU-1003 Max Sum
题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义dp[i]表示以a[i]为结尾的子序列的和的最大值,因而最大 ...
- 洛谷P1034 矩形覆盖
P1034 矩形覆盖 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4( ...
- 洛谷P4287 [SHOI2011]双倍回文(回文自动机)
传送门 听说有大佬用manacher$O(n)$过此题……太强啦…… 说一下PAM的做法吧.(看了题解之后发现)蛮简单的 我们肯定要先建出回文自动机的 然后如果是枚举每一个节点暴跳fail指针肯定得T ...
- SAP 权限层次
此文可是没有维护过权限的人对权限有一个整体的认知,转来加以记忆. 一是系统权限 主要是对模块为单位的权限划分,具体就是用户对该模块可见不可见,能不能对该模块进行再授权的操作.表现在用户界面就是用户登录 ...
- 小程序组件交互 -- 传入js
1.父组件(wxml页面)向子组件传递 在子组件中定义需要传入的属性 properties: { count: { type: Number, value: 0, observer:function( ...
- sysbench压力测试总结
sysbench压力测试工具简介sysbench是一个开源的.模块化的.跨平台的多线程性能测试工具,可以用来进行CPU.内存.磁盘I/O.线程.数据库的性能测试.目前支持的数据库有MySQL.Orac ...
- vs2017通过模块文件添加自定义注释
有时我们在VS里建立类和接口时,需要添加比较规范的注释信息,而每次都要复制粘贴比较麻烦,所以需要我们的IDE做一些支持,比较修改VS里自定义的注释模式,以添加自己公司需要的信息格式. 注释比较规范,是 ...
- Shell笔试题1
1.用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录下. #!/bin/bash#1.sh#判断一文件是不是字符或块设备文件,如果是将其拷贝到 /dev 目录下#f ...
- FFmpeg编译i386 OSX 脚本
话不多说, 直接上脚本 #!/bin/sh # directories PLATFORM="OSX" # FFmpeg脚本目录 SOURCE="ffmpeg-2.8.7& ...
- SpringMVC07SelfException 自定义异常
1.配置web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...