在 Cesium 中解析实体

Parsing Entities in Cesium

我在 KML 中有大约 7k-​​8k 个地标。我必须通过导入 KML 文件来解析 cesium 中的每个地标。我尝试通过 EntityCollection.values 数组进行解析,但由于并非所有实体都是地标,因此我找不到其他方法来解析 Cesium 中的所有地标实体。

我想了解以下内容:

  1. EntityCollection.values 中的实体如何分组?
  2. 有没有其他方法可以访问导入 KML 文件时创建的所有地标实体?

我试图解析 EntityCollection.values 数组,发现只有在未指定数量的实体之后才会出现地标。

当 Cesium KmlDataSource 加载 KML 源时,它会展平 KML 要素的结构,这样容器(文档和文件夹)和地标都会按照它们的顺序作为数组添加到实体集合中出现在源 KML 中。除了根级容器之外的所有容器都填充在数组中。

这是一个将 KML 源加载到 Cesium 并遍历实体的示例。

var url = "mykml.kml"; // source KML file path or URL
viewer.dataSources
    .add(Cesium.KmlDataSource.load(url))
    .then(
            function (kmlData) {
                parseElements(kmlData.entities)                
            }
        );

    function parseElements(entities) {      
        var e;
        var pointCount = 0;
        var values = entities.values;
        console.dir(values); // debug the array
        for (var i = 0; i < values.length; i++) {
            e = values[i];
            if (Cesium.defined(e.position)) {
                // Placemark with Point geometry
                pointCount++;
            }
            else if (Cesium.defined(e.polyline)) {
                // Placemark with LineString geometry               
            }
            else if (Cesium.defined(e.polygon)) {
                // Placemark with Polygon geometry
            }
            // check for other conditions
        }
        console.log(pointCount); // dump # of point placemarks
        viewer.flyTo(entities);     
    }

如果源 KML 有 ExtendedData then you can access this extended data via the entity's kml property which is a KmlFeatureData 个对象。

示例:

<Placemark>
   ...
   <ExtendedData>
      <Data name="holeNumber">
        <value>1</value>
      </Data>
      <Data name="holeYardage">
        <value>234</value>
      </Data>
      <Data name="holePar">
        <value>4</value>
      </Data>
  </ExtendedData>
</Placemark>

如果 var "e" 是从上面的 KML 创建的实体,那么下面的代码片段将输出 value=234.

  var data = e.kml.extendedData;
  if (Cesium.defined(data)) {
    console.log("value=", data.holeYardage.value);      
 }