1.. _vector.libkml:
2
3LIBKML Driver (.kml .kmz)
4=========================
5
6.. shortname:: LIBKML
7
8.. build_dependencies:: libkml
9
10The LIBKML driver is a client of
11`Libkml <https://github.com/libkml/libkml>`__ , a reference
12implementation of `KML <http://www.opengeospatial.org/standards/kml/>`__
13reading and writing, in the form of a cross platform C++ library. You
14must build and install Libkml in order to use this OGR driver. Note: you
15need to build libkml 1.3 or master.
16
17Note that if you build and include this LIBKML driver, it will become
18the default reader of KML for ogr, overriding the previous :ref:`KML
19driver <vector.kml>`. You can still specify either KML or LIBKML as
20the output driver via the command line
21
22Libkml from Google provides reading services for any valid KML file.
23However, please be advised that some KML facilities do not map into the
24Simple Features specification OGR uses as its internal structure.
25Therefore, a best effort will be made by the driver to understand the
26content of a KML file read by libkml into ogr, but your mileage may
27vary. Please try a few KML files as samples to get a sense of what is
28understood. In particular, nesting of feature sets more than one deep
29will be flattened to support ogr's internal format.
30
31Driver capabilities
32-------------------
33
34.. supports_create::
35
36.. supports_georeferencing::
37
38.. supports_virtualio::
39
40Datasource
41----------
42
43You may specify a datasource
44as a kml file ``somefile.kml`` , a directory ``somedir/`` , or a kmz
45file ``somefile.kmz`` .
46
47By default on directory and kmz datasources, an index file of all the
48layers will be read from or written to doc.kml. It contains a
49`<NetworkLink> <https://developers.google.com/kml/documentation/kmlreference#networklink>`__
50to each layer file in the datasource. This feature can be turned off by
51setting the environment variable LIBKML_USE_DOC.KML to "no"
52
53StyleTable
54~~~~~~~~~~
55
56Datasource style tables are written to the
57`<Document> <https://developers.google.com/kml/documentation/kmlreference#document>`__
58in a .kml, style/style.kml in a kmz file, or style.kml in a directory,
59as one or more
60`<Style> <https://developers.google.com/kml/documentation/kmlreference#style>`__
61elements. Not all of :ref:`ogr_feature_style` can translate into
62KML.
63
64Datasource creation options
65~~~~~~~~~~~~~~~~~~~~~~~~~~~
66
67The following datasource creation options can be
68used to generate a
69`<atom:Author> <https://developers.google.com/kml/documentation/kmlreference#atomauthor>`__
70element at the top Document level.
71
72-  **AUTHOR_NAME**
73-  **AUTHOR_URI**
74-  **AUTHOR_EMAIL**
75
76The href of an
77`<atom:link> <https://developers.google.com/kml/documentation/kmlreference#atomlink>`__
78element at the top Document level can be specified with the **LINK**
79creation option.
80
81The
82`<phoneNumber> <https://developers.google.com/kml/documentation/kmlreference#phonenumber>`__
83element at the top Document level can be specified with the
84**PHONENUMBER** creation option. The value must follow the syntax of
85`IETF RFC 3966 <http://tools.ietf.org/html/rfc3966>`__.
86
87Starting with GDAL 2.2, the DOCUMENT_ID datasource creation option can
88be used to specified the id of the root <Document> node. The default
89value is root_doc.
90
91Container properties
92^^^^^^^^^^^^^^^^^^^^
93
94The following dataset creation options can be used to set container
95options :
96
97-  **NAME**:
98   `<name> <https://developers.google.com/kml/documentation/kmlreference#name>`__
99   element
100-  **VISIBILITY**:
101   `<visibility> <https://developers.google.com/kml/documentation/kmlreference#visibility>`__
102   element
103-  **OPEN**:
104   `<open> <https://developers.google.com/kml/documentation/kmlreference#open>`__
105   element
106-  **SNIPPET**:
107   `<snippet> <https://developers.google.com/kml/documentation/kmlreference#snippet>`__
108   element
109-  **DESCRIPTION**:
110   `<description> <https://developers.google.com/kml/documentation/kmlreference#description>`__
111   element
112
113List style
114^^^^^^^^^^
115
116The following dataset creation options can be used to control how the
117main folder (folder of layers) appear in the Places panel of the Earth
118browser, trough a
119`<ListStyle> <https://developers.google.com/kml/documentation/kmlreference#liststyle>`__
120element:
121
122-  **LISTSTYLE_TYPE**: can be one of "check", "radioFolder",
123   "checkOffOnly" or "checkHideChildren". Sets the
124   `<listItemType> <https://developers.google.com/kml/documentation/kmlreference#listItemType>`__
125   element.
126-  **LISTSTYLE_ICON_HREF**: URL of the icon to display for the main
127   folder. Sets the href element of the
128   `<ItemIcon> <https://developers.google.com/kml/documentation/kmlreference#itemicon>`__
129   element.
130
131Balloon style
132^^^^^^^^^^^^^
133
134If a style *foo* is defined, it is possible to add a
135`<BalloonStyle> <https://developers.google.com/kml/documentation/kmlreference#balloonstyle>`__
136element to it, by specifying the **foo_BALLOONSTYLE_BGCOLOR** and/or
137**foo_BALLOONSTYLE_TEXT** elements.
138
139NetworkLinkControl
140^^^^^^^^^^^^^^^^^^
141
142A
143`<NetworkLinkControl> <https://developers.google.com/kml/documentation/kmlreference#networklinkcontrol>`__
144element can be defined if at least one of the following dataset creation
145option is specified:
146
147-  **NLC_MINREFRESHPERIOD** : to set the
148   `<minRefreshPeriod> <https://developers.google.com/kml/documentation/kmlreference#minrefreshperiod>`__
149   element
150-  **NLC_MAXSESSIONLENGTH** : to set the
151   `<maxSessionLength> <https://developers.google.com/kml/documentation/kmlreference#maxsessionlength>`__
152   element
153-  **NLC_COOKIE** : to set the
154   `<cookie> <https://developers.google.com/kml/documentation/kmlreference#cookie>`__
155   element
156-  **NLC_MESSAGE** : to set the
157   `<message> <https://developers.google.com/kml/documentation/kmlreference#message>`__
158   element
159-  **NLC_LINKNAME** : to set the
160   `<linkName> <https://developers.google.com/kml/documentation/kmlreference#linkname>`__
161   element
162-  **NLC_LINKDESCRIPTION** : to set the
163   `<linkDescription> <https://developers.google.com/kml/documentation/kmlreference#linkdescription>`__
164   element
165-  **NLC_LINKSNIPPET** : to set the
166   `<linkSnippet> <https://developers.google.com/kml/documentation/kmlreference#linksnippet>`__
167   element
168-  **NLC_EXPIRES** : to set the
169   `<expires> <https://developers.google.com/kml/documentation/kmlreference#expires>`__
170   element
171
172Update documents
173^^^^^^^^^^^^^^^^
174
175When defining the dataset creation option **UPDATE_TARGETHREF**, a
176NetworkLinkControl KML file with an
177`<Update> <https://developers.google.com/kml/documentation/kmlreference#update>`__
178element will be generated. See the `tutorial about
179update <https://developers.google.com/kml/documentation/updates>`__.
180
181The CreateFeature() operation on a layer will be translated as a
182`<Create> <https://developers.google.com/kml/documentation/kmlreference#create>`__
183element.
184
185The SetFeature() operation on a layer will be translated as a
186`<Change> <https://developers.google.com/kml/documentation/kmlreference#change>`__
187element.
188
189The DeleteFeature() operation on a layer will be translated as a
190`<Delete> <https://developers.google.com/kml/documentation/kmlreference#delete>`__
191element.
192
193Layer
194-----
195
196:cpp:class:`OGRLayer` are mapped
197to kml files as a
198`<Document> <https://developers.google.com/kml/documentation/kmlreference#document>`__
199or
200`<Folder> <https://developers.google.com/kml/documentation/kmlreference#folder>`__,
201and in kmz files or directories as a separate kml file.
202
203Style
204~~~~~
205
206Layer style tables can not be read from or written to a kml layer that
207is a
208`<Folder> <https://developers.google.com/kml/documentation/kmlreference#folder>`__,
209otherwise they are in the
210`<Document> <https://developers.google.com/kml/documentation/kmlreference#document>`__
211that is the layer.
212
213Schema
214~~~~~~
215
216Read and write of
217`<Schema> <https://developers.google.com/kml/documentation/kmlreference#schema>`__
218is supported for .kml files, .kmz files, and directories.
219
220Layer creation options
221~~~~~~~~~~~~~~~~~~~~~~
222
223The following layer creation options can be used
224to generate a
225`<LookAt> <https://developers.google.com/kml/documentation/kmlreference#lookat>`__
226element at the layer level.
227
228-  **LOOKAT_LONGITUDE** (required)
229-  **LOOKAT_LATITUDE** (required)
230-  **LOOKAT_RANGE** (required)
231-  **LOOKAT_HEADING**
232-  **LOOKAT_TILT**
233-  **LOOKAT_ALTITUDE**
234-  **LOOKAT_ALTITUDEMODE**
235
236Alternatively, a
237`<Camera> <https://developers.google.com/kml/documentation/kmlreference#camera>`__
238element can be generated.
239
240-  **CAMERA_LONGITUDE** (required)
241-  **CAMERA_LATITUDE** (required)
242-  **CAMERA_ALTITUDE** (required)
243-  **CAMERA_ALTITUDEMODE** (required)
244-  **CAMERA_HEADING**
245-  **CAMERA_TILT**
246-  **CAMERA_ROLL**
247
248A
249`<Region> <https://developers.google.com/kml/documentation/kmlreference#region>`__
250element can be generated to control when objects of the layer are
251visible or not. If REGION_XMIN, REGION_YMIN, REGION_XMAX and
252REGION_YMAX, the region coordinates are determined from the spatial
253extent of the features being written in the layer.
254
255-  **ADD_REGION=YES/NO** : defaults to NO
256-  **REGION_XMIN** (optional) : defines the west coordinate of the
257   region.
258-  **REGION_YMIN** (optional) : defines the south coordinate of the
259   region.
260-  **REGION_XMAX** (optional) : defines the east coordinate of the
261   region.
262-  **REGION_YMAX** (optional) : defines the north coordinate of the
263   region.
264-  **REGION_MIN_LOD_PIXELS** (optional) : minimum size in pixels of the
265   region so that it is displayed. Defaults to 256.
266-  **REGION_MAX_LOD_PIXELS** (optional) : maximum size in pixels of the
267   region so that it is displayed. Defaults to -1 (infinite).
268-  **REGION_MIN_FADE_EXTENT** (optional) : distance over which the
269   geometry fades, from fully opaque to fully transparent. Defaults to
270   0.
271-  **REGION_MAX_FADE_EXTENT** (optional) : distance over which the
272   geometry fades, from fully transparent to fully opaque. Defaults to
273   0.
274
275A
276`<ScreenOverlay> <https://developers.google.com/kml/documentation/kmlreference#screenoverlay>`__
277element can be added to display a logo, a legend, etc...
278
279-  **SO_HREF** (required) : URL of the image to display.
280-  **SO_NAME** (optional)
281-  **SO_DESCRIPTION** (optional)
282-  **SO_OVERLAY_X** (optional)
283-  **SO_OVERLAY_Y** (optional)
284-  **SO_OVERLAY_XUNITS** (optional)
285-  **SO_OVERLAY_YUNITS** (optional)
286-  **SO_SCREEN_X** (optional). Defaults to 0.05
287-  **SO_SCREEN_Y** (optional). Defaults to 0.05
288-  **SO_SCREEN_XUNITS** (optional). Defaults to Fraction
289-  **SO_SCREEN_YUNITS** (optional). Defaults to Fraction
290-  **SO_SIZE_X** (optional)
291-  **SO_SIZE_Y** (optional)
292-  **SO_SIZE_XUNITS** (optional)
293-  **SO_SIZE_YUNITS** (optional)
294
295By default, layers are written as
296`<Document> <https://developers.google.com/kml/documentation/kmlreference#document>`__
297elements. By settings the **FOLDER** layer creation option to YES, it is
298also possible to write them as
299`<Folder> <https://developers.google.com/kml/documentation/kmlreference#folder>`__
300elements (only in .kml files).
301
302The following layer creation options can be used to set container
303options :
304
305-  **NAME**:
306   `<name> <https://developers.google.com/kml/documentation/kmlreference#name>`__
307   element
308-  **VISIBILITY**:
309   `<visibility> <https://developers.google.com/kml/documentation/kmlreference#visibility>`__
310   element
311-  **OPEN**:
312   `<open> <https://developers.google.com/kml/documentation/kmlreference#open>`__
313   element
314-  **SNIPPET**:
315   `<snippet> <https://developers.google.com/kml/documentation/kmlreference#snippet>`__
316   element
317-  **DESCRIPTION**:
318   `<description> <https://developers.google.com/kml/documentation/kmlreference#description>`__
319   element
320
321The following layer creation options can be used to control how the
322folder of a layer appear in the Places panel of the Earth browser,
323trough a
324`<ListStyle> <https://developers.google.com/kml/documentation/kmlreference#liststyle>`__
325element:
326
327-  **LISTSTYLE_TYPE**: can be one of "check", "radioFolder",
328   "checkOffOnly" or "checkHideChildren". Sets the
329   `<listItemType> <https://developers.google.com/kml/documentation/kmlreference#listItemType>`__
330   element.
331-  **LISTSTYLE_ICON_HREF**: URL of the icon to display for the layer
332   folder. Sets the href element of the
333   `<ItemIcon> <https://developers.google.com/kml/documentation/kmlreference#itemicon>`__
334   element.
335
336Feature
337-------
338
339An :cpp:class:`OGRFeature`
340generally translates to kml as a
341`<Placemark> <https://developers.google.com/kml/documentation/kmlreference#placemark>`__,
342and vice-versa.
343
344If the model field is defined, a
345`<Model> <https://developers.google.com/kml/documentation/kmlreference#model>`__
346object within the Placemark will be generated.
347
348If the networklink field is defined, a
349`<NetworkLink> <https://developers.google.com/kml/documentation/kmlreference#networklink>`__
350will be generated. Other networklink fields are optional.
351
352If the photooverlay field is defined, a
353`<PhotoOverlay> <https://developers.google.com/kml/documentation/kmlreference#photooverlay>`__
354will be generated (provided that the camera_longitude, camera_latitude,
355camera_altitude, camera_altitudemode, head and/or tilt and/or roll,
356leftfov, rightfov, bottomfov, topfov, near fields are also set. The
357shape field is optional.
358
359In case the PhotoOverlay is a big image, it is highly recommended to
360tile it and generate overview levels, as explained in the `PhotoOverlay
361tutorial <https://developers.google.com/kml/documentation/photos>`__. In
362which case, the URL should contain the "$[level]", "$[x]" and "$[y]"
363sub-strings in the photooverlay field, and the imagepyramid_tilesize,
364imagepyramid_maxwidth, imagepyramid_maxheight and
365imagepyramid_gridorigin fields should be set.
366
367Placemark, Model, NetworkLink and PhotoOverlay objects can have an
368associated camera if the camera_longitude, camera_latitude,
369camera_altitude, camera_altitudemode, head and/or tilt and/or roll
370fields are defined.
371
372KML `<GroundOverlay> <https://developers.google.com/kml/documentation/kmlreference#groundoverlay>`__
373elements are supported for reading (unless the
374LIBKML_READ_GROUND_OVERLAY configuration option is set to FALSE). For
375such elements, there are icon and drawOrder fields.
376
377.. _style-1:
378
379Style
380~~~~~
381
382Style Strings at the feature level are Mapped to KML as either a
383`<Style> <https://developers.google.com/kml/documentation/kmlreference#style>`__
384or
385`<StyleUrl> <https://developers.google.com/kml/documentation/kmlreference#styleurl>`__
386in each
387`<Placemark> <https://developers.google.com/kml/documentation/kmlreference#placemark>`__.
388
389When reading a kml feature and the environment variable
390LIBKML_RESOLVE_STYLE is set to yes, styleurls are looked up in the style
391tables and the features style string is set to the style from the table.
392This is to allow reading of shared styles by applications, like
393mapserver, that do not read style tables.
394
395When reading a kml feature and the environment variable
396LIBKML_EXTERNAL_STYLE is set to yes, a styleurl that is external to the
397datasource is read from disk or fetched from the server and parsed into
398the datasource style table. If the style kml can not be read or
399LIBKML_EXTERNAL_STYLE is set to no then the styleurl is copied to the
400style string.
401
402When reading a kml StyleMap the default mapping is set to normal. If you
403wish to use the highlighted styles set the environment variable
404LIBKML_STYLEMAP_KEY to "highlight"
405
406When writing a kml, if there exist 2 styles of the form
407"astylename_normal" and "astylename_highlight" (where astylename is any
408string), then a StyleMap object will be creating from both styles and
409called "astylename".
410
411Fields
412------
413
414OGR fields (feature attributes) are mapped to kml with
415`<Schema> <https://developers.google.com/kml/documentation/kmlreference#schema>`__;
416and
417`<SimpleData> <https://developers.google.com/kml/documentation/kmlreference#simpledata>`__,
418except for some special fields as noted below.
419
420Note: it is also possible to export fields as
421`<Data> <https://developers.google.com/kml/documentation/kmlreference#data>`__
422elements if the LIBKML_USE_SCHEMADATA configuration option is set to NO.
423
424A rich set of environment variables are available to define how fields
425in input and output, map to a KML
426`<Placemark> <https://developers.google.com/kml/documentation/kmlreference#placemark>`__.
427For example, if you want a field called 'Cities' to map to the
428`<name> <https://developers.google.com/kml/documentation/kmlreference#name>`__;
429tag in KML, you can set an environment variable.
430
431Name
432   This String field maps to the kml tag
433   `<name> <https://developers.google.com/kml/documentation/kmlreference#name>`__.
434   The name of the ogr field can be changed with the environment
435   variable LIBKML_NAME_FIELD .
436description
437   This String field maps to the kml tag
438   `<description> <https://developers.google.com/kml/documentation/kmlreference#description>`__.
439   The name of the ogr field can be changed with the environment
440   variable LIBKML_DESCRIPTION_FIELD .
441timestamp
442   This string or datetime or date and/or time field maps to the kml tag
443   `<timestamp> <https://developers.google.com/kml/documentation/kmlreference#timestamp>`__.
444   The name of the ogr field can be changed with the environment
445   variable LIBKML_TIMESTAMP_FIELD .
446begin
447   This string or datetime or date and/or time field maps to the kml tag
448   `<begin> <https://developers.google.com/kml/documentation/kmlreference#begin>`__.
449   The name of the ogr field can be changed with the environment
450   variable LIBKML_BEGIN_FIELD .
451end
452   This string or datetime or date and/or time field maps to the kml tag
453   `<end> <https://developers.google.com/kml/documentation/kmlreference#end>`__.
454   The name of the ogr field can be changed with the environment
455   variable LIBKML_END_FIELD .
456altitudeMode
457   This string field maps to the kml tag
458   `<altitudeMode> <https://developers.google.com/kml/documentation/kmlreference#altitudemode>`__
459   or
460   `<gx:altitudeMode> <https://developers.google.com/kml/documentation/kmlreference#gxaltitudemode>`__.
461   The name of the ogr field can be changed with the environment
462   variable LIBKML_ALTITUDEMODE_FIELD .
463tessellate
464   This integer field maps to the kml tag
465   `<tessellate> <https://developers.google.com/kml/documentation/kmlreference#tessellate>`__.
466   The name of the ogr field can be changed with the environment
467   variable LIBKML_TESSELLATE_FIELD .
468extrude
469   This integer field maps to the kml tag
470   `<extrude> <https://developers.google.com/kml/documentation/kmlreference#extrude>`__.
471   The name of the ogr field can be changed with the environment
472   variable LIBKML_EXTRUDE_FIELD .
473visibility
474   This integer field maps to the kml tag
475   `<visibility> <https://developers.google.com/kml/documentation/kmlreference#visibility>`__.
476   The name of the ogr field can be changed with the environment
477   variable LIBKML_VISIBILITY_FIELD .
478icon
479   This string field maps to the kml tag
480   `<icon> <https://developers.google.com/kml/documentation/kmlreference#icon>`__.
481   The name of the ogr field can be changed with the environment
482   variable LIBKML_ICON_FIELD .
483drawOrder
484   This integer field maps to the kml tag
485   `<drawOrder> <https://developers.google.com/kml/documentation/kmlreference#draworder>`__.
486   The name of the ogr field can be changed with the environment
487   variable LIBKML_DRAWORDER_FIELD .
488snippet
489   This integer field maps to the kml tag
490   `<snippet> <https://developers.google.com/kml/documentation/kmlreference#snippet>`__.
491   The name of the ogr field can be changed with the environment
492   variable LIBKML_SNIPPET_FIELD .
493heading
494   This real field maps to the kml tag
495   `<heading> <https://developers.google.com/kml/documentation/kmlreference#heading>`__.
496   The name of the ogr field can be changed with the environment
497   variable LIBKML_HEADING_FIELD. When reading, this field is present
498   only if a Placemark has a Camera with a heading element.
499tilt
500   This real field maps to the kml tag
501   `<tilt> <https://developers.google.com/kml/documentation/kmlreference#tilt>`__.
502   The name of the ogr field can be changed with the environment
503   variable LIBKML_TILT_FIELD. When reading, this field is present only
504   if a Placemark has a Camera with a tilt element.
505roll
506   This real field maps to the kml tag
507   `<roll> <https://developers.google.com/kml/documentation/kmlreference#roll>`__.
508   The name of the ogr field can be changed with the environment
509   variable LIBKML_ROLL_FIELD. When reading, this field is present only
510   if a Placemark has a Camera with a roll element.
511model
512   This string field can be used to define the URL of a 3D
513   `<model> <https://developers.google.com/kml/documentation/kmlreference#model>`__.
514   The name of the ogr field can be changed with the environment
515   variable LIBKML_MODEL_FIELD.
516scale_x
517   This real field maps to the x element of the kml tag
518   `<scale> <https://developers.google.com/kml/documentation/kmlreference#scale>`__
519   for a 3D model. The name of the ogr field can be changed with the
520   environment variable LIBKML_SCALE_X_FIELD.
521scale_y
522   This real field maps to the y element of the kml tag
523   `<scale> <https://developers.google.com/kml/documentation/kmlreference#scale>`__\ for
524   a 3D model. The name of the ogr field can be changed with the
525   environment variable LIBKML_SCALE_Y_FIELD.
526scale_z
527   This real field maps to the z element of the kml tag
528   `<scale> <https://developers.google.com/kml/documentation/kmlreference#scale>`__\ for
529   a 3D model. The name of the ogr field can be changed with the
530   environment variable LIBKML_SCALE_Z_FIELD.
531networklink
532   This string field maps to the href element of the kml tag
533   `<href> <https://developers.google.com/kml/documentation/kmlreference#href>`__
534   of a NetworkLink. The name of the ogr field can be changed with the
535   environment variable LIBKML_NETWORKLINK_FIELD.
536networklink_refreshvisibility
537   This integer field maps to kml tag
538   `<refreshVisibility> <https://developers.google.com/kml/documentation/kmlreference#refreshvisibility>`__
539   of a NetworkLink. The name of the ogr field can be changed with the
540   environment variable LIBKML_NETWORKLINK_REFRESHVISIBILITY_FIELD.
541networklink_flytoview
542   This integer field maps to kml tag
543   `<flyToView> <https://developers.google.com/kml/documentation/kmlreference#flytoview>`__
544   of a NetworkLink. The name of the ogr field can be changed with the
545   environment variable LIBKML_NETWORKLINK_FLYTOVIEW_FIELD.
546networklink_refreshmode
547   This string field maps to kml tag
548   `<refreshMode> <https://developers.google.com/kml/documentation/kmlreference#refreshmode>`__
549   of a NetworkLink. The name of the ogr field can be changed with the
550   environment variable LIBKML_NETWORKLINK_REFRESHMODE_FIELD.
551networklink_refreshinterval
552   This real field maps to kml tag
553   `<refreshInterval> <https://developers.google.com/kml/documentation/kmlreference#refreshinterval>`__
554   of a NetworkLink. The name of the ogr field can be changed with the
555   environment variable LIBKML_NETWORKLINK_REFRESHINTERVAL_FIELD.
556networklink_viewrefreshmode
557   This string field maps to kml tag
558   `<viewRefreshMode> <https://developers.google.com/kml/documentation/kmlreference#viewrefreshmode>`__
559   of a NetworkLink. The name of the ogr field can be changed with the
560   environment variable LIBKML_NETWORKLINK_VIEWREFRESHMODE_FIELD.
561networklink_viewrefreshtime
562   This real field maps to kml tag
563   `<viewRefreshTime> <https://developers.google.com/kml/documentation/kmlreference#viewrefreshtime>`__
564   of a NetworkLink. The name of the ogr field can be changed with the
565   environment variable LIBKML_NETWORKLINK_VIEWREFRESHTIME_FIELD.
566networklink_viewboundscale
567   This real field maps to kml tag
568   `<viewBoundScale> <https://developers.google.com/kml/documentation/kmlreference#viewboundscale>`__
569   of a NetworkLink. The name of the ogr field can be changed with the
570   environment variable LIBKML_NETWORKLINK_VIEWBOUNDSCALE_FIELD.
571networklink_viewformat
572   This string field maps to kml tag
573   `<viewFormat> <https://developers.google.com/kml/documentation/kmlreference#viewformat>`__
574   of a NetworkLink. The name of the ogr field can be changed with the
575   environment variable LIBKML_NETWORKLINK_VIEWFORMAT_FIELD.
576networklink_httpquery
577   This string field maps to kml tag
578   `<httpQuery> <https://developers.google.com/kml/documentation/kmlreference#httpquery>`__
579   of a NetworkLink. The name of the ogr field can be changed with the
580   environment variable LIBKML_NETWORKLINK_HTTPQUERY_FIELD.
581camera_longitude
582   This real field maps to kml tag
583   `<longitude> <https://developers.google.com/kml/documentation/kmlreference#longitude>`__
584   of a
585   `<Camera> <https://developers.google.com/kml/documentation/kmlreference#camera>`__.
586   The name of the ogr field can be changed with the environment
587   variable LIBKML_CACameraMERA_LONGITUDE_FIELD.
588camera_latitude
589   This real field maps to kml tag
590   `<latitude> <https://developers.google.com/kml/documentation/kmlreference#latitude>`__
591   of a
592   `<Camera> <https://developers.google.com/kml/documentation/kmlreference#camera>`__.
593   The name of the ogr field can be changed with the environment
594   variable LIBKML_CAMERA_LATITUDE_FIELD.
595camera_altitude
596   This real field maps to kml tag
597   `<altitude> <https://developers.google.com/kml/documentation/kmlreference#altitude>`__
598   of a
599   `<Camera> <https://developers.google.com/kml/documentation/kmlreference#camera>`__.
600   The name of the ogr field can be changed with the environment
601   variable LIBKML_CAMERA_ALTITUDE_FIELD.
602camera_altitudemode
603   This real field maps to kml tag
604   `<altitudeMode> <https://developers.google.com/kml/documentation/kmlreference#altitudemode>`__
605   of a
606   `<Camera> <https://developers.google.com/kml/documentation/kmlreference#camera>`__.
607   The name of the ogr field can be changed with the environment
608   variable LIBKML_CAMERA_ALTITUDEMODE_FIELD.
609photooverlay
610   This string field maps to the href element of the kml tag
611   `<href> <https://developers.google.com/kml/documentation/kmlreference#href>`__
612   of a
613   `<PhotoOverlay> <https://developers.google.com/kml/documentation/kmlreference#photooverlay>`__.
614   The name of the ogr field can be changed with the environment
615   variable LIBKML_PHOTOOVERLAY_FIELD.
616leftfov
617   This real field maps to to kml tag
618   `<LeftFov> <https://developers.google.com/kml/documentation/kmlreference#leftfov>`__
619   of a
620   `<PhotoOverlay> <https://developers.google.com/kml/documentation/kmlreference#photooverlay>`__.
621   The name of the ogr field can be changed with the environment
622   variable LIBKML_LEFTFOV_FIELD.
623rightfov
624   This real field maps to to kml tag
625   `<RightFov> <https://developers.google.com/kml/documentation/kmlreference#rightfov>`__
626   of a
627   `<PhotoOverlay> <https://developers.google.com/kml/documentation/kmlreference#photooverlay>`__.
628   The name of the ogr field can be changed with the environment
629   variable LIBKML_RightFOV_FIELD.
630bottomfov
631   This real field maps to to kml tag
632   `<BottomFov> <https://developers.google.com/kml/documentation/kmlreference#bottomfov>`__
633   of a
634   `<PhotoOverlay> <https://developers.google.com/kml/documentation/kmlreference#photooverlay>`__.
635   The name of the ogr field can be changed with the environment
636   variable LIBKML_BOTTOMTFOV_FIELD.
637topfov
638   This real field maps to to kml tag
639   `<TopFov> <https://developers.google.com/kml/documentation/kmlreference#topfov>`__
640   of a
641   `<PhotoOverlay> <https://developers.google.com/kml/documentation/kmlreference#photooverlay>`__.
642   The name of the ogr field can be changed with the environment
643   variable LIBKML_TOPFOV_FIELD.
644near
645   This real field maps to to kml tag
646   `<Near> <https://developers.google.com/kml/documentation/kmlreference#leftfov>`__
647   of a
648   `<PhotoOverlay> <https://developers.google.com/kml/documentation/kmlreference#photooverlay>`__.
649   The name of the ogr field can be changed with the environment
650   variable LIBKML_NEAR_FIELD.
651shape
652   This string field maps to to kml tag
653   `<shape> <https://developers.google.com/kml/documentation/kmlreference#shape>`__
654   of a
655   `<PhotoOverlay> <https://developers.google.com/kml/documentation/kmlreference#photooverlay>`__.
656   The name of the ogr field can be changed with the environment
657   variable LIBKML_SHAPE_FIELD.
658imagepyramid_tilesize
659   This integer field maps to to kml tag
660   `<tileSize> <https://developers.google.com/kml/documentation/kmlreference#tilesize>`__
661   of a
662   `<ImagePyramid> <https://developers.google.com/kml/documentation/kmlreference#imagepyramid>`__.
663   The name of the ogr field can be changed with the environment
664   variable LIBKML_IMAGEPYRAMID_TILESIZE.
665imagepyramid_maxwidth
666   This integer field maps to to kml tag
667   `<maxWidth> <https://developers.google.com/kml/documentation/kmlreference#maxwidth>`__
668   of a
669   `<ImagePyramid> <https://developers.google.com/kml/documentation/kmlreference#imagepyramid>`__.
670   The name of the ogr field can be changed with the environment
671   variable LIBKML_IMAGEPYRAMID_MAXWIDTH.
672imagepyramid_maxheight
673   This integer field maps to to kml tag
674   `<maxHeight> <https://developers.google.com/kml/documentation/kmlreference#maxheight>`__
675   of a
676   `<ImagePyramid> <https://developers.google.com/kml/documentation/kmlreference#imagepyramid>`__.
677   The name of the ogr field can be changed with the environment
678   variable LIBKML_IMAGEPYRAMID_MAXHEIGHT.
679imagepyramid_gridorigin
680   This string field maps to to kml tag
681   `<gridOrigin> <https://developers.google.com/kml/documentation/kmlreference#maxheight>`__
682   of a
683   `<ImagePyramid> <https://developers.google.com/kml/documentation/kmlreference#imagepyramid>`__.
684   The name of the ogr field can be changed with the environment
685   variable LIBKML_IMAGEPYRAMID_GRIDORIGIN.
686OGR_STYLE
687   This string field maps to a features style string, OGR reads this
688   field if there is no style string set on the feature.
689
690Geometry
691--------
692
693Translation of :cpp:class:`OGRGeometry` to
694KML Geometry is pretty strait forwards with only a couple of exceptions.
695Point to
696`<Point> <https://developers.google.com/kml/documentation/kmlreference#point>`__
697(unless heading and/or tilt and/or roll field names are found, in which
698case a
699`Camera <https://developers.google.com/kml/documentation/kmlreference#camera>`__
700object will be generated), LineString to
701`<LineString> <https://developers.google.com/kml/documentation/kmlreference#linestring>`__,
702LinearRing to
703`<LinearRing> <https://developers.google.com/kml/documentation/kmlreference#linearring>`__,
704and Polygon to
705`<Polygon> <https://developers.google.com/kml/documentation/kmlreference#polygon>`__.
706In OGR a polygon contains an array of LinearRings, the first one being
707the outer ring. KML has the tags  
708`<outerBoundaryIs> <https://developers.google.com/kml/documentation/kmlreference#outerboundaryis>`__ and 
709`<innerBoundaryIs> <https://developers.google.com/kml/documentation/kmlreference#innerboundaryis>`__ to
710differentiate between the two. OGR has several Multi types of geometry :
711GeometryCollection, MultiPolygon, MultiPoint, and MultiLineString. When
712possible, OGR will try to map
713`<MultiGeometry> <https://developers.google.com/kml/documentation/kmlreference#multigeometry>`__
714to the more precise OGR geometry type (MultiPoint, MultiLineString or
715MultiPolygon), and default to GeometryCollection in case of mixed
716content.
717
718Sometimes kml geometry will span the dateline, In applications like qgis
719or mapserver this will create horizontal lines all the way around the
720globe. Setting the environment variable LIBKML_WRAPDATELINE to "yes"
721will cause the libkml driver to split the geometry at the dateline when
722read.
723
724VSI Virtual File System API support
725-----------------------------------
726
727The driver supports reading and writing to files managed by VSI Virtual
728File System API, which include "regular" files, as well as files in the
729/vsizip/ (read-write) , /vsigzip/ (read-write) , /vsicurl/ (read-only)
730domains.
731
732Writing to /dev/stdout or /vsistdout/ is also supported.
733
734Example
735-------
736
737The following bash script will build a
738:ref:`csv <vector.csv>` file and a
739:ref:`vrt <vector.vrt>` file, and then translate them
740to KML using :ref:`ogr2ogr` into a .kml
741file with timestamps and styling.
742
743::
744
745
746
747   #!/bin/bash
748   # Copyright (c) 2010, Brian Case
749   #
750   # Permission is hereby granted, free of charge, to any person obtaining a
751   # copy of this software and associated documentation files (the "Software"),
752   # to deal in the Software without restriction, including without limitation
753   # the rights to use, copy, modify, merge, publish, distribute, sublicense,
754   # and/or sell copies of the Software, and to permit persons to whom the
755   # Software is furnished to do so, subject to the following conditions:
756   #
757   # The above copyright notice and this permission notice shall be included
758   # in all copies or substantial portions of the Software.
759   #
760   # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
761   # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
762   # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
763   # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
764   # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
765   # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
766   # DEALINGS IN THE SOFTWARE.
767
768
769   icon="http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png"
770   rgba33="#FF9900"
771   rgba70="#FFFF00"
772   rgba150="#00FF00"
773   rgba300="#0000FF"
774   rgba500="#9900FF"
775   rgba800="#FF0000"
776
777   function docsv {
778
779       IFS=','
780
781       while read Date Time Lat Lon Mag Dep
782       do
783           ts=$(echo $Date | sed 's:/:-:g')T${Time%%.*}Z
784           rgba=""
785
786           if [[ $rgba == "" ]] && [[ $Dep -lt 33 ]]
787           then
788               rgba=$rgba33
789           fi
790
791           if [[ $rgba == "" ]] && [[ $Dep -lt 70 ]]
792           then
793               rgba=$rgba70
794           fi
795
796           if [[ $rgba == "" ]] && [[ $Dep -lt 150 ]]
797           then
798               rgba=$rgba150
799           fi
800
801           if [[ $rgba == "" ]] && [[ $Dep -lt 300 ]]
802           then
803               rgba=$rgba300
804           fi
805
806           if [[ $rgba == "" ]] && [[ $Dep -lt 500 ]]
807           then
808               rgba=$rgba500
809           fi
810
811           if [[ $rgba == "" ]]
812           then
813               rgba=$rgba800
814           fi
815
816
817
818           style="\"SYMBOL(s:$Mag,id:\"\"$icon\"\",c:$rgba)\""
819
820           echo $Date,$Time,$Lat,$Lon,$Mag,$Dep,$ts,"$style"
821       done
822
823   }
824
825
826   wget http://neic.usgs.gov/neis/gis/qed.asc -O /dev/stdout |\
827    tail -n +2 > qed.asc
828
829   echo Date,TimeUTC,Latitude,Longitude,Magnitude,Depth,timestamp,OGR_STYLE > qed.csv
830
831   docsv < qed.asc >> qed.csv
832
833   cat > qed.vrt << EOF
834   <OGRVRTDataSource>
835       <OGRVRTLayer name="qed">
836           <SrcDataSource>qed.csv</SrcDataSource>
837           <GeometryType>wkbPoint</GeometryType>
838           <LayerSRS>WGS84</LayerSRS>
839           <GeometryField encoding="PointFromColumns" x="Longitude" y="Latitude"/>
840       </OGRVRTLayer>
841   </OGRVRTDataSource>
842
843   EOF
844
845   ogr2ogr -f libkml qed.kml qed.vrt
846
847
848