获取地物要素信息

发送反馈


WMS 接口的 getFeatureInfo(String version, WMSQueryParameter wmsQueryParam) 方法可通过传入 WMS 版本号和 WMS 查询参数获取到地物信息。SuperMap iServer 定义了 WMSQueryParameter 对象类型,用于存储 WMS 查询参数。

在 WMSClientSample 类中添加 getFeatureInfo 方法,并在 Main 函数中添加相应的代码输出结果:

    // 获取地图要素信息

    public WMSFeatureInfo[] getFeatureInfo() {

        WMSFeatureInfo[] wmsFeatureInfos = null;

        // 构建 WMS 查询参数对象

        WMSQueryParameter wmsQueryParameter = new WMSQueryParameter();

        // WMS 服务的版本号

        String wmsVersion = "1.1.1";

        wmsQueryParameter.mapName = "China";

        wmsQueryParameter.entireBounds = new Rectangle2D(-180, -90, 180, 90);

        wmsQueryParameter.bounds = new Rectangle2D(-180, -90, 180, 90);

        wmsQueryParameter.layers = new String[1];

        wmsQueryParameter.layers[0] = "World_Division_pl@China";

        wmsQueryParameter.srs = "EPSG:4326";

        wmsQueryParameter.width = 400;  

        wmsQueryParameter.height = 400;

        wmsQueryParameter.queryLayers = new String[1];

        wmsQueryParameter.queryLayers[0] = "World_Division_pl@China";

        wmsQueryParameter.x = 150;

        wmsQueryParameter.y = 150;

        try {

            wmsFeatureInfos = wmsClient.getFeatureInfo(wmsVersion, wmsQueryParameter);

        } catch (Exception e) {            

            e.printStackTrace();

        }

        return wmsFeatureInfos;

    }

      

    public static void main(String[] args) {

        WMSClientSample wmsClientSample = new WMSClientSample();

        

        // 输出服务标题和地图标题

        WMSCapabilities capabilities = wmsClientSample.getCapabilities();

        String wmsTitle = capabilities.serviceDescription.title;

        String mapTitle = capabilities.mapTitle;

        System.out.println("WMS Title is " + wmsTitle);

        System.out.println("MapTitle is " + mapTitle);

        

        // 输出地图

        byte[] bytes = wmsClientSample.getMap();

        String fileName = "d:/map.png";

        FileOutputStream outputStream = null;

        try {

            outputStream = new FileOutputStream(fileName);

        } catch (FileNotFoundException e1) {

            e1.printStackTrace();

        }

        try {

            outputStream.write(bytes);

            outputStream.flush();

            outputStream.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

        System.out.println("请在 d:/ map.png 文件中查看地图");

        

        // 输出地物要素信息

        WMSFeatureInfo[] wmsFeatureInfo = wmsClientSample.getFeatureInfo();

        int length = wmsFeatureInfo.length;

        System.out.println("地物要素信息:");

        for (int i=0; i<length; i++) {

            String[] fieldNames = wmsFeatureInfo[i].fieldNames;

            String[] fieldValues = wmsFeatureInfo[i].fieldValues;

            int nameLength = fieldNames.length;

            for (int j=0; j<nameLength; j++) {

                String name = fieldNames[j];

                String value = fieldValues[j];

                System.out.println(name + " = " + value);

            }

        }

    }

在运行 WMSClientSample 之前,需要保证 http://localhost:8090/iserver/services/map-china400/wms111/China 服务已启动。

编译 WMSClientSample 工程之后,右键 WMSClientSample 工程,选择 Run as > Java Application,在控制台会输出地物要素信息如下:

地图要素信息:

SMAREA = 1.6060069623493825E15

SMID = 1

SMPERIMETER = 1.6030006674231339E8

SMUSERID = 0