This project has moved and is read-only. For the latest updates, please go here.

Parseing deep within folders

Dec 8, 2011 at 1:19 PM

Hi,

im trying to parse a large kml that can go several folders deep. the problem is there are no children (Features) in these deeper folders. even though there should be. ie KML > Document > Folder > Folder = Nothing > placemark > linestring

ive been using example

http://sharpkml.codeplex.com/discussions/275320

Thanks

Michael

Dec 8, 2011 at 1:28 PM

here is an example

http://autotest.diydrones.com/ArduPlane-test.mavlog.kmz

Dec 8, 2011 at 3:13 PM

Hmm... Well the good news is I can reproduce your problem... the bad news is I don't yet know why!

 

It's strange as I've simplified the file you gave to this:

<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
  <Document>
    <name>GPS device</name>
    <Folder>
      <name>Tracks</name>
      <Folder>
        <Folder>
          <name>Points</name>
          <Placemark>
            <name>-0</name>
            <Point>
              <altitudeMode>absolute</altitudeMode>
              <extrude>1</extrude>
              <coordinates>149.165222,-35.362850,584.250000</coordinates>
            </Point>
          </Placemark>
        </Folder>
      </Folder>
    </Folder>
  </Document>
</kml>

Which can be parsed to produce this tree:

<Container 'GPS device'>
  <Container 'Tracks'>
    <Container ''>
      <Container 'Points'>
        <SharpKml.Dom.Placemark />
      </Container 'Points'>
    </Container ''>
  </Container 'Tracks'>
</Container 'GPS device'>

Why the original file doesn't work I don't yet know, but I've ruled out the Kmz handling (i.e. the kml text is being extracted correctly) so bear with me while I look into it for you.

Sam

Dec 8, 2011 at 8:27 PM
Edited Dec 8, 2011 at 8:32 PM

I've tracked the problem down to an error in the parser when it encounters an empty node (<Snippet/> in the given file). I should be able to fix it at the weekend, however, as a temporary hack you could try this:

const string filePath = @"ArduPlane-test.mavlog.kmz";
KmlFile kml;

using (var kmz = KmzFile.Open(filePath))
{
    // <Snippet/> is causing the problems for the parser - temporary hack is to remove it!
    string text = kmz.ReadKml().Replace("<Snippet/>", "");
    using (var stream = new MemoryStream(Encoding.Default.GetBytes(text)))
    {
        kml = KmlFile.Load(stream);
    }
}

There doesn't appear to be anything wrong with your file and this is indeed a bug with the parser; I'll open an issue and get it fixed when I can. I've opened an issue here. Thanks for highlighting the issue!

Dec 10, 2011 at 3:44 PM

Michael,

I've uploaded a new version (1.04) which now fixes the problem. Here's a basic sampe that counts the number of children in each container:

using System;
using SharpKml.Dom;
using SharpKml.Engine;

class Program
{
    static void ReadContainer(Container container, string indent)
    {
        Console.WriteLine("{0}Container '{1}'", indent, container.Name);

        // Count the children.
        int count = 0;
        foreach (var child in container.Features)
        {
            var c = child as Container;
            if (c != null)
            {
                ReadContainer(c, indent + "  ");
            }
            else
            {
                count++;
            }
        }

        Console.WriteLine("{0}  +{1} other children.", indent, count);
    }

    static void Main(string[] args)
    {
        const string filePath = @"ArduPlane-test.mavlog.kmz";
        KmlFile kml;

        // Make sure we dispose of the kmz file.
        using (var kmz = KmzFile.Open(filePath))
        {
            kml = KmlFile.LoadFromKmz(kmz);
        }

        Kml root = kml.Root as Kml;
        if (root != null)
        {
            var document = root.Feature as Document;
            if (document != null)
            {
                ReadContainer(document, "");
            }
        }
    }
}
Dec 10, 2011 at 10:27 PM

thank you