Buffer query |
The data resource and its child resources provide data query and data operation functions. This example illustrates how to query features. The cross-datasource query is supported.
By looking up REST API, we know we can implement the POST request on the featureResults resource to query features. Please refer to featureResults.
This example shows how to perform buffer query on the Countries dataset in the World datasource (World:Countries). The reference geometry is a point at (102.00, 22.50) and the buffer distance is 1 degree.
Construct the buffer query parameters, i.e., the request body of the POST request in JSON format, as shown below:
//The name of the dataset to be queried
var datasetNames=["World:Countries"];
//The query mode. Here it is Buffer.
var getFeatureMode="BUFFER";
//The reference geometry for buffer query
var BufferPoint={
"id": 5000,
"parts": [1],
"points": [{
"x": 102.00,
"y": 22.50
}],
"style": null,
"type": "POINT"
}
//The buffer distance
var bufferDistance=1;
//Construct the request body needed for the buffer query
var entity={
"datasetNames":datasetNames,
"getFeatureMode":getFeatureMode,
"geometry":BufferPoint,
"bufferDistance":bufferDistance
}
Implement the POST request on the URI (i.e., the featureResults resource), as shown below:
var uri="http://localhost:8090/iserver/services/components-rest/rest/data/featureResults.json";
The newResourceLocation in the response is the query result, i.e., the featureResult resource. Parse the featureUriList field of the query result to get the representation of each element (please refer to featureResult). Please refer to feature to know the representation structure of the feature resource.
Sample code:
//Perform buffer query on the World:World dataset
function QueryFeature()
{
var commit=getcommit();
var uri="http://localhost:8090/iserver/services/components-rest/rest/data/featureResults.json";
//The name of the dataset to be queried
var datasetNames=["World:Countries"];
//The query mode. Here it is Buffer.
var getFeatureMode="BUFFER";
//The reference geometry for buffer query
var BufferPoint={
"id": 5000,
"parts": [1],
"points": [{
"x": 102.00,
"y": 22.50
}],
"style": null,
"type": "POINT"
}
//The buffer distance
var bufferDistance=100000;
//Construct the request body needed for the buffer query
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));
//Parse the json string returned from the server to a JavaScript object
var response = json_parse(commit.responseText, null);
//Get the Div container for display
var container = document.getElementById('container');
//Output the result
container.innerHTML="";
var str="";
if (response.succeed==true)
{
//Get the query result
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>Query succeeded.</p>';
str+='<p>There are '+features.featureCount+' features in the query result, and the URI of the query result is: </p><p>'+response.newResourceLocation+'</p>';
str+="The reference dataset is World:Countries and the fields of the dataset include SMID and COUNTRY. The values of the SMID and COUNTRY fields of the query result are as follows:"
str+="<ol>"
//Get the content of a single result feature
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>Query failed. </p>';
}
container.innerHTML+=str;
}
The result is as below.
The process for performing queries other than buffer query is very similar. The only difference is the query parameters, i.e., the content of the request body. For more details, please refer to the introduction to POST request parameters in featureResults.