コンテンツにスキップ

Mapray CloudのデータをFetchAPIで表示する - Building -

Mapray Cloudに保存されているBuildingを表示します。 おなじく、第1章でアップロードしたデータを表示してみます。 今回は、3D Dataset、PointCloudとは違い、Mapray Cloud APIを直接利用する方法も同時に学習します。

DataのID

Mapray Cloudに保存されているデータは全てIDで管理されています。 Mapray CloudのBuilding Dashboardで第1章 Mapray Cloudの建物データをアップロードしてみようでアップロードした、 kamakuraのIDを調べてください。

以下のプログラムの

<YOUR_BUILDING_ID>

を調べたIDで置き換えてください。

Mapray Cloud API

Mapray CloudにはRestAPIが用意されています。 これまで見てきた、3D DatasetやPoint Cloudはよく利用されるために mapray.cloud.CloudApiV2で、抽象化して、Mapray CloudのRest APIを呼び出す仕組みを用意していました。

しかし、他にも多数のMapray Cloud APIがあります。

Mapray Cloud API Reference

ここでは、直接、Mapray Cloud APIを呼び出す方法を学習しましょう。

Buildingのダウンロードと表示

Buildingはmapray.cloud.CloudApiV2で、mapray.Resourceで抽象化されていないため、Mapray Cloud APIを直接呼び出します。 fetchBuildingDataを参考にしましょう。 標準的なW3Cの仕様に従い、Fetch APIを呼び出しています。

Buiding APIでBuildingを取得する手順は以下です。

  1. データ情報の取得 Retrive B3D Dataset APIを使います。
  2. 取得した情報から、Tile化されたBuildingデータのURLを取得
  3. 取得したURLをBuilding用のDataProviderに渡す。

1.データ情報の取得

X-Api-Key
にMapray CloudのAPI KEYを指定し、Mapray Cloud APIをAPI Referenceの定義に従って呼び出すことでデータをGetできます。 Retrive B3D Dataset API を呼び出しています。

2.URLを取得

取得したデータにJSON型でデータが格納されています。 以下は例です

{
  "id": "4811188005240832",
  "owner_id": "4098494030324145",
  "name": "shinagawa_area",
  "description": "test",
  "attributions": [
    {
      "display": "©Mapray",
      "link": "https://mapray.com",
      "description": "Mapray",
      "logo": "https://mapray.com/images/logo.svg"
    }
  ],
  "srid": 4326,
  "format": "b3d",
  "url": "https://dummy.com/b3d/4811188005240832/",
  "fileinfo": {
    "format": 1,
    "rho": 7,
    "transform": [
      742.4224066826572,
      0,
      0
    ]
  },
  "created_at": "2017-09-04T05:56:10Z",
  "updated_at": "2017-09-08T08:12:13Z",
  "status": "ready",
  "error": "",
  "is_public": false,
  "public_url": ""
}

この中のurlがBuildingタイルデータが格納されているURLになります。

3.DataPrividerによるBuildingデータの取得

mapray.StandardB3dProviderを利用して、Buildingデータの実体をロードします。 ここでもMapray Cloudにアクセスするために、API Keyを設定する必要があります。

最後に、CollectionにPrividerを設定することで、カメラの位置と連動してBuildingデータがダウンロードされます。

Note

大規模データを扱うPointCloudとBilldingはCollectionという概念で扱います

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script src="https://resource.mapray.com/mapray-js/v0.9.6/mapray.min.js"></script>
        <script src="https://resource.mapray.com/ui/v0.9.6/maprayui.min.js"></script>
        <link rel="stylesheet" href="https://resource.mapray.com/styles/v1/mapray.css">
        <style>
            html, body, div#mapray-container { margin: 0; padding: 0; height: 100%; }
        </style>
    </head>

    <body>
        <div id="mapray-container"></div>
    </body>

    <script>
    // Set up your apikey, which can be created in Mapray Cloud.
      const apikey = "<YOUR_MAPRAY_API_KEY>";
     const BaseMapType = {
            PHOTO: 'photo',
            STANDARD: 'standard'
        };

        const TimeType = {
            DAYTIME: 'daytime',
            NIGHTTIME: 'nighttime'
        };

        const CloudData = {
            threeD: "<YOUR_3D_DATASET_ID>", // 3D DatasetのID
            pointCloud: "<YOUR_POINTCLOUD_ID>", // PointCloudのID
            building: "<YOUR_BUILDING_ID>" // BuildingのID
        }

        class CustomViewer extends maprayui.StandardUIViewer {
            constructor(container, api_key, options = {}) {
                super(container, api_key, options);
                this.elapsedTime = 0;  // Pin作成時間計算用
                this.latValue = 0;
                this.totalTime = 0;  // 起動からの累積時間

                this._render_mode = mapray.Viewer.RenderMode.SURFACE; // 描画モードをセット

                this._base_map_url = BaseMapType.PHOTO; // 現在のベースマップ

                this._time_type = TimeType.DAYTIME; // 現在の時間タイプ

                this._maprayApi = new mapray.cloud.CloudApiV2({
                    tokenType: mapray.cloud.CloudApi.TokenType.API_KEY,
                    token: api_key
                });
                // Imageレイヤ=を追加
                this.addLayer( {
                    type: mapray.Layer.Type.IMAGE,
                    image_provider: new mapray.StandardImageProvider({ url: "https://opentiles.mapray.com/xyz/night-satellite/", min_level: 0, max_level: 8 }),
                    opacity: 1.0,
                    draw_type: mapray.ImageLayer.DrawType.NIGHT,
                } );
            }

            /** @override */
            onUpdateFrame( delta_time ) {
                super.onUpdateFrame( delta_time );
                this.totalTime += delta_time;
                this.elapsedTime += delta_time;
                if ( this.elapsedTime > 3.0 && this.totalTime < 15.0 ) {
                    const pin = new mapray.PinEntity( this.viewer.scene );
                    pin.altitude_mode = mapray.AltitudeMode.CLAMP;
                    pin.addPin( new mapray.GeoPoint( 138.71934, 35.36 + this.latValue, 3780 ), { size: 15 } );
                    pin.setBGColor([1.0, 0.5, 0.0]);
                    this.addEntity( pin );
                    this.elapsedTime = 0;
                    this.latValue += 0.005;
                }

                const viewer = this.viewer;
                if ( viewer.render_mode !== this._render_mode ) {
                    viewer.render_mode = this._render_mode;
                }

                if ( this._time_type === TimeType.NIGHTTIME ) {
                    const sun_xy_degree = -45;
                    const sun_z_degree = 36.0;
                    uiviewer.viewer.sun.setSunDirection( [ Math.cos(Math.PI/180.0 * sun_xy_degree), Math.sin(Math.PI/180.0 * sun_xy_degree), Math.sin(Math.PI/180.0 * sun_z_degree) ] );

                    const moon_xy_degree = 135;
                    const moon_z_degree = 36.0;
                    uiviewer.viewer.moon.setMoonDirection( [ Math.cos(Math.PI/180.0 * moon_xy_degree), Math.sin(Math.PI/180.0 * moon_xy_degree), Math.sin(Math.PI/180.0 * moon_z_degree) ] );
                    uiviewer.viewer.moonVisualizer.setRadius(5);
                } else {
                    const sun_xy_degree = 135
                    const sun_z_degree = 36.0;
                    uiviewer.viewer.sun.setSunDirection( [ Math.cos(Math.PI/180.0 * sun_xy_degree), Math.sin(Math.PI/180.0 * sun_xy_degree), Math.sin(Math.PI/180.0 * sun_z_degree) ] );

                    const moon_xy_degree = -45;
                    const moon_z_degree = 36.0;
                    uiviewer.viewer.moon.setMoonDirection( [ Math.cos(Math.PI/180.0 * moon_xy_degree), Math.sin(Math.PI/180.0 * moon_xy_degree), Math.sin(Math.PI/180.0 * moon_z_degree) ] );
                    uiviewer.viewer.moonVisualizer.setRadius(5);
                }
            }

            /** @override */
            onKeyDown( event ) {
                switch ( event.key ) {
                    case "m": case "M": {
                        this._render_mode = (
                            this._render_mode === mapray.Viewer.RenderMode.SURFACE ?
                            mapray.Viewer.RenderMode.WIREFRAME :
                            mapray.Viewer.RenderMode.SURFACE
                        );
                    } break;
                    case "s": case "S": {
                        this._base_map_url = (
                            this._base_map_url === BaseMapType.PHOTO ?
                            BaseMapType.STANDARD :
                            BaseMapType.PHOTO
                        );

                        let image_provider;
                        if ( this._base_map_url === BaseMapType.PHOTO ) {
                            image_provider = new mapray.StandardImageProvider( {
                                url: "https://cyberjapandata.gsi.go.jp/xyz/seamlessphoto/",
                                format: ".jpg",
                                 min_level: 0,
                                max_level: 18
                            } );
                        } else {
                            image_provider = new mapray.StandardImageProvider( { 
                                url: "https://cyberjapandata.gsi.go.jp/xyz/std/", 
                                format: ".png", 
                                min_level: 2, 
                                max_level: 18 
                            } );
                        }
                        this.viewer.setImageProvider( image_provider, true );
                    } break;
                    case "t": case "T": {
                        this._time_type = (
                            this._time_type === TimeType.DAYTIME ?
                            TimeType.NIGHTTIME :
                            TimeType.DAYTIME
                        );
                    }
                    break;
                    case "d": case "D": {
                        this.get3DDataset();
                    } break;
                    case "p": case "P": {
                        this.getPointCloud();
                    } break;
                    case "b": case "B": {
                        this.getBuilding();
                    } break;
                    default: {
                        super.onKeyDown( event );
                    }
                }
            }

            async get3DDataset() {
                const datasetId = CloudData.threeD;
                const resource = this._maprayApi.get3DDatasetAsResource( datasetId );
                const loader = new mapray.SceneLoader( this.viewer.scene, resource, {
                    onEntity: ( loader, entity, feature ) => {
                        // Since the resource is a 3D dataset, the entity will be an instance of the ModelEntity class.
                        // When onEntity is specified, you need to manually add the entity.
                        entity.setPosition( new mapray.GeoPoint( 139.7454316, 35.658584, 351 ) );
                        entity.setScale( [ 20, 20, 20 ] );

                        this.addEntity( entity );
                        // Move the camera to the model's location.
                        const bounds = entity.getBounds();
                        this.startFlyCamera({
                            time: 3.0,
                            iscs_end: bounds.getCenter(),
                            end_altitude: 800,
                            end_from_lookat: 351
                        });
                    }
                });
                await loader.load();
            }

            async getPointCloud() {
                const datasetId = CloudData.pointCloud;
                const resource = this._maprayApi.getPointCloudDatasetAsResource( datasetId );
                const provider = new mapray.StandardPointCloudProvider( resource );
                const point_cloud = await this.viewer.point_cloud_collection.add( provider );
                const end_cam_lookup_pos = new mapray.GeoPoint( 139.745401, 35.65857, 368 );
                this.startFlyCamera({
                    time: 3.0,
                    iscs_end: end_cam_lookup_pos,
                    end_altitude: 800,
                    end_from_lookat: 351
                });
            }

            async getBuilding() {
                const datasetId = CloudData.building;
                const b3d = await this.fetchBuildingData( datasetId, apikey);

                const provider = new mapray.StandardB3dProvider( b3d.url, ".bin", {
                    meta_headers: {
                        "X-API-Key": apikey,
                    },
                    tile_headers: {
                        "X-API-Key": apikey,
                    },
                });
                const b3dScene = uiviewer.viewer.b3d_collection.createScene( provider );

                const end_cam_lookup_pos = new mapray.GeoPoint( 139.5526, 35.31992, 0 );
                this.startFlyCamera({
                    time: 3.0,
                    iscs_end: end_cam_lookup_pos,
                    end_altitude: 800,
                    end_from_lookat: 1000
                });
            }

            async fetchBuildingData( datasetID, apikey) {
                try {
                    const url = "https://api.mapray.com/b3ddatasets/v2/" + datasetID;

                    const response = await fetch(url, {
                        method: 'GET',
                        headers: {
                            'X-Api-Key': apikey
                        }
                    });

                    if (!response.ok) {
                        throw new Error(`response status: ${response.status}`);
                    }

                    return response.json();
                } catch (error) {
                    console.error(error.message);
                    throw error;
                }
            }
        }

        const uiviewer = new CustomViewer( "mapray-container", apikey, {
            atmosphere: new mapray.Atmosphere(),
            sun_visualizer: new mapray.SunVisualizer( 32 ),
            moon_visualizer: new mapray.MoonVisualizer( 'https://resource.mapray.com/assets/images/moon.jpg' ),
            star_visualizer: new mapray.StarVisualizer( 'https://resource.mapray.com/assets/data/star75.json', 'https://resource.mapray.com/assets/images/starmap_512n2.jpg' ),
        });

        uiviewer.viewer.atmosphere.setRayleigh(0.01);
        uiviewer.viewer.atmosphere.setMie(0.001);

        uiviewer.setCameraPosition({
            longitude: 142.0,
            latitude: 20.0,
            height: 2500000
        });
        uiviewer.setLookAtPosition({
            longitude: 138.729802,
            latitude: 35.363965,
            height: 3000
        });
    </script>
</html>