| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: KML Translator |
| 4 |
* Purpose: Implements OGRLIBKMLDriver |
| 5 |
* Author: Brian Case, rush at winkey dot org |
| 6 |
* |
| 7 |
****************************************************************************** |
| 8 |
* Copyright (c) 2010, Brian Case |
| 9 |
* Copyright (c) 2010-2014, Even Rouault <even dot rouault at spatialys.com> |
| 10 |
* |
| 11 |
* SPDX-License-Identifier: MIT |
| 12 |
*****************************************************************************/ |
| 13 |
|
| 14 |
#include "libkml_headers.h" |
| 15 |
|
| 16 |
#include <string> |
| 17 |
#include "ogr_libkml.h" |
| 18 |
#include "ogrlibkmlstyle.h" |
| 19 |
#include "ogr_p.h" |
| 20 |
#include "cpl_vsi_error.h" |
| 21 |
|
| 22 |
using kmlbase::Attributes; |
| 23 |
using kmldom::ContainerPtr; |
| 24 |
using kmldom::DocumentPtr; |
| 25 |
using kmldom::ElementPtr; |
| 26 |
using kmldom::FeaturePtr; |
| 27 |
using kmldom::FolderPtr; |
| 28 |
using kmldom::KmlFactory; |
| 29 |
using kmldom::KmlPtr; |
| 30 |
using kmldom::LinkPtr; |
| 31 |
using kmldom::LinkSnippetPtr; |
| 32 |
using kmldom::NetworkLinkControlPtr; |
| 33 |
using kmldom::NetworkLinkPtr; |
| 34 |
using kmldom::SchemaPtr; |
| 35 |
using kmldom::SnippetPtr; |
| 36 |
using kmldom::StyleSelectorPtr; |
| 37 |
using kmlengine::KmzFile; |
| 38 |
|
| 39 |
/************************************************************************/ |
| 40 |
/* OGRLIBKMLParse() */ |
| 41 |
/************************************************************************/ |
| 42 |
|
| 43 |
static ElementPtr OGRLIBKMLParse(const std::string &oKml, std::string *posError) |
| 44 |
{ |
| 45 |
try |
| 46 |
{ |
| 47 |
// To allow reading files using an explicit namespace prefix like |
| 48 |
// <kml:kml> we need to use ParseNS (see #6981). But if we use ParseNS, |
| 49 |
// we have issues reading gx: elements. So use ParseNS only when we have |
| 50 |
// errors with Parse. This is not completely satisfactory... |
| 51 |
ElementPtr element = kmldom::Parse(oKml, posError); |
| 52 |
if (!element) |
| 53 |
element = kmldom::ParseNS(oKml, posError); |
| 54 |
return element; |
| 55 |
} |
| 56 |
catch (const std::exception &ex) |
| 57 |
{ |
| 58 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 59 |
"LIBKML: libstdc++ exception during Parse() : %s", ex.what()); |
| 60 |
return nullptr; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/****************************************************************************** |
| 65 |
OGRLIBKMLDataSource Constructor |
| 66 |
|
| 67 |
Args: none |
| 68 |
|
| 69 |
Returns: nothing |
| 70 |
|
| 71 |
******************************************************************************/ |
| 72 |
|
| 73 |
OGRLIBKMLDataSource::OGRLIBKMLDataSource(KmlFactory *poKmlFactory) |
| 74 |
: papoLayers(nullptr), nLayers(0), nAllocated(0), bUpdate(false), |
| 75 |
bUpdated(false), m_papszOptions(nullptr), m_isKml(false), |
| 76 |
m_poKmlDSKml(nullptr), m_poKmlDSContainer(nullptr), |
| 77 |
m_poKmlUpdate(nullptr), m_isKmz(false), m_poKmlDocKml(nullptr), |
| 78 |
m_poKmlDocKmlRoot(nullptr), m_poKmlStyleKml(nullptr), m_isDir(false), |
| 79 |
m_poKmlFactory(poKmlFactory) |
| 80 |
{ |
| 81 |
} |
| 82 |
|
| 83 |
/************************************************************************/ |
| 84 |
/* OGRLIBKMLPreProcessInput() */ |
| 85 |
/************************************************************************/ |
| 86 |
|
| 87 |
// Substitute <snippet> by deprecated <Snippet> since libkml currently |
| 88 |
// only supports Snippet but ogckml22.xsd has deprecated it in favor of snippet. |
| 89 |
static void OGRLIBKMLPreProcessInput(std::string &oKml) |
| 90 |
{ |
| 91 |
size_t nPos = 0; |
| 92 |
while (true) |
| 93 |
{ |
| 94 |
nPos = oKml.find("<snippet>", nPos); |
| 95 |
if (nPos == std::string::npos) |
| 96 |
{ |
| 97 |
break; |
| 98 |
} |
| 99 |
oKml[nPos + 1] = 'S'; |
| 100 |
nPos = oKml.find("</snippet>", nPos); |
| 101 |
if (nPos == std::string::npos) |
| 102 |
{ |
| 103 |
break; |
| 104 |
} |
| 105 |
oKml[nPos + 2] = 'S'; |
| 106 |
} |
| 107 |
|
| 108 |
// Workaround Windows specific issue with libkml (at least the version |
| 109 |
// used by OSGeo4W at time of writing), where tabulations as |
| 110 |
// coordinate separators aren't properly handled |
| 111 |
//(see https://trac.osgeo.org/gdal/ticket/7231) |
| 112 |
// Another Windows specific issue is that if the content of |
| 113 |
// <coordinates> is non-empty and does not contain any digit, |
| 114 |
// libkml hangs (see https://trac.osgeo.org/gdal/ticket/7232) |
| 115 |
nPos = 0; |
| 116 |
while (true) |
| 117 |
{ |
| 118 |
nPos = oKml.find("<coordinates>", nPos); |
| 119 |
if (nPos == std::string::npos) |
| 120 |
{ |
| 121 |
break; |
| 122 |
} |
| 123 |
size_t nPosEnd = oKml.find("</coordinates>", nPos); |
| 124 |
if (nPosEnd == std::string::npos) |
| 125 |
{ |
| 126 |
break; |
| 127 |
} |
| 128 |
nPos += strlen("<coordinates>"); |
| 129 |
size_t nPosAfterCoordinates = nPos; |
| 130 |
bool bDigitFound = false; |
| 131 |
for (; nPos < nPosEnd; nPos++) |
| 132 |
{ |
| 133 |
char ch = oKml[nPos]; |
| 134 |
if (ch >= '0' && ch <= '9') |
| 135 |
bDigitFound = true; |
| 136 |
else if (ch == '\t' || ch == '\n') |
| 137 |
oKml[nPos] = ' '; |
| 138 |
} |
| 139 |
if (!bDigitFound) |
| 140 |
{ |
| 141 |
oKml.replace(nPosAfterCoordinates, |
| 142 |
nPosEnd + strlen("</coordinates>") - |
| 143 |
nPosAfterCoordinates, |
| 144 |
"</coordinates>"); |
| 145 |
nPos = nPosAfterCoordinates + strlen("</coordinates>"); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Some non conformant file may contain |
| 150 |
// MultiPolygon/MultiLineString/MultiPoint See |
| 151 |
// https://github.com/OSGeo/gdal/issues/4031 Replace them by MultiGeometry. |
| 152 |
nPos = 0; |
| 153 |
while (true) |
| 154 |
{ |
| 155 |
const char *pszStartTag = "<MultiPolygon>"; |
| 156 |
const char *pszEndTag = ""; |
| 157 |
CPL_IGNORE_RET_VAL(pszEndTag); // Make CSA happy |
| 158 |
auto nNewPos = oKml.find(pszStartTag, nPos); |
| 159 |
if (nNewPos != std::string::npos) |
| 160 |
{ |
| 161 |
pszEndTag = "</MultiPolygon>"; |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
pszStartTag = "<MultiLineString>"; |
| 166 |
nNewPos = oKml.find(pszStartTag, nPos); |
| 167 |
if (nNewPos != std::string::npos) |
| 168 |
{ |
| 169 |
pszEndTag = "</MultiLineString>"; |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
pszStartTag = "<MultiPoint>"; |
| 174 |
nNewPos = oKml.find(pszStartTag, nPos); |
| 175 |
if (nNewPos != std::string::npos) |
| 176 |
pszEndTag = "</MultiPoint>"; |
| 177 |
else |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
nPos = nNewPos; |
| 182 |
oKml.replace(nPos, strlen(pszStartTag), "<MultiGeometry>"); |
| 183 |
nPos = oKml.find(pszEndTag, nPos); |
| 184 |
if (nPos == std::string::npos) |
| 185 |
break; |
| 186 |
oKml.replace(nPos, strlen(pszEndTag), "</MultiGeometry>"); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/************************************************************************/ |
| 191 |
/* OGRLIBKMLRemoveSpaces() */ |
| 192 |
/************************************************************************/ |
| 193 |
|
| 194 |
static void OGRLIBKMLRemoveSpaces(std::string &osKml, |
| 195 |
const std::string &osNeedle) |
| 196 |
{ |
| 197 |
size_t nPos = 0; |
| 198 |
const std::string osLtNeedle(std::string("<").append(osNeedle)); |
| 199 |
std::string osTmp; |
| 200 |
std::string osRet; |
| 201 |
while (true) |
| 202 |
{ |
| 203 |
auto nPosNew = osKml.find(osLtNeedle, nPos); |
| 204 |
if (nPosNew == std::string::npos) |
| 205 |
{ |
| 206 |
osRet.append(osKml, nPos); |
| 207 |
break; |
| 208 |
} |
| 209 |
const size_t nPosOri = nPosNew; |
| 210 |
nPosNew = osKml.find(">\n", nPosNew); |
| 211 |
if (nPosNew == std::string::npos || nPosNew + 2 == osKml.size()) |
| 212 |
{ |
| 213 |
osRet.append(osKml, nPos); |
| 214 |
break; |
| 215 |
} |
| 216 |
// Skip \n character |
| 217 |
osRet.append(osKml, nPos, nPosNew - nPos + 1); |
| 218 |
nPos = nPosNew + 2; |
| 219 |
|
| 220 |
// Remove leading spaces of " </{osNeedle}>" |
| 221 |
osTmp.clear(); |
| 222 |
for (size_t nPosTmp = nPosOri - 1; osKml[nPosTmp] == ' '; nPosTmp--) |
| 223 |
{ |
| 224 |
osTmp += ' '; |
| 225 |
} |
| 226 |
osTmp += "</"; |
| 227 |
osTmp += osNeedle; |
| 228 |
osTmp += '>'; |
| 229 |
nPosNew = osKml.find(osTmp, nPos); |
| 230 |
if (nPosNew != std::string::npos) |
| 231 |
{ |
| 232 |
osRet.append(osKml, nPos, nPosNew - nPos); |
| 233 |
osRet += "</"; |
| 234 |
osRet += osNeedle; |
| 235 |
osRet += '>'; |
| 236 |
nPos = nPosNew + osTmp.size(); |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
osRet.append(osKml, nPos); |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
osKml = std::move(osRet); |
| 245 |
} |
| 246 |
|
| 247 |
/************************************************************************/ |
| 248 |
/* OGRLIBKMLPostProcessOutput() */ |
| 249 |
/************************************************************************/ |
| 250 |
|
| 251 |
// Substitute deprecated <Snippet> by <snippet> since libkml currently |
| 252 |
// only supports Snippet but ogckml22.xsd has deprecated it in favor of snippet. |
| 253 |
static void OGRLIBKMLPostProcessOutput(std::string &oKml) |
| 254 |
{ |
| 255 |
// Manually add <?xml> node since libkml does not produce it currently |
| 256 |
// and this is useful in some circumstances (#5407). |
| 257 |
if (!(oKml.size() >= 2 && oKml[0] == '<' && oKml[1] == '?')) |
| 258 |
oKml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + oKml; |
| 259 |
|
| 260 |
size_t nPos = 0; |
| 261 |
while (true) |
| 262 |
{ |
| 263 |
nPos = oKml.find("<Snippet>", nPos); |
| 264 |
if (nPos == std::string::npos) |
| 265 |
{ |
| 266 |
break; |
| 267 |
} |
| 268 |
oKml[nPos + 1] = 's'; |
| 269 |
nPos = oKml.find("</Snippet>", nPos); |
| 270 |
if (nPos == std::string::npos) |
| 271 |
{ |
| 272 |
break; |
| 273 |
} |
| 274 |
oKml[nPos + 2] = 's'; |
| 275 |
} |
| 276 |
|
| 277 |
// Fix indentation problems. |
| 278 |
OGRLIBKMLRemoveSpaces(oKml, "snippet"); |
| 279 |
OGRLIBKMLRemoveSpaces(oKml, "linkSnippet"); |
| 280 |
OGRLIBKMLRemoveSpaces(oKml, "SimpleData"); |
| 281 |
} |
| 282 |
|
| 283 |
/****************************************************************************** |
| 284 |
Method to write a single file ds .kml at ds destroy. |
| 285 |
|
| 286 |
Args: none |
| 287 |
|
| 288 |
Returns: nothing |
| 289 |
|
| 290 |
******************************************************************************/ |
| 291 |
|
| 292 |
bool OGRLIBKMLDataSource::WriteKml() |
| 293 |
{ |
| 294 |
std::string oKmlFilename = GetDescription(); |
| 295 |
|
| 296 |
if (m_poKmlDSContainer && m_poKmlDSContainer->IsA(kmldom::Type_Document)) |
| 297 |
{ |
| 298 |
DocumentPtr poKmlDocument = AsDocument(m_poKmlDSContainer); |
| 299 |
|
| 300 |
ParseDocumentOptions(m_poKmlDSKml, poKmlDocument); |
| 301 |
|
| 302 |
for (int iLayer = 0; iLayer < nLayers; iLayer++) |
| 303 |
{ |
| 304 |
SchemaPtr poKmlSchema = nullptr; |
| 305 |
|
| 306 |
if ((poKmlSchema = papoLayers[iLayer]->GetKmlSchema())) |
| 307 |
{ |
| 308 |
const size_t nKmlSchemas = |
| 309 |
poKmlDocument->get_schema_array_size(); |
| 310 |
SchemaPtr poKmlSchema2 = nullptr; |
| 311 |
|
| 312 |
for (size_t iKmlSchema = 0; iKmlSchema < nKmlSchemas; |
| 313 |
iKmlSchema++) |
| 314 |
{ |
| 315 |
poKmlSchema2 = |
| 316 |
poKmlDocument->get_schema_array_at(iKmlSchema); |
| 317 |
if (poKmlSchema2 == poKmlSchema) |
| 318 |
break; |
| 319 |
} |
| 320 |
|
| 321 |
if (poKmlSchema2 != poKmlSchema) |
| 322 |
poKmlDocument->add_schema(poKmlSchema); |
| 323 |
} |
| 324 |
|
| 325 |
papoLayers[iLayer]->Finalize(poKmlDocument); |
| 326 |
} |
| 327 |
} |
| 328 |
else |
| 329 |
{ |
| 330 |
ParseDocumentOptions(m_poKmlDSKml, nullptr); |
| 331 |
} |
| 332 |
|
| 333 |
std::string oKmlOut; |
| 334 |
oKmlOut = kmldom::SerializePretty(m_poKmlDSKml); |
| 335 |
OGRLIBKMLPostProcessOutput(oKmlOut); |
| 336 |
|
| 337 |
bool bRet = true; |
| 338 |
if (!oKmlOut.empty()) |
| 339 |
{ |
| 340 |
VSILFILE *fp = VSIFOpenExL(oKmlFilename.c_str(), "wb", true); |
| 341 |
if (fp == nullptr) |
| 342 |
{ |
| 343 |
CPLError(CE_Failure, CPLE_FileIO, "Error writing %s: %s", |
| 344 |
oKmlFilename.c_str(), VSIGetLastErrorMsg()); |
| 345 |
return false; |
| 346 |
} |
| 347 |
|
| 348 |
if (VSIFWriteL(oKmlOut.data(), 1, oKmlOut.size(), fp) != oKmlOut.size()) |
| 349 |
bRet = false; |
| 350 |
if (VSIFCloseL(fp) != 0) |
| 351 |
bRet = false; |
| 352 |
} |
| 353 |
return bRet; |
| 354 |
} |
| 355 |
|
| 356 |
/************************************************************************/ |
| 357 |
/* OGRLIBKMLCreateOGCKml22() */ |
| 358 |
/************************************************************************/ |
| 359 |
|
| 360 |
static KmlPtr OGRLIBKMLCreateOGCKml22(KmlFactory *poFactory, |
| 361 |
CSLConstList papszOptions = nullptr) |
| 362 |
{ |
| 363 |
const char *pszAuthorName = CSLFetchNameValue(papszOptions, "AUTHOR_NAME"); |
| 364 |
const char *pszAuthorURI = CSLFetchNameValue(papszOptions, "AUTHOR_URI"); |
| 365 |
const char *pszAuthorEmail = |
| 366 |
CSLFetchNameValue(papszOptions, "AUTHOR_EMAIL"); |
| 367 |
const char *pszLink = CSLFetchNameValue(papszOptions, "LINK"); |
| 368 |
const bool bWithAtom = pszAuthorName != nullptr || |
| 369 |
pszAuthorURI != nullptr || |
| 370 |
pszAuthorEmail != nullptr || pszLink != nullptr; |
| 371 |
|
| 372 |
KmlPtr kml = poFactory->CreateKml(); |
| 373 |
if (bWithAtom) |
| 374 |
{ |
| 375 |
const char *kAttrs[] = {"xmlns", "http://www.opengis.net/kml/2.2", |
| 376 |
"xmlns:atom", "http://www.w3.org/2005/Atom", |
| 377 |
nullptr}; |
| 378 |
kml->AddUnknownAttributes(Attributes::Create(kAttrs)); |
| 379 |
} |
| 380 |
else |
| 381 |
{ |
| 382 |
const char *kAttrs[] = {"xmlns", "http://www.opengis.net/kml/2.2", |
| 383 |
nullptr}; |
| 384 |
kml->AddUnknownAttributes(Attributes::Create(kAttrs)); |
| 385 |
} |
| 386 |
return kml; |
| 387 |
} |
| 388 |
|
| 389 |
/****************************************************************************** |
| 390 |
Method to write a ds .kmz at ds destroy. |
| 391 |
|
| 392 |
Args: none |
| 393 |
|
| 394 |
Returns: nothing |
| 395 |
|
| 396 |
******************************************************************************/ |
| 397 |
|
| 398 |
bool OGRLIBKMLDataSource::WriteKmz() |
| 399 |
{ |
| 400 |
std::string osTmpFilename; |
| 401 |
if (!VSISupportsRandomWrite(GetDescription(), false) || |
| 402 |
EQUAL(CPLGetConfigOption("CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE", ""), |
| 403 |
"FORCED")) |
| 404 |
{ |
| 405 |
osTmpFilename = CPLGenerateTempFilenameSafe( |
| 406 |
CPLGetBasenameSafe(GetDescription()).c_str()); |
| 407 |
} |
| 408 |
|
| 409 |
void *hZIP = CPLCreateZip(osTmpFilename.empty() ? GetDescription() |
| 410 |
: osTmpFilename.c_str(), |
| 411 |
nullptr); |
| 412 |
|
| 413 |
if (!hZIP) |
| 414 |
{ |
| 415 |
CPLError(CE_Failure, CPLE_NoWriteAccess, "Error creating %s: %s", |
| 416 |
GetDescription(), VSIGetLastErrorMsg()); |
| 417 |
return false; |
| 418 |
} |
| 419 |
|
| 420 |
bool bRet = true; |
| 421 |
|
| 422 |
/***** write out the doc.kml ****/ |
| 423 |
const char *pszUseDocKml = CPLGetConfigOption("LIBKML_USE_DOC.KML", "yes"); |
| 424 |
|
| 425 |
if (CPLTestBool(pszUseDocKml) && (m_poKmlDocKml || m_poKmlUpdate)) |
| 426 |
{ |
| 427 |
// If we do not have the doc.kmlroot |
| 428 |
// make it and add the container. |
| 429 |
if (!m_poKmlDocKmlRoot) |
| 430 |
{ |
| 431 |
m_poKmlDocKmlRoot = |
| 432 |
OGRLIBKMLCreateOGCKml22(m_poKmlFactory, m_papszOptions); |
| 433 |
|
| 434 |
auto kml = AsKml(m_poKmlDocKmlRoot); |
| 435 |
if (m_poKmlDocKml) |
| 436 |
{ |
| 437 |
if (kml) |
| 438 |
{ |
| 439 |
kml->set_feature(m_poKmlDocKml); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
ParseDocumentOptions(std::move(kml), AsDocument(m_poKmlDocKml)); |
| 444 |
} |
| 445 |
|
| 446 |
std::string oKmlOut = kmldom::SerializePretty(m_poKmlDocKmlRoot); |
| 447 |
OGRLIBKMLPostProcessOutput(oKmlOut); |
| 448 |
|
| 449 |
if (CPLCreateFileInZip(hZIP, "doc.kml", nullptr) != CE_None || |
| 450 |
CPLWriteFileInZip(hZIP, oKmlOut.data(), |
| 451 |
static_cast<int>(oKmlOut.size())) != CE_None) |
| 452 |
{ |
| 453 |
bRet = false; |
| 454 |
CPLError(CE_Failure, CPLE_FileIO, "ERROR adding %s to %s", |
| 455 |
"doc.kml", GetDescription()); |
| 456 |
} |
| 457 |
CPLCloseFileInZip(hZIP); |
| 458 |
} |
| 459 |
|
| 460 |
/***** loop though the layers and write them *****/ |
| 461 |
for (int iLayer = 0; iLayer < nLayers && !m_poKmlUpdate; iLayer++) |
| 462 |
{ |
| 463 |
ContainerPtr poKmlContainer = papoLayers[iLayer]->GetKmlLayer(); |
| 464 |
|
| 465 |
if (poKmlContainer->IsA(kmldom::Type_Document)) |
| 466 |
{ |
| 467 |
DocumentPtr poKmlDocument = AsDocument(poKmlContainer); |
| 468 |
SchemaPtr poKmlSchema = papoLayers[iLayer]->GetKmlSchema(); |
| 469 |
|
| 470 |
if (!poKmlDocument->get_schema_array_size() && poKmlSchema && |
| 471 |
poKmlSchema->get_simplefield_array_size()) |
| 472 |
{ |
| 473 |
poKmlDocument->add_schema(std::move(poKmlSchema)); |
| 474 |
} |
| 475 |
|
| 476 |
papoLayers[iLayer]->Finalize(std::move(poKmlDocument)); |
| 477 |
} |
| 478 |
|
| 479 |
// If we do not have the layers root |
| 480 |
// make it and add the container. |
| 481 |
KmlPtr poKmlKml = nullptr; |
| 482 |
|
| 483 |
if (!(poKmlKml = AsKml(papoLayers[iLayer]->GetKmlLayerRoot()))) |
| 484 |
{ |
| 485 |
poKmlKml = OGRLIBKMLCreateOGCKml22(m_poKmlFactory); |
| 486 |
|
| 487 |
poKmlKml->set_feature(poKmlContainer); |
| 488 |
} |
| 489 |
|
| 490 |
std::string oKmlOut = kmldom::SerializePretty(poKmlKml); |
| 491 |
OGRLIBKMLPostProcessOutput(oKmlOut); |
| 492 |
|
| 493 |
if (iLayer == 0 && CPLTestBool(pszUseDocKml)) |
| 494 |
{ |
| 495 |
CPL_IGNORE_RET_VAL(CPLCreateFileInZip(hZIP, "layers/", nullptr)); |
| 496 |
} |
| 497 |
|
| 498 |
const char *pszLayerFileName = nullptr; |
| 499 |
if (CPLTestBool(pszUseDocKml)) |
| 500 |
pszLayerFileName = |
| 501 |
CPLSPrintf("layers/%s", papoLayers[iLayer]->GetFileName()); |
| 502 |
else |
| 503 |
pszLayerFileName = papoLayers[iLayer]->GetFileName(); |
| 504 |
|
| 505 |
if (CPLCreateFileInZip(hZIP, pszLayerFileName, nullptr) != CE_None || |
| 506 |
CPLWriteFileInZip(hZIP, oKmlOut.data(), |
| 507 |
static_cast<int>(oKmlOut.size())) != CE_None) |
| 508 |
CPLError(CE_Failure, CPLE_FileIO, "ERROR adding %s to %s", |
| 509 |
papoLayers[iLayer]->GetFileName(), GetDescription()); |
| 510 |
CPLCloseFileInZip(hZIP); |
| 511 |
} |
| 512 |
|
| 513 |
/***** write the style table *****/ |
| 514 |
if (m_poKmlStyleKml) |
| 515 |
{ |
| 516 |
KmlPtr poKmlKml = OGRLIBKMLCreateOGCKml22(m_poKmlFactory); |
| 517 |
|
| 518 |
poKmlKml->set_feature(m_poKmlStyleKml); |
| 519 |
std::string oKmlOut = kmldom::SerializePretty(poKmlKml); |
| 520 |
OGRLIBKMLPostProcessOutput(oKmlOut); |
| 521 |
|
| 522 |
if (CPLCreateFileInZip(hZIP, "style/", nullptr) != CE_None || |
| 523 |
CPLCreateFileInZip(hZIP, "style/style.kml", nullptr) != CE_None || |
| 524 |
CPLWriteFileInZip(hZIP, oKmlOut.data(), |
| 525 |
static_cast<int>(oKmlOut.size())) != CE_None) |
| 526 |
{ |
| 527 |
bRet = false; |
| 528 |
CPLError(CE_Failure, CPLE_FileIO, "ERROR adding %s to %s", |
| 529 |
"style/style.kml", GetDescription()); |
| 530 |
} |
| 531 |
CPLCloseFileInZip(hZIP); |
| 532 |
} |
| 533 |
|
| 534 |
CPLCloseZip(hZIP); |
| 535 |
|
| 536 |
if (!osTmpFilename.empty()) |
| 537 |
{ |
| 538 |
if (bRet) |
| 539 |
{ |
| 540 |
bRet = CPLCopyFile(GetDescription(), osTmpFilename.c_str()) == 0; |
| 541 |
if (!bRet) |
| 542 |
CPLError(CE_Failure, CPLE_FileIO, |
| 543 |
"Cannot copy temporary file to %s", GetDescription()); |
| 544 |
} |
| 545 |
VSIUnlink(osTmpFilename.c_str()); |
| 546 |
} |
| 547 |
|
| 548 |
return bRet; |
| 549 |
} |
| 550 |
|
| 551 |
/****************************************************************************** |
| 552 |
Method to write a dir ds at ds destroy. |
| 553 |
|
| 554 |
Args: none |
| 555 |
|
| 556 |
Returns: nothing |
| 557 |
|
| 558 |
******************************************************************************/ |
| 559 |
|
| 560 |
bool OGRLIBKMLDataSource::WriteDir() |
| 561 |
{ |
| 562 |
/***** write out the doc.kml ****/ |
| 563 |
const char *pszUseDocKml = CPLGetConfigOption("LIBKML_USE_DOC.KML", "yes"); |
| 564 |
|
| 565 |
bool bRet = true; |
| 566 |
if (CPLTestBool(pszUseDocKml) && (m_poKmlDocKml || m_poKmlUpdate)) |
| 567 |
{ |
| 568 |
// If we don't have the doc.kml root |
| 569 |
// make it and add the container. |
| 570 |
if (!m_poKmlDocKmlRoot) |
| 571 |
{ |
| 572 |
m_poKmlDocKmlRoot = |
| 573 |
OGRLIBKMLCreateOGCKml22(m_poKmlFactory, m_papszOptions); |
| 574 |
auto kml = AsKml(m_poKmlDocKmlRoot); |
| 575 |
if (kml) |
| 576 |
{ |
| 577 |
if (m_poKmlDocKml) |
| 578 |
kml->set_feature(m_poKmlDocKml); |
| 579 |
} |
| 580 |
|
| 581 |
ParseDocumentOptions(std::move(kml), AsDocument(m_poKmlDocKml)); |
| 582 |
} |
| 583 |
|
| 584 |
std::string oKmlOut = kmldom::SerializePretty(m_poKmlDocKmlRoot); |
| 585 |
OGRLIBKMLPostProcessOutput(oKmlOut); |
| 586 |
|
| 587 |
const std::string osOutfile = |
| 588 |
CPLFormFilenameSafe(GetDescription(), "doc.kml", nullptr); |
| 589 |
|
| 590 |
VSILFILE *fp = VSIFOpenExL(osOutfile.c_str(), "wb", true); |
| 591 |
if (fp == nullptr) |
| 592 |
{ |
| 593 |
CPLError(CE_Failure, CPLE_FileIO, "Error writing %s to %s: %s", |
| 594 |
"doc.kml", GetDescription(), VSIGetLastErrorMsg()); |
| 595 |
return false; |
| 596 |
} |
| 597 |
|
| 598 |
if (VSIFWriteL(oKmlOut.data(), 1, oKmlOut.size(), fp) != oKmlOut.size()) |
| 599 |
bRet = false; |
| 600 |
if (VSIFCloseL(fp) != 0) |
| 601 |
bRet = false; |
| 602 |
} |
| 603 |
|
| 604 |
/***** loop though the layers and write them *****/ |
| 605 |
for (int iLayer = 0; iLayer < nLayers && !m_poKmlUpdate; iLayer++) |
| 606 |
{ |
| 607 |
ContainerPtr poKmlContainer = papoLayers[iLayer]->GetKmlLayer(); |
| 608 |
|
| 609 |
if (poKmlContainer->IsA(kmldom::Type_Document)) |
| 610 |
{ |
| 611 |
DocumentPtr poKmlDocument = AsDocument(poKmlContainer); |
| 612 |
SchemaPtr poKmlSchema = papoLayers[iLayer]->GetKmlSchema(); |
| 613 |
|
| 614 |
if (!poKmlDocument->get_schema_array_size() && poKmlSchema && |
| 615 |
poKmlSchema->get_simplefield_array_size()) |
| 616 |
{ |
| 617 |
poKmlDocument->add_schema(std::move(poKmlSchema)); |
| 618 |
} |
| 619 |
|
| 620 |
papoLayers[iLayer]->Finalize(std::move(poKmlDocument)); |
| 621 |
} |
| 622 |
|
| 623 |
// If we do not have the layers root |
| 624 |
// make it and add the container. |
| 625 |
KmlPtr poKmlKml = nullptr; |
| 626 |
|
| 627 |
if (!(poKmlKml = AsKml(papoLayers[iLayer]->GetKmlLayerRoot()))) |
| 628 |
{ |
| 629 |
poKmlKml = OGRLIBKMLCreateOGCKml22(m_poKmlFactory); |
| 630 |
|
| 631 |
poKmlKml->set_feature(poKmlContainer); |
| 632 |
} |
| 633 |
|
| 634 |
std::string oKmlOut = kmldom::SerializePretty(poKmlKml); |
| 635 |
OGRLIBKMLPostProcessOutput(oKmlOut); |
| 636 |
|
| 637 |
const std::string osOutfile = CPLFormFilenameSafe( |
| 638 |
GetDescription(), papoLayers[iLayer]->GetFileName(), nullptr); |
| 639 |
|
| 640 |
VSILFILE *fp = VSIFOpenL(osOutfile.c_str(), "wb"); |
| 641 |
if (fp == nullptr) |
| 642 |
{ |
| 643 |
CPLError(CE_Failure, CPLE_FileIO, "ERROR Writing %s to %s", |
| 644 |
papoLayers[iLayer]->GetFileName(), GetDescription()); |
| 645 |
return false; |
| 646 |
} |
| 647 |
|
| 648 |
if (VSIFWriteL(oKmlOut.data(), 1, oKmlOut.size(), fp) != oKmlOut.size()) |
| 649 |
bRet = false; |
| 650 |
if (VSIFCloseL(fp) != 0) |
| 651 |
bRet = false; |
| 652 |
} |
| 653 |
|
| 654 |
/***** write the style table *****/ |
| 655 |
if (m_poKmlStyleKml) |
| 656 |
{ |
| 657 |
KmlPtr poKmlKml = OGRLIBKMLCreateOGCKml22(m_poKmlFactory); |
| 658 |
|
| 659 |
poKmlKml->set_feature(m_poKmlStyleKml); |
| 660 |
std::string oKmlOut = kmldom::SerializePretty(poKmlKml); |
| 661 |
OGRLIBKMLPostProcessOutput(oKmlOut); |
| 662 |
|
| 663 |
const std::string osOutfile = |
| 664 |
CPLFormFilenameSafe(GetDescription(), "style.kml", nullptr); |
| 665 |
|
| 666 |
VSILFILE *fp = VSIFOpenL(osOutfile.c_str(), "wb"); |
| 667 |
if (fp == nullptr) |
| 668 |
{ |
| 669 |
CPLError(CE_Failure, CPLE_FileIO, "ERROR Writing %s to %s", |
| 670 |
"style.kml", GetDescription()); |
| 671 |
return false; |
| 672 |
} |
| 673 |
|
| 674 |
if (VSIFWriteL(oKmlOut.data(), 1, oKmlOut.size(), fp) != oKmlOut.size()) |
| 675 |
bRet = false; |
| 676 |
if (VSIFCloseL(fp) != 0) |
| 677 |
bRet = false; |
| 678 |
} |
| 679 |
return bRet; |
| 680 |
} |
| 681 |
|
| 682 |
/****************************************************************************** |
| 683 |
Method to write the datasource to disk. |
| 684 |
|
| 685 |
Args: none |
| 686 |
|
| 687 |
Returns nothing |
| 688 |
|
| 689 |
******************************************************************************/ |
| 690 |
|
| 691 |
CPLErr OGRLIBKMLDataSource::FlushCache(bool /* bAtClosing */) |
| 692 |
{ |
| 693 |
if (!bUpdated) |
| 694 |
return CE_None; |
| 695 |
|
| 696 |
CPLErr eErr = CE_None; |
| 697 |
if (bUpdate && IsKml()) |
| 698 |
{ |
| 699 |
if (!WriteKml()) |
| 700 |
eErr = CE_Failure; |
| 701 |
} |
| 702 |
else if (bUpdate && IsKmz()) |
| 703 |
{ |
| 704 |
if (!WriteKmz()) |
| 705 |
eErr = CE_Failure; |
| 706 |
} |
| 707 |
else if (bUpdate && IsDir()) |
| 708 |
{ |
| 709 |
if (!WriteDir()) |
| 710 |
eErr = CE_Failure; |
| 711 |
} |
| 712 |
|
| 713 |
bUpdated = false; |
| 714 |
return eErr; |
| 715 |
} |
| 716 |
|
| 717 |
/****************************************************************************** |
| 718 |
OGRLIBKMLDataSource Destructor |
| 719 |
|
| 720 |
Args: none |
| 721 |
|
| 722 |
Returns: nothing |
| 723 |
|
| 724 |
******************************************************************************/ |
| 725 |
|
| 726 |
OGRLIBKMLDataSource::~OGRLIBKMLDataSource() |
| 727 |
{ |
| 728 |
/***** sync the DS to disk *****/ |
| 729 |
OGRLIBKMLDataSource::FlushCache(true); |
| 730 |
|
| 731 |
for (int i = 0; i < nLayers; i++) |
| 732 |
delete papoLayers[i]; |
| 733 |
|
| 734 |
CPLFree(papoLayers); |
| 735 |
|
| 736 |
CSLDestroy(m_papszOptions); |
| 737 |
} |
| 738 |
|
| 739 |
/****************************************************************************** |
| 740 |
Method to parse a schemas out of a document. |
| 741 |
|
| 742 |
Args: poKmlDocument pointer to the document to parse |
| 743 |
|
| 744 |
Returns: nothing |
| 745 |
|
| 746 |
******************************************************************************/ |
| 747 |
|
| 748 |
SchemaPtr OGRLIBKMLDataSource::FindSchema(const char *pszSchemaUrl) |
| 749 |
{ |
| 750 |
if (!pszSchemaUrl || !*pszSchemaUrl) |
| 751 |
return nullptr; |
| 752 |
|
| 753 |
char *pszID = nullptr; |
| 754 |
char *pszFile = nullptr; |
| 755 |
char *pszSchemaName = nullptr; |
| 756 |
DocumentPtr poKmlDocument = nullptr; |
| 757 |
SchemaPtr poKmlSchemaResult = nullptr; |
| 758 |
|
| 759 |
if (*pszSchemaUrl == '#') |
| 760 |
{ |
| 761 |
pszID = CPLStrdup(pszSchemaUrl + 1); |
| 762 |
|
| 763 |
/***** kml *****/ |
| 764 |
if (IsKml() && m_poKmlDSContainer->IsA(kmldom::Type_Document)) |
| 765 |
poKmlDocument = AsDocument(m_poKmlDSContainer); |
| 766 |
|
| 767 |
/***** kmz *****/ |
| 768 |
else if ((IsKmz() || IsDir()) && m_poKmlDocKml && |
| 769 |
m_poKmlDocKml->IsA(kmldom::Type_Document)) |
| 770 |
poKmlDocument = AsDocument(m_poKmlDocKml); |
| 771 |
} |
| 772 |
else if (const char *pszPound = strchr(pszSchemaUrl, '#')) |
| 773 |
{ |
| 774 |
pszFile = CPLStrdup(pszSchemaUrl); |
| 775 |
pszID = CPLStrdup(pszPound + 1); |
| 776 |
pszFile[pszPound - pszSchemaUrl] = '\0'; |
| 777 |
} |
| 778 |
else |
| 779 |
{ |
| 780 |
pszSchemaName = CPLStrdup(pszSchemaUrl); |
| 781 |
|
| 782 |
/***** kml *****/ |
| 783 |
if (IsKml() && m_poKmlDSContainer->IsA(kmldom::Type_Document)) |
| 784 |
poKmlDocument = AsDocument(m_poKmlDSContainer); |
| 785 |
|
| 786 |
/***** kmz *****/ |
| 787 |
|
| 788 |
else if ((IsKmz() || IsDir()) && m_poKmlDocKml && |
| 789 |
m_poKmlDocKml->IsA(kmldom::Type_Document)) |
| 790 |
poKmlDocument = AsDocument(m_poKmlDocKml); |
| 791 |
} |
| 792 |
|
| 793 |
if (poKmlDocument) |
| 794 |
{ |
| 795 |
size_t nKmlSchemas = poKmlDocument->get_schema_array_size(); |
| 796 |
|
| 797 |
for (size_t iKmlSchema = 0; iKmlSchema < nKmlSchemas; iKmlSchema++) |
| 798 |
{ |
| 799 |
SchemaPtr poKmlSchema = |
| 800 |
poKmlDocument->get_schema_array_at(iKmlSchema); |
| 801 |
if (poKmlSchema->has_id() && pszID) |
| 802 |
{ |
| 803 |
if (EQUAL(pszID, poKmlSchema->get_id().c_str())) |
| 804 |
{ |
| 805 |
poKmlSchemaResult = std::move(poKmlSchema); |
| 806 |
break; |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
else if (poKmlSchema->has_name() && pszSchemaName) |
| 811 |
{ |
| 812 |
if (EQUAL(pszSchemaName, poKmlSchema->get_name().c_str())) |
| 813 |
{ |
| 814 |
poKmlSchemaResult = std::move(poKmlSchema); |
| 815 |
break; |
| 816 |
} |
| 817 |
} |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
CPLFree(pszFile); |
| 822 |
CPLFree(pszID); |
| 823 |
CPLFree(pszSchemaName); |
| 824 |
|
| 825 |
return poKmlSchemaResult; |
| 826 |
} |
| 827 |
|
| 828 |
/****************************************************************************** |
| 829 |
Method to allocate memory for the layer array, create the layer, |
| 830 |
and add it to the layer array. |
| 831 |
|
| 832 |
Args: pszLayerName the name of the layer |
| 833 |
eGType the layers geometry type |
| 834 |
poOgrDS pointer to the datasource the layer is in |
| 835 |
poKmlRoot pointer to the root kml element of the layer |
| 836 |
pszFileName the filename of the layer |
| 837 |
bNew true if its a new layer |
| 838 |
bUpdate true if the layer is writable |
| 839 |
nGuess a guess at the number of additional layers |
| 840 |
we are going to need |
| 841 |
|
| 842 |
Returns: Pointer to the new layer |
| 843 |
******************************************************************************/ |
| 844 |
|
| 845 |
OGRLIBKMLLayer *OGRLIBKMLDataSource::AddLayer( |
| 846 |
const char *pszLayerName, OGRwkbGeometryType eGType, |
| 847 |
const OGRSpatialReference *poSRS, OGRLIBKMLDataSource *poOgrDS, |
| 848 |
ElementPtr poKmlRoot, ContainerPtr poKmlContainer, const char *pszFileName, |
| 849 |
bool bNew, bool bUpdateIn, int nGuess) |
| 850 |
{ |
| 851 |
// Build unique layer name |
| 852 |
CPLString osUniqueLayername(pszLayerName); |
| 853 |
int nIter = 2; |
| 854 |
while (true) |
| 855 |
{ |
| 856 |
if (GetLayerByName(osUniqueLayername) == nullptr) |
| 857 |
break; |
| 858 |
osUniqueLayername = CPLSPrintf("%s (#%d)", pszLayerName, nIter); |
| 859 |
nIter++; |
| 860 |
} |
| 861 |
|
| 862 |
/***** check to see if we have enough space to store the layer *****/ |
| 863 |
if (nLayers == nAllocated) |
| 864 |
{ |
| 865 |
nAllocated += nGuess; |
| 866 |
papoLayers = static_cast<OGRLIBKMLLayer **>( |
| 867 |
CPLRealloc(papoLayers, sizeof(OGRLIBKMLLayer *) * nAllocated)); |
| 868 |
} |
| 869 |
|
| 870 |
/***** create the layer *****/ |
| 871 |
OGRLIBKMLLayer *poOgrLayer = new OGRLIBKMLLayer( |
| 872 |
osUniqueLayername, eGType, poSRS, poOgrDS, std::move(poKmlRoot), |
| 873 |
std::move(poKmlContainer), m_poKmlUpdate, pszFileName, bNew, bUpdateIn); |
| 874 |
|
| 875 |
/***** add the layer to the array *****/ |
| 876 |
const int iLayer = nLayers++; |
| 877 |
papoLayers[iLayer] = poOgrLayer; |
| 878 |
osUniqueLayername.toupper(); |
| 879 |
m_oMapLayers[std::move(osUniqueLayername)] = poOgrLayer; |
| 880 |
|
| 881 |
return poOgrLayer; |
| 882 |
} |
| 883 |
|
| 884 |
/****************************************************************************** |
| 885 |
Method to parse multiple layers out of a container. |
| 886 |
|
| 887 |
Args: poKmlContainer pointer to the container to parse |
| 888 |
|
| 889 |
Returns: number of features in the container that are not another |
| 890 |
container |
| 891 |
|
| 892 |
******************************************************************************/ |
| 893 |
|
| 894 |
int OGRLIBKMLDataSource::ParseLayers(ContainerPtr poKmlContainer, bool bRecurse) |
| 895 |
{ |
| 896 |
/***** if container is null just bail now *****/ |
| 897 |
if (!poKmlContainer) |
| 898 |
return 0; |
| 899 |
|
| 900 |
const size_t nKmlFeatures = poKmlContainer->get_feature_array_size(); |
| 901 |
|
| 902 |
/***** loop over the container to separate the style, layers, etc *****/ |
| 903 |
|
| 904 |
int nResult = 0; |
| 905 |
for (size_t iKmlFeature = 0; iKmlFeature < nKmlFeatures; iKmlFeature++) |
| 906 |
{ |
| 907 |
FeaturePtr poKmlFeat = |
| 908 |
poKmlContainer->get_feature_array_at(iKmlFeature); |
| 909 |
|
| 910 |
/***** container *****/ |
| 911 |
|
| 912 |
if (poKmlFeat->IsA(kmldom::Type_Container)) |
| 913 |
{ |
| 914 |
if (bRecurse) |
| 915 |
{ |
| 916 |
/***** see if the container has a name *****/ |
| 917 |
|
| 918 |
std::string oKmlFeatName; |
| 919 |
if (poKmlFeat->has_name()) |
| 920 |
{ |
| 921 |
/* Strip leading and trailing spaces */ |
| 922 |
const char *l_pszName = poKmlFeat->get_name().c_str(); |
| 923 |
while (*l_pszName == ' ' || *l_pszName == '\n' || |
| 924 |
*l_pszName == '\r' || *l_pszName == '\t') |
| 925 |
l_pszName++; |
| 926 |
oKmlFeatName = l_pszName; |
| 927 |
int nSize = (int)oKmlFeatName.size(); |
| 928 |
while (nSize > 0 && (oKmlFeatName[nSize - 1] == ' ' || |
| 929 |
oKmlFeatName[nSize - 1] == '\n' || |
| 930 |
oKmlFeatName[nSize - 1] == '\r' || |
| 931 |
oKmlFeatName[nSize - 1] == '\t')) |
| 932 |
{ |
| 933 |
nSize--; |
| 934 |
oKmlFeatName.resize(nSize); |
| 935 |
} |
| 936 |
} |
| 937 |
/***** use the feature index number as the name *****/ |
| 938 |
/***** not sure i like this c++ ich *****/ |
| 939 |
else |
| 940 |
{ |
| 941 |
std::stringstream oOut; |
| 942 |
oOut << iKmlFeature; |
| 943 |
oKmlFeatName = "Layer"; |
| 944 |
oKmlFeatName.append(oOut.str()); |
| 945 |
} |
| 946 |
|
| 947 |
/***** create the layer *****/ |
| 948 |
|
| 949 |
AddLayer(oKmlFeatName.c_str(), wkbUnknown, nullptr, this, |
| 950 |
nullptr, AsContainer(poKmlFeat), "", false, bUpdate, |
| 951 |
static_cast<int>(nKmlFeatures)); |
| 952 |
|
| 953 |
/***** check if any features are another layer *****/ |
| 954 |
ParseLayers(AsContainer(poKmlFeat), true); |
| 955 |
} |
| 956 |
} |
| 957 |
else |
| 958 |
{ |
| 959 |
nResult++; |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
return nResult; |
| 964 |
} |
| 965 |
|
| 966 |
/****************************************************************************** |
| 967 |
Function to get the container from the kmlroot. |
| 968 |
|
| 969 |
Args: poKmlRoot the root element |
| 970 |
|
| 971 |
Returns: root if its a container, if its a kml the container it |
| 972 |
contains, or NULL |
| 973 |
|
| 974 |
******************************************************************************/ |
| 975 |
|
| 976 |
static ContainerPtr GetContainerFromRoot(KmlFactory *poKmlFactory, |
| 977 |
ElementPtr poKmlRoot) |
| 978 |
{ |
| 979 |
ContainerPtr poKmlContainer = nullptr; |
| 980 |
|
| 981 |
const bool bReadGroundOverlay = |
| 982 |
CPLTestBool(CPLGetConfigOption("LIBKML_READ_GROUND_OVERLAY", "YES")); |
| 983 |
|
| 984 |
if (poKmlRoot) |
| 985 |
{ |
| 986 |
/***** skip over the <kml> we want the container *****/ |
| 987 |
if (poKmlRoot->IsA(kmldom::Type_kml)) |
| 988 |
{ |
| 989 |
KmlPtr poKmlKml = AsKml(poKmlRoot); |
| 990 |
|
| 991 |
if (poKmlKml && poKmlKml->has_feature()) |
| 992 |
{ |
| 993 |
FeaturePtr poKmlFeat = poKmlKml->get_feature(); |
| 994 |
|
| 995 |
if (poKmlFeat->IsA(kmldom::Type_Container)) |
| 996 |
poKmlContainer = AsContainer(poKmlFeat); |
| 997 |
else if (poKmlFeat->IsA(kmldom::Type_Placemark) || |
| 998 |
(bReadGroundOverlay && |
| 999 |
poKmlFeat->IsA(kmldom::Type_GroundOverlay))) |
| 1000 |
{ |
| 1001 |
poKmlContainer = poKmlFactory->CreateDocument(); |
| 1002 |
poKmlContainer->add_feature( |
| 1003 |
kmldom::AsFeature(kmlengine::Clone(poKmlFeat))); |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
else if (poKmlRoot->IsA(kmldom::Type_Container)) |
| 1008 |
{ |
| 1009 |
poKmlContainer = AsContainer(std::move(poKmlRoot)); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
return poKmlContainer; |
| 1014 |
} |
| 1015 |
|
| 1016 |
/****************************************************************************** |
| 1017 |
Method to parse a kml string into the style table. |
| 1018 |
******************************************************************************/ |
| 1019 |
|
| 1020 |
int OGRLIBKMLDataSource::ParseIntoStyleTable(std::string *poKmlStyleKml, |
| 1021 |
const char *pszMyStylePath) |
| 1022 |
{ |
| 1023 |
/***** parse the kml into the dom *****/ |
| 1024 |
std::string oKmlErrors; |
| 1025 |
ElementPtr poKmlRoot = OGRLIBKMLParse(*poKmlStyleKml, &oKmlErrors); |
| 1026 |
|
| 1027 |
if (!poKmlRoot) |
| 1028 |
{ |
| 1029 |
CPLError(CE_Failure, CPLE_OpenFailed, "ERROR parsing style kml %s :%s", |
| 1030 |
pszMyStylePath, oKmlErrors.c_str()); |
| 1031 |
return false; |
| 1032 |
} |
| 1033 |
|
| 1034 |
ContainerPtr poKmlContainer = nullptr; |
| 1035 |
|
| 1036 |
if (!(poKmlContainer = |
| 1037 |
GetContainerFromRoot(m_poKmlFactory, std::move(poKmlRoot)))) |
| 1038 |
{ |
| 1039 |
return false; |
| 1040 |
} |
| 1041 |
|
| 1042 |
ParseStyles(AsDocument(std::move(poKmlContainer)), &m_poStyleTable); |
| 1043 |
m_osStylePath = pszMyStylePath; |
| 1044 |
|
| 1045 |
return true; |
| 1046 |
} |
| 1047 |
|
| 1048 |
/****************************************************************************** |
| 1049 |
Method to open a kml file. |
| 1050 |
|
| 1051 |
Args: pszFilename file to open |
| 1052 |
bUpdate update mode |
| 1053 |
|
| 1054 |
Returns: True on success, false on failure |
| 1055 |
|
| 1056 |
******************************************************************************/ |
| 1057 |
|
| 1058 |
int OGRLIBKMLDataSource::OpenKml(const char *pszFilename, int bUpdateIn) |
| 1059 |
{ |
| 1060 |
std::string oKmlKml; |
| 1061 |
std::string osBuffer; |
| 1062 |
osBuffer.resize(4096); |
| 1063 |
|
| 1064 |
VSILFILE *fp = VSIFOpenL(pszFilename, "rb"); |
| 1065 |
if (fp == nullptr) |
| 1066 |
{ |
| 1067 |
CPLError(CE_Failure, CPLE_OpenFailed, "Cannot open %s", pszFilename); |
| 1068 |
return FALSE; |
| 1069 |
} |
| 1070 |
int nRead = 0; |
| 1071 |
while ((nRead = static_cast<int>( |
| 1072 |
VSIFReadL(&osBuffer[0], 1, osBuffer.size(), fp))) != 0) |
| 1073 |
{ |
| 1074 |
try |
| 1075 |
{ |
| 1076 |
oKmlKml.append(osBuffer.c_str(), nRead); |
| 1077 |
} |
| 1078 |
catch (const std::exception &ex) |
| 1079 |
{ |
| 1080 |
CPLDebug("LIBKML", "libstdc++ exception during ingestion: %s", |
| 1081 |
ex.what()); |
| 1082 |
VSIFCloseL(fp); |
| 1083 |
return FALSE; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
OGRLIBKMLPreProcessInput(oKmlKml); |
| 1087 |
VSIFCloseL(fp); |
| 1088 |
|
| 1089 |
CPLLocaleC oLocaleForcer; |
| 1090 |
|
| 1091 |
/***** parse the kml into the DOM *****/ |
| 1092 |
std::string oKmlErrors; |
| 1093 |
|
| 1094 |
m_poKmlDSKml = AsKml(OGRLIBKMLParse(oKmlKml, &oKmlErrors)); |
| 1095 |
|
| 1096 |
if (!m_poKmlDSKml) |
| 1097 |
{ |
| 1098 |
CPLError(CE_Failure, CPLE_OpenFailed, "ERROR parsing kml %s :%s", |
| 1099 |
pszFilename, oKmlErrors.c_str()); |
| 1100 |
|
| 1101 |
return FALSE; |
| 1102 |
} |
| 1103 |
|
| 1104 |
/***** get the container from root *****/ |
| 1105 |
if (!(m_poKmlDSContainer = |
| 1106 |
GetContainerFromRoot(m_poKmlFactory, m_poKmlDSKml))) |
| 1107 |
{ |
| 1108 |
CPLError(CE_Failure, CPLE_OpenFailed, "ERROR parsing kml %s :%s %s", |
| 1109 |
pszFilename, "This file does not fit the OGR model,", |
| 1110 |
"there is no container element at the root."); |
| 1111 |
|
| 1112 |
return FALSE; |
| 1113 |
} |
| 1114 |
|
| 1115 |
m_isKml = true; |
| 1116 |
|
| 1117 |
/***** get the styles *****/ |
| 1118 |
ParseStyles(AsDocument(m_poKmlDSContainer), &m_poStyleTable); |
| 1119 |
|
| 1120 |
/***** parse for layers *****/ |
| 1121 |
int nPlacemarks = ParseLayers(m_poKmlDSContainer, false); |
| 1122 |
|
| 1123 |
/***** if there is placemarks in the root its a layer *****/ |
| 1124 |
if (nPlacemarks) |
| 1125 |
{ |
| 1126 |
std::string layername_default(CPLGetBasenameSafe(pszFilename)); |
| 1127 |
|
| 1128 |
if (m_poKmlDSContainer->has_name()) |
| 1129 |
{ |
| 1130 |
layername_default = m_poKmlDSContainer->get_name(); |
| 1131 |
} |
| 1132 |
|
| 1133 |
AddLayer(layername_default.c_str(), wkbUnknown, nullptr, this, |
| 1134 |
m_poKmlDSKml, m_poKmlDSContainer, pszFilename, FALSE, |
| 1135 |
bUpdateIn, 1); |
| 1136 |
} |
| 1137 |
|
| 1138 |
ParseLayers(m_poKmlDSContainer, true); |
| 1139 |
|
| 1140 |
return TRUE; |
| 1141 |
} |
| 1142 |
|
| 1143 |
/****************************************************************************** |
| 1144 |
Method to open a kmz file. |
| 1145 |
|
| 1146 |
Args: pszFilename file to open |
| 1147 |
bUpdate update mode |
| 1148 |
|
| 1149 |
Returns: True on success, false on failure |
| 1150 |
|
| 1151 |
******************************************************************************/ |
| 1152 |
|
| 1153 |
int OGRLIBKMLDataSource::OpenKmz(const char *pszFilename, int bUpdateIn) |
| 1154 |
{ |
| 1155 |
std::string oKmlKmz; |
| 1156 |
char szBuffer[1024 + 1] = {}; |
| 1157 |
|
| 1158 |
VSILFILE *fp = VSIFOpenL(pszFilename, "rb"); |
| 1159 |
if (fp == nullptr) |
| 1160 |
{ |
| 1161 |
CPLError(CE_Failure, CPLE_OpenFailed, "Cannot open %s", pszFilename); |
| 1162 |
return FALSE; |
| 1163 |
} |
| 1164 |
int nRead = 0; |
| 1165 |
while ((nRead = static_cast<int>(VSIFReadL(szBuffer, 1, 1024, fp))) != 0) |
| 1166 |
{ |
| 1167 |
try |
| 1168 |
{ |
| 1169 |
oKmlKmz.append(szBuffer, nRead); |
| 1170 |
} |
| 1171 |
catch (const std::bad_alloc &) |
| 1172 |
{ |
| 1173 |
VSIFCloseL(fp); |
| 1174 |
return FALSE; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
VSIFCloseL(fp); |
| 1178 |
|
| 1179 |
KmzFile *poKmlKmzfile = KmzFile::OpenFromString(oKmlKmz); |
| 1180 |
|
| 1181 |
if (!poKmlKmzfile) |
| 1182 |
{ |
| 1183 |
CPLError(CE_Failure, CPLE_OpenFailed, "%s is not a valid kmz file", |
| 1184 |
pszFilename); |
| 1185 |
return FALSE; |
| 1186 |
} |
| 1187 |
|
| 1188 |
CPLLocaleC oLocaleForcer; |
| 1189 |
|
| 1190 |
/***** read the doc.kml *****/ |
| 1191 |
std::string oKmlKml; |
| 1192 |
std::string oKmlKmlPath; |
| 1193 |
if (!poKmlKmzfile->ReadKmlAndGetPath(&oKmlKml, &oKmlKmlPath)) |
| 1194 |
{ |
| 1195 |
return FALSE; |
| 1196 |
} |
| 1197 |
|
| 1198 |
/***** parse the kml into the DOM *****/ |
| 1199 |
std::string oKmlErrors; |
| 1200 |
ElementPtr poKmlDocKmlRoot = OGRLIBKMLParse(oKmlKml, &oKmlErrors); |
| 1201 |
|
| 1202 |
if (!poKmlDocKmlRoot) |
| 1203 |
{ |
| 1204 |
CPLError(CE_Failure, CPLE_OpenFailed, |
| 1205 |
"ERROR parsing kml layer %s from %s :%s", oKmlKmlPath.c_str(), |
| 1206 |
pszFilename, oKmlErrors.c_str()); |
| 1207 |
|
| 1208 |
return FALSE; |
| 1209 |
} |
| 1210 |
|
| 1211 |
/***** Get the child container from root. *****/ |
| 1212 |
ContainerPtr poKmlContainer = nullptr; |
| 1213 |
|
| 1214 |
if (!(poKmlContainer = |
| 1215 |
GetContainerFromRoot(m_poKmlFactory, poKmlDocKmlRoot))) |
| 1216 |
{ |
| 1217 |
CPLError(CE_Failure, CPLE_OpenFailed, "ERROR parsing %s from %s :%s", |
| 1218 |
oKmlKmlPath.c_str(), pszFilename, |
| 1219 |
"kml contains no Containers"); |
| 1220 |
|
| 1221 |
return FALSE; |
| 1222 |
} |
| 1223 |
|
| 1224 |
/***** loop over the container looking for network links *****/ |
| 1225 |
|
| 1226 |
size_t nKmlFeatures = poKmlContainer->get_feature_array_size(); |
| 1227 |
int nLinks = 0; |
| 1228 |
|
| 1229 |
for (size_t iKmlFeature = 0; iKmlFeature < nKmlFeatures; iKmlFeature++) |
| 1230 |
{ |
| 1231 |
FeaturePtr poKmlFeat = |
| 1232 |
poKmlContainer->get_feature_array_at(iKmlFeature); |
| 1233 |
|
| 1234 |
/***** is it a network link? *****/ |
| 1235 |
if (!poKmlFeat->IsA(kmldom::Type_NetworkLink)) |
| 1236 |
continue; |
| 1237 |
|
| 1238 |
NetworkLinkPtr poKmlNetworkLink = AsNetworkLink(poKmlFeat); |
| 1239 |
|
| 1240 |
/***** does it have a link? *****/ |
| 1241 |
if (!poKmlNetworkLink->has_link()) |
| 1242 |
continue; |
| 1243 |
|
| 1244 |
LinkPtr poKmlLink = poKmlNetworkLink->get_link(); |
| 1245 |
|
| 1246 |
/***** does the link have a href? *****/ |
| 1247 |
if (!poKmlLink->has_href()) |
| 1248 |
continue; |
| 1249 |
|
| 1250 |
const kmlengine::Href oKmlHref(poKmlLink->get_href()); |
| 1251 |
|
| 1252 |
/***** is the link relative? *****/ |
| 1253 |
if (oKmlHref.IsRelativePath()) |
| 1254 |
{ |
| 1255 |
nLinks++; |
| 1256 |
|
| 1257 |
std::string oKml; |
| 1258 |
if (poKmlKmzfile->ReadFile(oKmlHref.get_path().c_str(), &oKml)) |
| 1259 |
{ |
| 1260 |
/***** parse the kml into the DOM *****/ |
| 1261 |
oKmlErrors.clear(); |
| 1262 |
ElementPtr poKmlLyrRoot = OGRLIBKMLParse(oKml, &oKmlErrors); |
| 1263 |
|
| 1264 |
if (!poKmlLyrRoot) |
| 1265 |
{ |
| 1266 |
CPLError(CE_Failure, CPLE_OpenFailed, |
| 1267 |
"ERROR parsing kml layer %s from %s :%s", |
| 1268 |
oKmlHref.get_path().c_str(), pszFilename, |
| 1269 |
oKmlErrors.c_str()); |
| 1270 |
|
| 1271 |
continue; |
| 1272 |
} |
| 1273 |
|
| 1274 |
/***** get the container from root *****/ |
| 1275 |
ContainerPtr poKmlLyrContainer = |
| 1276 |
GetContainerFromRoot(m_poKmlFactory, poKmlLyrRoot); |
| 1277 |
|
| 1278 |
if (!poKmlLyrContainer) |
| 1279 |
{ |
| 1280 |
CPLError(CE_Failure, CPLE_OpenFailed, |
| 1281 |
"ERROR parsing kml layer %s from %s :%s", |
| 1282 |
oKmlHref.get_path().c_str(), pszFilename, |
| 1283 |
oKmlErrors.c_str()); |
| 1284 |
|
| 1285 |
continue; |
| 1286 |
} |
| 1287 |
|
| 1288 |
/***** create the layer *****/ |
| 1289 |
const std::string osLayerName = |
| 1290 |
poKmlNetworkLink->has_name() |
| 1291 |
? poKmlNetworkLink->get_name() |
| 1292 |
: CPLGetBasenameSafe(oKmlHref.get_path().c_str()); |
| 1293 |
|
| 1294 |
AddLayer(osLayerName.c_str(), wkbUnknown, nullptr, this, |
| 1295 |
std::move(poKmlLyrRoot), poKmlLyrContainer, |
| 1296 |
oKmlHref.get_path().c_str(), false, bUpdateIn, |
| 1297 |
static_cast<int>(nKmlFeatures)); |
| 1298 |
|
| 1299 |
/***** check if any features are another layer *****/ |
| 1300 |
ParseLayers(std::move(poKmlLyrContainer), true); |
| 1301 |
} |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
/***** if the doc.kml has links store it so if were in update mode we can |
| 1306 |
* write it *****/ |
| 1307 |
if (nLinks) |
| 1308 |
{ |
| 1309 |
m_poKmlDocKml = std::move(poKmlContainer); |
| 1310 |
m_poKmlDocKmlRoot = std::move(poKmlDocKmlRoot); |
| 1311 |
} |
| 1312 |
/***** if the doc.kml has no links treat it as a normal kml file *****/ |
| 1313 |
else |
| 1314 |
{ |
| 1315 |
/* TODO: There could still be a separate styles file in the KMZ |
| 1316 |
if there is this would be a layer style table IF its only a single |
| 1317 |
layer. |
| 1318 |
*/ |
| 1319 |
|
| 1320 |
/***** get the styles *****/ |
| 1321 |
ParseStyles(AsDocument(poKmlContainer), &m_poStyleTable); |
| 1322 |
|
| 1323 |
/***** parse for layers *****/ |
| 1324 |
const int nPlacemarks = ParseLayers(poKmlContainer, false); |
| 1325 |
|
| 1326 |
/***** if there is placemarks in the root its a layer *****/ |
| 1327 |
if (nPlacemarks) |
| 1328 |
{ |
| 1329 |
std::string layername_default(CPLGetBasenameSafe(pszFilename)); |
| 1330 |
|
| 1331 |
if (poKmlContainer->has_name()) |
| 1332 |
{ |
| 1333 |
layername_default = poKmlContainer->get_name(); |
| 1334 |
} |
| 1335 |
|
| 1336 |
AddLayer(layername_default.c_str(), wkbUnknown, nullptr, this, |
| 1337 |
std::move(poKmlDocKmlRoot), poKmlContainer, pszFilename, |
| 1338 |
false, bUpdateIn, 1); |
| 1339 |
} |
| 1340 |
|
| 1341 |
ParseLayers(std::move(poKmlContainer), true); |
| 1342 |
} |
| 1343 |
|
| 1344 |
/***** read the style table if it has one *****/ |
| 1345 |
std::string oKmlStyleKml; |
| 1346 |
if (poKmlKmzfile->ReadFile("style/style.kml", &oKmlStyleKml)) |
| 1347 |
ParseIntoStyleTable(&oKmlStyleKml, "style/style.kml"); |
| 1348 |
|
| 1349 |
/***** cleanup *****/ |
| 1350 |
delete poKmlKmzfile; |
| 1351 |
m_isKmz = true; |
| 1352 |
|
| 1353 |
return TRUE; |
| 1354 |
} |
| 1355 |
|
| 1356 |
/****************************************************************************** |
| 1357 |
Method to open a dir. |
| 1358 |
|
| 1359 |
Args: pszFilename Dir to open |
| 1360 |
bUpdate update mode |
| 1361 |
|
| 1362 |
Returns: True on success, false on failure |
| 1363 |
|
| 1364 |
******************************************************************************/ |
| 1365 |
|
| 1366 |
int OGRLIBKMLDataSource::OpenDir(const char *pszFilename, int bUpdateIn) |
| 1367 |
{ |
| 1368 |
char **papszDirList = VSIReadDir(pszFilename); |
| 1369 |
|
| 1370 |
if (papszDirList == nullptr) |
| 1371 |
return FALSE; |
| 1372 |
|
| 1373 |
const int nFiles = CSLCount(papszDirList); |
| 1374 |
|
| 1375 |
for (int iFile = 0; iFile < nFiles; iFile++) |
| 1376 |
{ |
| 1377 |
/***** make sure its a .kml file *****/ |
| 1378 |
if (!EQUAL(CPLGetExtensionSafe(papszDirList[iFile]).c_str(), "kml")) |
| 1379 |
continue; |
| 1380 |
|
| 1381 |
/***** read the file *****/ |
| 1382 |
std::string oKmlKml; |
| 1383 |
char szBuffer[1024 + 1] = {}; |
| 1384 |
|
| 1385 |
const CPLString osFilePath = |
| 1386 |
CPLFormFilenameSafe(pszFilename, papszDirList[iFile], nullptr); |
| 1387 |
|
| 1388 |
VSILFILE *fp = VSIFOpenL(osFilePath, "rb"); |
| 1389 |
if (fp == nullptr) |
| 1390 |
{ |
| 1391 |
CPLError(CE_Failure, CPLE_OpenFailed, "Cannot open %s", |
| 1392 |
osFilePath.c_str()); |
| 1393 |
continue; |
| 1394 |
} |
| 1395 |
|
| 1396 |
int nRead = 0; |
| 1397 |
while ((nRead = static_cast<int>(VSIFReadL(szBuffer, 1, 1024, fp))) != |
| 1398 |
0) |
| 1399 |
{ |
| 1400 |
try |
| 1401 |
{ |
| 1402 |
oKmlKml.append(szBuffer, nRead); |
| 1403 |
} |
| 1404 |
catch (const std::bad_alloc &) |
| 1405 |
{ |
| 1406 |
VSIFCloseL(fp); |
| 1407 |
CSLDestroy(papszDirList); |
| 1408 |
return FALSE; |
| 1409 |
} |
| 1410 |
} |
| 1411 |
VSIFCloseL(fp); |
| 1412 |
|
| 1413 |
CPLLocaleC oLocaleForcer; |
| 1414 |
|
| 1415 |
/***** parse the kml into the DOM *****/ |
| 1416 |
std::string oKmlErrors; |
| 1417 |
ElementPtr poKmlRoot = OGRLIBKMLParse(oKmlKml, &oKmlErrors); |
| 1418 |
|
| 1419 |
if (!poKmlRoot) |
| 1420 |
{ |
| 1421 |
CPLError(CE_Failure, CPLE_OpenFailed, |
| 1422 |
"ERROR parsing kml layer %s from %s :%s", |
| 1423 |
osFilePath.c_str(), pszFilename, oKmlErrors.c_str()); |
| 1424 |
|
| 1425 |
continue; |
| 1426 |
} |
| 1427 |
|
| 1428 |
/***** Get the container from the root *****/ |
| 1429 |
ContainerPtr poKmlContainer = nullptr; |
| 1430 |
|
| 1431 |
if (!(poKmlContainer = GetContainerFromRoot(m_poKmlFactory, poKmlRoot))) |
| 1432 |
{ |
| 1433 |
CPLError(CE_Failure, CPLE_OpenFailed, "ERROR parsing kml %s :%s %s", |
| 1434 |
pszFilename, "This file does not fit the OGR model,", |
| 1435 |
"there is no container element at the root."); |
| 1436 |
continue; |
| 1437 |
} |
| 1438 |
|
| 1439 |
/***** is it a style table? *****/ |
| 1440 |
if (EQUAL(papszDirList[iFile], "style.kml")) |
| 1441 |
{ |
| 1442 |
ParseStyles(AsDocument(poKmlContainer), &m_poStyleTable); |
| 1443 |
m_osStylePath = "style.kml"; |
| 1444 |
continue; |
| 1445 |
} |
| 1446 |
|
| 1447 |
// Root document generated by ourselves. Contains nothing of interest |
| 1448 |
if (poKmlContainer->has_id() && poKmlContainer->get_id() == "root_doc") |
| 1449 |
{ |
| 1450 |
continue; |
| 1451 |
} |
| 1452 |
|
| 1453 |
const std::string osLayerName = |
| 1454 |
poKmlContainer->has_name() |
| 1455 |
? poKmlContainer->get_name() |
| 1456 |
: std::string(CPLGetBasenameSafe(osFilePath.c_str())); |
| 1457 |
|
| 1458 |
/***** create the layer *****/ |
| 1459 |
AddLayer(osLayerName.c_str(), wkbUnknown, nullptr, this, |
| 1460 |
std::move(poKmlRoot), poKmlContainer, osFilePath.c_str(), |
| 1461 |
false, bUpdateIn, nFiles); |
| 1462 |
|
| 1463 |
/***** check if any features are another layer *****/ |
| 1464 |
ParseLayers(std::move(poKmlContainer), true); |
| 1465 |
} |
| 1466 |
|
| 1467 |
CSLDestroy(papszDirList); |
| 1468 |
|
| 1469 |
if (nLayers > 0) |
| 1470 |
{ |
| 1471 |
m_isDir = true; |
| 1472 |
return TRUE; |
| 1473 |
} |
| 1474 |
|
| 1475 |
return FALSE; |
| 1476 |
} |
| 1477 |
|
| 1478 |
/****************************************************************************** |
| 1479 |
Method to open a datasource. |
| 1480 |
|
| 1481 |
Args: pszFilename Darasource to open |
| 1482 |
bUpdate update mode |
| 1483 |
|
| 1484 |
Returns: True on success, false on failure |
| 1485 |
|
| 1486 |
******************************************************************************/ |
| 1487 |
|
| 1488 |
static bool CheckIsKMZ(const char *pszFilename) |
| 1489 |
{ |
| 1490 |
char **papszFiles = VSIReadDir(pszFilename); |
| 1491 |
char **papszIter = papszFiles; |
| 1492 |
bool bFoundKML = false; |
| 1493 |
while (papszIter && *papszIter) |
| 1494 |
{ |
| 1495 |
if (EQUAL(CPLGetExtensionSafe(*papszIter).c_str(), "kml")) |
| 1496 |
{ |
| 1497 |
bFoundKML = true; |
| 1498 |
break; |
| 1499 |
} |
| 1500 |
else |
| 1501 |
{ |
| 1502 |
CPLString osFilename(pszFilename); |
| 1503 |
osFilename += "/"; |
| 1504 |
osFilename += *papszIter; |
| 1505 |
if (CheckIsKMZ(osFilename)) |
| 1506 |
{ |
| 1507 |
bFoundKML = true; |
| 1508 |
break; |
| 1509 |
} |
| 1510 |
} |
| 1511 |
papszIter++; |
| 1512 |
} |
| 1513 |
CSLDestroy(papszFiles); |
| 1514 |
return bFoundKML; |
| 1515 |
} |
| 1516 |
|
| 1517 |
int OGRLIBKMLDataSource::Open(const char *pszFilename, bool bUpdateIn) |
| 1518 |
{ |
| 1519 |
bUpdate = CPL_TO_BOOL(bUpdateIn); |
| 1520 |
|
| 1521 |
/***** dir *****/ |
| 1522 |
VSIStatBufL sStatBuf; |
| 1523 |
if (!VSIStatExL(pszFilename, &sStatBuf, VSI_STAT_NATURE_FLAG) && |
| 1524 |
VSI_ISDIR(sStatBuf.st_mode)) |
| 1525 |
{ |
| 1526 |
return OpenDir(pszFilename, bUpdate); |
| 1527 |
} |
| 1528 |
|
| 1529 |
/***** kml *****/ |
| 1530 |
if (EQUAL(CPLGetExtensionSafe(pszFilename).c_str(), "kml")) |
| 1531 |
{ |
| 1532 |
return OpenKml(pszFilename, bUpdate); |
| 1533 |
} |
| 1534 |
|
| 1535 |
/***** kmz *****/ |
| 1536 |
if (EQUAL(CPLGetExtensionSafe(pszFilename).c_str(), "kmz")) |
| 1537 |
{ |
| 1538 |
return OpenKmz(pszFilename, bUpdate); |
| 1539 |
} |
| 1540 |
|
| 1541 |
VSILFILE *fp = VSIFOpenL(pszFilename, "rb"); |
| 1542 |
if (fp == nullptr) |
| 1543 |
return FALSE; |
| 1544 |
|
| 1545 |
char szBuffer[1024 + 1] = {}; |
| 1546 |
const int nRead = static_cast<int>(VSIFReadL(szBuffer, 1, 1024, fp)); |
| 1547 |
szBuffer[nRead] = 0; |
| 1548 |
|
| 1549 |
VSIFCloseL(fp); |
| 1550 |
|
| 1551 |
// Does it look like a zip file? |
| 1552 |
if (nRead == 1024 && szBuffer[0] == 0x50 && szBuffer[1] == 0x4B && |
| 1553 |
szBuffer[2] == 0x03 && szBuffer[3] == 0x04) |
| 1554 |
{ |
| 1555 |
CPLString osFilename("/vsizip/"); |
| 1556 |
osFilename += pszFilename; |
| 1557 |
if (!CheckIsKMZ(osFilename)) |
| 1558 |
return FALSE; |
| 1559 |
|
| 1560 |
return OpenKmz(pszFilename, bUpdate); |
| 1561 |
} |
| 1562 |
|
| 1563 |
if (strstr(szBuffer, "<kml>") || strstr(szBuffer, "<kml xmlns=")) |
| 1564 |
return OpenKml(pszFilename, bUpdate); |
| 1565 |
|
| 1566 |
return FALSE; |
| 1567 |
} |
| 1568 |
|
| 1569 |
/************************************************************************/ |
| 1570 |
/* IsValidPhoneNumber() */ |
| 1571 |
/************************************************************************/ |
| 1572 |
|
| 1573 |
// Very approximative validation of http://tools.ietf.org/html/rfc3966#page-6 |
| 1574 |
static bool IsValidPhoneNumber(const char *pszPhoneNumber) |
| 1575 |
{ |
| 1576 |
if (STARTS_WITH(pszPhoneNumber, "tel:")) |
| 1577 |
pszPhoneNumber += strlen("tel:"); |
| 1578 |
char ch = '\0'; |
| 1579 |
bool bDigitFound = false; |
| 1580 |
if (*pszPhoneNumber == '+') |
| 1581 |
pszPhoneNumber++; |
| 1582 |
while ((ch = *pszPhoneNumber) != '\0') |
| 1583 |
{ |
| 1584 |
if (ch >= '0' && ch <= '9') |
| 1585 |
bDigitFound = true; |
| 1586 |
else if (ch == ';') |
| 1587 |
break; |
| 1588 |
else if (!(ch == '-' || ch == '.' || ch == '(' || ch == ')')) |
| 1589 |
return false; |
| 1590 |
pszPhoneNumber++; |
| 1591 |
} |
| 1592 |
return bDigitFound; |
| 1593 |
} |
| 1594 |
|
| 1595 |
/************************************************************************/ |
| 1596 |
/* SetCommonOptions() */ |
| 1597 |
/************************************************************************/ |
| 1598 |
|
| 1599 |
void OGRLIBKMLDataSource::SetCommonOptions(ContainerPtr poKmlContainer, |
| 1600 |
CSLConstList papszOptions) |
| 1601 |
{ |
| 1602 |
const char *l_pszName = CSLFetchNameValue(papszOptions, "NAME"); |
| 1603 |
if (l_pszName != nullptr) |
| 1604 |
poKmlContainer->set_name(l_pszName); |
| 1605 |
|
| 1606 |
const char *pszVisibilility = CSLFetchNameValue(papszOptions, "VISIBILITY"); |
| 1607 |
if (pszVisibilility != nullptr) |
| 1608 |
poKmlContainer->set_visibility(CPLTestBool(pszVisibilility)); |
| 1609 |
|
| 1610 |
const char *pszOpen = CSLFetchNameValue(papszOptions, "OPEN"); |
| 1611 |
if (pszOpen != nullptr) |
| 1612 |
poKmlContainer->set_open(CPLTestBool(pszOpen)); |
| 1613 |
|
| 1614 |
const char *pszSnippet = CSLFetchNameValue(papszOptions, "SNIPPET"); |
| 1615 |
if (pszSnippet != nullptr) |
| 1616 |
{ |
| 1617 |
SnippetPtr poKmlSnippet = m_poKmlFactory->CreateSnippet(); |
| 1618 |
poKmlSnippet->set_text(pszSnippet); |
| 1619 |
poKmlContainer->set_snippet(poKmlSnippet); |
| 1620 |
} |
| 1621 |
|
| 1622 |
const char *pszDescription = CSLFetchNameValue(papszOptions, "DESCRIPTION"); |
| 1623 |
if (pszDescription != nullptr) |
| 1624 |
poKmlContainer->set_description(pszDescription); |
| 1625 |
} |
| 1626 |
|
| 1627 |
/************************************************************************/ |
| 1628 |
/* ParseDocumentOptions() */ |
| 1629 |
/************************************************************************/ |
| 1630 |
|
| 1631 |
void OGRLIBKMLDataSource::ParseDocumentOptions(KmlPtr poKml, |
| 1632 |
DocumentPtr poKmlDocument) |
| 1633 |
{ |
| 1634 |
if (poKmlDocument) |
| 1635 |
{ |
| 1636 |
const char *pszDocumentId = |
| 1637 |
CSLFetchNameValueDef(m_papszOptions, "DOCUMENT_ID", "root_doc"); |
| 1638 |
poKmlDocument->set_id(pszDocumentId); |
| 1639 |
|
| 1640 |
const char *pszAuthorName = |
| 1641 |
CSLFetchNameValue(m_papszOptions, "AUTHOR_NAME"); |
| 1642 |
const char *pszAuthorURI = |
| 1643 |
CSLFetchNameValue(m_papszOptions, "AUTHOR_URI"); |
| 1644 |
const char *pszAuthorEmail = |
| 1645 |
CSLFetchNameValue(m_papszOptions, "AUTHOR_EMAIL"); |
| 1646 |
const char *pszLink = CSLFetchNameValue(m_papszOptions, "LINK"); |
| 1647 |
|
| 1648 |
if (pszAuthorName != nullptr || pszAuthorURI != nullptr || |
| 1649 |
pszAuthorEmail != nullptr) |
| 1650 |
{ |
| 1651 |
kmldom::AtomAuthorPtr author = m_poKmlFactory->CreateAtomAuthor(); |
| 1652 |
if (pszAuthorName != nullptr) |
| 1653 |
author->set_name(pszAuthorName); |
| 1654 |
if (pszAuthorURI != nullptr) |
| 1655 |
{ |
| 1656 |
// Ad-hoc validation. The ABNF is horribly complicated: |
| 1657 |
// http://tools.ietf.org/search/rfc3987#page-7 |
| 1658 |
if (STARTS_WITH(pszAuthorURI, "http://") || |
| 1659 |
STARTS_WITH(pszAuthorURI, "https://")) |
| 1660 |
{ |
| 1661 |
author->set_uri(pszAuthorURI); |
| 1662 |
} |
| 1663 |
else |
| 1664 |
{ |
| 1665 |
CPLError(CE_Warning, CPLE_AppDefined, |
| 1666 |
"Invalid URI for AUTHOR_URI"); |
| 1667 |
} |
| 1668 |
} |
| 1669 |
if (pszAuthorEmail != nullptr) |
| 1670 |
{ |
| 1671 |
const char *pszArobase = strchr(pszAuthorEmail, '@'); |
| 1672 |
if (pszArobase != nullptr && |
| 1673 |
strchr(pszArobase + 1, '.') != nullptr) |
| 1674 |
{ |
| 1675 |
author->set_email(pszAuthorEmail); |
| 1676 |
} |
| 1677 |
else |
| 1678 |
{ |
| 1679 |
CPLError(CE_Warning, CPLE_AppDefined, |
| 1680 |
"Invalid email for AUTHOR_EMAIL"); |
| 1681 |
} |
| 1682 |
} |
| 1683 |
poKmlDocument->set_atomauthor(author); |
| 1684 |
} |
| 1685 |
|
| 1686 |
if (pszLink != nullptr) |
| 1687 |
{ |
| 1688 |
kmldom::AtomLinkPtr link = m_poKmlFactory->CreateAtomLink(); |
| 1689 |
link->set_href(pszLink); |
| 1690 |
link->set_rel("related"); |
| 1691 |
poKmlDocument->set_atomlink(link); |
| 1692 |
} |
| 1693 |
|
| 1694 |
const char *pszPhoneNumber = |
| 1695 |
CSLFetchNameValue(m_papszOptions, "PHONENUMBER"); |
| 1696 |
if (pszPhoneNumber != nullptr) |
| 1697 |
{ |
| 1698 |
if (IsValidPhoneNumber(pszPhoneNumber)) |
| 1699 |
{ |
| 1700 |
if (!STARTS_WITH(pszPhoneNumber, "tel:")) |
| 1701 |
poKmlDocument->set_phonenumber( |
| 1702 |
CPLSPrintf("tel:%s", pszPhoneNumber)); |
| 1703 |
else |
| 1704 |
poKmlDocument->set_phonenumber(pszPhoneNumber); |
| 1705 |
} |
| 1706 |
else |
| 1707 |
{ |
| 1708 |
CPLError(CE_Warning, CPLE_AppDefined, "Invalid phone number"); |
| 1709 |
} |
| 1710 |
} |
| 1711 |
|
| 1712 |
SetCommonOptions(poKmlDocument, m_papszOptions); |
| 1713 |
|
| 1714 |
CPLString osListStyleType = |
| 1715 |
CSLFetchNameValueDef(m_papszOptions, "LISTSTYLE_TYPE", ""); |
| 1716 |
CPLString osListStyleIconHref = |
| 1717 |
CSLFetchNameValueDef(m_papszOptions, "LISTSTYLE_ICON_HREF", ""); |
| 1718 |
createkmlliststyle(m_poKmlFactory, pszDocumentId, poKmlDocument, |
| 1719 |
poKmlDocument, osListStyleType, osListStyleIconHref); |
| 1720 |
} |
| 1721 |
|
| 1722 |
if (poKml) |
| 1723 |
{ |
| 1724 |
if (m_poKmlUpdate) |
| 1725 |
{ |
| 1726 |
NetworkLinkControlPtr nlc = |
| 1727 |
m_poKmlFactory->CreateNetworkLinkControl(); |
| 1728 |
poKml->set_networklinkcontrol(nlc); |
| 1729 |
if (m_poKmlUpdate->get_updateoperation_array_size() != 0) |
| 1730 |
{ |
| 1731 |
nlc->set_update(m_poKmlUpdate); |
| 1732 |
} |
| 1733 |
} |
| 1734 |
|
| 1735 |
const char *pszNLCMinRefreshPeriod = |
| 1736 |
CSLFetchNameValue(m_papszOptions, "NLC_MINREFRESHPERIOD"); |
| 1737 |
const char *pszNLCMaxSessionLength = |
| 1738 |
CSLFetchNameValue(m_papszOptions, "NLC_MAXSESSIONLENGTH"); |
| 1739 |
const char *pszNLCCookie = |
| 1740 |
CSLFetchNameValue(m_papszOptions, "NLC_COOKIE"); |
| 1741 |
const char *pszNLCMessage = |
| 1742 |
CSLFetchNameValue(m_papszOptions, "NLC_MESSAGE"); |
| 1743 |
const char *pszNLCLinkName = |
| 1744 |
CSLFetchNameValue(m_papszOptions, "NLC_LINKNAME"); |
| 1745 |
const char *pszNLCLinkDescription = |
| 1746 |
CSLFetchNameValue(m_papszOptions, "NLC_LINKDESCRIPTION"); |
| 1747 |
const char *pszNLCLinkSnippet = |
| 1748 |
CSLFetchNameValue(m_papszOptions, "NLC_LINKSNIPPET"); |
| 1749 |
const char *pszNLCExpires = |
| 1750 |
CSLFetchNameValue(m_papszOptions, "NLC_EXPIRES"); |
| 1751 |
|
| 1752 |
if (pszNLCMinRefreshPeriod != nullptr || |
| 1753 |
pszNLCMaxSessionLength != nullptr || pszNLCCookie != nullptr || |
| 1754 |
pszNLCMessage != nullptr || pszNLCLinkName != nullptr || |
| 1755 |
pszNLCLinkDescription != nullptr || pszNLCLinkSnippet != nullptr || |
| 1756 |
pszNLCExpires != nullptr) |
| 1757 |
{ |
| 1758 |
NetworkLinkControlPtr nlc = nullptr; |
| 1759 |
if (poKml->has_networklinkcontrol()) |
| 1760 |
{ |
| 1761 |
nlc = poKml->get_networklinkcontrol(); |
| 1762 |
} |
| 1763 |
else |
| 1764 |
{ |
| 1765 |
nlc = m_poKmlFactory->CreateNetworkLinkControl(); |
| 1766 |
poKml->set_networklinkcontrol(nlc); |
| 1767 |
} |
| 1768 |
if (pszNLCMinRefreshPeriod != nullptr) |
| 1769 |
{ |
| 1770 |
const double dfVal = CPLAtof(pszNLCMinRefreshPeriod); |
| 1771 |
if (dfVal >= 0) |
| 1772 |
nlc->set_minrefreshperiod(dfVal); |
| 1773 |
} |
| 1774 |
if (pszNLCMaxSessionLength != nullptr) |
| 1775 |
{ |
| 1776 |
const double dfVal = CPLAtof(pszNLCMaxSessionLength); |
| 1777 |
nlc->set_maxsessionlength(dfVal); |
| 1778 |
} |
| 1779 |
if (pszNLCCookie != nullptr) |
| 1780 |
{ |
| 1781 |
nlc->set_cookie(pszNLCCookie); |
| 1782 |
} |
| 1783 |
if (pszNLCMessage != nullptr) |
| 1784 |
{ |
| 1785 |
nlc->set_message(pszNLCMessage); |
| 1786 |
} |
| 1787 |
if (pszNLCLinkName != nullptr) |
| 1788 |
{ |
| 1789 |
nlc->set_linkname(pszNLCLinkName); |
| 1790 |
} |
| 1791 |
if (pszNLCLinkDescription != nullptr) |
| 1792 |
{ |
| 1793 |
nlc->set_linkdescription(pszNLCLinkDescription); |
| 1794 |
} |
| 1795 |
if (pszNLCLinkSnippet != nullptr) |
| 1796 |
{ |
| 1797 |
LinkSnippetPtr linksnippet = |
| 1798 |
m_poKmlFactory->CreateLinkSnippet(); |
| 1799 |
linksnippet->set_text(pszNLCLinkSnippet); |
| 1800 |
nlc->set_linksnippet(std::move(linksnippet)); |
| 1801 |
} |
| 1802 |
if (pszNLCExpires != nullptr) |
| 1803 |
{ |
| 1804 |
OGRField sField; |
| 1805 |
if (OGRParseXMLDateTime(pszNLCExpires, &sField)) |
| 1806 |
{ |
| 1807 |
char *pszXMLDate = OGRGetXMLDateTime(&sField); |
| 1808 |
nlc->set_expires(pszXMLDate); |
| 1809 |
CPLFree(pszXMLDate); |
| 1810 |
} |
| 1811 |
} |
| 1812 |
} |
| 1813 |
} |
| 1814 |
} |
| 1815 |
|
| 1816 |
/****************************************************************************** |
| 1817 |
Method to create a single file .kml ds. |
| 1818 |
|
| 1819 |
Args: pszFilename the datasource to create |
| 1820 |
papszOptions datasource creation options |
| 1821 |
|
| 1822 |
Returns: True on success, false on failure |
| 1823 |
|
| 1824 |
******************************************************************************/ |
| 1825 |
|
| 1826 |
int OGRLIBKMLDataSource::CreateKml(const char * /* pszFilename */, |
| 1827 |
CSLConstList papszOptions) |
| 1828 |
{ |
| 1829 |
m_poKmlDSKml = OGRLIBKMLCreateOGCKml22(m_poKmlFactory, papszOptions); |
| 1830 |
if (osUpdateTargetHref.empty()) |
| 1831 |
{ |
| 1832 |
DocumentPtr poKmlDocument = m_poKmlFactory->CreateDocument(); |
| 1833 |
m_poKmlDSKml->set_feature(poKmlDocument); |
| 1834 |
m_poKmlDSContainer = poKmlDocument; |
| 1835 |
} |
| 1836 |
|
| 1837 |
m_isKml = true; |
| 1838 |
bUpdated = true; |
| 1839 |
|
| 1840 |
return true; |
| 1841 |
} |
| 1842 |
|
| 1843 |
/****************************************************************************** |
| 1844 |
Method to create a .kmz ds. |
| 1845 |
|
| 1846 |
Args: pszFilename the datasource to create |
| 1847 |
papszOptions datasource creation options |
| 1848 |
|
| 1849 |
Returns: True on success, false on failure |
| 1850 |
|
| 1851 |
******************************************************************************/ |
| 1852 |
|
| 1853 |
int OGRLIBKMLDataSource::CreateKmz(const char * /* pszFilename */, |
| 1854 |
CSLConstList /* papszOptions */) |
| 1855 |
{ |
| 1856 |
/***** create the doc.kml *****/ |
| 1857 |
if (osUpdateTargetHref.empty()) |
| 1858 |
{ |
| 1859 |
const char *pszUseDocKml = |
| 1860 |
CPLGetConfigOption("LIBKML_USE_DOC.KML", "yes"); |
| 1861 |
|
| 1862 |
if (CPLTestBool(pszUseDocKml)) |
| 1863 |
{ |
| 1864 |
m_poKmlDocKml = m_poKmlFactory->CreateDocument(); |
| 1865 |
} |
| 1866 |
} |
| 1867 |
|
| 1868 |
// Layers are written in a layers/ subdirectory, hence ../style |
| 1869 |
// to access style/style.kml |
| 1870 |
m_osStylePath = "../style/style.kml"; |
| 1871 |
|
| 1872 |
m_isKmz = true; |
| 1873 |
bUpdated = true; |
| 1874 |
|
| 1875 |
return TRUE; |
| 1876 |
} |
| 1877 |
|
| 1878 |
/****************************************************************************** |
| 1879 |
Method to create a dir datasource. |
| 1880 |
|
| 1881 |
Args: pszFilename the datasource to create |
| 1882 |
papszOptions datasource creation options |
| 1883 |
|
| 1884 |
Returns: True on success, false on failure |
| 1885 |
|
| 1886 |
******************************************************************************/ |
| 1887 |
|
| 1888 |
int OGRLIBKMLDataSource::CreateDir(const char *pszFilename, |
| 1889 |
CSLConstList /* papszOptions */) |
| 1890 |
{ |
| 1891 |
if (VSIMkdir(pszFilename, 0755)) |
| 1892 |
{ |
| 1893 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 1894 |
"ERROR Creating dir: %s for KML datasource", pszFilename); |
| 1895 |
return FALSE; |
| 1896 |
} |
| 1897 |
|
| 1898 |
m_isDir = true; |
| 1899 |
bUpdated = true; |
| 1900 |
|
| 1901 |
if (osUpdateTargetHref.empty()) |
| 1902 |
{ |
| 1903 |
const char *pszUseDocKml = |
| 1904 |
CPLGetConfigOption("LIBKML_USE_DOC.KML", "yes"); |
| 1905 |
|
| 1906 |
if (CPLTestBool(pszUseDocKml)) |
| 1907 |
{ |
| 1908 |
m_poKmlDocKml = m_poKmlFactory->CreateDocument(); |
| 1909 |
} |
| 1910 |
} |
| 1911 |
|
| 1912 |
m_osStylePath = "style.kml"; |
| 1913 |
|
| 1914 |
return TRUE; |
| 1915 |
} |
| 1916 |
|
| 1917 |
/****************************************************************************** |
| 1918 |
Method to create a datasource. |
| 1919 |
|
| 1920 |
Args: pszFilename the datasource to create |
| 1921 |
papszOptions datasource creation options |
| 1922 |
|
| 1923 |
Returns: True on success, false on failure |
| 1924 |
|
| 1925 |
env vars: |
| 1926 |
LIBKML_USE_DOC.KML default: yes |
| 1927 |
|
| 1928 |
******************************************************************************/ |
| 1929 |
|
| 1930 |
int OGRLIBKMLDataSource::Create(const char *pszFilename, |
| 1931 |
CSLConstList papszOptions) |
| 1932 |
{ |
| 1933 |
if (strcmp(pszFilename, "/dev/stdout") == 0) |
| 1934 |
pszFilename = "/vsistdout/"; |
| 1935 |
|
| 1936 |
SetDescription(pszFilename); |
| 1937 |
bUpdate = true; |
| 1938 |
|
| 1939 |
osUpdateTargetHref = |
| 1940 |
CSLFetchNameValueDef(papszOptions, "UPDATE_TARGETHREF", ""); |
| 1941 |
if (!osUpdateTargetHref.empty()) |
| 1942 |
{ |
| 1943 |
m_poKmlUpdate = m_poKmlFactory->CreateUpdate(); |
| 1944 |
m_poKmlUpdate->set_targethref(osUpdateTargetHref.c_str()); |
| 1945 |
} |
| 1946 |
|
| 1947 |
m_papszOptions = CSLDuplicate(papszOptions); |
| 1948 |
|
| 1949 |
/***** kml *****/ |
| 1950 |
if (strcmp(pszFilename, "/vsistdout/") == 0 || |
| 1951 |
STARTS_WITH(pszFilename, "/vsigzip/") || |
| 1952 |
EQUAL(CPLGetExtensionSafe(pszFilename).c_str(), "kml")) |
| 1953 |
return CreateKml(pszFilename, papszOptions); |
| 1954 |
|
| 1955 |
/***** kmz *****/ |
| 1956 |
if (EQUAL(CPLGetExtensionSafe(pszFilename).c_str(), "kmz")) |
| 1957 |
return CreateKmz(pszFilename, papszOptions); |
| 1958 |
|
| 1959 |
/***** dir *****/ |
| 1960 |
return CreateDir(pszFilename, papszOptions); |
| 1961 |
} |
| 1962 |
|
| 1963 |
/****************************************************************************** |
| 1964 |
Method to get a layer by index. |
| 1965 |
|
| 1966 |
Args: iLayer the index of the layer to get |
| 1967 |
|
| 1968 |
Returns: pointer to the layer, or NULL if the layer does not exist |
| 1969 |
|
| 1970 |
******************************************************************************/ |
| 1971 |
|
| 1972 |
const OGRLayer *OGRLIBKMLDataSource::GetLayer(int iLayer) const |
| 1973 |
{ |
| 1974 |
if (iLayer < 0 || iLayer >= nLayers) |
| 1975 |
return nullptr; |
| 1976 |
|
| 1977 |
return papoLayers[iLayer]; |
| 1978 |
} |
| 1979 |
|
| 1980 |
/****************************************************************************** |
| 1981 |
Method to get a layer by name. |
| 1982 |
|
| 1983 |
Args: pszname name of the layer to get |
| 1984 |
|
| 1985 |
Returns: pointer to the layer, or NULL if the layer does not exist |
| 1986 |
|
| 1987 |
******************************************************************************/ |
| 1988 |
|
| 1989 |
OGRLayer *OGRLIBKMLDataSource::GetLayerByName(const char *pszName) |
| 1990 |
{ |
| 1991 |
auto oIter = m_oMapLayers.find(CPLString(pszName).toupper()); |
| 1992 |
if (oIter != m_oMapLayers.end()) |
| 1993 |
return oIter->second; |
| 1994 |
|
| 1995 |
return nullptr; |
| 1996 |
} |
| 1997 |
|
| 1998 |
/****************************************************************************** |
| 1999 |
Method to DeleteLayers in a .kml datasource. |
| 2000 |
|
| 2001 |
Args: iLayer index of the layer to delete |
| 2002 |
|
| 2003 |
Returns: OGRERR_NONE on success, OGRERR_FAILURE on failure |
| 2004 |
|
| 2005 |
******************************************************************************/ |
| 2006 |
|
| 2007 |
OGRErr OGRLIBKMLDataSource::DeleteLayerKml(int iLayer) |
| 2008 |
{ |
| 2009 |
OGRLIBKMLLayer *poOgrLayer = (OGRLIBKMLLayer *)papoLayers[iLayer]; |
| 2010 |
|
| 2011 |
/***** loop over the features *****/ |
| 2012 |
|
| 2013 |
const size_t nKmlFeatures = m_poKmlDSContainer->get_feature_array_size(); |
| 2014 |
|
| 2015 |
for (size_t iKmlFeature = 0; iKmlFeature < nKmlFeatures; iKmlFeature++) |
| 2016 |
{ |
| 2017 |
FeaturePtr poKmlFeat = |
| 2018 |
m_poKmlDSContainer->get_feature_array_at(iKmlFeature); |
| 2019 |
|
| 2020 |
if (poKmlFeat == poOgrLayer->GetKmlLayer()) |
| 2021 |
{ |
| 2022 |
m_poKmlDSContainer->DeleteFeatureAt(iKmlFeature); |
| 2023 |
break; |
| 2024 |
} |
| 2025 |
} |
| 2026 |
|
| 2027 |
return OGRERR_NONE; |
| 2028 |
} |
| 2029 |
|
| 2030 |
/****************************************************************************** |
| 2031 |
Method to DeleteLayers in a .kmz datasource. |
| 2032 |
|
| 2033 |
Args: iLayer index of the layer to delete |
| 2034 |
|
| 2035 |
Returns: OGRERR_NONE on success, OGRERR_FAILURE on failure |
| 2036 |
|
| 2037 |
******************************************************************************/ |
| 2038 |
|
| 2039 |
OGRErr OGRLIBKMLDataSource::DeleteLayerKmz(int iLayer) |
| 2040 |
{ |
| 2041 |
OGRLIBKMLLayer *poOgrLayer = papoLayers[iLayer]; |
| 2042 |
|
| 2043 |
const char *pszUseDocKml = CPLGetConfigOption("LIBKML_USE_DOC.KML", "yes"); |
| 2044 |
|
| 2045 |
if (CPLTestBool(pszUseDocKml) && m_poKmlDocKml) |
| 2046 |
{ |
| 2047 |
/***** loop over the features *****/ |
| 2048 |
const size_t nKmlFeatures = m_poKmlDocKml->get_feature_array_size(); |
| 2049 |
|
| 2050 |
for (size_t iKmlFeature = 0; iKmlFeature < nKmlFeatures; iKmlFeature++) |
| 2051 |
{ |
| 2052 |
FeaturePtr poKmlFeat = |
| 2053 |
m_poKmlDocKml->get_feature_array_at(iKmlFeature); |
| 2054 |
|
| 2055 |
if (poKmlFeat->IsA(kmldom::Type_NetworkLink)) |
| 2056 |
{ |
| 2057 |
NetworkLinkPtr poKmlNetworkLink = AsNetworkLink(poKmlFeat); |
| 2058 |
|
| 2059 |
/***** does it have a link? *****/ |
| 2060 |
if (poKmlNetworkLink->has_link()) |
| 2061 |
{ |
| 2062 |
LinkPtr poKmlLink = poKmlNetworkLink->get_link(); |
| 2063 |
|
| 2064 |
/***** does the link have a href? *****/ |
| 2065 |
if (poKmlLink->has_href()) |
| 2066 |
{ |
| 2067 |
const kmlengine::Href oKmlHref(poKmlLink->get_href()); |
| 2068 |
|
| 2069 |
/***** is the link relative? *****/ |
| 2070 |
if (oKmlHref.IsRelativePath()) |
| 2071 |
{ |
| 2072 |
if (EQUAL(oKmlHref.get_path().c_str(), |
| 2073 |
poOgrLayer->GetFileName())) |
| 2074 |
{ |
| 2075 |
m_poKmlDocKml->DeleteFeatureAt(iKmlFeature); |
| 2076 |
break; |
| 2077 |
} |
| 2078 |
} |
| 2079 |
} |
| 2080 |
} |
| 2081 |
} |
| 2082 |
} |
| 2083 |
} |
| 2084 |
|
| 2085 |
return OGRERR_NONE; |
| 2086 |
} |
| 2087 |
|
| 2088 |
/****************************************************************************** |
| 2089 |
Method to delete a layer in a datasource. |
| 2090 |
|
| 2091 |
Args: iLayer index of the layer to delete |
| 2092 |
|
| 2093 |
Returns: OGRERR_NONE on success, OGRERR_FAILURE on failure |
| 2094 |
|
| 2095 |
******************************************************************************/ |
| 2096 |
|
| 2097 |
OGRErr OGRLIBKMLDataSource::DeleteLayer(int iLayer) |
| 2098 |
{ |
| 2099 |
if (!bUpdate) |
| 2100 |
return OGRERR_UNSUPPORTED_OPERATION; |
| 2101 |
|
| 2102 |
if (iLayer >= nLayers) |
| 2103 |
return OGRERR_FAILURE; |
| 2104 |
|
| 2105 |
if (IsKml()) |
| 2106 |
{ |
| 2107 |
DeleteLayerKml(iLayer); |
| 2108 |
} |
| 2109 |
else if (IsKmz()) |
| 2110 |
{ |
| 2111 |
DeleteLayerKmz(iLayer); |
| 2112 |
} |
| 2113 |
else if (IsDir()) |
| 2114 |
{ |
| 2115 |
DeleteLayerKmz(iLayer); |
| 2116 |
|
| 2117 |
/***** delete the file the layer corresponds to *****/ |
| 2118 |
const std::string osFilePath = CPLFormFilenameSafe( |
| 2119 |
GetDescription(), papoLayers[iLayer]->GetFileName(), nullptr); |
| 2120 |
VSIStatBufL oStatBufL; |
| 2121 |
if (!VSIStatL(osFilePath.c_str(), &oStatBufL)) |
| 2122 |
{ |
| 2123 |
if (VSIUnlink(osFilePath.c_str())) |
| 2124 |
{ |
| 2125 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 2126 |
"ERROR Deleting Layer %s from filesystem as %s", |
| 2127 |
papoLayers[iLayer]->GetName(), osFilePath.c_str()); |
| 2128 |
} |
| 2129 |
} |
| 2130 |
} |
| 2131 |
|
| 2132 |
m_oMapLayers.erase(CPLString(papoLayers[iLayer]->GetName()).toupper()); |
| 2133 |
delete papoLayers[iLayer]; |
| 2134 |
memmove(papoLayers + iLayer, papoLayers + iLayer + 1, |
| 2135 |
sizeof(void *) * (nLayers - iLayer - 1)); |
| 2136 |
nLayers--; |
| 2137 |
bUpdated = true; |
| 2138 |
|
| 2139 |
return OGRERR_NONE; |
| 2140 |
} |
| 2141 |
|
| 2142 |
/****************************************************************************** |
| 2143 |
Method to create a layer in a single file .kml. |
| 2144 |
|
| 2145 |
Args: pszLayerName name of the layer to create |
| 2146 |
poOgrSRS the SRS of the layer |
| 2147 |
eGType the layers geometry type |
| 2148 |
papszOptions layer creation options |
| 2149 |
|
| 2150 |
Returns: return a pointer to the new layer or NULL on failure |
| 2151 |
|
| 2152 |
******************************************************************************/ |
| 2153 |
|
| 2154 |
OGRLIBKMLLayer *OGRLIBKMLDataSource::CreateLayerKml( |
| 2155 |
const char *pszLayerName, const OGRSpatialReference *poSRS, |
| 2156 |
OGRwkbGeometryType eGType, CSLConstList papszOptions) |
| 2157 |
{ |
| 2158 |
ContainerPtr poKmlLayerContainer = nullptr; |
| 2159 |
|
| 2160 |
if (m_poKmlDSContainer) |
| 2161 |
{ |
| 2162 |
if (CPLFetchBool(papszOptions, "FOLDER", false)) |
| 2163 |
poKmlLayerContainer = m_poKmlFactory->CreateFolder(); |
| 2164 |
else |
| 2165 |
poKmlLayerContainer = m_poKmlFactory->CreateDocument(); |
| 2166 |
poKmlLayerContainer->set_id( |
| 2167 |
OGRLIBKMLGetSanitizedNCName(pszLayerName).c_str()); |
| 2168 |
|
| 2169 |
m_poKmlDSContainer->add_feature(poKmlLayerContainer); |
| 2170 |
} |
| 2171 |
|
| 2172 |
/***** create the layer *****/ |
| 2173 |
OGRLIBKMLLayer *poOgrLayer = |
| 2174 |
AddLayer(pszLayerName, eGType, poSRS, this, nullptr, |
| 2175 |
poKmlLayerContainer, "", true, bUpdate, 1); |
| 2176 |
|
| 2177 |
/***** add the layer name as a <Name> *****/ |
| 2178 |
if (poKmlLayerContainer) |
| 2179 |
poKmlLayerContainer->set_name(pszLayerName); |
| 2180 |
else if (CPLFetchBool(papszOptions, "FOLDER", false)) |
| 2181 |
{ |
| 2182 |
poOgrLayer->SetUpdateIsFolder(TRUE); |
| 2183 |
} |
| 2184 |
|
| 2185 |
return poOgrLayer; |
| 2186 |
} |
| 2187 |
|
| 2188 |
/****************************************************************************** |
| 2189 |
Method to create a layer in a .kmz or dir. |
| 2190 |
|
| 2191 |
Args: pszLayerName name of the layer to create |
| 2192 |
poOgrSRS the SRS of the layer |
| 2193 |
eGType the layers geometry type |
| 2194 |
papszOptions layer creation options |
| 2195 |
|
| 2196 |
Returns: return a pointer to the new layer or NULL on failure |
| 2197 |
|
| 2198 |
******************************************************************************/ |
| 2199 |
|
| 2200 |
OGRLIBKMLLayer *OGRLIBKMLDataSource::CreateLayerKmz( |
| 2201 |
const char *pszLayerName, const OGRSpatialReference *poSRS, |
| 2202 |
OGRwkbGeometryType eGType, CSLConstList papszOptions) |
| 2203 |
{ |
| 2204 |
DocumentPtr poKmlDocument = nullptr; |
| 2205 |
|
| 2206 |
if (!m_poKmlUpdate) |
| 2207 |
{ |
| 2208 |
/***** add a network link to doc.kml *****/ |
| 2209 |
const char *pszUseDocKml = |
| 2210 |
CPLGetConfigOption("LIBKML_USE_DOC.KML", "yes"); |
| 2211 |
|
| 2212 |
if (CPLTestBool(pszUseDocKml) && m_poKmlDocKml) |
| 2213 |
{ |
| 2214 |
poKmlDocument = AsDocument(m_poKmlDocKml); |
| 2215 |
|
| 2216 |
NetworkLinkPtr poKmlNetLink = m_poKmlFactory->CreateNetworkLink(); |
| 2217 |
poKmlNetLink->set_name( |
| 2218 |
CSLFetchNameValueDef(papszOptions, "NAME", pszLayerName)); |
| 2219 |
|
| 2220 |
LinkPtr poKmlLink = m_poKmlFactory->CreateLink(); |
| 2221 |
|
| 2222 |
std::string oHref; |
| 2223 |
if (IsKmz()) |
| 2224 |
oHref.append("layers/"); |
| 2225 |
oHref.append(pszLayerName); |
| 2226 |
oHref.append(".kml"); |
| 2227 |
poKmlLink->set_href(oHref); |
| 2228 |
|
| 2229 |
poKmlNetLink->set_link(poKmlLink); |
| 2230 |
poKmlDocument->add_feature(poKmlNetLink); |
| 2231 |
} |
| 2232 |
|
| 2233 |
/***** create the layer *****/ |
| 2234 |
|
| 2235 |
poKmlDocument = m_poKmlFactory->CreateDocument(); |
| 2236 |
poKmlDocument->set_id( |
| 2237 |
OGRLIBKMLGetSanitizedNCName(pszLayerName).c_str()); |
| 2238 |
} |
| 2239 |
|
| 2240 |
OGRLIBKMLLayer *poOgrLayer = |
| 2241 |
AddLayer(pszLayerName, eGType, poSRS, this, nullptr, poKmlDocument, |
| 2242 |
CPLFormFilenameSafe(nullptr, pszLayerName, ".kml").c_str(), |
| 2243 |
true, bUpdate, 1); |
| 2244 |
|
| 2245 |
/***** add the layer name as a <Name> *****/ |
| 2246 |
if (!m_poKmlUpdate) |
| 2247 |
{ |
| 2248 |
poKmlDocument->set_name(pszLayerName); |
| 2249 |
} |
| 2250 |
|
| 2251 |
return poOgrLayer; |
| 2252 |
} |
| 2253 |
|
| 2254 |
/****************************************************************************** |
| 2255 |
ICreateLayer() |
| 2256 |
|
| 2257 |
Args: pszLayerName name of the layer to create |
| 2258 |
poOgrSRS the SRS of the layer |
| 2259 |
eGType the layers geometry type |
| 2260 |
papszOptions layer creation options |
| 2261 |
|
| 2262 |
Returns: return a pointer to the new layer or NULL on failure |
| 2263 |
|
| 2264 |
******************************************************************************/ |
| 2265 |
|
| 2266 |
OGRLayer * |
| 2267 |
OGRLIBKMLDataSource::ICreateLayer(const char *pszLayerName, |
| 2268 |
const OGRGeomFieldDefn *poGeomFieldDefn, |
| 2269 |
CSLConstList papszOptions) |
| 2270 |
{ |
| 2271 |
if (!bUpdate) |
| 2272 |
return nullptr; |
| 2273 |
|
| 2274 |
if (CPLLaunderForFilenameSafe(pszLayerName, nullptr) != pszLayerName) |
| 2275 |
{ |
| 2276 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 2277 |
"Illegal characters in '%s' to form a valid filename", |
| 2278 |
pszLayerName); |
| 2279 |
return nullptr; |
| 2280 |
} |
| 2281 |
|
| 2282 |
if ((IsKmz() || IsDir()) && EQUAL(pszLayerName, "doc")) |
| 2283 |
{ |
| 2284 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 2285 |
"'doc' is an invalid layer name in a KMZ file"); |
| 2286 |
return nullptr; |
| 2287 |
} |
| 2288 |
|
| 2289 |
const auto eGType = poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone; |
| 2290 |
const auto poOgrSRS = |
| 2291 |
poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr; |
| 2292 |
|
| 2293 |
OGRLIBKMLLayer *poOgrLayer = nullptr; |
| 2294 |
|
| 2295 |
/***** kml DS *****/ |
| 2296 |
if (IsKml()) |
| 2297 |
{ |
| 2298 |
poOgrLayer = |
| 2299 |
CreateLayerKml(pszLayerName, poOgrSRS, eGType, papszOptions); |
| 2300 |
} |
| 2301 |
else if (IsKmz() || IsDir()) |
| 2302 |
{ |
| 2303 |
poOgrLayer = |
| 2304 |
CreateLayerKmz(pszLayerName, poOgrSRS, eGType, papszOptions); |
| 2305 |
} |
| 2306 |
|
| 2307 |
const char *pszLookatLongitude = |
| 2308 |
CSLFetchNameValue(papszOptions, "LOOKAT_LONGITUDE"); |
| 2309 |
const char *pszLookatLatitude = |
| 2310 |
CSLFetchNameValue(papszOptions, "LOOKAT_LATITUDE"); |
| 2311 |
const char *pszLookatAltitude = |
| 2312 |
CSLFetchNameValue(papszOptions, "LOOKAT_ALTITUDE"); |
| 2313 |
const char *pszLookatHeading = |
| 2314 |
CSLFetchNameValue(papszOptions, "LOOKAT_HEADING"); |
| 2315 |
const char *pszLookatTilt = CSLFetchNameValue(papszOptions, "LOOKAT_TILT"); |
| 2316 |
const char *pszLookatRange = |
| 2317 |
CSLFetchNameValue(papszOptions, "LOOKAT_RANGE"); |
| 2318 |
const char *pszLookatAltitudeMode = |
| 2319 |
CSLFetchNameValue(papszOptions, "LOOKAT_ALTITUDEMODE"); |
| 2320 |
if (poOgrLayer != nullptr && pszLookatLongitude != nullptr && |
| 2321 |
pszLookatLatitude != nullptr && pszLookatRange != nullptr) |
| 2322 |
{ |
| 2323 |
poOgrLayer->SetLookAt(pszLookatLongitude, pszLookatLatitude, |
| 2324 |
pszLookatAltitude, pszLookatHeading, |
| 2325 |
pszLookatTilt, pszLookatRange, |
| 2326 |
pszLookatAltitudeMode); |
| 2327 |
} |
| 2328 |
else |
| 2329 |
{ |
| 2330 |
const char *pszCameraLongitude = |
| 2331 |
CSLFetchNameValue(papszOptions, "CAMERA_LONGITUDE"); |
| 2332 |
const char *pszCameraLatitude = |
| 2333 |
CSLFetchNameValue(papszOptions, "CAMERA_LATITUDE"); |
| 2334 |
const char *pszCameraAltitude = |
| 2335 |
CSLFetchNameValue(papszOptions, "CAMERA_ALTITUDE"); |
| 2336 |
const char *pszCameraHeading = |
| 2337 |
CSLFetchNameValue(papszOptions, "CAMERA_HEADING"); |
| 2338 |
const char *pszCameraTilt = |
| 2339 |
CSLFetchNameValue(papszOptions, "CAMERA_TILT"); |
| 2340 |
const char *pszCameraRoll = |
| 2341 |
CSLFetchNameValue(papszOptions, "CAMERA_ROLL"); |
| 2342 |
const char *pszCameraAltitudeMode = |
| 2343 |
CSLFetchNameValue(papszOptions, "CAMERA_ALTITUDEMODE"); |
| 2344 |
if (poOgrLayer != nullptr && pszCameraLongitude != nullptr && |
| 2345 |
pszCameraLatitude != nullptr && pszCameraAltitude != nullptr && |
| 2346 |
pszCameraAltitudeMode != nullptr) |
| 2347 |
{ |
| 2348 |
poOgrLayer->SetCamera(pszCameraLongitude, pszCameraLatitude, |
| 2349 |
pszCameraAltitude, pszCameraHeading, |
| 2350 |
pszCameraTilt, pszCameraRoll, |
| 2351 |
pszCameraAltitudeMode); |
| 2352 |
} |
| 2353 |
} |
| 2354 |
|
| 2355 |
const char *pszRegionAdd = |
| 2356 |
CSLFetchNameValueDef(papszOptions, "ADD_REGION", "FALSE"); |
| 2357 |
const char *pszRegionXMin = CSLFetchNameValue(papszOptions, "REGION_XMIN"); |
| 2358 |
const char *pszRegionYMin = CSLFetchNameValue(papszOptions, "REGION_YMIN"); |
| 2359 |
const char *pszRegionXMax = CSLFetchNameValue(papszOptions, "REGION_XMAX"); |
| 2360 |
const char *pszRegionYMax = CSLFetchNameValue(papszOptions, "REGION_YMAX"); |
| 2361 |
const char *pszRegionMinLodPixels = |
| 2362 |
CSLFetchNameValueDef(papszOptions, "REGION_MIN_LOD_PIXELS", "256"); |
| 2363 |
const char *pszRegionMaxLodPixels = |
| 2364 |
CSLFetchNameValueDef(papszOptions, "REGION_MAX_LOD_PIXELS", "-1"); |
| 2365 |
const char *pszRegionMinFadeExtent = |
| 2366 |
CSLFetchNameValueDef(papszOptions, "REGION_MIN_FADE_EXTENT", "0"); |
| 2367 |
const char *pszRegionMaxFadeExtent = |
| 2368 |
CSLFetchNameValueDef(papszOptions, "REGION_MAX_FADE_EXTENT", "0"); |
| 2369 |
if (poOgrLayer != nullptr && CPLTestBool(pszRegionAdd)) |
| 2370 |
{ |
| 2371 |
poOgrLayer->SetWriteRegion( |
| 2372 |
CPLAtof(pszRegionMinLodPixels), CPLAtof(pszRegionMaxLodPixels), |
| 2373 |
CPLAtof(pszRegionMinFadeExtent), CPLAtof(pszRegionMaxFadeExtent)); |
| 2374 |
if (pszRegionXMin != nullptr && pszRegionYMin != nullptr && |
| 2375 |
pszRegionXMax != nullptr && pszRegionYMax != nullptr) |
| 2376 |
{ |
| 2377 |
const double xmin = CPLAtof(pszRegionXMin); |
| 2378 |
const double ymin = CPLAtof(pszRegionYMin); |
| 2379 |
const double xmax = CPLAtof(pszRegionXMax); |
| 2380 |
const double ymax = CPLAtof(pszRegionYMax); |
| 2381 |
if (xmin < xmax && ymin < ymax) |
| 2382 |
poOgrLayer->SetRegionBounds(xmin, ymin, xmax, ymax); |
| 2383 |
} |
| 2384 |
} |
| 2385 |
|
| 2386 |
const char *pszSOHref = CSLFetchNameValue(papszOptions, "SO_HREF"); |
| 2387 |
const char *pszSOName = CSLFetchNameValue(papszOptions, "SO_NAME"); |
| 2388 |
const char *pszSODescription = |
| 2389 |
CSLFetchNameValue(papszOptions, "SO_DESCRIPTION"); |
| 2390 |
const char *pszSOOverlayX = CSLFetchNameValue(papszOptions, "SO_OVERLAY_X"); |
| 2391 |
const char *pszSOOverlayY = CSLFetchNameValue(papszOptions, "SO_OVERLAY_Y"); |
| 2392 |
const char *pszSOOverlayXUnits = |
| 2393 |
CSLFetchNameValue(papszOptions, "SO_OVERLAY_XUNITS"); |
| 2394 |
const char *pszSOOverlayYUnits = |
| 2395 |
CSLFetchNameValue(papszOptions, "SO_OVERLAY_YUNITS"); |
| 2396 |
const char *pszSOScreenX = CSLFetchNameValue(papszOptions, "SO_SCREEN_X"); |
| 2397 |
const char *pszSOScreenY = CSLFetchNameValue(papszOptions, "SO_SCREEN_Y"); |
| 2398 |
const char *pszSOScreenXUnits = |
| 2399 |
CSLFetchNameValue(papszOptions, "SO_SCREEN_XUNITS"); |
| 2400 |
const char *pszSOScreenYUnits = |
| 2401 |
CSLFetchNameValue(papszOptions, "SO_SCREEN_YUNITS"); |
| 2402 |
const char *pszSOSizeX = CSLFetchNameValue(papszOptions, "SO_SIZE_X"); |
| 2403 |
const char *pszSOSizeY = CSLFetchNameValue(papszOptions, "SO_SIZE_Y"); |
| 2404 |
const char *pszSOSizeXUnits = |
| 2405 |
CSLFetchNameValue(papszOptions, "SO_SIZE_XUNITS"); |
| 2406 |
const char *pszSOSizeYUnits = |
| 2407 |
CSLFetchNameValue(papszOptions, "SO_SIZE_YUNITS"); |
| 2408 |
if (poOgrLayer != nullptr && pszSOHref != nullptr) |
| 2409 |
{ |
| 2410 |
poOgrLayer->SetScreenOverlay( |
| 2411 |
pszSOHref, pszSOName, pszSODescription, pszSOOverlayX, |
| 2412 |
pszSOOverlayY, pszSOOverlayXUnits, pszSOOverlayYUnits, pszSOScreenX, |
| 2413 |
pszSOScreenY, pszSOScreenXUnits, pszSOScreenYUnits, pszSOSizeX, |
| 2414 |
pszSOSizeY, pszSOSizeXUnits, pszSOSizeYUnits); |
| 2415 |
} |
| 2416 |
|
| 2417 |
const char *pszListStyleType = |
| 2418 |
CSLFetchNameValue(papszOptions, "LISTSTYLE_TYPE"); |
| 2419 |
const char *pszListStyleIconHref = |
| 2420 |
CSLFetchNameValue(papszOptions, "LISTSTYLE_ICON_HREF"); |
| 2421 |
if (poOgrLayer != nullptr) |
| 2422 |
{ |
| 2423 |
poOgrLayer->SetListStyle(pszListStyleType, pszListStyleIconHref); |
| 2424 |
} |
| 2425 |
|
| 2426 |
if (poOgrLayer != nullptr && poOgrLayer->GetKmlLayer()) |
| 2427 |
{ |
| 2428 |
SetCommonOptions(poOgrLayer->GetKmlLayer(), papszOptions); |
| 2429 |
} |
| 2430 |
|
| 2431 |
/***** mark the dataset as updated *****/ |
| 2432 |
if (poOgrLayer) |
| 2433 |
bUpdated = true; |
| 2434 |
|
| 2435 |
return poOgrLayer; |
| 2436 |
} |
| 2437 |
|
| 2438 |
/****************************************************************************** |
| 2439 |
Method to get a datasources style table. |
| 2440 |
|
| 2441 |
Args: none |
| 2442 |
|
| 2443 |
Returns: pointer to the datasources style table, or NULL if it does |
| 2444 |
not have one |
| 2445 |
|
| 2446 |
******************************************************************************/ |
| 2447 |
|
| 2448 |
OGRStyleTable *OGRLIBKMLDataSource::GetStyleTable() |
| 2449 |
{ |
| 2450 |
return m_poStyleTable; |
| 2451 |
} |
| 2452 |
|
| 2453 |
/****************************************************************************** |
| 2454 |
Method to write a style table to a single file .kml ds. |
| 2455 |
|
| 2456 |
Args: poStyleTable pointer to the style table to add |
| 2457 |
|
| 2458 |
Returns: nothing |
| 2459 |
|
| 2460 |
******************************************************************************/ |
| 2461 |
|
| 2462 |
void OGRLIBKMLDataSource::SetStyleTable2Kml(OGRStyleTable *poStyleTable) |
| 2463 |
{ |
| 2464 |
if (!m_poKmlDSContainer) |
| 2465 |
return; |
| 2466 |
|
| 2467 |
/***** delete all the styles *****/ |
| 2468 |
|
| 2469 |
DocumentPtr poKmlDocument = AsDocument(m_poKmlDSContainer); |
| 2470 |
int nKmlStyles = |
| 2471 |
static_cast<int>(poKmlDocument->get_styleselector_array_size()); |
| 2472 |
|
| 2473 |
for (int iKmlStyle = nKmlStyles - 1; iKmlStyle >= 0; iKmlStyle--) |
| 2474 |
{ |
| 2475 |
poKmlDocument->DeleteStyleSelectorAt(iKmlStyle); |
| 2476 |
} |
| 2477 |
|
| 2478 |
/***** add the new style table to the document *****/ |
| 2479 |
|
| 2480 |
styletable2kml(poStyleTable, m_poKmlFactory, AsContainer(poKmlDocument), |
| 2481 |
m_papszOptions); |
| 2482 |
} |
| 2483 |
|
| 2484 |
/****************************************************************************** |
| 2485 |
Method to write a style table to a kmz ds. |
| 2486 |
|
| 2487 |
Args: poStyleTable pointer to the style table to add |
| 2488 |
|
| 2489 |
Returns: nothing |
| 2490 |
|
| 2491 |
******************************************************************************/ |
| 2492 |
|
| 2493 |
void OGRLIBKMLDataSource::SetStyleTable2Kmz(OGRStyleTable *poStyleTable) |
| 2494 |
{ |
| 2495 |
if (m_poKmlStyleKml || poStyleTable != nullptr) |
| 2496 |
{ |
| 2497 |
/***** replace the style document with a new one *****/ |
| 2498 |
|
| 2499 |
m_poKmlStyleKml = m_poKmlFactory->CreateDocument(); |
| 2500 |
m_poKmlStyleKml->set_id("styleId"); |
| 2501 |
|
| 2502 |
styletable2kml(poStyleTable, m_poKmlFactory, m_poKmlStyleKml); |
| 2503 |
} |
| 2504 |
} |
| 2505 |
|
| 2506 |
/****************************************************************************** |
| 2507 |
Method to write a style table to a datasource. |
| 2508 |
|
| 2509 |
Args: poStyleTable pointer to the style table to add |
| 2510 |
|
| 2511 |
Returns: nothing |
| 2512 |
|
| 2513 |
Note: This method assumes ownership of the style table. |
| 2514 |
|
| 2515 |
******************************************************************************/ |
| 2516 |
|
| 2517 |
void OGRLIBKMLDataSource::SetStyleTableDirectly(OGRStyleTable *poStyleTable) |
| 2518 |
{ |
| 2519 |
if (!bUpdate) |
| 2520 |
return; |
| 2521 |
|
| 2522 |
if (m_poStyleTable) |
| 2523 |
delete m_poStyleTable; |
| 2524 |
|
| 2525 |
m_poStyleTable = poStyleTable; |
| 2526 |
|
| 2527 |
/***** a kml datasource? *****/ |
| 2528 |
if (IsKml()) |
| 2529 |
SetStyleTable2Kml(m_poStyleTable); |
| 2530 |
|
| 2531 |
else if (IsKmz() || IsDir()) |
| 2532 |
SetStyleTable2Kmz(m_poStyleTable); |
| 2533 |
|
| 2534 |
bUpdated = true; |
| 2535 |
} |
| 2536 |
|
| 2537 |
/****************************************************************************** |
| 2538 |
Method to write a style table to a datasource. |
| 2539 |
|
| 2540 |
Args: poStyleTable pointer to the style table to add |
| 2541 |
|
| 2542 |
Returns: nothing |
| 2543 |
|
| 2544 |
Note: This method copies the style table, and the user will still be |
| 2545 |
responsible for its destruction. |
| 2546 |
|
| 2547 |
******************************************************************************/ |
| 2548 |
|
| 2549 |
void OGRLIBKMLDataSource::SetStyleTable(OGRStyleTable *poStyleTable) |
| 2550 |
{ |
| 2551 |
if (!bUpdate) |
| 2552 |
return; |
| 2553 |
|
| 2554 |
if (poStyleTable) |
| 2555 |
SetStyleTableDirectly(poStyleTable->Clone()); |
| 2556 |
else |
| 2557 |
SetStyleTableDirectly(nullptr); |
| 2558 |
} |
| 2559 |
|
| 2560 |
/****************************************************************************** |
| 2561 |
Test if capability is available. |
| 2562 |
|
| 2563 |
Args: pszCap datasource capability name to test |
| 2564 |
|
| 2565 |
Returns: TRUE or FALSE |
| 2566 |
|
| 2567 |
ODsCCreateLayer: True if this datasource can create new layers. |
| 2568 |
ODsCDeleteLayer: True if this datasource can delete existing layers. |
| 2569 |
|
| 2570 |
******************************************************************************/ |
| 2571 |
|
| 2572 |
int OGRLIBKMLDataSource::TestCapability(const char *pszCap) const |
| 2573 |
{ |
| 2574 |
if (EQUAL(pszCap, ODsCCreateLayer)) |
| 2575 |
return bUpdate; |
| 2576 |
else if (EQUAL(pszCap, ODsCDeleteLayer)) |
| 2577 |
return bUpdate; |
| 2578 |
else if (EQUAL(pszCap, ODsCRandomLayerWrite)) |
| 2579 |
return bUpdate; |
| 2580 |
else if (EQUAL(pszCap, ODsCZGeometries)) |
| 2581 |
return TRUE; |
| 2582 |
|
| 2583 |
return FALSE; |
| 2584 |
} |