Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#4231 closed enhancement (fixed)

KML Import - Retain IconStyle and child Elements/Attributes

Reported by: xander Owned by: warmerdam
Priority: normal Milestone:
Component: OGR_SF Version: unspecified
Severity: normal Keywords: libkml
Cc: winkey

Description

Currently when a KML file is read, the IconStyle? (and its children) of placemarks are thrown away. Unfortunately, this field is used to give a reference to an icon to put at that point and the heading (rotation) of the icon. Retaining the contents of the IconStyle? as an attribute or some sort of key/value pair that could be queried would be really helpful.

My personal use is in Mapserver - being able to read points from a KML file and then render them as oriented icons. It is not currently possible as OGR drops the data.

The first affected file is ogrlibkmlfeaturestyle.cpp. In method kml2featurestyle(...), the following statement is where the IconStyle? would be identified for further processing:

/* does the placemark have a styleselector and a style url? */

if ( poKmlPlacemark->has_styleselector ( )

&& poKmlPlacemark->has_styleurl ( ) ) {

/* todo do the style and styleurl part */

}

Change History (7)

comment:1 Changed 6 years ago by Even Rouault

Cc: winkey added
Component: defaultOGR_SF
Keywords: libkml added

comment:2 Changed 6 years ago by winkey

xander

Can you attach a kml and the mapfile your having problems with?

Currently i believe styleurl and style are parsed properly, styleselectors are not.

have you tried any of the enviroment settings listed in http://www.gdal.org/ogr/drv_libkml.html

Mapserver cannot pull in a icon from a url so you need to add this as a symbol

  SYMBOL
    NAME "http://atmos.ucsd.edu//kml/images/hi.png"
    TYPE PIXMAP
    IMAGE "../images/hi.png"
    #TRANSPARENT 
  END

  SYMBOL
    NAME "http://atmos.ucsd.edu//kml/images/lo.png"
    TYPE PIXMAP
    IMAGE "../images/lo.png"
    #TRANSPARENT 0
  END

Brian

comment:3 in reply to:  2 Changed 6 years ago by xander

Re, "kml and mapfile"

No need for a sample mapfile - OGR has thrown away the information before it ever gets to Mapserver.

Below is a listing of a KML that will exhibit the problem. It is a subset of the wind file that NOAA generates.

Note: I've already walked this through a debugger. As I mentioned in the original message, the method kml2featurestyle(...) in ogrlibkmlfeaturestyle.cpp throws away the IconStyle? - the first "if" statement in the method finds a styleselector and a styleurl and returns without doing any processing. Since the information isn't kept anywhere, even as a key/value pair, it isn't available for later rendering or translation. The request for an enhancement is in hopes that in the future there will be a way to get at the IconStyle? elements/attributes rather than have them dropped during processing.

<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.2"> <Document><visibility>1</visibility><LookAt?><longitude>-95.00</longitude><latitude>40.00</latitude><range>200000.0</range> <tilt>0</tilt><heading>0</heading></LookAt?> <Style id="MetarBalloonStyle?"><BalloonStyle?><bgColor>ffffffc8</bgColor> <text><![CDATA[<table width="400" border="0" padding ="10"> <tr><td><img src="icons/noaanws.gif" width=100> </td><td><img src="icons/madis.gif" height=67 width=100></td></tr> </table><b><font size="+3">$[name]</font></b><br/><br/><font face="Courier">$[description]</font>]]> <A href="http://www.ncdc.noaa.gov/oa/climate/conversion/swometardecoder.html">Decoding Information</A> <br></br><A href="http://madis.noaa.gov">MADIS webpage</A> </text></BalloonStyle?></Style>

<Placemark><name>KBOS BOSTON/LOGAN_INTL</name> <Snippet maxLines="0">empty</Snippet>\n<description><![CDATA[<table width 400 border="1" padding ="1">\n<tr><td><B>OBS DATE/TIME</B></td><td><B>08/31/2011 10:54</B></td></tr> <tr><td><B>ELEV</B></td><td><B>9 M</B></td><td><B>30 FT</B></td></tr> <tr><td><B>TEMP</B></td><td><B>68 F</B></td><td><B>20 C</B></td></tr> <tr><td><B>DWPT</B></td><td><B>55</B></td><td><B>13 C</B></td></tr> <tr><td><B>RH</B></td><td><B>65 %</B></td></tr> <tr><td><B>WIND DIR</B></td><td><B>290</B></td></tr> <tr><td><B>WIND SPD</B></td><td><B>4 KT</B></td><td><B>5 MPH</B></td></tr> <tr><td><B>WIND GUST</B></td><td><B>M</B></td><td><B>M</B></td></tr> <tr><td><B>ALT SETTING</B></td><td><B>30.17 IN HG</B></td></tr> <tr><td><B>PRES MSL</B></td><td><B>1022 MB</B></td></tr> <tr><td><B>3HR PRES TEND</B></td><td><B>M</B></td></tr> <tr><td><B>PRES WX</B></td><td><B></B></td></tr> <tr><td><B>1 HR PREC</B></td><td><B>T</B></td></tr> <tr><td><B>VSBY</B></td><td><B>10 STAT MI</B></td></tr> <tr><td><B>SKY</B></td><td><B>250FEW 228 228 228</B></td></tr> <tr><td><B>OBS TYPE</B></td><td><B>ASOS</B></td></tr> </table>]]></description> <styleUrl>#MetarBalloonStyle?</styleUrl><Style> <IconStyle?><scale>1.0</scale><color>ff999999</color><heading>380</heading> <Icon><href>http://www.srh.noaa.gov/gis/kml/icons/wind/5.png</href> </Icon></IconStyle?> <LabelStyle?><scale>2.0</scale></LabelStyle?></Style> <Point><coordinates>-71.03,42.369999,0</coordinates></Point> <visibility>1</visibility></Placemark> </Document></kml>

Xander

comment:4 Changed 6 years ago by winkey

I stated a couple of items wrong in my previous comment

Currently I believe styleurl OR style are parsed properly, stylemaps are not.

One of the reasons a styleurl AND style together are not implemented at this time is there is nothing in the OGR feature style spec for both, nor is there a style tool for @style_name.

Here is a log from irc on the subject.

FreeNode-#gdal.log:2010 Mar 05 06:23:36 <danmo> About referring to a style in a table and adding to it, I don't think anything was planned... you'd have to define it and since you are the first one to go there AFAIK you could add it to the document
FreeNode-#gdal.log:2010 Mar 05 06:23:46 <winkey> it almost seems to me that there should be a style tool for @style_name
FreeNode-#gdal.log:2010 Mar 05 06:24:50 <danmo> maybe... as I said that part was never fully implemented/tested, so you may find some flaws, and in the spirit of open source would have to contribute to the evolution of the beast ;)
FreeNode-#gdal.log:2010 Mar 05 06:25:30 <winkey> say its its wind barbs, you could point to a @style_name to get the right barb but every barb would need a angle to rotate it
FreeNode-#gdal.log:2010 Mar 05 06:26:39 <danmo> I see what you mean... so you'd need an @style_name(attrib_name) kind of ref
FreeNode-#gdal.log:2010 Mar 05 06:27:06 <danmo> could be a nice addition
FreeNode-#gdal.log:2010 Mar 05 06:28:04 <danmo> or maybe we just need a way to refer to an attribute field inside a style definition, and don't need @style_name(attrib_name)
FreeNode-#gdal.log:2010 Mar 05 06:30:29 <winkey> "@10ktbarb,SYMBOL(a:180.0)"   << style string in a feature
FreeNode-#gdal.log:2010 Mar 05 06:31:23 <winkey> gets @10ktbarb from a style table, SYMBOL(a:180.0) rotates it 180deg
FreeNode-#gdal.log:2010 Mar 05 06:31:56 <winkey> either that or that becomes a style table itself
FreeNode-#gdal.log:2010 Mar 05 06:37:28 <winkey> see i could easily create something with with 65K wind barbs, while there is only 20 different barbs there is a few hundred different angles of rotation

Also i should note that last I checked the libkml driver is the only software that supports StyleTables?, Therefore in order to use document or layer level styles with mapserver you need to set LIBKML_RESOLVE_STYLE=YES

I am working on a fix to set the style string to the style in the StyleTable? that a styleurl points to then modify it with the contents of the features style when LIBKML_RESOLVE_STYLE=YES

comment:5 Changed 6 years ago by winkey

Resolution: fixed
Status: newclosed

Fix commited in r23055


$ cat testy123.kml
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
    <Document>
        <Style id="wind5">
            <IconStyle>
                <scale>1.0</scale>
                <color>ff999999</color>
                <Icon><href>http://www.srh.noaa.gov/gis/kml/icons/wind/5.png</href></Icon>
            </IconStyle>
            <LabelStyle>
                <scale>2.0</scale>
            </LabelStyle>
        </Style>
        <Placemark>
            <name>KBOS    BOSTON/LOGAN_INTL</name>
            <styleUrl>#wind5</styleUrl>
            <Style id="1">
                <IconStyle>
                    <heading>380</heading>
                </IconStyle>
            </Style>
            <Point><coordinates>-71.03,42.369999,0</coordinates></Point>
            <visibility>1</visibility>
        </Placemark>
    </Document>
</kml>

$ LIBKML_RESOLVE_STYLE=YES ogrinfo testy123.kml -al

INFO: Open of `testy123.kml'
      using driver `LIBKML' successful.

Layer name: testy123
Geometry: Unknown (any)
Feature Count: 1
Extent: (-71.030000, 42.369999) - (-71.030000, 42.369999)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        TOWGS84[0,0,0,0,0,0,0],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9108"]],
    AUTHORITY["EPSG","4326"]]
Name: String (0.0)
description: String (0.0)
timestamp: DateTime (0.0)
begin: DateTime (0.0)
end: DateTime (0.0)
altitudeMode: String (0.0)
tessellate: Integer (0.0)
extrude: Integer (0.0)
visibility: Integer (0.0)
OGRFeature(testy123):1
  Name (String) = KBOS    BOSTON/LOGAN_INTL
  description (String) = (null)
  timestamp (DateTime) = (null)
  begin (DateTime) = (null)
  end (DateTime) = (null)
  altitudeMode (String) = (null)
  tessellate (Integer) = -1
  extrude (Integer) = -1
  visibility (Integer) = 1
  Style = LABEL(w:200.000000);SYMBOL(id:http://www.srh.noaa.gov/gis/kml/icons/wind/5.png,a:380.000000,c:#999999FF,s:1.000000)
  POINT (-71.03 42.369999 0)

}}

comment:6 Changed 6 years ago by Even Rouault

r23061 /trunk/gdal/ogr/ogrsf_frmts/libkml/ (ogrlibkmlfeaturestyle.cpp ogrlibkmlstyle.cpp): LIBKML: fix memory leaks introduced in r23055, and an older one (#4231)

comment:7 Changed 6 years ago by Even Rouault

r23062 /trunk/gdal/ogr/ogrsf_frmts/libkml/ogrlibkmlfeaturestyle.cpp: LIBKML: fix segfault, perhaps introduced by the move of CPLFree(pszUrl) done in r23061 (#4231)

Note: See TracTickets for help on using tickets.