# KML - Keyhole Markup Language

Keyhole Markup Language (KML) is an XML-based language for managing the display of 3D geospatial data. KML has been submitted as an OGC standard candidate as of this writing, and is supported in one way or another on the major GeoBrowsers. Note that KML by specification uses only a single projection, EPSG:4326. All OGR KML output will be presented in EPSG:4326.

At this time, only vector layers are handled by the KML driver. (there are additional scripts supplied with the GDAL project that can build other kinds of output)

### KML Reading

KML reading is only available if GDAL/OGR is built with the Expat XML Parser, otherwise only KML writing will be supported.

Currently supported geometry types are `Point`, `Linestring` and `Polygon`. There are other limitations as well, for example: nested folders in a source KML file are disregarded; folder `<description>` tags will not carry through to ouput; folders containing multiple geometry types, like `POINT` and `POLYGON`, will be disregarded. The last example can be a source of confusion. If the only Folder in a KML source file contains mixed or unsupported geometry types, the Folder will be rejected and the driver will report 'no layers found.'

### KML Writing

Since not all features of KML are able to be represented in the Simple Features geometry model, you will not be able to generate many KML-specific attributes from within GDAL/OGR. Please try a few test files to get a sense of what is possible.

When outputting KML, the OGR KML driver will translate each OGR Layer into a KML Folder. (you may encounter unexpected behavior if you try to mix the geometry types of elements in a layer, e.g. `LINESTRING` and `POINT` data.

The KML Driver will rename some layers, or source KML folder names, into new names it considers valid, for example '`Layer #0`', the default name of the first unnamed Layer, becomes `'Layer__0'`.

KML is mix of formatting and feature data. The <description> tag of a Placemark will be displayed in most geobrowsers as an HTML-filled balloon. When writing KML, Layer element attributes are added as description tag content with the following HTML structure:

    Attribute name: Attribute content

Limited support is available for fills, line color and other styling attributes. Please try a few sample files to get a better sense of actual behavior.

### Creation Options

The following creation options are supported:

- NameField: Allows you to specify the field to use for the KML <name> element. Default value : 'Name'

    ogr2ogr -f KML output.kml input.shp -dsco NameField=RegionName

- DescriptionField: Allows you to specify the field to use for the KML <description> element. Default value : 'Description'
- AltitudeMode: Allows you to specify the AltitudeMode to use for KML geometries. This will only affect 3D geometries and must be one of the valid KML options. See the relevant KML reference material for further information.

      ogr2ogr -f KML output.kml input.shp -dsco AltitudeMode=absolute

## Example

The ogr2ogr utility can be used to dump the results of a PostGIS query to KML:

    ogr2ogr -f KML output.kml PG:'host=myserver dbname=warmerda' -sql "SELECT pop_1994 from canada where province_name = 'Alberta'"

How to dump contents of .kml file as OGR sees it:

    ogrinfo -ro somedisplay.kml

## Caveats

Google Earth seems to have some limits regarding the number of coordinates in complex geometries like polygons. If the problem appears, then problematic geometries are displayed completely or partially covered by vertical stripes. Unfortunately, there are no exact number given in the KML specification about this limitation, so the KML driver will not warn about potential problems. One of possible and tested solutions is to simplify a line or a polygon to remove some coordinates. Here is the whole discussion about this issue on the Google KML Developer Forum, in the polygon displays with vertical stripes thread.

## See Also

- KML Specification
- KML Tutorial