Ad

How Do I Access A Nested Tag In A Kml In Openlayers?

- 1 answer

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
          })
        });
      }

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.

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?

Ad

Answer

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

Ad
source: stackoverflow.com
Ad