samcragg / sharpkml

SharpKML is an implementation of the Open Geospatial Consortium (OGC) KML 2.2 standard developed in C#, able to read/write both KML files and KMZ files.
MIT License
158 stars 51 forks source link

Wrong parsing result for KML file with leading commas in the coordinates #46

Closed yoliva closed 1 year ago

yoliva commented 2 years ago

Hi! We are getting an odd behavior while parsing files with leading commas and without altitude values.

current behavior: What the parser is doing is returning a geometry with a single vector taking (firstPoint.Latitude, firstPoint.longitude, secondPointLatitude) and throwing away the rest of the coordinates. We use the files on google earth and seems to be working fine as we can see in the attached image

expected behavior: we would expect to receive a parser error or maybe just ignore the "," characters as google earth does.

File example:

<?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>Untitled Project</name>
    <gx:CascadingStyle kml:id="__managed_style_2A6D6AD34423575D18C9">
        <Style>
            <IconStyle>
                <scale>1.2</scale>
                <Icon>
                    <href>https://earth.google.com/earth/rpc/cc/icon?color=1976d2&amp;id=2000&amp;scale=4</href>
                </Icon>
                <hotSpot x="64" y="128" xunits="pixels" yunits="insetPixels"/>
            </IconStyle>
            <LabelStyle>
            </LabelStyle>
            <LineStyle>
                <color>ff2dc0fb</color>
                <width>6</width>
            </LineStyle>
            <PolyStyle>
                <color>40ffffff</color>
            </PolyStyle>
            <BalloonStyle>
                <displayMode>hide</displayMode>
            </BalloonStyle>
        </Style>
    </gx:CascadingStyle>
    <gx:CascadingStyle kml:id="__managed_style_1A556FFF7423575D18C9">
        <Style>
            <IconStyle>
                <Icon>
                    <href>https://earth.google.com/earth/rpc/cc/icon?color=1976d2&amp;id=2000&amp;scale=4</href>
                </Icon>
                <hotSpot x="64" y="128" xunits="pixels" yunits="insetPixels"/>
            </IconStyle>
            <LabelStyle>
            </LabelStyle>
            <LineStyle>
                <color>ff2dc0fb</color>
                <width>4</width>
            </LineStyle>
            <PolyStyle>
                <color>40ffffff</color>
            </PolyStyle>
            <BalloonStyle>
                <displayMode>hide</displayMode>
            </BalloonStyle>
        </Style>
    </gx:CascadingStyle>
    <StyleMap id="__managed_style_0B0498AB9323575D18C9">
        <Pair>
            <key>normal</key>
            <styleUrl>#__managed_style_1A556FFF7423575D18C9</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#__managed_style_2A6D6AD34423575D18C9</styleUrl>
        </Pair>
    </StyleMap>
    <Placemark id="0465ADB7B023575D18B1">
        <name>SamplePolygon</name>
        <LookAt>
            <longitude>-96.37549845507505</longitude>
            <latitude>33.69573399847625</latitude>
            <altitude>161.6919297143338</altitude>
            <heading>0</heading>
            <tilt>0</tilt>
            <gx:fovy>35</gx:fovy>
            <range>1493.557827594923</range>
            <altitudeMode>absolute</altitudeMode>
        </LookAt>
        <styleUrl>#__managed_style_0B0498AB9323575D18C9</styleUrl>
        <Polygon>
            <outerBoundaryIs>
                <LinearRing>
                    <coordinates>
                        -96.3771267813479,33.69746891969778, -96.37757927526032,33.69532740118915, -96.37468917139492,33.69530114437216, -96.37444803536881,33.69749495506764, -96.3771267813479,33.69746891969778,</coordinates>
                </LinearRing>
            </outerBoundaryIs>
        </Polygon>
    </Placemark>
</Document>
</kml>

result of loading the file on google earth: image

Please let me know if you need any extra details from my side. Thanks!!

samcragg commented 1 year ago

Sorry, I've not been keeping track of these issues. You're correct, we're parsing it wrong. According to the spec:

The coordinate separator is a comma and the tuple separator is a whitespace

The parser is trying to be flexible and skipping whitespace between points, however, this is incorrect when the altitude part is optional. I'll update it and add some tests over the weekend.

Thanks for reporting it

samcragg commented 1 year ago

That's fixed now in the latest release, thanks again for reporting it.

yoliva commented 1 year ago

Awesome @samcragg! We will update our references. Thanks!