File: ogrlibkmlfeaturestyle.cpp

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 (411 lines) | stat: -rw-r--r-- 12,337 bytes parent folder | download | duplicates (2)
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
409
410
411
/******************************************************************************
 *
 * Project:  KML Translator
 * Purpose:  Implements OGRLIBKMLDriver
 * Author:   Brian Case, rush at winkey dot org
 *
 ******************************************************************************
 * 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.
 *****************************************************************************/

#include <ogrsf_frmts.h>
#include <ogr_featurestyle.h>
#include <string>
#include <iostream>
using namespace std;

#include <kml/dom.h>

using kmldom::KmlFactory;;
using kmldom::IconStylePtr;
using kmldom::PolyStylePtr;
using kmldom::LineStylePtr;
using kmldom::LabelStylePtr;
using kmldom::StylePtr;
using kmldom::Style;
using kmldom::StyleMapPtr;
using kmldom::StyleSelectorPtr;


#include "ogrlibkmlfeaturestyle.h"
#include "ogrlibkmlstyle.h"

/******************************************************************************
 function to write out a features style to kml

args:
            poOgrLayer      the layer the feature is in
            poOgrFeat       the feature
            poKmlFactory    the kml dom factory
            poKmlPlacemark  the placemark to add it to

returns:
            nothing
******************************************************************************/

void featurestyle2kml (
    OGRLIBKMLDataSource * poOgrDS,
    OGRLayer * poOgrLayer,
    OGRFeature * poOgrFeat,
    KmlFactory * poKmlFactory,
    PlacemarkPtr poKmlPlacemark )
{

    /***** get the style table *****/

    OGRStyleTable *poOgrSTBL;

    const char *pszStyleString = poOgrFeat->GetStyleString (  );

    /***** does the feature have style? *****/

    if ( pszStyleString ) {

        /***** does it ref a style table? *****/

        if ( *pszStyleString == '@' ) {

            /***** is the name in the layer style table *****/

            OGRStyleTable *hSTBLLayer;
            const char *pszTest = NULL;

            if ( ( hSTBLLayer = poOgrLayer->GetStyleTable (  ) ) )
                pszTest = hSTBLLayer->Find ( pszStyleString );

            if ( pszTest ) {
                string oTmp = "#";

                oTmp.append ( pszStyleString + 1 );

                poKmlPlacemark->set_styleurl ( oTmp );
            }


            /***** assume its a dataset style, mayby the user will add it later *****/

            else {
                string oTmp;

                if ( poOgrDS->GetStylePath (  ) )
                    oTmp.append ( poOgrDS->GetStylePath (  ) );
                oTmp.append ( "#" );
                oTmp.append ( pszStyleString + 1 );

                poKmlPlacemark->set_styleurl ( oTmp );
            }
        }

        /***** no style table ref *****/

        else {
            StylePtr poKmlStyle = poKmlFactory->CreateStyle (  );

            /***** parse the style string *****/

            addstylestring2kml ( pszStyleString, poKmlStyle, poKmlFactory,
                                 poKmlPlacemark, poOgrFeat );

            /***** add the style to the placemark *****/

            poKmlPlacemark->set_styleselector ( poKmlStyle );

        }
    }

    /***** get the style table *****/

    else if ( ( poOgrSTBL = poOgrFeat->GetStyleTable (  ) ) ) {


        StylePtr poKmlStyle = poKmlFactory->CreateStyle (  );

        /***** parse the style table *****/

        poOgrSTBL->ResetStyleStringReading (  );
        const char *pszStyleString;

        while ( ( pszStyleString = poOgrSTBL->GetNextStyle (  ) ) ) {

            if ( *pszStyleString == '@' ) {

                /***** is the name in the layer style table *****/

                OGRStyleTable *poOgrSTBLLayer;
                const char *pszTest = NULL;

                if ( ( poOgrSTBLLayer = poOgrLayer->GetStyleTable (  ) ) )
                    poOgrSTBLLayer->Find ( pszStyleString );

                if ( pszTest ) {
                    string oTmp = "#";

                    oTmp.append ( pszStyleString + 1 );

                    poKmlPlacemark->set_styleurl ( oTmp );
                }

                /***** assume its a dataset style,      *****/
                /***** mayby the user will add it later *****/

                else {
                    string oTmp;

                    if ( poOgrDS->GetStylePath (  ) )
                        oTmp.append ( poOgrDS->GetStylePath (  ) );
                    oTmp.append ( "#" );
                    oTmp.append ( pszStyleString + 1 );

                    poKmlPlacemark->set_styleurl ( oTmp );
                }
            }

            else {

                /***** parse the style string *****/

                addstylestring2kml ( pszStyleString, poKmlStyle,
                                     poKmlFactory, poKmlPlacemark, poOgrFeat );

                /***** add the style to the placemark *****/

                poKmlPlacemark->set_styleselector ( poKmlStyle );

            }
        }
    }
}


/******************************************************************************
 function to read a kml style into ogr's featurestyle
******************************************************************************/

void kml2featurestyle (
    PlacemarkPtr poKmlPlacemark,
    OGRLIBKMLDataSource * poOgrDS,
    OGRLayer * poOgrLayer,
    OGRFeature * poOgrFeat )
{

    /***** does the placemark have a style url? *****/

    if (    poKmlPlacemark->has_styleurl (  ) ) {

        const string poKmlStyleUrl = poKmlPlacemark->get_styleurl (  );

        /***** is the name in the layer style table *****/

        char *pszUrl = CPLStrdup ( poKmlStyleUrl.c_str (  ) );

        OGRStyleTable *poOgrSTBLLayer;
        const char *pszTest = NULL;

        /***** is it a layer style ? *****/

        if ( *pszUrl == '#'
             && ( poOgrSTBLLayer = poOgrLayer->GetStyleTable (  ) ) )
        {
             pszTest = poOgrSTBLLayer->Find ( pszUrl + 1 );
        }

        if ( pszTest ) {

            /***** should we resolve the style *****/

            const char *pszResolve = CPLGetConfigOption ( "LIBKML_RESOLVE_STYLE", "no" );

            if (EQUAL(pszResolve, "yes")) {

                poOgrFeat->SetStyleString ( pszTest );
            }

            else {

                *pszUrl = '@';

                poOgrFeat->SetStyleString( pszUrl );

            }

        }

        /***** is it a dataset style? *****/
        
        else {

            int nPathLen = strlen ( poOgrDS->GetStylePath (  ) );

            if (    nPathLen == 0
                 || EQUALN ( pszUrl, poOgrDS->GetStylePath (  ), nPathLen ))
            {

                /***** should we resolve the style *****/

                const char *pszResolve = CPLGetConfigOption ( "LIBKML_RESOLVE_STYLE", "no" );

                if (    EQUAL(pszResolve, "yes")
                     && ( poOgrSTBLLayer = poOgrDS->GetStyleTable (  ) )
                     && ( pszTest = poOgrSTBLLayer->Find ( pszUrl + nPathLen + 1) )
                   )
                {

                    poOgrFeat->SetStyleString ( pszTest );
                }

                else {

                    pszUrl[nPathLen] = '@';
                    poOgrFeat->SetStyleString ( pszUrl + nPathLen );
                }
       
            }
            
            /**** its someplace else *****/

            else {

                const char *pszFetch = CPLGetConfigOption ( "LIBKML_EXTERNAL_STYLE", "no" );

                if ( EQUAL(pszFetch, "yes") ) {

                    /***** load up the style table *****/

                    char *pszUrlTmp = CPLStrdup(pszUrl);
                    char *pszPound;
                    if ((pszPound = strchr(pszUrlTmp, '#'))) {
                        
                        *pszPound = '\0';
                    }

                    /***** try it as a url then a file *****/

                    VSILFILE *fp = NULL;

                    if (    (fp = VSIFOpenL( CPLFormFilename( "/vsicurl/",
                                                              pszUrlTmp,
                                                              NULL),
                                             "r" ))
                        ||  (fp = VSIFOpenL( pszUrlTmp, "r" )))
                    {

                        char szbuf[1025];
                        std::string oStyle = "";

                        /***** loop, read and copy to a string *****/

                        size_t nRead;
                    
                        do {
                            
                            nRead = VSIFReadL(szbuf, 1, sizeof(szbuf) - 1, fp);
                            
                            if (nRead == 0)
                                break;

                            /***** copy buf to the string *****/

                            szbuf[nRead] = '\0';
                            oStyle.append( szbuf );

                        } while (!VSIFEofL(fp));

                        VSIFCloseL(fp);

                        /***** parse the kml into the ds style table *****/

                        if ( poOgrDS->ParseIntoStyleTable (&oStyle, pszUrlTmp)) {

                            kml2featurestyle (poKmlPlacemark,
                                              poOgrDS,
                                              poOgrLayer,
                                              poOgrFeat );
                        }

                        else {

                            /***** if failed just store the url *****/

                            poOgrFeat->SetStyleString ( pszUrl );
                        }
                    }
                    CPLFree(pszUrlTmp);
                }

                else {

                    poOgrFeat->SetStyleString ( pszUrl );
                }
            }

        }
        CPLFree ( pszUrl );

    }

    /***** does the placemark have a style selector *****/

   if ( poKmlPlacemark->has_styleselector (  ) ) {

        StyleSelectorPtr poKmlStyleSelector =
            poKmlPlacemark->get_styleselector (  );

        /***** is the style a style? *****/

        if ( poKmlStyleSelector->IsA ( kmldom::Type_Style ) ) {
            StylePtr poKmlStyle = AsStyle ( poKmlStyleSelector );

            OGRStyleMgr *poOgrSM = new OGRStyleMgr;

            /***** if were resolveing style the feature  *****/
            /***** might already have styling to add too *****/
            
            const char *pszResolve = CPLGetConfigOption ( "LIBKML_RESOLVE_STYLE", "no" );
            if (EQUAL(pszResolve, "yes")) {
                 poOgrSM->InitFromFeature ( poOgrFeat );
            }
            else {

                /***** if featyurestyle gets a name tool this needs changed to the above *****/
                
                poOgrSM->InitStyleString ( NULL );
            }

            /***** read the style *****/

            kml2stylestring ( poKmlStyle, poOgrSM );

            /***** add the style to the feature *****/
            
            poOgrFeat->SetStyleString(poOgrSM->GetStyleString(NULL));

            delete poOgrSM;
        }

        /***** is the style a stylemap? *****/

        else if ( poKmlStyleSelector->IsA ( kmldom::Type_StyleMap ) ) {
            /* todo need to figure out what to do with a style map */
        }


    }
}