Adding features |
The data resource and its child resources provide the capabilities of data query and data operation. This example illustrates how to add a feature to a dataset.
Look up REST API and we know that by implementing the POST request on the features resource, we can add features to a dataset. To add features to the Capitals dataset, we need to implement the POST request on http://localhost:8090/iserver/services/components-rest/rest/data/datasources/name/World/datasets/name/Capitals/features.json (taking rjson format as an example). And the feature information collection needs to be included in the request body. The structure of a single element of the feature information collection is as follows:
Name | Type | Description |
ID | int | The ID of the feature. |
fieldNames | String[] | The field name collection of the feature. |
fieldValues | String[] | The field value collection of the feature. |
geometry | Geometry | The geometry corresponding to the feature. |
Through the representation of the Capitals dataset resource, we know that the dataset type of Capitals is POINT, therefore, the feature added to the dataset should be a geometry of the POINT type.
Through the child resource fields of the Capital resource, we know that the fields of the Capitals dataset include: SMID, SMX, SMY, SMLIBTILEID, SMUSERID, CAPITAL, COUNTRY, and CAP_POP.
Suppose the value of the SMUSERID field of the feature to be added is 5000 and construct the feature information as shown below. Please note that values assigned for ID, SMID, and geometry.id have no sense because the server will automatically give them identical values. Besides, the value transferred for SMLIBTILEID will not work because its value is fixed.
{
"ID": 5000,
"fieldNames": [
"SMID",
"SMX",
"SMY",
"SMLIBTILEID",
"SMUSERID",
"CAPITAL",
"COUNTRY",
"CAP_POP"
],
"fieldValues": [
"5000",
"100.56",
"100.55",
"5001",
"5014",
"Sample capital",
"Sample country",
"582000.0"
],
"geometry": {
"id": 5000,
"parts": null,
"points": [{
"x": 100.56,
"y": 100.55
}],
"style": null,
"type": "POINT"
}
}
Implement the POST request on http://localhost:8090/iserver/services/components-rest/rest/data/datasources/name/World/datasets/name/Capitals/features.json?returnContent=true and include the feature information above in the request body as the feature information collection of an element to create the feature as shown below. In the URI, returnContent=true indicates the ID of the feature to be created will be directly returned.
//Add feature to the Capitals dataset
function AddFeature()
{
var commit=getcommit();
var uri="http://localhost:8090/iserver/services/components-rest/rest/data/datasources/name/World/datasets/name/Capitals/features.rjson";
//The feature information to be added
var NewFeature={
"ID": 5000,
"fieldNames": [
"SMID",
"SMX",
"SMY",
"SMLIBTILEID",
"SMUSERID",
"CAPITAL",
"COUNTRY",
"CAP_POP"
],
"fieldValues": [
"5000",
"100.56",
"100.55",
"5001",
"5014",
"Sample capital",
"Sample country",
"582000.0"
],
"geometry": {
"id": 5000,
"parts": null,
"points": [{
"x": 100.56,
"y": 100.55
}],
"style": null,
"type": "POINT"
}
}
//The feature information collection (with only one element) to be added
var entity=[NewFeature];
commit.open("POST",encodeURI(uri)+"?returnContent=true",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="";
if (response.length>0)
{
container.innerHTML+='<p>Create feature successful. </p>';
container.innerHTML+='<p>ID of the feature to be added is: '+response[0]+'</p>';
}else{
container.innerHTML+='<p>Create feature failed. </p>';
}
}
The result is as below.
Access the URI http://localhost:8090/iserver/services/components-rest/rest/data/datasources/name/World/datasets/name/Capitals/features/169.rjson to get the rjson representation of the newly created resource.