Cesium学习笔记(二)使用REST API上传数据

Step1. Provide ion information about your data

使用官网提供的数据文件:Reichstag.zip
To create a new asset, we POST information about our data and what we want to do with it to the /v1/assets endpoint. The POST body looks like this:

const postBody = {
    name: 'Reichstag',
    description: 'See [Wikipedia](https://en.wikipedia.org/?curid=217577).',
    type: '3DTILES',
    options: {
        sourceType: 'CITYGML',
        clampToTerrain: true,
        baseTerrainId: 1
    }
}
  • name是一个非空字符串
  • description是一个Markdown兼容字符串
  • type是三种类型之一:3DTILESIMAGERYTERRAIN,现在正上传CityGML,它只能用于生成3DTILES
  • options是the ion tilling pipeline的选项,并且特定于正在上传的数据类型。在这个例子中指定:
    • sourceType指示我们正在上传CityGML数据
    • clampToTerrain告诉pipeline,我们想要它调整文件中建筑物的高度使它们被放置在地面上
    • baseTerrainID指定一个terrain asset id当作地面。在上面的代码中,“1”代表CesiumWorldTerrain。如果未定义并且clamoToTerrain = true,那么建筑物会被放到WGS84椭球面。

为了实际创建资产,我们使用以下代码发布上述对象:

const response = await request({
    url: 'https://api.cesium.com/v1/assets',
    method: 'POST',
    headers: { Authorization: `Bearer ${accessToken}` },
    json: true,
    body: postBody
});

这个服务返回的response具有三条属性:

  • response.uploadLocation提供临时凭证和位置信息,用于将源数据上传到ion,凭据持续约1小时。
  • response.onComplete包含我们完成数据上传后将用于回发到服务器的信息。
  • response.assetMetadata包含这个asset的所有信息包括它的id、状态,以及我们刚刚发布的名称和描述。
    这个response如下:
{
  "assetMetadata": {
    "id": 21111,
    "type": "3DTILES",
    "name": "Reichstag",
    "description": "See [Wikipedia](https://en.wikipedia.org/?curid=217577).",
    "attribution": "",
    "bytes": 0,
    "dateAdded": "2019-04-19T00:30:54.111Z",
    "status": "AWAITING_FILES",
    "percentComplete": 0
  },
  "uploadLocation": {
    "bucket": "assets.cesium.com",
    "prefix": "sources/21111/",
    "accessKey": "ASIATXRQLSFCKKLAIVXW",
    "secretAccessKey": "Qa6YC2yq78hoCR4KYF4atHEnytfgLJMNV0KPdpw8",
    "sessionToken": "FQoGZXIvYXdzEIr//wEaDIEDvhx6N7x4..."
  },
  "onComplete": {
    "method": "POST",
    "url": "https://api.cesium.com/v1/assets/21111/uploadComplete",
    "fields": {}
  }
}

Step 2.Upload your data to the provided Amazon S3 bucket

我们可以使用任何与S3兼容的库来执行上传,但是我们建议使用多种语言广泛提供的官方AWS开发工具包。我们需要的一切都在response.uploadLocation对象中。
每个asset的S3 API初始化都是相同的:

const AWS = require('aws-sdk');
const uploadLocation = response.uploadLocation;

const s3 = new AWS.S3({
    apiVersion: '2006-03-01',
    region: 'us-east-1',
    signatureVersion: 'v4',
    endpoint: uploadLocation.endpoint,
    credentials: new AWS.Credentials(
        uploadLocation.accessKey,
        uploadLocation.secretAccessKey,
        uploadLocation.sessionToken)
});

至此,我们可以使用s3实例上传所需数量的文件。在此示例中,所有内容都包含在单个zip文件中,因此我们可以使用单个上载调用。如果有多个文件,只需使用不同的“Body”和“Key”属性进行多个s3.upload调用。确保使用Reichstag.zip的位置替换path_to_file。

const input = 'path_to_file/Reichstag.zip';
const fs = require('fs');
await s3.upload({
    Body: fs.createReadStream(input),
    Bucket: uploadLocation.bucket,
    Key: `${uploadLocation.prefix}Reichstag.zip`
}).promise();

Step 3: Notify ion that you are finished uploading

数据上传后,我们将使用response.onComplete对象中的信息发出第二个POST请求。这告诉服务器所有源数据都已上传,并且可以开始tiling过程。

const onComplete = response.onComplete;
await request({
    url: onComplete.url,
    method: onComplete.method,
    headers: { Authorization: `Bearer ${accessToken}` },
    json: true,
    body: onComplete.fields
});

Step 4: Monitor the tiling status

现在asset已经被tiled into 3D Tiles 并且没有步骤要做。可以转到Asset Dashboard查看tile进度,还可以使用REST API来监视进度,以便在完成时采取一些操作。
这可以使用GET / v1 / assets / {assetId}端点来实现,其中{assetId}是初始POST响应中的response.assetMetadata.id

async function waitUntilReady() {
    const assetId = response.assetMetadata.id;

    // Issue a GET request for the metadata
    const assetMetadata = await request({
        url: `https://api.cesium.com/v1/assets/${assetId}`,
        headers: { Authorization: `Bearer ${accessToken}` },
        json: true
    });

    const status = assetMetadata.status;
    if (status === 'COMPLETE') {
        console.log('Asset tiled successfully');
        console.log(`View in ion: https://cesium.com/ion/assets/${assetMetadata.id}`);
    } else if (status === 'DATA_ERROR') {
        console.log('ion detected a problem with the uploaded data.');
    } else if (status === 'ERROR') {
        console.log('An unknown tiling error occurred, please contact support@cesium.com.');
    } else {
        if (status === 'NOT_STARTED') {
            console.log('Tiling pipeline initializing.');
        } else { // IN_PROGRESS
            console.log(`Asset is ${assetMetadata.percentComplete}% complete.`);
        }

        // Not done yet, check again in 10 seconds
        setTimeout(waitUntilReady, 10000);
    }
}

waitUntilReady();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值