| 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 "ogr_geometry.h" |
| 17 |
#include "ogr_p.h" |
| 18 |
#include "ogrlibkmlgeometry.h" |
| 19 |
|
| 20 |
using kmlbase::Vec3; |
| 21 |
using kmldom::CoordinatesPtr; |
| 22 |
using kmldom::ElementPtr; |
| 23 |
using kmldom::GeometryPtr; |
| 24 |
using kmldom::GxLatLonQuadPtr; |
| 25 |
using kmldom::GxMultiTrackPtr; |
| 26 |
using kmldom::GxTrackPtr; |
| 27 |
using kmldom::InnerBoundaryIsPtr; |
| 28 |
using kmldom::KmlFactory; |
| 29 |
using kmldom::LatLonBoxPtr; |
| 30 |
using kmldom::LinearRingPtr; |
| 31 |
using kmldom::LineStringPtr; |
| 32 |
using kmldom::MultiGeometryPtr; |
| 33 |
using kmldom::OuterBoundaryIsPtr; |
| 34 |
using kmldom::PointPtr; |
| 35 |
using kmldom::PolygonPtr; |
| 36 |
|
| 37 |
static bool NormalizeLongLat(double &x, double &y) |
| 38 |
{ |
| 39 |
if (x >= -180 && x <= 180) |
| 40 |
{ |
| 41 |
// nominal |
| 42 |
} |
| 43 |
else if (x > 180 && x <= 180 + 360) |
| 44 |
x -= 360; |
| 45 |
else if (x < -180 && x >= -180 - 360) |
| 46 |
x += 360; |
| 47 |
else |
| 48 |
{ |
| 49 |
const bool bStrictCompliance = |
| 50 |
CPLTestBool(CPLGetConfigOption("LIBKML_STRICT_COMPLIANCE", "TRUE")); |
| 51 |
CPLError(bStrictCompliance ? CE_Failure : CE_Warning, CPLE_AppDefined, |
| 52 |
"Invalid longitude %g", x); |
| 53 |
if (bStrictCompliance) |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
constexpr double EPSILON = 1e-8; |
| 58 |
if (y >= -90 && y <= 90) |
| 59 |
{ |
| 60 |
// nominal |
| 61 |
} |
| 62 |
else if (y > 90 && y < 90 + EPSILON) |
| 63 |
{ |
| 64 |
y = 90; |
| 65 |
} |
| 66 |
else if (y < -90 && y > -90 - EPSILON) |
| 67 |
{ |
| 68 |
y = -90; |
| 69 |
} |
| 70 |
else |
| 71 |
{ |
| 72 |
const bool bStrictCompliance = |
| 73 |
CPLTestBool(CPLGetConfigOption("LIBKML_STRICT_COMPLIANCE", "TRUE")); |
| 74 |
CPLError(bStrictCompliance ? CE_Failure : CE_Warning, CPLE_AppDefined, |
| 75 |
"Invalid latitude %g", y); |
| 76 |
if (bStrictCompliance) |
| 77 |
return false; |
| 78 |
} |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
/****************************************************************************** |
| 83 |
Function to write out a ogr geometry to kml. |
| 84 |
|
| 85 |
Args: |
| 86 |
poOgrGeom the ogr geometry |
| 87 |
extra used in recursion, just pass -1 |
| 88 |
poKmlFactory pointer to the libkml dom factory |
| 89 |
|
| 90 |
Returns: |
| 91 |
ElementPtr to the geometry created |
| 92 |
|
| 93 |
******************************************************************************/ |
| 94 |
|
| 95 |
ElementPtr geom2kml(OGRGeometry *poOgrGeom, int extra, KmlFactory *poKmlFactory) |
| 96 |
{ |
| 97 |
if (!poOgrGeom) |
| 98 |
{ |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
/***** libkml geom vars *****/ |
| 103 |
CoordinatesPtr coordinates = nullptr; |
| 104 |
|
| 105 |
// This will be the return value. |
| 106 |
ElementPtr poKmlGeometry = nullptr; |
| 107 |
|
| 108 |
/***** Other vars *****/ |
| 109 |
int numpoints = 0; |
| 110 |
const OGRwkbGeometryType type = poOgrGeom->getGeometryType(); |
| 111 |
|
| 112 |
switch (type) |
| 113 |
{ |
| 114 |
case wkbPoint: |
| 115 |
{ |
| 116 |
const OGRPoint *poOgrPoint = poOgrGeom->toPoint(); |
| 117 |
PointPtr poKmlPoint = nullptr; |
| 118 |
if (poOgrPoint->getCoordinateDimension() == 0) |
| 119 |
{ |
| 120 |
poKmlPoint = poKmlFactory->CreatePoint(); |
| 121 |
poKmlGeometry = poKmlPoint; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
double x = poOgrPoint->getX(); |
| 126 |
double y = poOgrPoint->getY(); |
| 127 |
|
| 128 |
if (!NormalizeLongLat(x, y)) |
| 129 |
return nullptr; |
| 130 |
|
| 131 |
coordinates = poKmlFactory->CreateCoordinates(); |
| 132 |
coordinates->add_latlng(y, x); |
| 133 |
poKmlPoint = poKmlFactory->CreatePoint(); |
| 134 |
poKmlGeometry = poKmlPoint; |
| 135 |
poKmlPoint->set_coordinates(coordinates); |
| 136 |
} |
| 137 |
|
| 138 |
break; |
| 139 |
} |
| 140 |
case wkbPoint25D: |
| 141 |
{ |
| 142 |
const OGRPoint *poOgrPoint = poOgrGeom->toPoint(); |
| 143 |
|
| 144 |
double x = poOgrPoint->getX(); |
| 145 |
double y = poOgrPoint->getY(); |
| 146 |
const double z = poOgrPoint->getZ(); |
| 147 |
|
| 148 |
if (!NormalizeLongLat(x, y)) |
| 149 |
return nullptr; |
| 150 |
|
| 151 |
coordinates = poKmlFactory->CreateCoordinates(); |
| 152 |
coordinates->add_latlngalt(y, x, z); |
| 153 |
PointPtr poKmlPoint = poKmlFactory->CreatePoint(); |
| 154 |
poKmlGeometry = poKmlPoint; |
| 155 |
poKmlPoint->set_coordinates(coordinates); |
| 156 |
|
| 157 |
break; |
| 158 |
} |
| 159 |
case wkbLineString: |
| 160 |
{ |
| 161 |
OGRLineString *poOgrLineString = poOgrGeom->toLineString(); |
| 162 |
|
| 163 |
if (extra >= 0) |
| 164 |
{ |
| 165 |
poOgrGeom->toLinearRing()->closeRings(); |
| 166 |
} |
| 167 |
|
| 168 |
numpoints = poOgrLineString->getNumPoints(); |
| 169 |
if (extra >= 0) |
| 170 |
{ |
| 171 |
if (numpoints < 4 && CPLTestBool(CPLGetConfigOption( |
| 172 |
"LIBKML_STRICT_COMPLIANCE", "TRUE"))) |
| 173 |
{ |
| 174 |
CPLError(CE_Failure, CPLE_NotSupported, |
| 175 |
"A linearring should have at least 4 points"); |
| 176 |
return nullptr; |
| 177 |
} |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
if (numpoints < 2 && CPLTestBool(CPLGetConfigOption( |
| 182 |
"LIBKML_STRICT_COMPLIANCE", "TRUE"))) |
| 183 |
{ |
| 184 |
CPLError(CE_Failure, CPLE_NotSupported, |
| 185 |
"A linestring should have at least 2 points"); |
| 186 |
return nullptr; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
coordinates = poKmlFactory->CreateCoordinates(); |
| 191 |
|
| 192 |
OGRPoint point; |
| 193 |
|
| 194 |
for (int i = 0; i < numpoints; i++) |
| 195 |
{ |
| 196 |
poOgrLineString->getPoint(i, &point); |
| 197 |
|
| 198 |
double x = point.getX(); |
| 199 |
double y = point.getY(); |
| 200 |
|
| 201 |
if (!NormalizeLongLat(x, y)) |
| 202 |
return nullptr; |
| 203 |
|
| 204 |
coordinates->add_latlng(y, x); |
| 205 |
} |
| 206 |
|
| 207 |
/***** Check if its a wkbLinearRing *****/ |
| 208 |
if (extra < 0) |
| 209 |
{ |
| 210 |
LineStringPtr poKmlLineString = |
| 211 |
poKmlFactory->CreateLineString(); |
| 212 |
poKmlGeometry = poKmlLineString; |
| 213 |
poKmlLineString->set_coordinates(coordinates); |
| 214 |
|
| 215 |
break; |
| 216 |
} |
| 217 |
[[fallthrough]]; |
| 218 |
} |
| 219 |
/***** fallthrough *****/ |
| 220 |
|
| 221 |
case wkbLinearRing: // This case is for readability only. |
| 222 |
{ |
| 223 |
LinearRingPtr poKmlLinearRing = poKmlFactory->CreateLinearRing(); |
| 224 |
poKmlLinearRing->set_coordinates(coordinates); |
| 225 |
|
| 226 |
if (!extra) |
| 227 |
{ |
| 228 |
OuterBoundaryIsPtr poKmlOuterRing = |
| 229 |
poKmlFactory->CreateOuterBoundaryIs(); |
| 230 |
poKmlOuterRing->set_linearring(poKmlLinearRing); |
| 231 |
poKmlGeometry = poKmlOuterRing; |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
InnerBoundaryIsPtr poKmlInnerRing = |
| 236 |
poKmlFactory->CreateInnerBoundaryIs(); |
| 237 |
poKmlGeometry = poKmlInnerRing; |
| 238 |
poKmlInnerRing->set_linearring(poKmlLinearRing); |
| 239 |
} |
| 240 |
|
| 241 |
break; |
| 242 |
} |
| 243 |
case wkbLineString25D: |
| 244 |
{ |
| 245 |
const OGRLineString *poOgrLineString = poOgrGeom->toLineString(); |
| 246 |
|
| 247 |
if (extra >= 0) |
| 248 |
{ |
| 249 |
poOgrGeom->toLinearRing()->closeRings(); |
| 250 |
} |
| 251 |
|
| 252 |
numpoints = poOgrLineString->getNumPoints(); |
| 253 |
if (extra >= 0) |
| 254 |
{ |
| 255 |
if (numpoints < 4 && CPLTestBool(CPLGetConfigOption( |
| 256 |
"LIBKML_STRICT_COMPLIANCE", "TRUE"))) |
| 257 |
{ |
| 258 |
CPLError(CE_Failure, CPLE_NotSupported, |
| 259 |
"A linearring should have at least 4 points"); |
| 260 |
return nullptr; |
| 261 |
} |
| 262 |
} |
| 263 |
else |
| 264 |
{ |
| 265 |
if (numpoints < 2 && CPLTestBool(CPLGetConfigOption( |
| 266 |
"LIBKML_STRICT_COMPLIANCE", "TRUE"))) |
| 267 |
{ |
| 268 |
CPLError(CE_Failure, CPLE_NotSupported, |
| 269 |
"A linestring should have at least 2 points"); |
| 270 |
return nullptr; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
coordinates = poKmlFactory->CreateCoordinates(); |
| 275 |
OGRPoint point; |
| 276 |
|
| 277 |
for (int i = 0; i < numpoints; i++) |
| 278 |
{ |
| 279 |
poOgrLineString->getPoint(i, &point); |
| 280 |
|
| 281 |
double x = point.getX(); |
| 282 |
double y = point.getY(); |
| 283 |
const double z = point.getZ(); |
| 284 |
|
| 285 |
if (!NormalizeLongLat(x, y)) |
| 286 |
return nullptr; |
| 287 |
|
| 288 |
coordinates->add_latlngalt(y, x, z); |
| 289 |
} |
| 290 |
|
| 291 |
/***** Check if its a wkbLinearRing *****/ |
| 292 |
if (extra < 0) |
| 293 |
{ |
| 294 |
LineStringPtr poKmlLineString = |
| 295 |
poKmlFactory->CreateLineString(); |
| 296 |
poKmlGeometry = poKmlLineString; |
| 297 |
poKmlLineString->set_coordinates(coordinates); |
| 298 |
|
| 299 |
break; |
| 300 |
} |
| 301 |
/***** fallthrough *****/ |
| 302 |
|
| 303 |
// case wkbLinearRing25D: // This case is for readability only. |
| 304 |
|
| 305 |
LinearRingPtr poKmlLinearRing = poKmlFactory->CreateLinearRing(); |
| 306 |
poKmlLinearRing->set_coordinates(coordinates); |
| 307 |
|
| 308 |
if (!extra) |
| 309 |
{ |
| 310 |
OuterBoundaryIsPtr poKmlOuterRing = |
| 311 |
poKmlFactory->CreateOuterBoundaryIs(); |
| 312 |
poKmlGeometry = poKmlOuterRing; |
| 313 |
poKmlOuterRing->set_linearring(poKmlLinearRing); |
| 314 |
} |
| 315 |
else |
| 316 |
{ |
| 317 |
InnerBoundaryIsPtr poKmlInnerRing = |
| 318 |
poKmlFactory->CreateInnerBoundaryIs(); |
| 319 |
poKmlGeometry = poKmlInnerRing; |
| 320 |
poKmlInnerRing->set_linearring(poKmlLinearRing); |
| 321 |
} |
| 322 |
|
| 323 |
break; |
| 324 |
} |
| 325 |
case wkbPolygon: |
| 326 |
{ |
| 327 |
if (CPLTestBool( |
| 328 |
CPLGetConfigOption("LIBKML_STRICT_COMPLIANCE", "TRUE")) && |
| 329 |
OGRGeometryFactory::haveGEOS()) |
| 330 |
{ |
| 331 |
std::string osReason; |
| 332 |
if (!poOgrGeom->IsValid(&osReason)) |
| 333 |
{ |
| 334 |
CPLError( |
| 335 |
CE_Failure, CPLE_NotSupported, "Invalid polygon %s: %s", |
| 336 |
poOgrGeom->exportToWkt().c_str(), osReason.c_str()); |
| 337 |
return nullptr; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
PolygonPtr poKmlPolygon = poKmlFactory->CreatePolygon(); |
| 342 |
poKmlGeometry = poKmlPolygon; |
| 343 |
|
| 344 |
OGRPolygon *poOgrPolygon = poOgrGeom->toPolygon(); |
| 345 |
ElementPtr poKmlTmpGeometry = |
| 346 |
geom2kml(poOgrPolygon->getExteriorRing(), 0, poKmlFactory); |
| 347 |
if (!poKmlTmpGeometry) |
| 348 |
return nullptr; |
| 349 |
poKmlPolygon->set_outerboundaryis( |
| 350 |
AsOuterBoundaryIs(poKmlTmpGeometry)); |
| 351 |
|
| 352 |
const int nGeom = poOgrPolygon->getNumInteriorRings(); |
| 353 |
for (int i = 0; i < nGeom; i++) |
| 354 |
{ |
| 355 |
poKmlTmpGeometry = geom2kml(poOgrPolygon->getInteriorRing(i), |
| 356 |
i + 1, poKmlFactory); |
| 357 |
if (!poKmlTmpGeometry) |
| 358 |
return nullptr; |
| 359 |
poKmlPolygon->add_innerboundaryis( |
| 360 |
AsInnerBoundaryIs(poKmlTmpGeometry)); |
| 361 |
} |
| 362 |
|
| 363 |
break; |
| 364 |
} |
| 365 |
case wkbPolygon25D: |
| 366 |
{ |
| 367 |
if (CPLTestBool( |
| 368 |
CPLGetConfigOption("LIBKML_STRICT_COMPLIANCE", "TRUE")) && |
| 369 |
OGRGeometryFactory::haveGEOS()) |
| 370 |
{ |
| 371 |
std::string osReason; |
| 372 |
if (!poOgrGeom->IsValid(&osReason)) |
| 373 |
{ |
| 374 |
CPLError( |
| 375 |
CE_Failure, CPLE_NotSupported, "Invalid polygon %s: %s", |
| 376 |
poOgrGeom->exportToWkt().c_str(), osReason.c_str()); |
| 377 |
return nullptr; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
PolygonPtr poKmlPolygon = poKmlFactory->CreatePolygon(); |
| 382 |
poKmlGeometry = poKmlPolygon; |
| 383 |
|
| 384 |
OGRPolygon *poOgrPolygon = poOgrGeom->toPolygon(); |
| 385 |
ElementPtr poKmlTmpGeometry = |
| 386 |
geom2kml(poOgrPolygon->getExteriorRing(), 0, poKmlFactory); |
| 387 |
if (!poKmlTmpGeometry) |
| 388 |
return nullptr; |
| 389 |
poKmlPolygon->set_outerboundaryis( |
| 390 |
AsOuterBoundaryIs(poKmlTmpGeometry)); |
| 391 |
|
| 392 |
const int nGeom = poOgrPolygon->getNumInteriorRings(); |
| 393 |
for (int i = 0; i < nGeom; i++) |
| 394 |
{ |
| 395 |
poKmlTmpGeometry = geom2kml(poOgrPolygon->getInteriorRing(i), |
| 396 |
i + 1, poKmlFactory); |
| 397 |
if (!poKmlTmpGeometry) |
| 398 |
return nullptr; |
| 399 |
poKmlPolygon->add_innerboundaryis( |
| 400 |
AsInnerBoundaryIs(poKmlTmpGeometry)); |
| 401 |
} |
| 402 |
|
| 403 |
break; |
| 404 |
} |
| 405 |
case wkbMultiPoint: |
| 406 |
case wkbMultiLineString: |
| 407 |
case wkbMultiPolygon: |
| 408 |
case wkbGeometryCollection: |
| 409 |
case wkbMultiPoint25D: |
| 410 |
case wkbMultiLineString25D: |
| 411 |
case wkbMultiPolygon25D: |
| 412 |
case wkbGeometryCollection25D: |
| 413 |
{ |
| 414 |
OGRGeometryCollection *poOgrMultiGeom = |
| 415 |
poOgrGeom->toGeometryCollection(); |
| 416 |
|
| 417 |
const int nGeom = poOgrMultiGeom->getNumGeometries(); |
| 418 |
|
| 419 |
if (nGeom == 1 && CPLTestBool(CPLGetConfigOption( |
| 420 |
"LIBKML_STRICT_COMPLIANCE", "TRUE"))) |
| 421 |
{ |
| 422 |
CPLDebug("LIBKML", |
| 423 |
"Turning multiple geometry into single geometry"); |
| 424 |
poKmlGeometry = geom2kml(poOgrMultiGeom->getGeometryRef(0), -1, |
| 425 |
poKmlFactory); |
| 426 |
if (!poKmlGeometry) |
| 427 |
return nullptr; |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
if (nGeom == 0 && CPLTestBool(CPLGetConfigOption( |
| 432 |
"LIBKML_STRICT_COMPLIANCE", "TRUE"))) |
| 433 |
{ |
| 434 |
CPLError(CE_Warning, CPLE_AppDefined, |
| 435 |
"Empty multi geometry are not recommended"); |
| 436 |
} |
| 437 |
|
| 438 |
MultiGeometryPtr poKmlMultiGeometry = |
| 439 |
poKmlFactory->CreateMultiGeometry(); |
| 440 |
poKmlGeometry = poKmlMultiGeometry; |
| 441 |
|
| 442 |
for (int i = 0; i < nGeom; i++) |
| 443 |
{ |
| 444 |
ElementPtr poKmlTmpGeometry = geom2kml( |
| 445 |
poOgrMultiGeom->getGeometryRef(i), -1, poKmlFactory); |
| 446 |
if (!poKmlTmpGeometry) |
| 447 |
return nullptr; |
| 448 |
poKmlMultiGeometry->add_geometry( |
| 449 |
AsGeometry(std::move(poKmlTmpGeometry))); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
break; |
| 454 |
} |
| 455 |
case wkbUnknown: |
| 456 |
case wkbNone: |
| 457 |
default: |
| 458 |
break; |
| 459 |
} |
| 460 |
|
| 461 |
return poKmlGeometry; |
| 462 |
} |
| 463 |
|
| 464 |
/****************************************************************************** |
| 465 |
Recursive function to read a kml geometry and translate to ogr. |
| 466 |
|
| 467 |
Args: |
| 468 |
poKmlGeometry pointer to the kml geometry to translate |
| 469 |
poOgrSRS pointer to the spatial ref to set on the geometry |
| 470 |
|
| 471 |
Returns: |
| 472 |
pointer to the new ogr geometry object |
| 473 |
|
| 474 |
******************************************************************************/ |
| 475 |
|
| 476 |
static OGRGeometry *kml2geom_rec(const GeometryPtr &poKmlGeometry, |
| 477 |
OGRSpatialReference *poOgrSRS) |
| 478 |
{ |
| 479 |
/***** ogr geom vars *****/ |
| 480 |
OGRPoint *poOgrPoint = nullptr; |
| 481 |
OGRLineString *poOgrLineString = nullptr; |
| 482 |
OGRLinearRing *poOgrLinearRing = nullptr; |
| 483 |
OGRPolygon *poOgrPolygon = nullptr; |
| 484 |
OGRGeometryCollection *poOgrMultiGeometry = nullptr; |
| 485 |
OGRGeometry *poOgrGeometry = nullptr; |
| 486 |
OGRGeometry *poOgrTmpGeometry = nullptr; |
| 487 |
|
| 488 |
switch (poKmlGeometry->Type()) |
| 489 |
{ |
| 490 |
case kmldom::Type_Point: |
| 491 |
{ |
| 492 |
PointPtr poKmlPoint = AsPoint(poKmlGeometry); |
| 493 |
if (poKmlPoint->has_coordinates()) |
| 494 |
{ |
| 495 |
CoordinatesPtr poKmlCoordinates = poKmlPoint->get_coordinates(); |
| 496 |
const size_t nCoords = |
| 497 |
poKmlCoordinates->get_coordinates_array_size(); |
| 498 |
if (nCoords > 0) |
| 499 |
{ |
| 500 |
const Vec3 oKmlVec = |
| 501 |
poKmlCoordinates->get_coordinates_array_at(0); |
| 502 |
|
| 503 |
if (oKmlVec.has_altitude()) |
| 504 |
poOgrPoint = new OGRPoint(oKmlVec.get_longitude(), |
| 505 |
oKmlVec.get_latitude(), |
| 506 |
oKmlVec.get_altitude()); |
| 507 |
else |
| 508 |
poOgrPoint = new OGRPoint(oKmlVec.get_longitude(), |
| 509 |
oKmlVec.get_latitude()); |
| 510 |
|
| 511 |
poOgrGeometry = poOgrPoint; |
| 512 |
} |
| 513 |
else |
| 514 |
{ |
| 515 |
poOgrGeometry = new OGRPoint(); |
| 516 |
} |
| 517 |
} |
| 518 |
else |
| 519 |
{ |
| 520 |
poOgrGeometry = new OGRPoint(); |
| 521 |
} |
| 522 |
|
| 523 |
break; |
| 524 |
} |
| 525 |
case kmldom::Type_LineString: |
| 526 |
{ |
| 527 |
LineStringPtr poKmlLineString = AsLineString(poKmlGeometry); |
| 528 |
poOgrLineString = new OGRLineString(); |
| 529 |
if (poKmlLineString->has_coordinates()) |
| 530 |
{ |
| 531 |
CoordinatesPtr poKmlCoordinates = |
| 532 |
poKmlLineString->get_coordinates(); |
| 533 |
|
| 534 |
const size_t nCoords = |
| 535 |
poKmlCoordinates->get_coordinates_array_size(); |
| 536 |
for (size_t i = 0; i < nCoords; i++) |
| 537 |
{ |
| 538 |
const Vec3 oKmlVec = |
| 539 |
poKmlCoordinates->get_coordinates_array_at(i); |
| 540 |
if (oKmlVec.has_altitude()) |
| 541 |
poOgrLineString->addPoint(oKmlVec.get_longitude(), |
| 542 |
oKmlVec.get_latitude(), |
| 543 |
oKmlVec.get_altitude()); |
| 544 |
else |
| 545 |
poOgrLineString->addPoint(oKmlVec.get_longitude(), |
| 546 |
oKmlVec.get_latitude()); |
| 547 |
} |
| 548 |
} |
| 549 |
poOgrGeometry = poOgrLineString; |
| 550 |
|
| 551 |
break; |
| 552 |
} |
| 553 |
case kmldom::Type_LinearRing: |
| 554 |
{ |
| 555 |
LinearRingPtr poKmlLinearRing = AsLinearRing(poKmlGeometry); |
| 556 |
poOgrLinearRing = new OGRLinearRing(); |
| 557 |
if (poKmlLinearRing->has_coordinates()) |
| 558 |
{ |
| 559 |
CoordinatesPtr poKmlCoordinates = |
| 560 |
poKmlLinearRing->get_coordinates(); |
| 561 |
|
| 562 |
const size_t nCoords = |
| 563 |
poKmlCoordinates->get_coordinates_array_size(); |
| 564 |
for (size_t i = 0; i < nCoords; i++) |
| 565 |
{ |
| 566 |
const Vec3 oKmlVec = |
| 567 |
poKmlCoordinates->get_coordinates_array_at(i); |
| 568 |
if (oKmlVec.has_altitude()) |
| 569 |
poOgrLinearRing->addPoint(oKmlVec.get_longitude(), |
| 570 |
oKmlVec.get_latitude(), |
| 571 |
oKmlVec.get_altitude()); |
| 572 |
else |
| 573 |
poOgrLinearRing->addPoint(oKmlVec.get_longitude(), |
| 574 |
oKmlVec.get_latitude()); |
| 575 |
} |
| 576 |
} |
| 577 |
poOgrGeometry = poOgrLinearRing; |
| 578 |
|
| 579 |
break; |
| 580 |
} |
| 581 |
case kmldom::Type_Polygon: |
| 582 |
{ |
| 583 |
PolygonPtr poKmlPolygon = AsPolygon(poKmlGeometry); |
| 584 |
|
| 585 |
poOgrPolygon = new OGRPolygon(); |
| 586 |
if (poKmlPolygon->has_outerboundaryis()) |
| 587 |
{ |
| 588 |
OuterBoundaryIsPtr poKmlOuterRing = |
| 589 |
poKmlPolygon->get_outerboundaryis(); |
| 590 |
LinearRingPtr poKmlLinearRing = |
| 591 |
poKmlOuterRing->get_linearring(); |
| 592 |
if (poKmlLinearRing) |
| 593 |
{ |
| 594 |
poOgrTmpGeometry = kml2geom_rec(poKmlLinearRing, poOgrSRS); |
| 595 |
|
| 596 |
poOgrPolygon->addRingDirectly( |
| 597 |
poOgrTmpGeometry->toLinearRing()); |
| 598 |
} |
| 599 |
} |
| 600 |
const size_t nRings = |
| 601 |
poKmlPolygon->get_innerboundaryis_array_size(); |
| 602 |
for (size_t i = 0; i < nRings; i++) |
| 603 |
{ |
| 604 |
InnerBoundaryIsPtr poKmlInnerRing = |
| 605 |
poKmlPolygon->get_innerboundaryis_array_at(i); |
| 606 |
LinearRingPtr poKmlLinearRing = |
| 607 |
poKmlInnerRing->get_linearring(); |
| 608 |
if (poKmlLinearRing) |
| 609 |
{ |
| 610 |
poOgrTmpGeometry = kml2geom_rec(poKmlLinearRing, poOgrSRS); |
| 611 |
|
| 612 |
poOgrPolygon->addRingDirectly( |
| 613 |
poOgrTmpGeometry->toLinearRing()); |
| 614 |
} |
| 615 |
} |
| 616 |
poOgrGeometry = poOgrPolygon; |
| 617 |
|
| 618 |
break; |
| 619 |
} |
| 620 |
case kmldom::Type_MultiGeometry: |
| 621 |
{ |
| 622 |
MultiGeometryPtr poKmlMultiGeometry = |
| 623 |
AsMultiGeometry(poKmlGeometry); |
| 624 |
const size_t nGeom = poKmlMultiGeometry->get_geometry_array_size(); |
| 625 |
|
| 626 |
// Detect subgeometry type to instantiate appropriate |
| 627 |
// multi geometry type. |
| 628 |
kmldom::KmlDomType type = kmldom::Type_Unknown; |
| 629 |
for (size_t i = 0; i < nGeom; i++) |
| 630 |
{ |
| 631 |
GeometryPtr poKmlTmpGeometry = |
| 632 |
poKmlMultiGeometry->get_geometry_array_at(i); |
| 633 |
if (type == kmldom::Type_Unknown) |
| 634 |
{ |
| 635 |
type = poKmlTmpGeometry->Type(); |
| 636 |
} |
| 637 |
else if (type != poKmlTmpGeometry->Type()) |
| 638 |
{ |
| 639 |
type = kmldom::Type_Unknown; |
| 640 |
break; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
if (type == kmldom::Type_Point) |
| 645 |
poOgrMultiGeometry = new OGRMultiPoint(); |
| 646 |
else if (type == kmldom::Type_LineString) |
| 647 |
poOgrMultiGeometry = new OGRMultiLineString(); |
| 648 |
else if (type == kmldom::Type_Polygon) |
| 649 |
poOgrMultiGeometry = new OGRMultiPolygon(); |
| 650 |
else |
| 651 |
poOgrMultiGeometry = new OGRGeometryCollection(); |
| 652 |
|
| 653 |
for (size_t i = 0; i < nGeom; i++) |
| 654 |
{ |
| 655 |
GeometryPtr poKmlTmpGeometry = |
| 656 |
poKmlMultiGeometry->get_geometry_array_at(i); |
| 657 |
poOgrTmpGeometry = kml2geom_rec(poKmlTmpGeometry, poOgrSRS); |
| 658 |
|
| 659 |
poOgrMultiGeometry->addGeometryDirectly(poOgrTmpGeometry); |
| 660 |
} |
| 661 |
poOgrGeometry = poOgrMultiGeometry; |
| 662 |
break; |
| 663 |
} |
| 664 |
case kmldom::Type_GxTrack: |
| 665 |
{ |
| 666 |
GxTrackPtr poKmlGxTrack = AsGxTrack(poKmlGeometry); |
| 667 |
#if defined(__GNUC__) |
| 668 |
#pragma GCC diagnostic push |
| 669 |
#pragma GCC diagnostic ignored "-Wnull-dereference" |
| 670 |
#endif |
| 671 |
const size_t nCoords = poKmlGxTrack->get_gx_coord_array_size(); |
| 672 |
#if defined(__GNUC__) |
| 673 |
#pragma GCC diagnostic pop |
| 674 |
#endif |
| 675 |
poOgrLineString = new OGRLineString(); |
| 676 |
for (size_t i = 0; i < nCoords; i++) |
| 677 |
{ |
| 678 |
const Vec3 oKmlVec = poKmlGxTrack->get_gx_coord_array_at(i); |
| 679 |
if (oKmlVec.has_altitude()) |
| 680 |
poOgrLineString->addPoint(oKmlVec.get_longitude(), |
| 681 |
oKmlVec.get_latitude(), |
| 682 |
oKmlVec.get_altitude()); |
| 683 |
else |
| 684 |
poOgrLineString->addPoint(oKmlVec.get_longitude(), |
| 685 |
oKmlVec.get_latitude()); |
| 686 |
} |
| 687 |
poOgrGeometry = poOgrLineString; |
| 688 |
break; |
| 689 |
} |
| 690 |
case kmldom::Type_GxMultiTrack: |
| 691 |
{ |
| 692 |
GxMultiTrackPtr poKmlGxMultiTrack = AsGxMultiTrack(poKmlGeometry); |
| 693 |
#if defined(__GNUC__) |
| 694 |
#pragma GCC diagnostic push |
| 695 |
#pragma GCC diagnostic ignored "-Wnull-dereference" |
| 696 |
#endif |
| 697 |
const size_t nGeom = poKmlGxMultiTrack->get_gx_track_array_size(); |
| 698 |
#if defined(__GNUC__) |
| 699 |
#pragma GCC diagnostic pop |
| 700 |
#endif |
| 701 |
poOgrMultiGeometry = new OGRMultiLineString(); |
| 702 |
for (size_t j = 0; j < nGeom; j++) |
| 703 |
{ |
| 704 |
GxTrackPtr poKmlGxTrack = |
| 705 |
poKmlGxMultiTrack->get_gx_track_array_at(j); |
| 706 |
const size_t nCoords = poKmlGxTrack->get_gx_coord_array_size(); |
| 707 |
poOgrLineString = new OGRLineString(); |
| 708 |
for (size_t i = 0; i < nCoords; i++) |
| 709 |
{ |
| 710 |
const Vec3 oKmlVec = poKmlGxTrack->get_gx_coord_array_at(i); |
| 711 |
if (oKmlVec.has_altitude()) |
| 712 |
poOgrLineString->addPoint(oKmlVec.get_longitude(), |
| 713 |
oKmlVec.get_latitude(), |
| 714 |
oKmlVec.get_altitude()); |
| 715 |
else |
| 716 |
poOgrLineString->addPoint(oKmlVec.get_longitude(), |
| 717 |
oKmlVec.get_latitude()); |
| 718 |
} |
| 719 |
poOgrMultiGeometry->addGeometryDirectly(poOgrLineString); |
| 720 |
} |
| 721 |
poOgrGeometry = poOgrMultiGeometry; |
| 722 |
break; |
| 723 |
} |
| 724 |
|
| 725 |
default: |
| 726 |
{ |
| 727 |
break; |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
if (poOgrGeometry) |
| 732 |
poOgrGeometry->assignSpatialReference(poOgrSRS); |
| 733 |
|
| 734 |
return poOgrGeometry; |
| 735 |
} |
| 736 |
|
| 737 |
static OGRGeometry *kml2geom_latlonbox_int(const LatLonBoxPtr &poKmlLatLonBox, |
| 738 |
OGRSpatialReference *poOgrSRS) |
| 739 |
{ |
| 740 |
if (!poKmlLatLonBox->has_north() || !poKmlLatLonBox->has_south() || |
| 741 |
!poKmlLatLonBox->has_east() || !poKmlLatLonBox->has_west()) |
| 742 |
{ |
| 743 |
return nullptr; |
| 744 |
} |
| 745 |
const double north = poKmlLatLonBox->get_north(); |
| 746 |
const double south = poKmlLatLonBox->get_south(); |
| 747 |
const double east = poKmlLatLonBox->get_east(); |
| 748 |
const double west = poKmlLatLonBox->get_west(); |
| 749 |
|
| 750 |
OGRLinearRing *poOgrRing = new OGRLinearRing(); |
| 751 |
poOgrRing->addPoint(east, north, 0.0); |
| 752 |
poOgrRing->addPoint(east, south, 0.0); |
| 753 |
poOgrRing->addPoint(west, south, 0.0); |
| 754 |
poOgrRing->addPoint(west, north, 0.0); |
| 755 |
poOgrRing->addPoint(east, north, 0.0); |
| 756 |
|
| 757 |
OGRPolygon *poOgrPolygon = new OGRPolygon(); |
| 758 |
poOgrPolygon->addRingDirectly(poOgrRing); |
| 759 |
poOgrPolygon->assignSpatialReference(poOgrSRS); |
| 760 |
|
| 761 |
return poOgrPolygon; |
| 762 |
} |
| 763 |
|
| 764 |
static OGRGeometry * |
| 765 |
kml2geom_latlonquad_int(const GxLatLonQuadPtr &poKmlLatLonQuad, |
| 766 |
OGRSpatialReference *poOgrSRS) |
| 767 |
{ |
| 768 |
if (!poKmlLatLonQuad->has_coordinates()) |
| 769 |
return nullptr; |
| 770 |
|
| 771 |
const CoordinatesPtr &poKmlCoordinates = poKmlLatLonQuad->get_coordinates(); |
| 772 |
|
| 773 |
OGRLinearRing *poOgrLinearRing = new OGRLinearRing(); |
| 774 |
|
| 775 |
size_t nCoords = poKmlCoordinates->get_coordinates_array_size(); |
| 776 |
for (size_t i = 0; i < nCoords; i++) |
| 777 |
{ |
| 778 |
Vec3 oKmlVec = poKmlCoordinates->get_coordinates_array_at(i); |
| 779 |
if (oKmlVec.has_altitude()) |
| 780 |
poOgrLinearRing->addPoint(oKmlVec.get_longitude(), |
| 781 |
oKmlVec.get_latitude(), |
| 782 |
oKmlVec.get_altitude()); |
| 783 |
else |
| 784 |
poOgrLinearRing->addPoint(oKmlVec.get_longitude(), |
| 785 |
oKmlVec.get_latitude()); |
| 786 |
} |
| 787 |
poOgrLinearRing->closeRings(); |
| 788 |
|
| 789 |
OGRPolygon *poOgrPolygon = new OGRPolygon(); |
| 790 |
poOgrPolygon->addRingDirectly(poOgrLinearRing); |
| 791 |
poOgrPolygon->assignSpatialReference(poOgrSRS); |
| 792 |
|
| 793 |
return poOgrPolygon; |
| 794 |
} |
| 795 |
|
| 796 |
/****************************************************************************** |
| 797 |
Main function to read a kml geometry and translate to ogr. |
| 798 |
|
| 799 |
Args: |
| 800 |
poKmlGeometry pointer to the kml geometry to translate |
| 801 |
poOgrSRS pointer to the spatial ref to set on the geometry |
| 802 |
|
| 803 |
Returns: |
| 804 |
pointer to the new ogr geometry object |
| 805 |
|
| 806 |
******************************************************************************/ |
| 807 |
|
| 808 |
OGRGeometry *kml2geom(GeometryPtr poKmlGeometry, OGRSpatialReference *poOgrSRS) |
| 809 |
{ |
| 810 |
/***** Get the geometry *****/ |
| 811 |
OGRGeometry *poOgrGeometry = |
| 812 |
kml2geom_rec(std::move(poKmlGeometry), poOgrSRS); |
| 813 |
|
| 814 |
/***** Split the geometry at the dateline? *****/ |
| 815 |
const char *pszWrap = CPLGetConfigOption("LIBKML_WRAPDATELINE", "no"); |
| 816 |
if (!CPLTestBool(pszWrap)) |
| 817 |
return poOgrGeometry; |
| 818 |
|
| 819 |
char **papszTransformOptions = CSLAddString(nullptr, "WRAPDATELINE=YES"); |
| 820 |
|
| 821 |
/***** Transform *****/ |
| 822 |
OGRGeometry *poOgrDstGeometry = OGRGeometryFactory::transformWithOptions( |
| 823 |
poOgrGeometry, nullptr, papszTransformOptions); |
| 824 |
|
| 825 |
/***** Replace the original geom *****/ |
| 826 |
if (poOgrDstGeometry) |
| 827 |
{ |
| 828 |
delete poOgrGeometry; |
| 829 |
poOgrGeometry = poOgrDstGeometry; |
| 830 |
} |
| 831 |
|
| 832 |
CSLDestroy(papszTransformOptions); |
| 833 |
|
| 834 |
return poOgrGeometry; |
| 835 |
} |
| 836 |
|
| 837 |
OGRGeometry *kml2geom_latlonbox(LatLonBoxPtr poKmlLatLonBox, |
| 838 |
OGRSpatialReference *poOgrSRS) |
| 839 |
{ |
| 840 |
/***** Get the geometry *****/ |
| 841 |
OGRGeometry *poOgrGeometry = |
| 842 |
kml2geom_latlonbox_int(poKmlLatLonBox, poOgrSRS); |
| 843 |
|
| 844 |
/***** Split the geometry at the dateline? *****/ |
| 845 |
const char *pszWrap = CPLGetConfigOption("LIBKML_WRAPDATELINE", "no"); |
| 846 |
|
| 847 |
if (!CPLTestBool(pszWrap)) |
| 848 |
return poOgrGeometry; |
| 849 |
|
| 850 |
char **papszTransformOptions = CSLAddString(nullptr, "WRAPDATELINE=YES"); |
| 851 |
|
| 852 |
/***** Transform *****/ |
| 853 |
OGRGeometry *poOgrDstGeometry = OGRGeometryFactory::transformWithOptions( |
| 854 |
poOgrGeometry, nullptr, papszTransformOptions); |
| 855 |
|
| 856 |
/***** Replace the original geom *****/ |
| 857 |
if (poOgrDstGeometry) |
| 858 |
{ |
| 859 |
delete poOgrGeometry; |
| 860 |
poOgrGeometry = poOgrDstGeometry; |
| 861 |
} |
| 862 |
|
| 863 |
CSLDestroy(papszTransformOptions); |
| 864 |
|
| 865 |
return poOgrGeometry; |
| 866 |
} |
| 867 |
|
| 868 |
OGRGeometry *kml2geom_latlonquad(GxLatLonQuadPtr poKmlLatLonQuad, |
| 869 |
OGRSpatialReference *poOgrSRS) |
| 870 |
{ |
| 871 |
/***** Get the geometry *****/ |
| 872 |
OGRGeometry *poOgrGeometry = |
| 873 |
kml2geom_latlonquad_int(poKmlLatLonQuad, poOgrSRS); |
| 874 |
|
| 875 |
/***** Split the geometry at the dateline? *****/ |
| 876 |
const char *pszWrap = CPLGetConfigOption("LIBKML_WRAPDATELINE", "no"); |
| 877 |
if (!CPLTestBool(pszWrap)) |
| 878 |
return poOgrGeometry; |
| 879 |
|
| 880 |
char **papszTransformOptions = CSLAddString(nullptr, "WRAPDATELINE=YES"); |
| 881 |
|
| 882 |
/***** Transform *****/ |
| 883 |
OGRGeometry *poOgrDstGeometry = OGRGeometryFactory::transformWithOptions( |
| 884 |
poOgrGeometry, nullptr, papszTransformOptions); |
| 885 |
|
| 886 |
/***** Replace the original geom *****/ |
| 887 |
if (poOgrDstGeometry) |
| 888 |
{ |
| 889 |
delete poOgrGeometry; |
| 890 |
poOgrGeometry = poOgrDstGeometry; |
| 891 |
} |
| 892 |
|
| 893 |
CSLDestroy(papszTransformOptions); |
| 894 |
|
| 895 |
return poOgrGeometry; |
| 896 |
} |