数据要素的缓冲区查询

发送反馈


data 及其子资源提供了对数据的查询和操作的功能,本文的例子将介绍查询要素的过程,可以进行跨数据源的要素查询。

查 REST API 可知,查询要素可通过对 featureResults 资源执行 POST 请求来进行,请参见 REST API 中 featureResults 资源页面

本示例中,对数据集“World:Countries”,即 World 数据源(前一个)的 Countries 数据集(后一个)执行缓冲区查询操作,目标对象为点(102.00, 22.50),缓冲区距离为 1 度。

构建缓冲区查询的参数,即 POST 请求的请求体内容(JSON 格式)如下:

//查询的目标数据集名称

var datasetNames=["World:Countries"];

//查询模式,这里为缓冲区查询

var getFeatureMode="BUFFER";

//缓冲区查询所需的几何对象

var BufferPoint={

        "id": 5000,

        "parts": [1],

        "points": [{

                "x": 102.00,

                "y": 22.50

        }],

        "style": null,

        "type": "POINT"

        }

//缓冲区距离

var bufferDistance=1;

//构建缓冲区查询所需的请求体

var entity={

        "datasetNames":datasetNames,

        "getFeatureMode":getFeatureMode,

        "geometry":BufferPoint,

        "bufferDistance":bufferDistance

}

对如下 URI(即 featureResults 资源)执行 POST 请求:

var uri="http://localhost:8090/iserver/services/components-rest/rest/data/featureResults.json";

得到的响应中的 newResourceLocation 即为查询结果(featureResult 资源),对查询结果的 featureUriList 字段进行解析(参见 featureResult 资源页面),即得到具体每个要素的表述,对于具体要素资源的表述结构请参见 feature 资源页面

缓冲区查询的示例代码如下:

//往对 World:World 数据集进行缓冲区查询

function QueryFeature()

{

        var commit=getcommit();

        var uri="http://localhost:8090/iserver/services/components-rest/rest/data/featureResults.json";

        //查询的目标数据集名称

        var datasetNames=["World:Countries"];

        //查询模式,这里为缓冲区查询

        var getFeatureMode="BUFFER";

        //缓冲区查询所需的几何对象

        var BufferPoint={

                "id": 5000,

                "parts": [1],

                "points": [{

                        "x": 102.00,

                        "y": 22.50

                }],

                "style": null,

                "type": "POINT"

                }

        //缓冲区距离

        var bufferDistance=100000;

        //构建缓冲区查询所需的请求体

        var entity={

                "datasetNames":datasetNames,

                "getFeatureMode":getFeatureMode,

                "geometry":BufferPoint,

                "bufferDistance":bufferDistance

        }

        commit.open("POST",encodeURI(uri),false,"","");

        commit.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        commit.send(toJSON(entity));

        //解析从服务器端返回的 json 字符串,解析为一个 JavaScript 对象。

        var response = json_parse(commit.responseText, null);       

        //获取用作显示的 Div 容器。

        var container = document.getElementById('container');

        //输出结果

        container.innerHTML="";

        var str="";

        if (response.succeed==true)

        {

                //获取查询结果集内容。

                commit.open("GET",encodeURI(response.newResourceLocation),false,"","");

                commit.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

                commit.send(null);

                var features=json_parse(commit.responseText, null);

                

                str+='<p>查询成功。</p>';

                str+='<p>查询结果中共有'+features.featureCount+'个要素,查询结果的 URI 为:</p><p>'+response.newResourceLocation+'</p>';

                str+="示例查询的目标数据集为 World:Countries,该数据集有 SMID、COUNTRY 字段,列出结果要素的 SMID、COUNTRY 字段的值如下所示:"

                str+="<ol>"

                //获取单个结果要素的内容。

                for (var i=0;i<features.featureCount;i++ )

                {

                        commit.open("GET",encodeURI(features.featureUriList[i]+".json?hasGeometry=false"),false,"","");

                        commit.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

                        commit.send(null);

                        var feature = json_parse(commit.responseText, null);

                        str+="<li>";

                        for(var j=0;j<feature.fieldNames.length;j++){

                                if(feature.fieldNames[j]=="SMID"){

                                        str+="SMID:"+feature.fieldValues[j]+",";

                                        continue;

                                }else if(feature.fieldNames[j]=="COUNTRY"){

                                        str+="COUNTRY:"+feature.fieldValues[j];

                                        continue;

                                }

                        }

                        str+="</li>";

                }

                str+="</ol>"

        }else{

           str+='<p>查询失败。</p>';

        }

        container.innerHTML+=str;

}

结果如下:

对数据集进行其他类型查询的过程,跟缓冲区查询类似,只是所使用的查询参数,即 POST 请求的请求体内容不同,具体请参见  featureResults 资源页面 中关于 POST 请求参数的描述。