-
Notifications
You must be signed in to change notification settings - Fork 353
/
Feature.ts
110 lines (97 loc) · 3.94 KB
/
Feature.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
import { makeObservable, observable } from "mobx";
import Ellipsoid from "terriajs-cesium/Source/Core/Ellipsoid";
import ConstantPositionProperty from "terriajs-cesium/Source/DataSources/ConstantPositionProperty";
import Entity from "terriajs-cesium/Source/DataSources/Entity";
import Cesium3DTileFeature from "terriajs-cesium/Source/Scene/Cesium3DTileFeature";
import Cesium3DTilePointFeature from "terriajs-cesium/Source/Scene/Cesium3DTilePointFeature";
import ImageryLayer from "terriajs-cesium/Source/Scene/ImageryLayer";
import ImageryLayerFeatureInfo from "terriajs-cesium/Source/Scene/ImageryLayerFeatureInfo";
import JsonValue from "../../Core/Json";
import { BaseModel } from "../Definition/Model";
import { TerriaFeatureData } from "./FeatureData";
const customProperties = ["entityCollection", "properties", "data"];
/** Terria wrapper around Cesium Entity
* Adds a few extra properties
* -
*/
export default class TerriaFeature extends Entity {
/** This object can be used to pass Terria-specific properties */
data?: TerriaFeatureData | JsonValue;
cesiumEntity?: Entity;
imageryLayer?: ImageryLayer | undefined;
/** This comes from Cesium.scene.drillPick
* No type provided
*/
cesiumPrimitive?: any;
_catalogItem?: BaseModel;
_cesium3DTileFeature?: Cesium3DTileFeature | Cesium3DTilePointFeature;
/** Flag if loading featureInfoUrl (see `FeatureInfoUrlTemplateMixin.getFeaturesFromPickResult`) */
@observable loadingFeatureInfoUrl?: boolean = false;
constructor(options: Entity.ConstructorOptions) {
super(options);
makeObservable(this);
addCustomFeatureProperties(this);
}
/**
* Creates a new Feature from an Entity.
* Note the custom properties are also copied into the new Feature properly.
*/
static fromEntity(entity: Entity): TerriaFeature {
const feature = new TerriaFeature({ id: entity.id });
feature.merge(entity);
for (let i = 0; i < customProperties.length; i++) {
if (entity.propertyNames.indexOf(customProperties[i]) === -1) {
(feature as any)[customProperties[i]] = (entity as any)[
customProperties[i]
]; // Assume no merging or cloning needed.
}
}
feature.cesiumEntity = entity;
return feature;
}
/**
* If the given entity is part of an entityCollection, and those entities are themselves features,
* then return the matching feature from the collection.
* Otherwise, return a new Feature from this entity.
*/
static fromEntityCollectionOrEntity(entity: Entity): TerriaFeature {
// If this entity is part of a collection, get the feature with this id from that collection.
let feature;
if (entity.entityCollection) {
feature = entity.entityCollection.getById(entity.id);
}
if (!feature || !(feature instanceof TerriaFeature)) {
feature = TerriaFeature.fromEntity(entity);
}
return feature;
}
static fromImageryLayerFeatureInfo(imageryFeature: ImageryLayerFeatureInfo) {
const feature = new TerriaFeature({
id: imageryFeature.name,
name: imageryFeature.name,
description: imageryFeature.description,
properties: imageryFeature.properties
});
feature.data = imageryFeature.data;
feature.imageryLayer = imageryFeature.imageryLayer;
if (imageryFeature.position) {
feature.position = new ConstantPositionProperty(
Ellipsoid.WGS84.cartographicToCartesian(imageryFeature.position)
);
}
return feature;
}
}
// Features have 'entityCollection', 'properties' and 'data' properties, which we must add to the entity's property list,
// if they're not already there. (In case they are added in a future version of Cesium.)
function addCustomFeatureProperties(entity: Entity) {
for (let i = 0; i < customProperties.length; i++) {
const propertyName = customProperties[i];
if (
entity.propertyNames.indexOf(propertyName) === -1 &&
!(propertyName in entity)
) {
entity.addProperty(customProperties[i]);
}
}
}