I Heart KML Schema!

Update: this information is now out of date because of improvements to the KML format in version 2.2. You can read about this in a later post and in Google’s great tutorial on extended data.

Recently, Chris over at FortiusOne has written a couple articles, focusing largely on the Schema tag in KML. One thing in particular from this article got me thinking:

Google Earth will display your custom fields in the markers that get displayed in a Schema-based KML file, but it’s still up to you to make a pretty-looking description if you want, and if you want to include some of the data in your custom fields, you’ll have to duplicate the information which isn’t ideal.

This certainly seems like a bit of a roadblock for the good old Schema tag. It really breaks the whole separation of content from style idea, which surprised me because this separation had been accommodated in other KML tags. So I started hacking… and found that this isn’t really a limitation. One word: BalloonStyle.

Using this tag, you can easily use Schema to transport relatively pure attributes, while pointing to style information located elsewhere in the document or, if you’re in the mood, in a different document entirely.

Here’s some data in a relatively simple schema:

 <Newt>
   <name>Sammy G</name>
   <styleUrl>http://www.jasonbirch.com/files/kml_schema_style.kml#newt_style</styleUrl>
   <Point>
     <coordinates>1.75,1.75,0</coordinates>
   </Point>
   <newt_id>36</newt_id>
   <newt_breed>Common Orange Slitherer</newt_breed>
   <newt_slime_factor>7.2</newt_slime_factor>
   <newt_tail_length>6</newt_tail_length>
 </Newt>

Which can be combined with the following BalloonStyle:

 <Style id="newt_style">
  <BalloonStyle>
   <text><![CDATA[
    <h1>$[name]</h1>
    <table>
     <tr>
      <td>Breed:</td>
      <td>$[newt_breed]</td>
     </tr>
     <tr>
      <td>Tail Length:</td>
      <td>$[newt_tail_length]</td>
     </tr>
     <tr>
      <td>Slime Factor:</td>
      <td>$[newt_slime_factor]</td>
     </tr>
     <tr>
      <td colspan=”2″><img src=
       ”http://www.jasonbirch.com/files/jason_small.jpg?id=$[newt_id]”
       /></td>
     </tr>
    </table>
   ]]></text>
  </BalloonStyle>
 </Style>

To give you the fully separated content/style shown below:

Sammy G Meets World

Now, I think that’s downright cool. I can have my cake and eat it too. If you want to see this example live in Google Earth ™, just click on the image above. You can also view the source of the content document, or of the style document. I’ll leave it as an exercise for the reader to determine whether this technique can be used with more complex tags than SimpleField, such as SimpleArrayField and ObjField :)

I am aware of one other application (Safe Software’s FME) that is using the Schema tag to pass around attributes, and have really liked the idea ever since I first saw it in Rebecca Moore’s anti-logging KML file. It’s easy to use, doesn’t require external documents, and combined with other KML goodness allows you to pack styling around with your fully attributed data.

There is a nasty rumour floating around that Schema has been deprecated. It isn’t listed as such in the KML docs yet, and I certainly hope that it isn’t too late to ensure continued support for this handy tag from Google.

-J

Related posts

7 Responses to “I Heart KML Schema!”

  1. 1Brian Flood on Jun 29, 2007 at 8:24 am:

    hi jason

    good post, this is a very nice way to keep raw attributes and styling information in the same placemark. the new $[name] balloonstyle tags are quite nice

    With A2E, I decided to not go this route because we format the attribute information before adding it to the description. At the very least, ArcMap’s builtin formatters are used (dates look nice, currency looks correct, different numeric notations are used etc) but you can also apply multiple conditions to a field. So if the value is A, “apply this styling” or “insert this html”. The result is a value that is not the original attribute but some formatted version of it based on some logic. I did think of adding Schema tags as well but the bloat of the placemark text can be significant. It’s on the list to make it optional :)

    anyways, I heard the Schema depreciation rumor as well. I think I saw it in the actual kml docs awhile back. don’t know where it stands right now…

    cheers
    brian

  2. 2Jason Birch on Jun 30, 2007 at 3:09 am:

    Well, you could make it all string-based :)

    I hope that if we make enough noise about Schema (it’s SOOO much easier to deal with than Metadata) they’ll reconsider and support both going forward.

    I certainly see some limitations to this approach. For instance, I don’t know how stable that BalloonStyle is if I re-style the object for point style, etc.

  3. 3Andy on Jul 4, 2007 at 1:36 pm:

    Hi Jason,
    First of all, great post! Second, Schema will be an excellent use for data transmission with KML.

    I am posting this message because I am pretty sure that we can extract the data from more complex tags such as SimpleArrayField…

    Actually if you don’t use the description tag, all the values from the new tags are displayed in the balloon and you get something like array[0] = xyz where x,y and z are three different values concatenated from three different tags…

    As you might already know that, I was wondering if you could just post a quick example that works with SimpleArrayField… it will be great!

    Thx
    Andy

  4. 4ben on Aug 21, 2007 at 12:38 pm:

    Hi Jason,

    We have also discovered that in addtion to using the balloon style, you can also directly embed the $[yourCustomElement] references directly within a description field, such as:

    My Folder Name
    1

    New Element Placemark Example Name
    Sample Description references the TOM element value: $[TOM]
    sampleTomValue
    5

  5. 5ben on Aug 21, 2007 at 2:20 pm:

    Whoops forgot about the angle brackets in my previous post, trying again…

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://earth.google.com/kml/2.1">
    <Document>
    <Schema parent="Placemark" name="NewElementExampleTag">
    <SimpleField type="wstring" name="TOM"></SimpleField>
    <SimpleField type="int" name="HARRY"></SimpleField>
    </Schema>
    <Folder>
    <name>My Folder Name</name>
    <open>1</open>
    <NewElementExampleTag>
    <name>New Element Placemark Example Name</name>
    <description>Sample Description references the TOM element value: $[TOM]</description>
    <TOM>sampleTomValue</TOM>
    <HARRY>5</HARRY>
    </NewElementExampleTag>
    </Folder>
    </Document>
    </kml>

  6. 6Jason Birch on Aug 21, 2007 at 7:54 pm:

    Good to know. Thanks Ben.

Trackbacks / Pingbacks:

  1. Random Nodes

    Trackback on Sep 8, 2007 at 4:24 am