简体   繁体   English

如何在Openlayers的kml中访问嵌套标签?

[英]How do I access a nested tag in a kml in openlayers?

I am trying to modify the earthquake cluster map to display some of my own data. 我正在尝试修改地震群图以显示一些我自己的数据。 I am running into problems when using an attribute from my KML to style it. 使用我的KML中的属性设置样式时,我遇到了问题。 This is a sample feature from my KML: 这是我的KML的示例功能:

<Placemark>
    <name>REIERSTAD 2 ORION DW 16-2-7-6</name>
    <ExtendedData><SchemaData schemaUrl="#alberta_wells">
        <SimpleData name="UWI">F2/16-02-007-06W4/0</SimpleData>
        <SimpleData name="KeyList">0074060216F20</SimpleData>
        <SimpleData name="Field">0998</SimpleData>
        <SimpleData name="Pool">0158098</SimpleData>
        <SimpleData name="OSDep">0000000</SimpleData>
        <SimpleData name="LicStatus">Issued</SimpleData>
        <SimpleData name="License">0043029</SimpleData>
        <SimpleData name="LicDate">19720719</SimpleData>
        <SimpleData name="Licensee">0FF30</SimpleData>
        <SimpleData name="FDDate">19720719</SimpleData>
        <SimpleData name="TotalDep">0457.00</SimpleData>
        <SimpleData name="WellStat">0600080000</SimpleData>
        <SimpleData name="StatDate">19720721</SimpleData>
    </SchemaData></ExtendedData>
      <Point><coordinates>-110.707313,49.537234</coordinates></Point>
  </Placemark>

And here is the snippet from the earthquake cluster example that handles the styling of individual features: 这是地震集群示例中的片段,用于处理各个要素的样式:

function createEarthquakeStyle(feature) {
        // 2012_Earthquakes_Mag5.kml stores the magnitude of each earthquake in a
        // standards-violating <magnitude> tag in each Placemark.  We extract it
        // from the Placemark's name instead.
        var name = feature.get('name');
        var magnitude = parseFloat(name.substr(2));
        var radius = 5 + 20 * (magnitude - 5);

        return new ol.style.Style({
          geometry: feature.getGeometry(),
          image: new ol.style.RegularShape({
            radius1: radius,
            radius2: 3,
            points: 5,
            angle: Math.PI,
            fill: earthquakeFill,
            stroke: earthquakeStroke
          })
        });
      }

I want to use the "TotalDep" value as my radius, replacing what the example is currently using (magnitude). 我想将“ TotalDep”值用作我的半径,以替换示例当前使用的值(大小)。 However, my KML has this "TotalDep" value buried in multiple tags. 但是,我的KML将此“ TotalDep”值埋在多个代码中。 I have been trying to use a DOMParser to extract this value like so: 我一直在尝试使用DOMParser提取此值,如下所示:

function createEarthquakeStyle(feature) {
  var extendedData = feature.get('ExtendedData');
  console.log(extendedData)
  xmlDoc = parser.parseFromString(extendedData, "text/xml")
  console.log(xmlDoc)
  var wellDepth
  var nodeList = xmlDoc.getElementsByTagName("SimpleData")
  for (var i = 0; i < nodeList.length; i++) {
    if (nodeList[i].getAttribute("name") == "TotalDep") {
      wellDepth = parseFloat(nodeList[i].nodeValue)
    }
  }
  var radius = wellDepth / 10.0;

  return new ol.style.Style({
    geometry: feature.getGeometry(),
    image: new ol.style.RegularShape({
      radius1: radius,
      radius2: 3,
      points: 5,
      angle: Math.PI,
      fill: earthquakeFill,
      stroke: earthquakeStroke
    })
  });
}

It is not working, features will not show up once you zoom in far enough to collapse the clusters to single features. 它无法正常工作,一旦放大到足以将群集折叠为单个功能,功能就不会显示。

I see that in the original example, ".get("name")" is called on the input feature, which returns the contents inside the "name" tag. 我看到在原始示例中,在输入功能部件上调用了“ .get(“ name”)“,该功能返回了“ name”标签内的内容。 I thought that calling ".get("ExtendedData")" would return the contents of the "ExtendedData" tag, but it does not seem to return anything when I try to print it to console. 我以为调用“ .get(” ExtendedData“)”会返回“ ExtendedData”标记的内容,但是当我尝试将其打印到控制台时似乎没有返回任何内容。 Even converting the variable extendedData to String before logging it to console logs "undefined". 甚至将变量extendedData转换为String,然后再将其记录到控制台,日志“未定义”。

I guess what I'm asking is how do you drill down into nested tags when trying to access those values? 我想我想问的是,当尝试访问这些值时,如何深入嵌套标签?

Ok so apparently I have somehow massively overcomplicated this, but I am bewildered as to how I have it working now. 好的,显然我使它复杂化了很多,但是我对现在的工作方式感到困惑。 It seems that simply calling 似乎只是打电话

var wellDepth = feature.get("TotalDep")

returns the value contained in this tag: 返回此标记中包含的值:

<SimpleData name="TotalDep">0457.00</SimpleData>

I guess the ".get()" function will search both tag names AND tag attributes? 我猜“ .get()”函数将同时搜索标记名称和标记属性? And also looks through nested tags? 还可以通过嵌套标签查找吗? This is some straight-up wizardry going on here... I went from being horribly frustrated to massively impressed by this. 这是一些直截了当的巫术……我从对此感到非常沮丧,到对此印象深刻。

As one final tip to anybody who may be desperately search for answers on how to access attributes from your KML when writing a style function: Try calling feature.getKeys() and logging it to console. 作为向所有可能拼命寻找有关在编写样式函数时如何从KML访问属性的人的答案的最后提示:尝试调用feature.getKeys()并将其记录到控制台。 You will get a list of all possible things you can access by using feature.get(). 您将获得使用feature.get()可以访问的所有可能事物的列表。 That's how I figured this out. 我就是这样想的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM