~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to ogr/ogrsf_frmts/kml/ogrkmllayer.cpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/******************************************************************************
2
 
 * $Id: ogrkmllayer.cpp 16908 2009-05-02 14:53:26Z rouault $
 
2
 * $Id: ogrkmllayer.cpp 22872 2011-08-06 20:32:14Z rouault $
3
3
 *
4
4
 * Project:  KML Driver
5
5
 * Purpose:  Implementation of OGRKMLLayer class.
102
102
 
103
103
    bWriter_ = bWriterIn;
104
104
    nWroteFeatureCount_ = 0;
 
105
    bClosedForWriting = (bWriterIn == FALSE);
105
106
 
106
107
    pszName_ = CPLStrdup(pszName);
107
108
}
243
244
    if( !bWriter_ )
244
245
        return OGRERR_FAILURE;
245
246
 
246
 
    FILE *fp = poDS_->GetOutputFP();
 
247
    if( bClosedForWriting )
 
248
    {
 
249
        CPLError(CE_Failure, CPLE_NotSupported,
 
250
                 "Interleaved feature adding to different layers is not supported");
 
251
        return OGRERR_FAILURE;
 
252
    }
 
253
 
 
254
    VSILFILE *fp = poDS_->GetOutputFP();
247
255
    CPLAssert( NULL != fp );
248
256
 
249
257
    // If we haven't writen any features yet, output the layer's schema
250
258
    if (0 == nWroteFeatureCount_)
251
259
    {
252
 
        VSIFPrintf( fp, "<Schema name=\"%s\" id=\"%s\">\n", pszName_, pszName_ );
 
260
        VSIFPrintfL( fp, "<Schema name=\"%s\" id=\"%s\">\n", pszName_, pszName_ );
253
261
        OGRFeatureDefn *featureDefinition = GetLayerDefn();
254
262
        for (int j=0; j < featureDefinition->GetFieldCount(); j++)
255
263
        {
300
308
                pszKMLEltName = "SimpleField";
301
309
                break;
302
310
            }
303
 
            VSIFPrintf( fp, "\t<%s name=\"%s\" type=\"%s\"></%s>\n", 
 
311
            VSIFPrintfL( fp, "\t<%s name=\"%s\" type=\"%s\"></%s>\n", 
304
312
                        pszKMLEltName, fieldDefinition->GetNameRef() ,pszKMLType, pszKMLEltName );
305
313
        }
306
 
        VSIFPrintf( fp, "</Schema>\n" );
 
314
        VSIFPrintfL( fp, "</Schema>\n" );
307
315
    }
308
316
 
309
 
    VSIFPrintf( fp, "  <Placemark>\n" );
 
317
    VSIFPrintfL( fp, "  <Placemark>\n" );
310
318
 
311
319
    if( poFeature->GetFID() == OGRNullFID )
312
320
        poFeature->SetFID( iNextKMLId_++ );
327
335
 
328
336
                char *pszEscaped = OGRGetXML_UTF8_EscapedString( pszRaw );
329
337
 
330
 
                VSIFPrintf( fp, "\t<name>%s</name>\n", pszEscaped);
 
338
                VSIFPrintfL( fp, "\t<name>%s</name>\n", pszEscaped);
331
339
                CPLFree( pszEscaped );
332
340
            }
333
341
        }
348
356
 
349
357
                char *pszEscaped = OGRGetXML_UTF8_EscapedString( pszRaw );
350
358
 
351
 
                VSIFPrintf( fp, "\t<description>%s</description>\n", pszEscaped);
 
359
                VSIFPrintfL( fp, "\t<description>%s</description>\n", pszEscaped);
352
360
                CPLFree( pszEscaped );
353
361
            }
354
362
        }
355
363
    }
356
364
 
357
 
    OGRwkbGeometryType eGeomType = wkbFlatten(poFeatureDefn_->GetGeomType());
 
365
    OGRwkbGeometryType eGeomType = wkbNone;
 
366
    if (poFeature->GetGeometryRef() != NULL)
 
367
        eGeomType = wkbFlatten(poFeature->GetGeometryRef()->getGeometryType());
358
368
    if ( wkbPolygon == eGeomType
359
369
         || wkbMultiPolygon == eGeomType
360
370
         || wkbLineString == eGeomType
361
371
         || wkbMultiLineString == eGeomType )
362
372
    {
 
373
        OGRStylePen *poPen = NULL;
 
374
        OGRStyleMgr oSM;
 
375
 
 
376
        if( poFeature->GetStyleString() != NULL )
 
377
        {
 
378
            oSM.InitFromFeature( poFeature );
 
379
 
 
380
            int i;
 
381
            for(i=0; i<oSM.GetPartCount();i++)
 
382
            {
 
383
                OGRStyleTool *poTool = oSM.GetPart(i);
 
384
                if (poTool && poTool->GetType() == OGRSTCPen )
 
385
                {
 
386
                    poPen = (OGRStylePen*) poTool;
 
387
                    break;
 
388
                }
 
389
                delete poTool;
 
390
            }
 
391
        }
 
392
 
 
393
        VSIFPrintfL( fp, "\t<Style>");
 
394
        if( poPen != NULL )
 
395
        {
 
396
            GBool  bDefault;
 
397
            int    bHasWidth = FALSE;
 
398
 
 
399
            /* Require width to be returned in pixel */
 
400
            poPen->SetUnit(OGRSTUPixel);
 
401
            double fW = poPen->Width(bDefault);
 
402
            if( bDefault )
 
403
                fW = 1;
 
404
            else
 
405
                bHasWidth = TRUE;
 
406
            const char* pszColor = poPen->Color(bDefault);
 
407
            int nColorLen = CPLStrnlen(pszColor, 10);
 
408
            if( pszColor != NULL && pszColor[0] == '#' && !bDefault && nColorLen >= 7)
 
409
            {
 
410
                char acColor[9] = {0};
 
411
                /* Order of KML color is aabbggrr, whereas OGR color is #rrggbb[aa] ! */
 
412
                if(nColorLen == 9)
 
413
                {
 
414
                    acColor[0] = pszColor[7]; /* A */
 
415
                    acColor[1] = pszColor[8];
 
416
                }
 
417
                else
 
418
                {
 
419
                    acColor[0] = 'F';
 
420
                    acColor[1] = 'F';
 
421
                }
 
422
                acColor[2] = pszColor[5]; /* B */
 
423
                acColor[3] = pszColor[6];
 
424
                acColor[4] = pszColor[3]; /* G */
 
425
                acColor[5] = pszColor[4];
 
426
                acColor[6] = pszColor[1]; /* R */
 
427
                acColor[7] = pszColor[2];
 
428
                VSIFPrintfL( fp, "<LineStyle><color>%s</color>", acColor);
 
429
                if (bHasWidth)
 
430
                    VSIFPrintfL( fp, "<width>%g</width>", fW);
 
431
                VSIFPrintfL( fp, "</LineStyle>");
 
432
            }
 
433
            else
 
434
                VSIFPrintfL( fp, "<LineStyle><color>ff0000ff</color></LineStyle>");
 
435
        }
 
436
        else
 
437
            VSIFPrintfL( fp, "<LineStyle><color>ff0000ff</color></LineStyle>");
 
438
        delete poPen;
363
439
        //If we're dealing with a polygon, add a line style that will stand out a bit
364
 
        VSIFPrintf( fp, "  <Style><LineStyle><color>ff0000ff</color></LineStyle>");
365
 
        VSIFPrintf( fp, "  <PolyStyle><fill>0</fill></PolyStyle></Style>\n" );
 
440
        VSIFPrintfL( fp, "<PolyStyle><fill>0</fill></PolyStyle></Style>\n" );
366
441
    }
367
442
 
368
443
    int bHasFoundOtherField = FALSE;
376
451
        {
377
452
            if (!bHasFoundOtherField)
378
453
            {                
379
 
                VSIFPrintf( fp, "\t<ExtendedData><SchemaData schemaUrl=\"#%s\">\n", pszName_ );
 
454
                VSIFPrintfL( fp, "\t<ExtendedData><SchemaData schemaUrl=\"#%s\">\n", pszName_ );
380
455
                bHasFoundOtherField = TRUE;
381
456
            }
382
457
            const char *pszRaw = poFeature->GetFieldAsString( iField );
386
461
 
387
462
            char *pszEscaped = OGRGetXML_UTF8_EscapedString( pszRaw );
388
463
 
389
 
            VSIFPrintf( fp, "\t\t<SimpleData name=\"%s\">%s</SimpleData>\n", 
 
464
            VSIFPrintfL( fp, "\t\t<SimpleData name=\"%s\">%s</SimpleData>\n", 
390
465
                        poField->GetNameRef(), pszEscaped);
391
466
 
392
467
            CPLFree( pszEscaped );
395
470
 
396
471
    if (bHasFoundOtherField)
397
472
    {
398
 
        VSIFPrintf( fp, "\t</SchemaData></ExtendedData>\n" );
 
473
        VSIFPrintfL( fp, "\t</SchemaData></ExtendedData>\n" );
399
474
    }
400
475
 
401
476
    // Write out Geometry - for now it isn't indented properly.
421
496
            OGR_G_ExportToKML( (OGRGeometryH)poWGS84Geom,
422
497
                               poDS_->GetAltitudeMode());
423
498
        
424
 
        VSIFPrintf( fp, "      %s\n", pszGeometry );
 
499
        VSIFPrintfL( fp, "      %s\n", pszGeometry );
425
500
        CPLFree( pszGeometry );
426
501
 
427
502
        poWGS84Geom->getEnvelope( &sGeomBounds );
433
508
        }
434
509
    }
435
510
    
436
 
    VSIFPrintf( fp, "  </Placemark>\n" );
 
511
    VSIFPrintfL( fp, "  </Placemark>\n" );
437
512
    nWroteFeatureCount_++;
438
513
    return OGRERR_NONE;
439
514
}
462
537
//        return poFClass->GetFeatureCount() != -1;
463
538
    }
464
539
 
 
540
    else if (EQUAL(pszCap, OLCStringsAsUTF8))
 
541
        return TRUE;
 
542
 
465
543
    return FALSE;
466
544
}
467
545