/
GeoJSONLoader.ts
517 lines (408 loc) · 17 KB
/
GeoJSONLoader.ts
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import Loader from "./Loader"
import GeoMath, { Vector3, Vector4 } from "./GeoMath";
import Scene from "./Scene";
import GLEnv from "./GLEnv";
import GeoPoint from "./GeoPoint";
import CredentialMode from "./CredentialMode";
import MarkerLineEntity from "./MarkerLineEntity";
import PolygonEntity from "./PolygonEntity";
import PinEntity from "./PinEntity";
import Resource, { URLResource } from "./Resource";
import AltitudeMode from "./AltitudeMode";
import GeoJSON from "./GeoJSON";
/**
* GeoJSON形式([rfc7946](https://tools.ietf.org/html/rfc7946))のデータをシーンに読み込みます。
*/
class GeoJSONLoader extends Loader {
private _getPointFGColor: (geojson: GeoJSON.FeatureJson) => Vector3 | number[];
private _getPointBGColor: (geojson: GeoJSON.FeatureJson) => Vector3 | number[];
private _getPointSize: (geojson: GeoJSON.FeatureJson) => number;
private _getPointIconId: (geojson: GeoJSON.FeatureJson) => string | undefined;
private _getLineColor: (geojson: GeoJSON.FeatureJson) => Vector3 | Vector4 | number[];
private _getLineWidth: (geojson: GeoJSON.FeatureJson) => number;
private _getFillColor: (geojson: GeoJSON.FeatureJson) => Vector4 | number[];
private _getExtrudedHeight: (geojson: GeoJSON.FeatureJson) => number;
private _getAltitudeMode: (geojson: GeoJSON.FeatureJson) => AltitudeMode;
private _getAltitude: (geojson: GeoJSON.FeatureJson) => number | undefined;
private _glenv: GLEnv;
private _cancelled: boolean;
private _finished: boolean;
/**
* url で指定したシーンデータの読み込みを開始し、scene にエンティティを構築する。
*
* 読み込みが終了したとき options.callback を呼び出す。
* @param scene 読み込み先のシーン
* @param resource シーンファイルの URL
* @param options オプション集合
*/
constructor( scene: Scene, resource: Resource | string, options: GeoJSONLoader.Option = {} )
{
let res: Resource;
if (resource instanceof Resource) {
res = resource;
}
else if ( typeof resource === "string" ) {
res = new URLResource(resource, {
type: Resource.Type.JSON,
transform: options.transform
});
}
else {
throw new Error( "Unsupported Resource: " + resource);
}
super( scene, res, {
onEntity: options.onEntity,
onLoad: options.onLoad
} );
// PinEntity
this._getPointFGColor = options.getPointFGColor || defaultGetPointFGColorCallback;
this._getPointBGColor = options.getPointBGColor || defaultGetPointBGColorCallback;
this._getPointSize = options.getPointSize || defaultGetPointSizeCallback;
this._getPointIconId = options.getPointIconId || defaultGetPointIconIdCallback;
// MarkerLineEntity
this._getLineColor = options.getLineColor || defaultGetLineColorCallback;
this._getLineWidth = options.getLineWidth || defaultGetLineWidthCallback;
// PolygonEntity
this._getFillColor = options.getFillColor || defaultGetFillColorCallback;
this._getExtrudedHeight = options.getExtrudedHeight || defaultGetExtrudedHeightCallback;
// Common
this._getAltitudeMode = options.getAltitudeMode || defaultGetAltitudeModeCallback;
this._getAltitude = options.getAltitude || defaultGetAltitudeCallback;
this._glenv = scene.glenv;
// this._references = {};
this._cancelled = false;
this._finished = false;
}
/**
* 読み込み処理の実態。継承クラスによって実装される。
*/
protected override _load(): Promise<void>
{
return (
this._resource.load( { type: Resource.Type.JSON } )
.then( geoJson => {
// JSON データの取得に成功
this._check_cancel();
this._load_geojson_object( geoJson );
} )
);
}
/**
* Load GeoJSON Object
*/
private _load_geojson_object( geojson: GeoJSON.FeatureCollectionJson | GeoJSON.FeatureJson | GeoJSON.GeometryJson ): boolean
{
var success;
if ( GeoJSON.isFeatureCollectionJson( geojson ) ) {
success = false;
for ( var i = 0, len = geojson.features.length; i < len; i++ ) {
var feature = geojson.features[i];
// @ts-ignore
var s = this._load_geojson_object( feature.featureId ? feature.feature : feature ); // @ToDo: Unknown
// var s = this._load_geojson_object( feature );
if (s && !success) success = s;
}
}
else if ( GeoJSON.isFeatureJson( geojson ) ) {
success = this._load_geometry_object( geojson.geometry, geojson );
}
else {
success = this._load_geometry_object( geojson, undefined );
}
if ( this._cancelled ) return false;
return success;
}
/**
* Load Geometry Object
*/
private _load_geometry_object( geometry: GeoJSON.GeometryJson, geojson?: GeoJSON.FeatureJson ): boolean {
const vGeojson = geojson || {
id: "",
type: "Feature",
geometry: geometry,
} as GeoJSON.FeatureJson;
switch ( geometry.type ) {
case GEOMETRY_TYPES.POINT:
case GEOMETRY_TYPES.MULTI_POINT:
return this._loadPoint( geometry as (GeoJSON.PointGeometryJson | GeoJSON.MultiPointGeometryJson), vGeojson );
case GEOMETRY_TYPES.LINE_STRING:
case GEOMETRY_TYPES.MULTI_LINE_STRING:
return this._loadLines( geometry as (GeoJSON.LineStringGeometryJson | GeoJSON.MultiLineStringGeometryJson), vGeojson );
case GEOMETRY_TYPES.POLYGON:
case GEOMETRY_TYPES.MULTI_POLYGON:
return this._loadPolygons( geometry as (GeoJSON.PolygonGeometryJson | GeoJSON.MultiPolygonGeometryJson), vGeojson );
case GEOMETRY_TYPES.GEOMETRY_COLLECTION:
return true;
default:
throw new Error( "Invalid GeoJSON type: " + geometry.type );
}
}
/**
* Load Line and LineString
*/
private _loadLines( geometry: GeoJSON.LineStringGeometryJson | GeoJSON.MultiLineStringGeometryJson, geojson: GeoJSON.FeatureJson ): boolean
{
const color4 = this._getLineColor( geojson );
const width = this._getLineWidth( geojson );
const altitude = this._getAltitude( geojson );
const altitude_mode = this._getAltitudeMode( geojson );
if ( !geometry || color4.length !== 4 ) {
return false;
}
var type = geometry.type;
var coords = geometry.coordinates;
var rgb = color4.slice(0, 3) as Vector3;
var alpha = color4[3];
// If multiline, split entity
if ( type === GEOMETRY_TYPES.MULTI_LINE_STRING ) {
const multiLineStringGeometry = geometry as GeoJSON.MultiLineStringGeometryJson;
let noError = true;
multiLineStringGeometry.coordinates.forEach( points => {
const flag = this._generateLine( points, width, rgb, alpha, altitude_mode, altitude, geojson );
if ( !flag ) {
noError = false;
}
return flag;
});
return noError;
}
else { // type === GEOMETRY_TYPES.LINE_STRING
const lineStringGeometry = geometry as GeoJSON.LineStringGeometryJson;
return this._generateLine( lineStringGeometry.coordinates, width, rgb, alpha, altitude_mode, altitude, geojson )
}
}
/**
* Create MarkerLineEntity
*/
private _generateLine( points: GeoJSON.CoordinatesJson[], width: number, color: Vector3, opacity: number, altitude_mode: AltitudeMode, altitude: number | undefined, geojson: GeoJSON.FeatureJson ): boolean
{
if ( !points ) {
return false;
}
var entity = new MarkerLineEntity( this._scene );
// @ts-ignore
entity.altitude_mode = altitude_mode;
var fp = this._flatten( points, altitude );
entity.addPoints( fp );
// @ts-ignore
entity.setLineWidth( width );
// @ts-ignore
entity.setColor( color );
// @ts-ignore
entity.setOpacity( opacity );
// @ts-ignore
this._onEntity( this, entity, geojson );
return true;
}
/**
* Load Point and MultiPoint
*/
private _loadPoint( geometry: GeoJSON.PointGeometryJson | GeoJSON.MultiPointGeometryJson, geojson: GeoJSON.FeatureJson ): boolean
{
const fgColor = this._getPointFGColor( geojson );
const bgColor = this._getPointBGColor( geojson );
const iconId = this._getPointIconId( geojson );
const size = this._getPointSize( geojson );
const altitude_mode = this._getAltitudeMode( geojson );
const altitude = this._getAltitude( geojson );
if ( !geometry ) {
return false;
}
var type = geometry.type;
var props = {
"fg_color": fgColor.slice( 0, 3 ) as Vector3,
"bg_color": bgColor.slice( 0, 3 ) as Vector3,
"size": size,
};
// If multiline, split entity
const entity = new PinEntity( this._scene );
entity.altitude_mode = altitude_mode;
if ( GeoJSON.isPointGeometryJson( geometry ) ) {
var alt = this._getActualValue( altitude, geometry.coordinates[2], GeoJSONLoader.defaultAltitude );
var coords = new GeoPoint( geometry.coordinates[0], geometry.coordinates[1], alt );
if ( iconId ) {
entity.addMakiIconPin( iconId, coords, props );
}
else {
entity.addPin( coords, props );
}
}
else { // type === GEOMETRY_TYPES.MULTI_POINT
for ( var i = 0; i < geometry.coordinates.length; i++ ) {
var targetCoordinates = geometry.coordinates[i];
var alt = this._getActualValue( altitude, targetCoordinates[2], GeoJSONLoader.defaultAltitude );
var coords = new GeoPoint( targetCoordinates[0], targetCoordinates[1], alt );
if ( iconId ) {
entity.addMakiIconPin( iconId, coords, props );
// entity.addPin( coords, props );
}
else {
entity.addPin( coords, props );
}
}
}
// @ts-ignore
this._onEntity( this, entity, geojson );
return true;
}
/**
* Load Polygon and MultiPolygon
*/
private _loadPolygons( geometry: GeoJSON.PolygonGeometryJson | GeoJSON.MultiPolygonGeometryJson, geojson: GeoJSON.FeatureJson ): boolean
{
const color4 = this._getFillColor( geojson );
const altitude_mode = this._getAltitudeMode( geojson );
const altitude = this._getAltitude( geojson );
const extruded_height = this._getExtrudedHeight( geojson );
if ( !geometry || color4.length !== 4 ) {
return false;
}
var type = geometry.type;
var coords = geometry.coordinates;
var rgb = color4.slice(0, 3) as Vector3;
var alpha = color4[3];
// If multiline, split entity
if ( type === GEOMETRY_TYPES.MULTI_POLYGON ) {
const multiPolygonGeometry = geometry as GeoJSON.MultiPolygonGeometryJson;
let noError = true;
multiPolygonGeometry.coordinates.forEach( points => {
const flag = this._generatePolygon( points, rgb, alpha, altitude_mode, altitude, extruded_height, geojson );
if ( !flag ) {
noError = false;
}
return flag;
});
return noError;
}
else { // type === GEOMETRY_TYPES.POLYGON
const polygonGeometry = geometry as GeoJSON.PolygonGeometryJson;
return this._generatePolygon( polygonGeometry.coordinates, rgb, alpha, altitude_mode, altitude, extruded_height, geojson )
}
}
/**
* Create Polygon
*/
private _generatePolygon( pointsList: GeoJSON.CoordinatesJson[][], color: Vector3, opacity: number, altitude_mode: AltitudeMode, altitude: number | undefined, extruded_height: number, geojson: GeoJSON.FeatureJson ): boolean
{
if ( !pointsList ) {
return false;
}
const entity = new PolygonEntity( this._scene );
// @ts-ignore
entity.altitude_mode = altitude_mode;
entity.extruded_height = extruded_height;
entity.setColor( color );
entity.setOpacity( opacity );
for ( let i=0; i< pointsList.length; i++ ) {
const fp = this._flatten( pointsList[ i ], altitude, pointsList[ i ].length-1 );
if ( !fp ) return false;
if ( i === 0 ) entity.addOuterBoundary( fp );
else entity.addInnerBoundary( fp );
}
// @ts-ignore
this._onEntity( this, entity, geojson );
return true;
}
private _getActualValue( valueFromCallback: number | undefined, valueInGeoJSON: number | undefined, defaultValue: number ): number {
return (
valueFromCallback != undefined ? valueFromCallback: // value from callback is the most prioritized
valueInGeoJSON != undefined ? valueInGeoJSON: // value in GeoJSON will be used if defined
defaultValue // default value
);
}
private _flatten( array: number[][], altitude: number | undefined, len: number = array.length ): number[]
{
return array.reduce( (arr: number[], v: number[], index: number) => (
index >= len ? arr :
arr.concat( v.slice(0, 2), this._getActualValue( altitude, v[2], GeoJSONLoader.defaultAltitude ) )
), [] );
};
}
namespace GeoJSONLoader {
export interface Option {
/** リソース要求変換関数 */
transform?: Resource.TransformCallback;
/** 終了コールバック関数 */
callback?: Loader.FinishCallback;
/** 読み込み完了時のコールバック関数 */
onLoad?: Loader.FinishCallback;
/** エンティティコールバック関数 */
onEntity?: Loader.EntityCallback;
getPointFGColor?: (geojson: GeoJSON.FeatureJson) => Vector3;
getPointBGColor?: (geojson: GeoJSON.FeatureJson) => Vector3;
getPointSize?: (geojson: GeoJSON.FeatureJson) => number;
getPointIconId?: (geojson: GeoJSON.FeatureJson) => string | undefined;
getLineColor?: (geojson: GeoJSON.FeatureJson) => Vector3 | Vector4;
getLineWidth?: (geojson: GeoJSON.FeatureJson) => number;
getFillColor?: (geojson: GeoJSON.FeatureJson) => Vector4;
getExtrudedHeight?: (geojson: GeoJSON.FeatureJson) => number;
getAltitudeMode?: (geojson: GeoJSON.FeatureJson) => AltitudeMode;
getAltitude?: (geojson: GeoJSON.FeatureJson) => number | undefined;
}
export const _defaultHeaders = {};
export const defaultLineColor = [0, 0, 0, 1];
export const defaultFillColor = [0, 0, 0, 1];
export const defaultLineWidth = 1;
export const defaultPointFGColor = [1.0, 1.0, 1.0];
export const defaultPointBGColor = [0.35, 0.61, 0.81];
export const defaultPointSize = 30;
export const defaultPointIconId = undefined;
export const defaultAltitude = 0.0;
export const defaultExtrudedHeight = 0.0;
} // namespace GeoJSONLoader
function defaultGetLineColorCallback( geojson: GeoJSON.FeatureJson )
{
return GeoJSONLoader.defaultLineColor;
}
function defaultGetLineWidthCallback( geojson: GeoJSON.FeatureJson )
{
return GeoJSONLoader.defaultLineWidth;
}
function defaultGetFillColorCallback( geojson: GeoJSON.FeatureJson )
{
return GeoJSONLoader.defaultFillColor;
}
function defaultGetPointFGColorCallback( geojson: GeoJSON.FeatureJson )
{
return GeoJSONLoader.defaultPointFGColor;
}
function defaultGetPointBGColorCallback( geojson: GeoJSON.FeatureJson )
{
return GeoJSONLoader.defaultPointBGColor;
}
function defaultGetPointSizeCallback( geojson: GeoJSON.FeatureJson )
{
return GeoJSONLoader.defaultPointSize;
}
function defaultGetPointIconIdCallback( geojson: GeoJSON.FeatureJson )
{
return GeoJSONLoader.defaultPointIconId;
}
function defaultGetAltitudeModeCallback( geojson: GeoJSON.FeatureJson )
{
return AltitudeMode.ABSOLUTE;
}
function defaultGetAltitudeCallback( geojson: GeoJSON.FeatureJson )
{
return undefined;
}
function defaultGetExtrudedHeightCallback( geojson: GeoJSON.FeatureJson )
{
return GeoJSONLoader.defaultExtrudedHeight;
}
var TYPES = {
FEATURE: "Feature",
FEATURE_COLLECTION: "FeatureCollection",
};
var GEOMETRY_TYPES = {
POINT: "Point",
MULTI_POINT: "MultiPoint",
LINE_STRING: "LineString",
MULTI_LINE_STRING: "MultiLineString",
POLYGON: "Polygon",
MULTI_POLYGON: "MultiPolygon",
GEOMETRY_COLLECTION: "GeometryCollection",
FEATURE: "Feature"
};
var SUPPORTED_GEOMETRY_TYPES = Object.values( GEOMETRY_TYPES );
export default GeoJSONLoader;