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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
/******************************************************************************
* $Id: ogrkmllayer.cpp 26347 2013-08-20 18:22:32Z rouault $
*
* Project: KML Driver
* Purpose: Implementation of OGRKMLLayer class.
* Author: Christopher Condit, condit@sdsc.edu
* Jens Oberender, j.obi@troja.net
*
******************************************************************************
* Copyright (c) 2006, Christopher Condit
*
* 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 "ogr_kml.h"
#include "ogr_api.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "ogr_p.h"
/* Function utility to dump OGRGeometry to KML text. */
char *OGR_G_ExportToKML( OGRGeometryH hGeometry, const char* pszAltitudeMode );
/************************************************************************/
/* OGRKMLLayer() */
/************************************************************************/
OGRKMLLayer::OGRKMLLayer( const char * pszName,
OGRSpatialReference *poSRSIn, int bWriterIn,
OGRwkbGeometryType eReqType,
OGRKMLDataSource *poDSIn )
{
poCT_ = NULL;
/* KML should be created as WGS84. */
if( poSRSIn != NULL )
{
poSRS_ = new OGRSpatialReference(NULL);
poSRS_->SetWellKnownGeogCS( "WGS84" );
if (!poSRS_->IsSame(poSRSIn))
{
poCT_ = OGRCreateCoordinateTransformation( poSRSIn, poSRS_ );
if( poCT_ == NULL && poDSIn->IsFirstCTError() )
{
/* If we can't create a transformation, issue a warning - but continue the transformation*/
char *pszWKT = NULL;
poSRSIn->exportToPrettyWkt( &pszWKT, FALSE );
CPLError( CE_Warning, CPLE_AppDefined,
"Failed to create coordinate transformation between the\n"
"input coordinate system and WGS84. This may be because they\n"
"are not transformable, or because projection services\n"
"(PROJ.4 DLL/.so) could not be loaded.\n"
"KML geometries may not render correctly.\n"
"This message will not be issued any more. \n"
"\nSource:\n%s\n",
pszWKT );
CPLFree( pszWKT );
poDSIn->IssuedFirstCTError();
}
}
}
else
{
poSRS_ = NULL;
}
iNextKMLId_ = 0;
nTotalKMLCount_ = -1;
nLastAsked = -1;
nLastCount = -1;
poDS_ = poDSIn;
poFeatureDefn_ = new OGRFeatureDefn( pszName );
poFeatureDefn_->Reference();
poFeatureDefn_->SetGeomType( eReqType );
OGRFieldDefn oFieldName( "Name", OFTString );
poFeatureDefn_->AddFieldDefn( &oFieldName );
OGRFieldDefn oFieldDesc( "Description", OFTString );
poFeatureDefn_->AddFieldDefn( &oFieldDesc );
bWriter_ = bWriterIn;
nWroteFeatureCount_ = 0;
bClosedForWriting = (bWriterIn == FALSE);
pszName_ = CPLStrdup(pszName);
}
/************************************************************************/
/* ~OGRKMLLayer() */
/************************************************************************/
OGRKMLLayer::~OGRKMLLayer()
{
if( NULL != poFeatureDefn_ )
poFeatureDefn_->Release();
if( NULL != poSRS_ )
poSRS_->Release();
if( NULL != poCT_ )
delete poCT_;
CPLFree( pszName_ );
}
/************************************************************************/
/* GetLayerDefn() */
/************************************************************************/
OGRFeatureDefn* OGRKMLLayer::GetLayerDefn()
{
return poFeatureDefn_;
}
/************************************************************************/
/* ResetReading() */
/************************************************************************/
void OGRKMLLayer::ResetReading()
{
iNextKMLId_ = 0;
nLastAsked = -1;
nLastCount = -1;
}
/************************************************************************/
/* GetNextFeature() */
/************************************************************************/
OGRFeature *OGRKMLLayer::GetNextFeature()
{
#ifndef HAVE_EXPAT
return NULL;
#else
/* -------------------------------------------------------------------- */
/* Loop till we find a feature matching our criteria. */
/* -------------------------------------------------------------------- */
KML *poKMLFile = poDS_->GetKMLFile();
poKMLFile->selectLayer(nLayerNumber_);
while(TRUE)
{
Feature *poFeatureKML = NULL;
poFeatureKML = poKMLFile->getFeature(iNextKMLId_++, nLastAsked, nLastCount);
if(poFeatureKML == NULL)
return NULL;
CPLAssert( poFeatureKML != NULL );
OGRFeature *poFeature = new OGRFeature( poFeatureDefn_ );
if(poFeatureKML->poGeom)
{
poFeature->SetGeometryDirectly(poFeatureKML->poGeom);
poFeatureKML->poGeom = NULL;
}
// Add fields
poFeature->SetField( poFeatureDefn_->GetFieldIndex("Name"), poFeatureKML->sName.c_str() );
poFeature->SetField( poFeatureDefn_->GetFieldIndex("Description"), poFeatureKML->sDescription.c_str() );
poFeature->SetFID( iNextKMLId_ - 1 );
// Clean up
delete poFeatureKML;
if( poFeature->GetGeometryRef() != NULL && poSRS_ != NULL)
{
poFeature->GetGeometryRef()->assignSpatialReference( poSRS_ );
}
/* Check spatial/attribute filters */
if ((m_poFilterGeom == NULL || FilterGeometry( poFeature->GetGeometryRef() ) ) &&
(m_poAttrQuery == NULL || m_poAttrQuery->Evaluate( poFeature )) )
{
// Return the feature
return poFeature;
}
else
{
delete poFeature;
}
}
#endif /* HAVE_EXPAT */
}
/************************************************************************/
/* GetFeatureCount() */
/************************************************************************/
int OGRKMLLayer::GetFeatureCount( int bForce )
{
int nCount = 0;
#ifdef HAVE_EXPAT
if (m_poFilterGeom == NULL && m_poAttrQuery == NULL)
{
KML *poKMLFile = poDS_->GetKMLFile();
if( NULL != poKMLFile )
{
poKMLFile->selectLayer(nLayerNumber_);
nCount = poKMLFile->getNumFeatures();
}
}
else
return OGRLayer::GetFeatureCount(bForce);
#endif
return nCount;
}
/************************************************************************/
/* WriteSchema() */
/************************************************************************/
void OGRKMLLayer::WriteSchema()
{
if (0 != nWroteFeatureCount_)
{
VSILFILE *fp = poDS_->GetOutputFP();
int nFieldsWritten = 0;
OGRFeatureDefn *featureDefinition = GetLayerDefn();
for (int j=0; j < featureDefinition->GetFieldCount(); j++)
{
OGRFieldDefn *fieldDefinition = featureDefinition->GetFieldDefn(j);
if (NULL != poDS_->GetNameField() &&
EQUAL(fieldDefinition->GetNameRef(), poDS_->GetNameField()) )
continue;
if (NULL != poDS_->GetDescriptionField() &&
EQUAL(fieldDefinition->GetNameRef(), poDS_->GetDescriptionField()) )
continue;
if( nFieldsWritten == 0 )
{
VSIFPrintfL( fp, "<Schema name=\"%s\" id=\"%s\">\n", pszName_, pszName_ );
}
nFieldsWritten ++;
const char* pszKMLType = NULL;
const char* pszKMLEltName = NULL;
// Match the OGR type to the GDAL type
switch (fieldDefinition->GetType())
{
case OFTInteger:
pszKMLType = "int";
pszKMLEltName = "SimpleField";
break;
case OFTIntegerList:
pszKMLType = "int";
pszKMLEltName = "SimpleArrayField";
break;
case OFTReal:
pszKMLType = "float";
pszKMLEltName = "SimpleField";
break;
case OFTRealList:
pszKMLType = "float";
pszKMLEltName = "SimpleArrayField";
break;
case OFTString:
pszKMLType = "string";
pszKMLEltName = "SimpleField";
break;
case OFTStringList:
pszKMLType = "string";
pszKMLEltName = "SimpleArrayField";
break;
case OFTBinary:
pszKMLType = "bool";
pszKMLEltName = "SimpleField";
break;
//TODO: KML doesn't handle these data types yet...
case OFTDate:
case OFTTime:
case OFTDateTime:
pszKMLType = "string";
pszKMLEltName = "SimpleField";
break;
default:
pszKMLType = "string";
pszKMLEltName = "SimpleField";
break;
}
VSIFPrintfL( fp, "\t<%s name=\"%s\" type=\"%s\"></%s>\n",
pszKMLEltName, fieldDefinition->GetNameRef() ,pszKMLType, pszKMLEltName );
}
if( nFieldsWritten > 0 )
VSIFPrintfL( fp, "</Schema>\n" );
}
}
/************************************************************************/
/* CreateFeature() */
/************************************************************************/
OGRErr OGRKMLLayer::CreateFeature( OGRFeature* poFeature )
{
CPLAssert( NULL != poFeature );
CPLAssert( NULL != poDS_ );
if( !bWriter_ )
return OGRERR_FAILURE;
if( bClosedForWriting )
{
CPLError(CE_Failure, CPLE_NotSupported,
"Interleaved feature adding to different layers is not supported");
return OGRERR_FAILURE;
}
VSILFILE *fp = poDS_->GetOutputFP();
CPLAssert( NULL != fp );
VSIFPrintfL( fp, " <Placemark>\n" );
if( poFeature->GetFID() == OGRNullFID )
poFeature->SetFID( iNextKMLId_++ );
// Find and write the name element
if (NULL != poDS_->GetNameField())
{
for( int iField = 0; iField < poFeatureDefn_->GetFieldCount(); iField++ )
{
OGRFieldDefn *poField = poFeatureDefn_->GetFieldDefn( iField );
if( poFeature->IsFieldSet( iField )
&& EQUAL(poField->GetNameRef(), poDS_->GetNameField()) )
{
const char *pszRaw = poFeature->GetFieldAsString( iField );
while( *pszRaw == ' ' )
pszRaw++;
char *pszEscaped = OGRGetXML_UTF8_EscapedString( pszRaw );
VSIFPrintfL( fp, "\t<name>%s</name>\n", pszEscaped);
CPLFree( pszEscaped );
}
}
}
if (NULL != poDS_->GetDescriptionField())
{
for( int iField = 0; iField < poFeatureDefn_->GetFieldCount(); iField++ )
{
OGRFieldDefn *poField = poFeatureDefn_->GetFieldDefn( iField );
if( poFeature->IsFieldSet( iField )
&& EQUAL(poField->GetNameRef(), poDS_->GetDescriptionField()) )
{
const char *pszRaw = poFeature->GetFieldAsString( iField );
while( *pszRaw == ' ' )
pszRaw++;
char *pszEscaped = OGRGetXML_UTF8_EscapedString( pszRaw );
VSIFPrintfL( fp, "\t<description>%s</description>\n", pszEscaped);
CPLFree( pszEscaped );
}
}
}
OGRwkbGeometryType eGeomType = wkbNone;
if (poFeature->GetGeometryRef() != NULL)
eGeomType = wkbFlatten(poFeature->GetGeometryRef()->getGeometryType());
if ( wkbPolygon == eGeomType
|| wkbMultiPolygon == eGeomType
|| wkbLineString == eGeomType
|| wkbMultiLineString == eGeomType )
{
OGRStylePen *poPen = NULL;
OGRStyleMgr oSM;
if( poFeature->GetStyleString() != NULL )
{
oSM.InitFromFeature( poFeature );
int i;
for(i=0; i<oSM.GetPartCount();i++)
{
OGRStyleTool *poTool = oSM.GetPart(i);
if (poTool && poTool->GetType() == OGRSTCPen )
{
poPen = (OGRStylePen*) poTool;
break;
}
delete poTool;
}
}
VSIFPrintfL( fp, "\t<Style>");
if( poPen != NULL )
{
GBool bDefault;
int bHasWidth = FALSE;
/* Require width to be returned in pixel */
poPen->SetUnit(OGRSTUPixel);
double fW = poPen->Width(bDefault);
if( bDefault )
fW = 1;
else
bHasWidth = TRUE;
const char* pszColor = poPen->Color(bDefault);
int nColorLen = CPLStrnlen(pszColor, 10);
if( pszColor != NULL && pszColor[0] == '#' && !bDefault && nColorLen >= 7)
{
char acColor[9] = {0};
/* Order of KML color is aabbggrr, whereas OGR color is #rrggbb[aa] ! */
if(nColorLen == 9)
{
acColor[0] = pszColor[7]; /* A */
acColor[1] = pszColor[8];
}
else
{
acColor[0] = 'F';
acColor[1] = 'F';
}
acColor[2] = pszColor[5]; /* B */
acColor[3] = pszColor[6];
acColor[4] = pszColor[3]; /* G */
acColor[5] = pszColor[4];
acColor[6] = pszColor[1]; /* R */
acColor[7] = pszColor[2];
VSIFPrintfL( fp, "<LineStyle><color>%s</color>", acColor);
if (bHasWidth)
VSIFPrintfL( fp, "<width>%g</width>", fW);
VSIFPrintfL( fp, "</LineStyle>");
}
else
VSIFPrintfL( fp, "<LineStyle><color>ff0000ff</color></LineStyle>");
}
else
VSIFPrintfL( fp, "<LineStyle><color>ff0000ff</color></LineStyle>");
delete poPen;
//If we're dealing with a polygon, add a line style that will stand out a bit
VSIFPrintfL( fp, "<PolyStyle><fill>0</fill></PolyStyle></Style>\n" );
}
int bHasFoundOtherField = FALSE;
// Write all fields as SchemaData
for( int iField = 0; iField < poFeatureDefn_->GetFieldCount(); iField++ )
{
OGRFieldDefn *poField = poFeatureDefn_->GetFieldDefn( iField );
if( poFeature->IsFieldSet( iField ))
{
if (NULL != poDS_->GetNameField() &&
EQUAL(poField->GetNameRef(), poDS_->GetNameField()) )
continue;
if (NULL != poDS_->GetDescriptionField() &&
EQUAL(poField->GetNameRef(), poDS_->GetDescriptionField()) )
continue;
if (!bHasFoundOtherField)
{
VSIFPrintfL( fp, "\t<ExtendedData><SchemaData schemaUrl=\"#%s\">\n", pszName_ );
bHasFoundOtherField = TRUE;
}
const char *pszRaw = poFeature->GetFieldAsString( iField );
while( *pszRaw == ' ' )
pszRaw++;
char *pszEscaped;
if (poFeatureDefn_->GetFieldDefn(iField)->GetType() == OFTReal)
{
pszEscaped = CPLStrdup( pszRaw );
/* Use point as decimal separator */
char* pszComma = strchr(pszEscaped, ',');
if (pszComma)
*pszComma = '.';
}
else
{
pszEscaped = OGRGetXML_UTF8_EscapedString( pszRaw );
}
VSIFPrintfL( fp, "\t\t<SimpleData name=\"%s\">%s</SimpleData>\n",
poField->GetNameRef(), pszEscaped);
CPLFree( pszEscaped );
}
}
if (bHasFoundOtherField)
{
VSIFPrintfL( fp, "\t</SchemaData></ExtendedData>\n" );
}
// Write out Geometry - for now it isn't indented properly.
if( poFeature->GetGeometryRef() != NULL )
{
char* pszGeometry = NULL;
OGREnvelope sGeomBounds;
OGRGeometry* poWGS84Geom;
if (NULL != poCT_)
{
poWGS84Geom = poFeature->GetGeometryRef()->clone();
poWGS84Geom->transform( poCT_ );
}
else
{
poWGS84Geom = poFeature->GetGeometryRef();
}
// TODO - porting
// pszGeometry = poFeature->GetGeometryRef()->exportToKML();
pszGeometry =
OGR_G_ExportToKML( (OGRGeometryH)poWGS84Geom,
poDS_->GetAltitudeMode());
VSIFPrintfL( fp, " %s\n", pszGeometry );
CPLFree( pszGeometry );
poWGS84Geom->getEnvelope( &sGeomBounds );
poDS_->GrowExtents( &sGeomBounds );
if (NULL != poCT_)
{
delete poWGS84Geom;
}
}
VSIFPrintfL( fp, " </Placemark>\n" );
nWroteFeatureCount_++;
return OGRERR_NONE;
}
/************************************************************************/
/* TestCapability() */
/************************************************************************/
int OGRKMLLayer::TestCapability( const char * pszCap )
{
if( EQUAL(pszCap, OLCSequentialWrite) )
{
return bWriter_;
}
else if( EQUAL(pszCap, OLCCreateField) )
{
return bWriter_ && iNextKMLId_ == 0;
}
else if( EQUAL(pszCap,OLCFastFeatureCount) )
{
// if( poFClass == NULL
// || m_poFilterGeom != NULL
// || m_poAttrQuery != NULL )
return FALSE;
// return poFClass->GetFeatureCount() != -1;
}
else if (EQUAL(pszCap, OLCStringsAsUTF8))
return TRUE;
return FALSE;
}
/************************************************************************/
/* CreateField() */
/************************************************************************/
OGRErr OGRKMLLayer::CreateField( OGRFieldDefn *poField, int bApproxOK )
{
if( !bWriter_ || iNextKMLId_ != 0 )
return OGRERR_FAILURE;
OGRFieldDefn oCleanCopy( poField );
poFeatureDefn_->AddFieldDefn( &oCleanCopy );
return OGRERR_NONE;
}
/************************************************************************/
/* GetSpatialRef() */
/************************************************************************/
OGRSpatialReference *OGRKMLLayer::GetSpatialRef()
{
return poSRS_;
}
/************************************************************************/
/* SetLayerNumber() */
/************************************************************************/
void OGRKMLLayer::SetLayerNumber( int nLayer )
{
nLayerNumber_ = nLayer;
}
|