KML: Getting Started

KML(Keyhole Markup Language) is an XML data format used to display information in a geographical context  . Just as Web browsers read and display HTML files,  land-based browsers such as Google Earth read and display KML files. KML is a language readable byeveryone , composed of text and punctuation . It can be created and edited through a basic text editor, saved, and then previewed in a land-based browser . You do not need to be a wizard to master the basics of KML, and you’ll notice than this knowledge will make it possible to create powerful presentations that depict your geographical data and images on the numerous land-based free browsers such as : Google Earth, Microsoft Virtual Earth, NASA WorldWind, ArcGisExplorer, ..

You can create KML files with the interface Google Earth, or you can use a simple text editor  to write rough KML from Zeron. KML files and their related images can be stored in KMZ archives so that all related files and images and models are included  in, just,  one zipped KMZ container . To share your KML and KMZ  files , you can e-mail them,  as files attached , host them locally to be shared on a private or business network, or publicly on a web server . 

Once you have correctly configured your server and shared the web address for your KML files, anyone using Google Earth ( or any compatible application) can display the KML files you create .

You can use KML to add your own landmarks , geometries , annotations and images on Google Earth basic image. If you host KML files in a server, you can even update your presentation in the system at regular intervals or whenever  your data changes.

The files publicly hosted are indexed by Web search  engines for easy access to all the Web users.

It is very easy to personalize your KML presentations, thanks to the use of customized icons ,bullets , colours , placemarks , and labels . KML  allows you to display your elements at specific time ranges and change the display according to the user’s zoom level, with increasing levels of detail as the user approaches the ground.

ML 2.2 supports the presentation of astronomical and terrestrial data. When you include a special hint (hint = “target = sky”) at the beginning of a KML file, the browser interprets the data in a different way and projects it onto a virtual celestial sphere that surrounds the Earth. In Sky mode, the Google Earth camera looks to the sky rather than down to Earth. The main difference for the creator of the KML is that he must perform some arithmetic calculations to convert the astronomical coordinates (ascension and right declination) into terrestrial coordinates (longitude and latitude). Everything else in the KML works the same way in Sky (Sky) and Earth (Earth) modes.

To see more examples of this article, download and install Google Earth http://earth.google.com .

In these series of articles we will be using a Google Maps plugin. It does not allow to use all the features of Google Earth.

Your first KML

Here is a first KML to get started.

<?xml version=”1.0″ encoding=”UTF-8″?>
<kml xmlns=”http://www.opengis.net/kml/2.2″>   <Placemark>
<name>Simple placemark</name>
<description>Attached to the ground. Intelligently places itself         at the height of the underlying terrain.</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark> </kml>

This is what you get when loaded in Google Maps or Google Earth.

Click  the icon , you see appear the bullet corresponding with the text defined in the KML file .

To start practicing the KML language you can enter this sample text in any word processor (Notepad will do the job ).
Save the file with an extension.kml and open the file in Google Earth on your computer Once you have saved the file  , you can also edit it . For example , try to change it, re-save it with a different name and, finally, open it with Google Earth.

Then , try changing the values for the element < coordinates > , save the file and look where the new icon appears.

The best way to master KML is to experiment with examples of files, to modify the values and  display the results in your browser . If you make a mistake, you will see nothing in the browser, and this is the proof that there is an error in your file.

Google Earth provides a feature to control mistakes: Select Options> General and under KML Error Handling, choose Show for all errors.

You can also use a KML validator to check your KML code. For example, see the KMLValidator by Galdos Systems at

www.kmlvalidator.com .

 

Structure of a KML file

Each KML file starts with both lines illustrated in this example .

? <?xml version=”1.0″ encoding=”UTF-8″?>
<kml xmlns=”http://www.opengis.net/kml/2.2″

If you create a KML from scratch, make sure you copy these two lines textually at the beginning of the file . A KML file can only contain one  element < kml >. Do not forget the closing </ kml > tag at the end of the file .

The file contains an element that has three children . The brackets <> indicate the names KML elements:

 Edit
Tag Contents
<name> Label for the Placemark
<description> Text (and optional images) providing additional information

about the placemark. It appears in the information box.

This balloon appears when the user clicks on the site.

 

<Point> Contains the <coordinates> element.

The <coordinates> element contains values for the longitude, latitude, and altitude of <Placemark>.

Modifier

Since the KML is an XML data format, it has a consistent structure that respects some rules

An element begins with its name in brackets (< Placemark >). An element ends with the name in square brackets preceded by a forward slash (</ Placemark >). The value of the element  is contained between these two tags . 

Simple elements and Complex elements

In KML, any word enclosed in brackets (<>) is an element.

When an element name begins with a capital letter he is an element complex, which means it can contain other elements.

 

For example, in this snippet, <item> element is a complex that contains the <coordinates>element :

<Point>
<coordinates> -122.084583,37.42227,0 </ coordinates>
</ Point>

 

The names of the simple elements  start with a low case letter . The Simple elements  can contain other elements .

A single element only contains  data as characters (in XML terms: letters, numbers and symbols that are not used for XML are utilized for mark-up purposes). In the example Bonjour.kml,<name>, <description>, and <coordinates> are examples of simple elements .

Complex elements are also called parents because they contain other elements. The  simple

elements are called children. In a KML file, children are recessed more spaces of the position of their parent in the file, but this convention is simply for readability. Browsers do not distinguish the different levels of indentation

A few rules to follow scrupulously

A few general rules to keep in mind when creating KML files:

The case is important. Each element name must be written exactly as indicated in the KML2.2 Reference , respecting the capital and lower case. 
• The order is significant. TheKML children elements  must be listed in the same order indicated in the KML Reference 2.2. You can omit children elements  but you can not rearrange them. 
• The children elements  can only belong to the allowed parent elements . You must follow the individual  syntax individual of the KML 2.2 Reference.

 

And the cherry on the cake …

  Google Earth copy and paste.

When displaying a KML file in Google Earth, you can easily copy the functionality of Google Earth and paste in a text editor such than Notepad .

 

It is somehow counter intuitive that you could copy a graphic feature of the 3D viewer of Google Earth and when you paste it in a text editor, it is converted in its corresponding KML text format , but try ! it works !

Follow the following steps  to observe the KML code of a feature such as reference mark, GroundOverlay , Polygon (shape) or LineString (path ) in Google Earth.

 

In the 3D viewer (or panel Places ) from Google Earth, place your cursor in the feature to highlight.

Click with the right button of the mouse and select Copy   in the drop-down menu that appears .

Open a text editor and paste the contents of the clipboard.

KML code for the function selected appears in the text editor. (Make sure to use a text editor that does not add formatting additional or information to the file text).

Save the file with an extension. kml .

In the next article we will discuss the two tags KML flagship : < Placemark > and<description>.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *