Geoserver REST API 網址:https://docs.geoserver.org/stable/en/user/rest/index.html#rest
一, 發布shp
1.1 新增數據存儲
該API的參數說明(建議選擇external,直接引用本地文件,避免打包上傳):
method | 說明 |
---|---|
url | 服務鏈接的數據形式,使用網絡鏈接 |
file | 壓縮包形式上傳,使用formdata上傳數據 |
external | 本地已存在的文件,傳入路徑 |
代碼調用方式:
/**
* @method 新建數據存儲
* @param {string} workspace_name 工作區名稱
* @param {stirng} store_name 數據源名稱
* @param {string} file_path 文件路徑
* @returns
*/
async function create_datastore(workspace_name, store_name, file_path) {
try{
// const request = require('request-promise');
let result = await request({
method: 'PUT',
uri: config.geoserver_url + `/rest/workspaces/${workspace_name}/datastores/${store_name}/external.shp`,
headers: {
'Content-Type': 'text/plain',
'Authorization': config.geoserver_auth
},
body: file_path,
// json: true //不能加這個,否則會報500
});
return result;
}catch(err) {
if(err) throw err;
}
}
1.2 數據源設置
注意:connectionParameters里的參數,會把之前已存在的配置,如url等完全覆蓋,如需增加配置,注意先獲取entry,然后進行push,最后調用更新接口。
獲取entry屬性的接口如下:
/rest/workspaces/${workspace_name}/datastores/${store_name}
取響應數據中的['dataStore']['connectionParameters']['entry']
即為所需
部分參數所對應項:
entry.push({"@key":"charset","$":"UTF-8"}); //字符集設置
entry.push({"@key":"create spatial index","$":"true"}); //如果缺少空間索引或者空間索引過時,重新建立空間索引
entry.push({"@key":"memory mapped buffer","$":"true"}); //使用內存映射的緩沖區
entry.push({"@key":"cache and reuse memory maps","$":"true"}); //高速緩存和重用內存映射
1.3 發布圖層
新版geoserver在創建數據源后,會自動進行圖層的發布,但在layer preview里預覽,會直接下載wms文件進行報錯,此時可以通過更改featureType來進行發布更新
featureTypeName一般是文件名,body設置坐標系即可(注意是featureType,不是FeatureTypeInfo)
二, 發布Geotiff
2.1 直接發布
發布georiff時,創建coveragestores即可,新版geoserver會自動進行發布
代碼調用方式:
/**
* @method 新建數據存儲
* @param {string} workspace_name 工作區名稱
* @param {stirng} store_name 數據源名稱
* @param {string} file_path 文件路徑
* @returns null
*/
async function create_datastore(workspace_name, store_name, file_path) {
try{
// 獲取工作區
let workspace = await get_workspace(workspace_name);
if(!workspace) {
await request({
method: 'POST',
uri: config.geoserver_url + `/rest/workspaces`,
headers: {
'Content-Type': 'application/json',
'Authorization': config.geoserver_auth
},
body: {
workspace: {
name: workspace_name
}
},
json: true
});
}
await request({
method: 'PUT',
uri: config.geoserver_url + `/rest/workspaces/${workspace_name}/coveragestores/${store_name}/external.geotiff?charset=UTF-8`,
headers: {
'Content-Type': 'text/plain',
'Authorization': config.geoserver_auth
},
body: file_path, //路徑不能存在中文,否則會報500,原因還未知
// json: true //不能加這個,否則會報500
});
return;
}catch(err) {
// console.log('創建資源錯誤')
if(err) throw err;
}
}