zuchi / kml-reader

Golang KML Reader
MIT License
0 stars 1 forks source link

More complexity #1

Open Gys opened 1 week ago

Gys commented 1 week ago

Hi!

Indeed there do not seem to be many KML readers. Maybe because KML is formatted as XML which is (as a format) easy to parse.

I came across your reader while looking at a KML file with a little more complexity. I assume KML in the wild is more like this, also considering what the different KML writers are capable of. For example: https://pkg.go.dev/github.com/twpayne/go-kml/v3

Edit: I posted this because according to your reader this document is empty. But looking into your code I see your definition of KML is more extensive than I thought. Sorry for that. Still, the file below seems not to parse well with your code. The file imports into the mobile app Organic Maps without problems.

<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2' xmlns:gx='http://www.google.com/kml/ext/2.2' xmlns:kml='http://www.opengis.net/kml/2.2' xmlns:atom='http://www.w3.org/2005/Atom'>
<Document>

    <name>Jacobswegen-wandelaars-ES-20240724</name>

    <!-- ======= Start Styles Routes ======= -->
    <StyleMap id="msn_Hoofdroute1">
        <Pair>
            <key>normal</key>
            <styleUrl>#sn_Hoofdroute1</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#sh_Hoofdroute1</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="sn_Hoofdroute1">
        <LineStyle>
            <color>ff00ffff</color>
            <width>4</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>
    <Style id="sh_Hoofdroute1">
        <LineStyle>
            <color>ff00ffaa</color>
            <width>6</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>

    <StyleMap id="msn_Hoofdroute2">
        <Pair>
            <key>normal</key>
            <styleUrl>#sn_Hoofdroute2</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#sh_Hoofdroute2</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="sn_Hoofdroute2">
        <LineStyle>
            <color>ff1c19d7</color>>
            <width>4</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>
    <Style id="sh_Hoofdroute2">
        <LineStyle>
            <color>ff00ffaa</color>
            <width>6</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>

    <StyleMap id="msn_Hoofdroute3">
        <Pair>
            <key>normal</key>
            <styleUrl>#sn_Hoofdroute3</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#sh_Hoofdroute3</styleUrl>
        </Pair>
    </StyleMap>

    <Style id="sn_Hoofdroute3">
        <LineStyle>
            <color>ff0055ff</color>>
            <width>4</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>
    <Style id="sh_Hoofdroute3">
        <LineStyle>
            <color>ff00ffaa</color>
            <width>6</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>

    <StyleMap id="msn_Hoofdroute4">
        <Pair>
            <key>normal</key>
            <styleUrl>#sn_Hoofdroute4</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#sh_Hoofdroute4</styleUrl>
        </Pair>
    </StyleMap>

    <Style id="sn_Hoofdroute4">
        <LineStyle>
            <color>ff701919</color>>
            <width>4</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>
    <Style id="sh_Hoofdroute4">
        <LineStyle>
            <color>ff00ffaa</color>
            <width>6</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>

    <StyleMap id="msn_Link">
        <Pair>
            <key>normal</key>
            <styleUrl>#sn_Link</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#sh_Link</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="sn_Link">
        <LineStyle>
            <color>ffb67b2c</color>>
            <width>4</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004A88D4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>
    <Style id="sh_Link">
        <LineStyle>
            <color>ff00ffaa</color>
            <width>6</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004A88D4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>

    <StyleMap id="msn_Variant">
        <Pair>
            <key>normal</key>
            <styleUrl>#sn_Variant</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#sh_Variant</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="sn_Variant">
        <LineStyle>
            <color>ffff00ff</color>>
            <width>4</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>
    <Style id="sh_Variant">
        <LineStyle>
            <color>ff00ffaa</color>
            <width>6</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>

    <StyleMap id="msn_NietInGebruik">
        <Pair>
            <key>normal</key>
            <styleUrl>#sn_NietInGebruik</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#sh_NietInGebruik</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="sn_NietInGebruik">
        <LineStyle>
            <color>ff294c75</color>>
            <width>4</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>
    <Style id="sh_NietInGebruik">
        <LineStyle>
            <color>ff00ffaa</color>
            <width>6</width>
        </LineStyle>
        <BalloonStyle>
              <bgColor>004a88d4</bgColor>
              <text>
                <![CDATA[
                <b><font color="#BB0000" size="+1">$[name]</font></b>
                <br><br>
                <font face="Arial">$[description]</font>
                <!--$[geDirections]-->
                ]]>
              </text>
        </BalloonStyle>
    </Style>

    <Style id="placemark-yellow">
        <IconStyle>
            <Icon>
                <href>https://omaps.app/placemarks/placemark-yellow.png</href>
            </Icon>
        </IconStyle>
    </Style>

    <!-- ======= End Styles Routes ======= -->

    <!-- ======= Start Main Folder Spanje ======= -->
    <Folder>
        <name>Jacobswegen in Spanje</name>
        <open>1</open>

        <!-- ======= Start Hoofdroutes Spanje ======= -->
        <Folder>
            <name>Routes</name>

            <Placemark>
                <name>Camino Frances</name>
                <description>
                    <![CDATA[Saint Jean Pied de Port - Santiago de Compostela: 761 km<br><br><a href="www.santiago.nl/downloads">Bron</a><br><a href="https://www.santiago.nl/product-categorie/gidsen/gidsen-spanje/">Diverse gidsen</a>]]>
                </description>
                <styleUrl>#msn_Hoofdroute1</styleUrl>
                <MultiGeometry>
                    <LineString>
                        <tessellate>1</tessellate>
                        <coordinates>
                            -1.23585,43.16339,191 -1.23641,43.1631,186 -1.23697,43.16281,178 -1.23721,43.16239,171 -1.23745,43.16197,171 ....
Gys commented 1 week ago

I am using this struct now to read the file:

type Kml struct {
XMLName  xml.Name `xml:"kml"`
Text     string   `xml:",chardata"`
Xmlns    string   `xml:"xmlns,attr"`
Gx       string   `xml:"gx,attr"`
Kml      string   `xml:"kml,attr"`
Atom     string   `xml:"atom,attr"`
Document struct {
    Text string `xml:",chardata"`
    Name struct {
        Text string `xml:",chardata"`
    } `xml:"name"`
    StyleMap []struct {
        Text string `xml:",chardata"`
        ID   string `xml:"id,attr"`
        Pair []struct {
            Text string `xml:",chardata"`
            Key  struct {
                Text string `xml:",chardata"`
            } `xml:"key"`
            StyleUrl struct {
                Text string `xml:",chardata"`
            } `xml:"styleUrl"`
        } `xml:"Pair"`
    } `xml:"StyleMap"`
    Style []struct {
        Text      string `xml:",chardata"`
        ID        string `xml:"id,attr"`
        LineStyle struct {
            Text  string `xml:",chardata"`
            Color struct {
                Text string `xml:",chardata"`
            } `xml:"color"`
            Width struct {
                Text string `xml:",chardata"`
            } `xml:"width"`
        } `xml:"LineStyle"`
        BalloonStyle struct {
            Chardata string `xml:",chardata"`
            BgColor  struct {
                Text string `xml:",chardata"`
            } `xml:"bgColor"`
            Text struct {
                Text string `xml:",chardata"`
            } `xml:"text"`
        } `xml:"BalloonStyle"`
        IconStyle struct {
            Text string `xml:",chardata"`
            Icon struct {
                Text string `xml:",chardata"`
                Href struct {
                    Text string `xml:",chardata"`
                } `xml:"href"`
            } `xml:"Icon"`
        } `xml:"IconStyle"`
    } `xml:"Style"`
    Folder []struct {
        Text string `xml:",chardata"`
        Name struct {
            Text string `xml:",chardata"`
        } `xml:"name"`
        Open struct {
            Text string `xml:",chardata"`
        } `xml:"open"`
        Folder []struct {
            Text string `xml:",chardata"`
            Name struct {
                Text string `xml:",chardata"`
            } `xml:"name"`
            Placemark []struct {
                Text string `xml:",chardata"`
                Name struct {
                    Text string `xml:",chardata"`
                } `xml:"name"`
                Description struct {
                    Text string `xml:",chardata"`
                } `xml:"description"`
                StyleUrl struct {
                    Text string `xml:",chardata"`
                } `xml:"styleUrl"`
                MultiGeometry struct {
                    Text       string `xml:",chardata"`
                    LineString struct {
                        Text       string `xml:",chardata"`
                        Tessellate struct {
                            Text string `xml:",chardata"`
                        } `xml:"tessellate"`
                        Coordinates struct {
                            Text string `xml:",chardata"`
                        } `xml:"coordinates"`
                    } `xml:"LineString"`
                } `xml:"MultiGeometry"`
                BalloonVisibility struct {
                    Text string `xml:",chardata"`
                } `xml:"balloonVisibility"`
                Point struct {
                    Text        string `xml:",chardata"`
                    Coordinates struct {
                        Text string `xml:",chardata"`
                    } `xml:"coordinates"`
                } `xml:"Point"`
                LineString struct {
                    Text       string `xml:",chardata"`
                    Tessellate struct {
                        Text string `xml:",chardata"`
                    } `xml:"tessellate"`
                    Coordinates struct {
                        Text string `xml:",chardata"`
                    } `xml:"coordinates"`
                } `xml:"LineString"`
            } `xml:"Placemark"`
        } `xml:"Folder"`
    } `xml:"Folder"`
} `xml:"Document"`
}