如何在Openlayers的kml中访问嵌套标签? [英] How do I access a nested tag in a kml in openlayers?

查看:171
本文介绍了如何在Openlayers的kml中访问嵌套标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将地震群图修改为显示我自己的一些数据.使用我的KML中的属性设置样式时,我遇到了问题.这是我的KML的一个示例功能:

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. This is a sample feature from my 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
          })
        });
      }

我想使用"TotalDep"值作为我的半径,替换示例当前使用的(幅度).但是,我的KML将此"TotalDep"值埋在多个代码中.我一直在尝试使用DOMParser提取该值,如下所示:

I want to use the "TotalDep" value as my radius, replacing what the example is currently using (magnitude). However, my KML has this "TotalDep" value buried in multiple tags. I have been trying to use a DOMParser to extract this value like so:

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.

我看到在原始示例中,在输入功能部件上调用了".get("name"),该功能返回了"name"标签内的内容.我以为调用".get(" ExtendedData)"会返回"ExtendedData"标记的内容,但是当我尝试将其打印到控制台时似乎没有返回任何内容.甚至在将变量extendedData转换为String之前,将其记录到控制台日志未定义".

I see that in the original example, ".get("name")" is called on the input feature, which returns the contents inside the "name" tag. 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. Even converting the variable extendedData to String before logging it to console logs "undefined".

我想我想问的是,当尝试访问这些值时,您如何深入嵌套标签?

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>

我猜想".get()"函数将同时搜索标记名称和标记属性?还可以通过嵌套标签查找吗?这是一些直截了当的巫术……我从对此感到非常沮丧,到对此印象深刻.

I guess the ".get()" function will search both tag names AND tag attributes? 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.

作为最后一个向所有可能在编写样式函数时急切地寻求有关如何从KML中访问属性的人的答案的提示:尝试调用feature.getKeys()并将其记录到控制台.您将获得使用feature.get()可以访问的所有可能事物的列表.我就是这样想的.

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. You will get a list of all possible things you can access by using feature.get(). That's how I figured this out.

这篇关于如何在Openlayers的kml中访问嵌套标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆