Distance query |
Distance query is to find the geometry objects whose distance to a specified geometry is within a specified value. The parameters queryMode and DistanceQuery need to be specified.
To illustrate how to perform distance query, we first construct a simple geometry (Geometry) as shown below:
//Distance query
function queryByDistance()
{
var geometry={};
//Set the ID of the geometry
geometry.id=1001;
geometry.parts=[3];
geometry.points=[{x:12800000,y:4800000},{x:13000000,y:4800000},{x:12800000,y:5000000},{x:13000000,y:5000000}];
//geometry.style={};
geometry.type="REGION";
}
Query all geometry objects in the China_Province_pl@China layer of China whose distance to the specified geometry is within 10,000. Note: The distance unit, as well as the unit for geometry.points are identical to the unit of the coordinate system of the China features, which is "meter".
The sample code for distance query is as follows:
//Distance query
function queryByDistance()
{
……
var commit=getcommit();
var uri="http://localhost:8090/iserver/services/map-china400/rest/maps/China/queryResults.json";
//Set the request body parameters
var entry={};
//Set the query mode to DistanceQuery
entry.queryMode="DistanceQuery";
//Attribute query parameters
entry.queryParameters={"networkType":null,"startRecord":0,"queryParams":[{"orderBy":null,"ids":null,"name":"China_Province_pl@China","attributeFilter":null,"groupBy":null,"linkItems":null,"joinItems":null,"fields":["SMID","Name"]}],"customParams":null,"expectCount":100000,"queryOption":"ATTRIBUTEANDGEOMETRY"}
//The spatial geometry object
entry.geometry=geometry;
//Set the distance to 10,000
entry.distance=10000;
commit.open("POST",encodeURI(uri),false,"","");
commit.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
commit.send(toJSON(entry));
//Parse the json string returned from the server as a JavaScript object
var response = json_parse(commit.responseText, null);
//Get the Div container for display
var container = document.getElementById('container');
container.innerHTML="";
//Determine whether the query is successful
if(!response.succeed)
{
//Query failed
container.innerHTML+="<p>Query failed</p>";
container.innerHTML+="<p>Error code: "+response.error.code+"</p><p>Cause: "+response.error.errorMsg+"</p>";
}else
{
//Query succeeded
container.innerHTML+="<p>Query succeeded</p>";
container.innerHTML+="<p>URI of queryResult resource: "+response.newResourceLocation+"</p>";
}
}
queryParameters affects the query result. In this example, fields and queryOption are set to specify which fields (smID and NAME) and what information (ATTRIBUTEANDGEOMETRY) should be included in the result.
Perform the query to get the queryResult resource at the URI http://localhost:8090/iserver/services/map-china400/rest/maps/China/queryResults/5q5ocs7k_391bf1e7c7fb471d9e34f6519409463d.json. The response includes the spatial and attribute information, including the smID and Name fields, of the features.
The queryResult needs to be further parsed. Please refer to Parsing queryResult.