使用REST API发布托管要素服务

0
分享 2019-01-04
最近在解决用户问题的时候把调用rest api来发布服务的过程走了一遍,因为在网上没有找到相关完整示例,这里就分享一下示例代码。相关接口的参数参考 rest API文档。该示例使用的是3.27的API,往ArcGIS Online上发布托管要素服务,但同样适用于向Portal上发布服务。主要分三个步骤:
1. 调用createService接口创建空的托管要素服务,成功后获得一个不含有任何图层的托管要素服务
2. 调用update接口修改item的title,tag信息
3. 调用addToDefintion来完成图层的添加
详细代码如下:
require([
"esri/config",
"esri/request",
"dojo/domReady!"
], function(esriConfig, esriRequest) {

// 空服务的相关参数
var object1 = {
"maxRecordCount": 2000,
"supportedQueryFormats": "JSON",
"capabilities": "Query",
"description": "",
"allowGeometryUpdates": true,
"hasStaticData": true,
"units": "esriMeters",
"syncEnabled": false,
"editorTrackingInfo": {
"enableEditorTracking": false,
"enableOwnershipAccessControl": false,
"allowOthersToQuery": true,
"allowOthersToUpdate": true,
"allowOthersToDelete": false,
"allowAnonymousToUpdate": true,
"allowAnonymousToDelete": true
},
"xssPreventionInfo": {
"xssPreventionEnabled": true,
"xssPreventionRule": "InputOnly",
"xssInputRule": "rejectInvalid"
},
"initialExtent": {
"xmin": 73.56076587885269,
"ymin": 15.780177333592768,
"xmax": 134.7740624133472,
"ymax": 53.55971148837324,
"spatialReference": {
"wkid": 4326
}
},
"spatialReference": {
"wkid": 4326
},
"tables": [],
"name": "testcc"
}
var createParameters = JSON.stringify(object1);

// replace with your token or you can request the token
var token = "token" //换成您的临时token,或者您获取的token
// instantiate a request for creating an empty service
var createServiceRequest = new esriRequest({
url: "https://www.arcgis.com/sharing ... ot%3B, //把users换成您的user,以下同样
content: {
createParameters: createParameters,
outputType: "featureService",
f: "json",
token: token,
tags: "test"
}
}, {
usePost: true
});

createServiceRequest.then(function(response) {
var url = response.serviceurl;
var itemId = response.itemId;

// 使用update接口来更新item信息,包括添加title,tags
var updateUrl = "https://www.arcgis.com/sharing ... ot%3B + itemId + "/update";

// instantiate a request for updating the item info
var updateItemInfo = new esriRequest({
url: updateUrl,
content: {
title: "testcc",
snippet: "test description",
tags: "test",
extent: "-180,-58.976,-80.156,73.136",
f: "json",
token: token
}
}, {
usePost: true
});

updateItemInfo.then(function(response) {
console.log(response);
});
// update url to add 'admin' between '/rest/' and '/services/'
// 要使用admin的url来进行添加图层
var l = url.split("/rest");
var addToDefinitionUrl = l[0] + "/rest/admin" + l[1] + "/addToDefinition";

// 添加图层的相应参数
var object2 = {
"layers": [{
"adminLayerInfo": {
"tableName": "testcc.testcc",
"geometryField": {
"name": "Shape",
"srid": 4326
}
},
"id": 0,
"name": "testcc",
"type": "Feature Layer",
"displayField": "",
"description": "",
"copyrightText": "",
"defaultVisibility": true,
"relationships": [],
"isDataVersioned": false,
"supportsRollbackOnFailureParameter": true,
"supportsAdvancedQueries": true,
"geometryType": "esriGeometryPoint",
"minScale": 0,
"maxScale": 0,
"extent": {
"xmin": 73.56076587885269,
"ymin": 15.780177333592768,
"xmax": 134.7740624133472,
"ymax": 53.55971148837324,
"spatialReference": {
"wkid": 4326
}
},
"drawingInfo": {
"transparency": 0,
"labelingInfo": null,
"renderer": {
"type": "simple",
"symbol": {
"color": [20, 158, 206, 130],
"size": 18,
"angle": 0,
"xoffset": 0,
"yoffset": 0,
"type": "esriSMS",
"style": "esriSMSCircle",
"outline": {
"color": [255, 255, 255, 220],
"width": 2.25,
"type": "esriSLS",
"style": "esriSLSSolid"
}
}
}
},
"allowGeometryUpdates": true,
"hasAttachments": true,
"htmlPopupType": "esriServerHTMLPopupTypeNone",
"hasM": false,
"hasZ": false,
"objectIdField": "OBJECTID",
"globalIdField": "",
"typeIdField": "",
"fields": [{
"name": "OBJECTID",
"type": "esriFieldTypeOID",
"alias": "OBJECTID",
"sqlType": "sqlTypeOther",
"nullable": false,
"editable": false,
"domain": null,
"defaultValue": null
}, {
"name": "name",
"type": "esriFieldTypeString",
"alias": "name",
"sqlType": "sqlTypeNVarchar",
"nullable": true,
"editable": true,
"domain": null,
"defaultValue": null,
"length": 256
}],
"indexes": [],
"types": [],
"templates": [{
"name": "New Feature",
"description": "",
"drawingTool": "esriFeatureEditToolPoint",
"prototype": {
"attributes": {
"name": null
}
}
}],
"supportedQueryFormats": "JSON",
"hasStaticData": true,
"maxRecordCount": 10000,
"capabilities": "Query"
}]
}
var addToDefinition = JSON.stringify(object2);
// instantiate a request for adding layers to the created service
var addToDefinitionRequest = new esriRequest({
url: addToDefinitionUrl,
content: {
addToDefinition: addToDefinition,
f: "json",
token: token
}
});

addToDefinitionRequest.then(function(response) {
console.log(response);
});
});
});

0 个评论

要回复文章请先登录注册