File: drv_libkml.html

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (408 lines) | stat: -rw-r--r-- 16,013 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>LIBKML Driver</title>
</head>
 
<body bgcolor="#ffffff">
 
  <h1>LIBKML Driver (.kml .kmz)</h1>
    
  <p>
    The LIBKML driver is a client of
    <a href="http://code.google.com/p/libkml/">Libkml</a> from Google, a
    reference implementation of
    <a href="http://www.opengeospatial.org/standards/kml/">KML</a>
    reading and writing, in the form of a cross platform C++ library.
    You must build and install Libkml in order to use this OGR driver.
    Note: you need to build libkml from its latest SVN version (libkml 1.2 isn't
    enough).
  </p>
    
  <p>
    Note that if you build and include this LIBKML driver, it will become the
    default reader of KML for ogr, overriding the previous KML driver. You can
    still specify either KML or LIBKML as the ouput driver via the command line
  </p>
    
  <p>
    Libkml from Google provides reading services for any valid KML file.
    However, please be advised that some KML facilities do not map into the
    Simple Features specification ogr uses as its internal structure.
    Therefore, a best effort will be made by the driver to understand the
    content of a KML file read by libkml into ogr, but your mileage may vary.
    Please try a few KML files as samples to get a sense of what is understood.
    In particular, nesting of feature sets more than one deep will be flattened
    to support ogr's internal format.
  </p>
  
  <h2>Datasource</h2>
  
  <p>
    You may specify a
    <a href="http://www.gdal.org/ogr/ogr_arch.html#ogr_arch_data_source">datasource </a>
    as a kml file <code>somefile.kml</code> , 
    a directory <code>somedir/</code> , or a kmz file <code>somefile.kmz</code> .
  </p>
  
  <p>
    By default on directory and kmz datasources, an index file of all the
    layers will be read from or written to doc.kml. It conatains a
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#networklink">
    &lt;NetworkLink&gt;</a> to each layer file in the datasource. This feature
    can be turned off by setting the enviroment variable LIBKML_USE_DOC.KML to
    "no"
   </p>
  
  <h3>StyleTable</h3>
  
  <p>
    Datasource style tables are written to the
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#document">
    &lt;Document&gt;</a> in a .kml, style/style.kml
    in a kmz file, or style.kml in a directory, as one or more
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#style">
    &lt;Style&gt;</a> elements. Not all of
    <a href="http://www.gdal.org/ogr/ogr_feature_style.html">OGR Feature Style</a>
    can translate into KML.
  </p>
  
  <h2>Layer</h2>
  
  <p>
    <a href="http://www.gdal.org/ogr/ogr_arch.html#ogr_arch_layer">
    Layers</a> are mapped to kml files as a
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#document">
    &lt;Document&gt;</a>
    or
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#folder">
    &lt;Folder&gt;</a>, and in kmz files or directorys as a seperate kml file.
  </p>
  
  <h3>Style</h3>
  <p>
    Layer style tables can not be read from or written to a kml layer that is a 
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#folder">
    &lt;Folder&gt;</a>, otherwise they are in the
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#document">
    &lt;Document&gt;</a> that is the layer.
  </p>
  
  <h3>Schema</h3>
  
  <p>
    Read and write of
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#schema">
    &lt;Schema&gt;</a> is supported for .kml files , .kmz files, and
    directorys.
  </p>
 
  <h2>Feature</h2>
  
  <p>
    An OGR <a href="http://www.gdal.org/ogr/ogr_arch.html#ogr_arch_feature">feature</a>
    translates to kml as a
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#placemark">
    &lt;Placemark&gt;</a>, and vice-versa.
  </p>

  <p>
    Starting with OGR 1.10, KML 
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#groundoverlay">
    &lt;GroundOverlay&gt;</a> elements are supported for reading (unless the 
    LIBKML_READ_GROUND_OVERLAY configuration option is set to FALSE). For such elements, there
    are icon and drawOrder fields.
  </p>
  
  <h3>Style</h3>
  
  <p>
    Style Strings at the feature level are Mapped to KML as either a 
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#style">
    &lt;Style&gt;</a> or
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#styleurl">
    &lt;StyleUrl&gt;</a> in each
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#placemark">
    &lt;Placemark&gt;</a>.
  </p>
  <p>
     When reading a kml feature and the enviroment variable LIBKML_RESOLVE_STYLE
     is set to yes, styleurls are looked up in the style tables and the features
     style string is set to the style from the table. This is to allow reading 
     of shared styles by applications, like mapserver, that do not read style
     tables.
  </p>
  
  <p>
     When reading a kml feature and the enviroment variable LIBKML_EXTERNAL_STYLE
     is set to yes, a styleurl that is external to the datasource is read from 
     disk or fetched from the server and parsed into the datasource style table.
     If the style kml can not be read or LIBKML_EXTERNAL_STYLE is set to no then
     the styleurl is copyed to the style string.
  </p>
    
  <p>
     When reading a kml StyleMap the default mapping is set to normal. If you
     wish to use the highlighted styles set the enviroment variable
     LIBKML_STYLEMAP_KEY to "highlight"
  </p>
    
  <h2>Fields</h2>
  
  <p> OGR fields (feature atributes) are mapped to kml with
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#schema">
    &lt;Schema&gt</a>; and
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#simpledata">
    &lt;SimpleData&gt;</a>, except for some special fields as noted below.
  </p>
  
  <p>
    A rich set of environment variables are available to define how fields in
    input and output, map to a KML 
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#placemark">
    &lt;Placemark&gt;</a>. For example, if you want a field called 'Cities'
    to map to the
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#name">
    &lt;name&gt;</a>; tag in KML, you can set an
    environment variable. 
  </p>
 
  <dl>
    <dt>Name</dt>
    <dd>
      This String field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#name">
      &lt;name&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_NAME_FIELD .
    </dd>
    <dt>description</dt>
    <dd>This String field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#description">
      &lt;description&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_DESCRIPTION_FIELD .
    </dd>
    <dt>timestamp</dt>
    <dd>This string or datetime or date and/or time field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#timestamp">
      &lt;timestamp&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_TIMESTAMP_FIELD .
    </dd>
    <dt>begin</dt>
    <dd>This string or datetime or date and/or time field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#begin">
      &lt;begin&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_BEGIN_FIELD .
    </dd>
    <dt>end</dt>
    <dd>This string or datetime or date and/or time field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#end">
      &lt;end&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_END_FIELD .
    </dd>
    <dt>altitudeMode</dt>
    <dd>This string field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#altitudemode">
      &lt;altitudeMode&gt;</a> or
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#gxaltitudemode">
      &lt;gx:altitudeMode&gt;</a>. The name of the ogr field can be changed
      with the enviroment variable LIBKML_ALTITUDEMODE_FIELD .
    </dd>
    <dt>tessellate</dt>
    <dd>This integer field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#tessellate">
      &lt;tessellate&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_TESSELLATE_FIELD .
    </dd>
    <dt>extrude</dt>
    <dd>This integer field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#extrude">
      &lt;extrude&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_EXTRUDE_FIELD .
    </dd>
    <dt>visibility</dt>
    <dd>This integer field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#visibility">
      &lt;visibility&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_VISIBILITY_FIELD .
    </dd>
    <dt>icon</dt>
    <dd>This string field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#icon">
      &lt;icon&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_ICON_FIELD .
    </dd>
    <dt>drawOrder</dt>
    <dd>This integer field maps to the kml tag 
      <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#draworder">
      &lt;drawOrder&gt;</a>. The name of the ogr field can be changed with the
      enviroment variable LIBKML_DRAWORDER_FIELD .
    </dd>
    <dt>OGR_STYLE</dt>
    <dd>This string field maps to a features style string, OGR reads this field
      if there is no style string set on the feature.
    </dd>
  </dl>
  
  <h2>Geometry</h2>
  
  <p>
    Translation of OGR
    <a href="http://www.gdal.org/ogr/ogr_arch.html#ogr_arch_geometry">
    Geometry</a> to KML Geometry is pretty strait forwards with only a couple
    of exceptions. Point to 
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#point">
    &lt;Point&gt;</a>, LineString to
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#linestring">
    &lt;LineString&gt;</a>, LinearRing to
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#linearring">
    &lt;LinearRing&gt;</a>, and Polygon to
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#polygon">
    &lt;Polygon&gt;</a>. In OGR a polygon contains an array of LinearRings,
    the first one being the outer ring. KML has the tags
    &nbsp;<a href="http://code.google.com/apis/kml/documentation/kmlreference.html#outerboundaryis">
    &lt;outerBoundaryIs&gt;</a>&nbsp;and&nbsp; 
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#innerboundaryis">
    &lt;innerBoundaryIs&gt;</a>&nbsp;to differentiate between the two. OGR has
    several Multi types of geometry : GeometryCollection, MultiPolygon,
    MultiPoint, and MultiLineString. When possible, OGR will try to map 
    <a href="http://code.google.com/apis/kml/documentation/kmlreference.html#multigeometry">
    &lt;MultiGeometry&gt;</a> to the more precise OGR geometry type (MultiPoint, MultiLineString or MultiPolygon),
    and default to GeometryCollection in case of mixed content.
  </p>
  
  <p>
    Sometimes kml geometry will span the dateline, In applications like qgis or
    mapserver this will create horizontal lines all the way around the globe.
    Setting the enviroment variable LIBKML_WRAPDATELINE to "yes" will cause the
    libkml driver to split the geometry at the dateline when read.
  </p>

<h2>VSI Virtual File System API support</h2>

(Some features below might require OGR >= 1.9.0)<p>

The driver supports reading and writing to files managed by VSI Virtual File System API, which include
"regular" files, as well as files in the /vsizip/ (read-write) , /vsigzip/ (read-write) , /vsicurl/ (read-only) domains.<p>

Writing to /dev/stdout or /vsistdout/ is also supported.<p>

  <h2>Example</h2>
 
  <p>
    The following bash script will build a
    <a href="http://www.gdal.org/ogr/drv_csv.html">csv</a> file and a
    <a href="http://www.gdal.org/ogr/drv_vrt.html">vrt</a> file, and then
    translate them to KML using 
    <a href="http://www.gdal.org/ogr2ogr.html">ogr2ogr</a> into a .kml file with
    timestamps and styling.
  </p>
 
  <pre>
 
 
#!/bin/bash
# Copyright (c) 2010, Brian Case
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
 
 
icon="http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png"
rgba33="#FF9900"
rgba70="#FFFF00"
rgba150="#00FF00"
rgba300="#0000FF"
rgba500="#9900FF"
rgba800="#FF0000"
 
function docsv {
 
    IFS=','
    
    while read Date Time Lat Lon Mag Dep
    do
        ts=$(echo $Date | sed 's:/:-:g')T${Time%%.*}Z
        rgba=""
        
        if [[ $rgba == "" ]] && [[ $Dep -lt 33 ]]
        then
            rgba=$rgba33
        fi
        
        if [[ $rgba == "" ]] && [[ $Dep -lt 70 ]]
        then
            rgba=$rgba70
        fi
        
        if [[ $rgba == "" ]] && [[ $Dep -lt 150 ]]
        then
            rgba=$rgba150
        fi
        
        if [[ $rgba == "" ]] && [[ $Dep -lt 300 ]]
        then
            rgba=$rgba300
        fi
        
        if [[ $rgba == "" ]] && [[ $Dep -lt 500 ]]
        then
            rgba=$rgba500
        fi
        
        if [[ $rgba == "" ]]
        then
            rgba=$rgba800
        fi
        
        
        
        style="\"SYMBOL(s:$Mag,id:\"\"$icon\"\",c:$rgba)\""
        
        echo $Date,$Time,$Lat,$Lon,$Mag,$Dep,$ts,"$style"
    done
        
}
 
 
wget http://neic.usgs.gov/neis/gis/qed.asc -O /dev/stdout |\
 tail -n +2 &gt; qed.asc
 
echo Date,TimeUTC,Latitude,Longitude,Magnitude,Depth,timestamp,OGR_STYLE &gt; qed.csv
 
docsv &lt; qed.asc &gt;&gt; qed.csv
 
cat &gt; qed.vrt &lt;&lt; EOF
&lt;OGRVRTDataSource&gt;
    &lt;OGRVRTLayer name="qed"&gt;
        &lt;SrcDataSource&gt;qed.csv&lt;/SrcDataSource&gt;
        &lt;GeometryType&gt;wkbPoint&lt;/GeometryType&gt;
        &lt;LayerSRS&gt;WGS84&lt;/LayerSRS&gt;
        &lt;GeometryField encoding="PointFromColumns" x="Longitude" y="Latitude"/&gt;
    &lt;/OGRVRTLayer&gt;
&lt;/OGRVRTDataSource&gt;
 
EOF
 
ogr2ogr -f libkml qed.kml qed.vrt
 
  </pre>
 
</body>
</html>