Debian Codegraph @

ogr/ogrsf_frmts/libkml/ogrlibkmlfield.cpp

raw
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 #include "ogrlibkmlfield.h"
16
17 #include <set>
18 #include <string>
19
20 #include "ogr_feature.h"
21 #include "ogr_p.h"
22 #include "ogrsf_frmts.h"
23
24 using kmldom::CameraPtr;
25 using kmldom::DataPtr;
26 using kmldom::ExtendedDataPtr;
27 using kmldom::FeaturePtr;
28 using kmldom::GeometryPtr;
29 using kmldom::GroundOverlayPtr;
30 using kmldom::GxMultiTrackPtr;
31 using kmldom::GxTrackPtr;
32 using kmldom::IconPtr;
33 using kmldom::KmlFactory;
34 using kmldom::LineStringPtr;
35 using kmldom::MultiGeometryPtr;
36 using kmldom::PlacemarkPtr;
37 using kmldom::PointPtr;
38 using kmldom::PolygonPtr;
39 using kmldom::SchemaDataPtr;
40 using kmldom::SchemaPtr;
41 using kmldom::SimpleDataPtr;
42 using kmldom::SimpleFieldPtr;
43 using kmldom::SnippetPtr;
44 using kmldom::TimePrimitivePtr;
45 using kmldom::TimeSpanPtr;
46 using kmldom::TimeStampPtr;
47
48 static void ogr2altitudemode_rec(const GeometryPtr &poKmlGeometry,
49 int iAltitudeMode, int isGX)
50 {
51 switch (poKmlGeometry->Type())
52 {
53 case kmldom::Type_Point:
54 {
55 PointPtr poKmlPoint = AsPoint(poKmlGeometry);
56
57 if (!isGX)
58 poKmlPoint->set_altitudemode(iAltitudeMode);
59 else
60 poKmlPoint->set_gx_altitudemode(iAltitudeMode);
61
62 break;
63 }
64 case kmldom::Type_LineString:
65 {
66 LineStringPtr poKmlLineString = AsLineString(poKmlGeometry);
67
68 if (!isGX)
69 poKmlLineString->set_altitudemode(iAltitudeMode);
70 else
71 poKmlLineString->set_gx_altitudemode(iAltitudeMode);
72
73 break;
74 }
75 case kmldom::Type_LinearRing:
76 {
77 break;
78 }
79 case kmldom::Type_Polygon:
80 {
81 PolygonPtr poKmlPolygon = AsPolygon(poKmlGeometry);
82
83 if (!isGX)
84 poKmlPolygon->set_altitudemode(iAltitudeMode);
85 else
86 poKmlPolygon->set_gx_altitudemode(iAltitudeMode);
87
88 break;
89 }
90 case kmldom::Type_MultiGeometry:
91 {
92 MultiGeometryPtr poKmlMultiGeometry =
93 AsMultiGeometry(poKmlGeometry);
94
95 const size_t nGeom = poKmlMultiGeometry->get_geometry_array_size();
96 for (size_t i = 0; i < nGeom; i++)
97 {
98 ogr2altitudemode_rec(
99 poKmlMultiGeometry->get_geometry_array_at(i), iAltitudeMode,
100 isGX);
101 }
102
103 break;
104 }
105 default:
106 {
107 break;
108 }
109 }
110 }
111
112 static void ogr2extrude_rec(bool bExtrude, const GeometryPtr &poKmlGeometry)
113 {
114 switch (poKmlGeometry->Type())
115 {
116 case kmldom::Type_Point:
117 {
118 PointPtr const poKmlPoint = AsPoint(poKmlGeometry);
119 poKmlPoint->set_extrude(bExtrude);
120 break;
121 }
122 case kmldom::Type_LineString:
123 {
124 LineStringPtr const poKmlLineString = AsLineString(poKmlGeometry);
125 poKmlLineString->set_extrude(bExtrude);
126 break;
127 }
128 case kmldom::Type_LinearRing:
129 {
130 break;
131 }
132 case kmldom::Type_Polygon:
133 {
134 PolygonPtr const poKmlPolygon = AsPolygon(poKmlGeometry);
135 poKmlPolygon->set_extrude(bExtrude);
136 break;
137 }
138 case kmldom::Type_MultiGeometry:
139 {
140 MultiGeometryPtr const poKmlMultiGeometry =
141 AsMultiGeometry(poKmlGeometry);
142
143 const size_t nGeom = poKmlMultiGeometry->get_geometry_array_size();
144 for (size_t i = 0; i < nGeom; i++)
145 {
146 ogr2extrude_rec(bExtrude,
147 poKmlMultiGeometry->get_geometry_array_at(i));
148 }
149 break;
150 }
151 default:
152 {
153 break;
154 }
155 }
156 }
157
158 static void ogr2tessellate_rec(bool bTessellate,
159 const GeometryPtr &poKmlGeometry)
160 {
161 switch (poKmlGeometry->Type())
162 {
163 case kmldom::Type_Point:
164 {
165 break;
166 }
167 case kmldom::Type_LineString:
168 {
169 LineStringPtr const poKmlLineString = AsLineString(poKmlGeometry);
170 poKmlLineString->set_tessellate(bTessellate);
171 break;
172 }
173 case kmldom::Type_LinearRing:
174 {
175 break;
176 }
177 case kmldom::Type_Polygon:
178 {
179 PolygonPtr const poKmlPolygon = AsPolygon(poKmlGeometry);
180
181 poKmlPolygon->set_tessellate(bTessellate);
182 break;
183 }
184 case kmldom::Type_MultiGeometry:
185 {
186 MultiGeometryPtr const poKmlMultiGeometry =
187 AsMultiGeometry(poKmlGeometry);
188
189 const size_t nGeom = poKmlMultiGeometry->get_geometry_array_size();
190 for (size_t i = 0; i < nGeom; i++)
191 {
192 ogr2tessellate_rec(
193 bTessellate, poKmlMultiGeometry->get_geometry_array_at(i));
194 }
195
196 break;
197 }
198 default:
199 {
200 break;
201 }
202 }
203 }
204
205 /************************************************************************/
206 /* OGRLIBKMLSanitizeUTF8String() */
207 /************************************************************************/
208
209 static char *OGRLIBKMLSanitizeUTF8String(const char *pszString)
210 {
211 if (!CPLIsUTF8(pszString, -1) &&
212 CPLTestBool(CPLGetConfigOption("OGR_FORCE_ASCII", "YES")))
213 {
214 static bool bFirstTime = true;
215 if (bFirstTime)
216 {
217 bFirstTime = false;
218 CPLError(
219 CE_Warning, CPLE_AppDefined,
220 "%s is not a valid UTF-8 string. Forcing it to ASCII. "
221 "If you still want the original string and change the XML file "
222 "encoding afterwards, you can define OGR_FORCE_ASCII=NO as "
223 " configuration option. This warning won't be issued anymore",
224 pszString);
225 }
226 else
227 {
228 CPLDebug("OGR",
229 "%s is not a valid UTF-8 string. Forcing it to ASCII",
230 pszString);
231 }
232 return CPLForceToASCII(pszString, -1, '?');
233 }
234
235 return CPLStrdup(pszString);
236 }
237
238 /******************************************************************************
239 Function to output ogr fields in kml.
240
241 Args:
242 poOgrFeat pointer to the feature the field is in
243 poOgrLayer pointer to the layer the feature is in
244 poKmlFactory pointer to the libkml dom factory
245 poKmlPlacemark pointer to the placemark to add to
246
247 Returns:
248 nothing
249
250 env vars:
251 LIBKML_TIMESTAMP_FIELD default: OFTDate or OFTDateTime named timestamp
252 LIBKML_TIMESPAN_BEGIN_FIELD default: OFTDate or OFTDateTime named begin
253 LIBKML_TIMESPAN_END_FIELD default: OFTDate or OFTDateTime named end
254 LIBKML_DESCRIPTION_FIELD default: none
255 LIBKML_NAME_FIELD default: OFTString field named name
256
257 ******************************************************************************/
258
259 void field2kml(OGRFeature *poOgrFeat, OGRLIBKMLLayer *poOgrLayer,
260 KmlFactory *poKmlFactory, FeaturePtr poKmlFeature,
261 int bUseSimpleFieldIn, const fieldconfig &oFC)
262 {
263 const bool bUseSimpleField = CPL_TO_BOOL(bUseSimpleFieldIn);
264 SchemaDataPtr poKmlSchemaData = nullptr;
265 if (bUseSimpleField)
266 {
267 poKmlSchemaData = poKmlFactory->CreateSchemaData();
268 SchemaPtr poKmlSchema = poOgrLayer->GetKmlSchema();
269
270 /***** set the url to the schema *****/
271 if (poKmlSchema && poKmlSchema->has_id())
272 {
273 std::string oKmlSchemaID = poKmlSchema->get_id();
274 std::string oKmlSchemaURL = "#";
275 oKmlSchemaURL.append(oKmlSchemaID);
276
277 poKmlSchemaData->set_schemaurl(oKmlSchemaURL);
278 }
279 }
280
281 TimeSpanPtr poKmlTimeSpan = nullptr;
282
283 const int nFields = poOgrFeat->GetFieldCount();
284 int iSkip1 = -1;
285 int iSkip2 = -1;
286 int iAltitudeMode = kmldom::ALTITUDEMODE_CLAMPTOGROUND;
287 int isGX = false;
288
289 ExtendedDataPtr poKmlExtendedData = nullptr;
290
291 for (int i = 0; i < nFields; i++)
292 {
293 /***** If the field is set to skip, do so *****/
294 if (i == iSkip1 || i == iSkip2)
295 continue;
296
297 /***** If the field isn't set just bail now *****/
298 if (!poOgrFeat->IsFieldSetAndNotNull(i))
299 continue;
300
301 const OGRFieldDefn *poOgrFieldDef = poOgrFeat->GetFieldDefnRef(i);
302 const OGRFieldType type = poOgrFieldDef->GetType();
303 const char *name = poOgrFieldDef->GetNameRef();
304
305 SimpleDataPtr poKmlSimpleData = nullptr;
306 DataPtr poKmlData = nullptr;
307 OGRField sFieldDT;
308
309 // TODO(schwehr): Refactor to get rid of gotos.
310 switch (type)
311 {
312 case OFTString: // String of ASCII chars
313 {
314 char *const pszUTF8String =
315 OGRLIBKMLSanitizeUTF8String(poOgrFeat->GetFieldAsString(i));
316 if (pszUTF8String[0] == '\0')
317 {
318 CPLFree(pszUTF8String);
319 continue;
320 }
321
322 /***** id *****/
323 if (EQUAL(name, oFC.idfield))
324 {
325 poKmlFeature->set_id(pszUTF8String);
326 CPLFree(pszUTF8String);
327 continue;
328 }
329 /***** name *****/
330 if (EQUAL(name, oFC.namefield))
331 {
332 poKmlFeature->set_name(pszUTF8String);
333 CPLFree(pszUTF8String);
334 continue;
335 }
336 /***** description *****/
337 else if (EQUAL(name, oFC.descfield))
338 {
339 poKmlFeature->set_description(pszUTF8String);
340 CPLFree(pszUTF8String);
341 continue;
342 }
343 /***** altitudemode *****/
344 else if (EQUAL(name, oFC.altitudeModefield))
345 {
346 const char *pszAltitudeMode = pszUTF8String;
347
348 iAltitudeMode =
349 kmlAltitudeModeFromString(pszAltitudeMode, isGX);
350
351 if (poKmlFeature->IsA(kmldom::Type_Placemark))
352 {
353 PlacemarkPtr const poKmlPlacemark =
354 AsPlacemark(poKmlFeature);
355 if (poKmlPlacemark->has_geometry())
356 {
357 GeometryPtr poKmlGeometry =
358 poKmlPlacemark->get_geometry();
359
360 ogr2altitudemode_rec(poKmlGeometry, iAltitudeMode,
361 isGX);
362 }
363 }
364
365 CPLFree(pszUTF8String);
366
367 continue;
368 }
369 /***** timestamp *****/
370 else if (EQUAL(name, oFC.tsfield))
371 {
372 TimeStampPtr poKmlTimeStamp =
373 poKmlFactory->CreateTimeStamp();
374 poKmlTimeStamp->set_when(pszUTF8String);
375 poKmlFeature->set_timeprimitive(poKmlTimeStamp);
376
377 CPLFree(pszUTF8String);
378
379 continue;
380 }
381 /***** begin *****/
382 if (EQUAL(name, oFC.beginfield))
383 {
384 if (!poKmlTimeSpan)
385 {
386 poKmlTimeSpan = poKmlFactory->CreateTimeSpan();
387 poKmlFeature->set_timeprimitive(poKmlTimeSpan);
388 }
389
390 poKmlTimeSpan->set_begin(pszUTF8String);
391
392 CPLFree(pszUTF8String);
393
394 continue;
395 }
396 /***** end *****/
397 else if (EQUAL(name, oFC.endfield))
398 {
399 if (!poKmlTimeSpan)
400 {
401 poKmlTimeSpan = poKmlFactory->CreateTimeSpan();
402 poKmlFeature->set_timeprimitive(poKmlTimeSpan);
403 }
404
405 poKmlTimeSpan->set_end(pszUTF8String);
406
407 CPLFree(pszUTF8String);
408
409 continue;
410 }
411 /***** snippet *****/
412 else if (EQUAL(name, oFC.snippetfield))
413 {
414 SnippetPtr snippet = poKmlFactory->CreateSnippet();
415 snippet->set_text(pszUTF8String);
416 poKmlFeature->set_snippet(snippet);
417
418 CPLFree(pszUTF8String);
419
420 continue;
421 }
422 /***** other special fields *****/
423 else if (EQUAL(name, oFC.iconfield) ||
424 EQUAL(name, oFC.modelfield) ||
425 EQUAL(name, oFC.networklinkfield) ||
426 EQUAL(name, oFC.networklink_refreshMode_field) ||
427 EQUAL(name, oFC.networklink_viewRefreshMode_field) ||
428 EQUAL(name, oFC.networklink_viewFormat_field) ||
429 EQUAL(name, oFC.networklink_httpQuery_field) ||
430 EQUAL(name, oFC.camera_altitudemode_field) ||
431 EQUAL(name, oFC.photooverlayfield) ||
432 EQUAL(name, oFC.photooverlay_shape_field) ||
433 EQUAL(name, oFC.imagepyramid_gridorigin_field))
434 {
435 CPLFree(pszUTF8String);
436
437 continue;
438 }
439
440 /***** other *****/
441
442 if (bUseSimpleField)
443 {
444 poKmlSimpleData = poKmlFactory->CreateSimpleData();
445 poKmlSimpleData->set_name(name);
446 poKmlSimpleData->set_text(pszUTF8String);
447 }
448 else
449 {
450 poKmlData = poKmlFactory->CreateData();
451 poKmlData->set_name(name);
452 poKmlData->set_value(pszUTF8String);
453 }
454
455 CPLFree(pszUTF8String);
456
457 break;
458 }
459
460 // This code checks if there's a OFTTime field with the same name
461 // that could be used to compose a DateTime. Not sure this is really
462 // supported in OGR data model to have 2 fields with same name.
463 case OFTDate: // Date
464 {
465 memcpy(&sFieldDT, poOgrFeat->GetRawFieldRef(i),
466 sizeof(OGRField));
467
468 for (int iTimeField = i + 1; iTimeField < nFields; iTimeField++)
469 {
470 if (iTimeField == iSkip1 || iTimeField == iSkip2)
471 continue;
472
473 const OGRFieldDefn *poOgrFieldDef2 =
474 poOgrFeat->GetFieldDefnRef(i);
475 const OGRFieldType type2 = poOgrFieldDef2->GetType();
476 const char *name2 = poOgrFieldDef2->GetNameRef();
477
478 if (EQUAL(name2, name) && type2 == OFTTime &&
479 (EQUAL(name, oFC.tsfield) ||
480 EQUAL(name, oFC.beginfield) ||
481 EQUAL(name, oFC.endfield)))
482 {
483 const OGRField *const psField2 =
484 poOgrFeat->GetRawFieldRef(iTimeField);
485 sFieldDT.Date.Hour = psField2->Date.Hour;
486 sFieldDT.Date.Minute = psField2->Date.Minute;
487 sFieldDT.Date.Second = psField2->Date.Second;
488 sFieldDT.Date.TZFlag = psField2->Date.TZFlag;
489
490 if (0 > iSkip1)
491 iSkip1 = iTimeField;
492 else
493 iSkip2 = iTimeField;
494 }
495 }
496
497 goto Do_DateTime;
498 }
499
500 // This code checks if there's a OFTTime field with the same name
501 // that could be used to compose a DateTime. Not sure this is really
502 // supported in OGR data model to have 2 fields with same name.
503 case OFTTime: // Time
504 {
505 memcpy(&sFieldDT, poOgrFeat->GetRawFieldRef(i),
506 sizeof(OGRField));
507
508 for (int iTimeField = i + 1; iTimeField < nFields; iTimeField++)
509 {
510 if (iTimeField == iSkip1 || iTimeField == iSkip2)
511 continue;
512
513 const OGRFieldDefn *const poOgrFieldDef2 =
514 poOgrFeat->GetFieldDefnRef(i);
515 OGRFieldType type2 = poOgrFieldDef2->GetType();
516 const char *const name2 = poOgrFieldDef2->GetNameRef();
517
518 if (EQUAL(name2, name) && type2 == OFTDate &&
519 (EQUAL(name, oFC.tsfield) ||
520 EQUAL(name, oFC.beginfield) ||
521 EQUAL(name, oFC.endfield)))
522 {
523 const OGRField *psField2 =
524 poOgrFeat->GetRawFieldRef(iTimeField);
525 sFieldDT.Date.Year = psField2->Date.Year;
526 sFieldDT.Date.Month = psField2->Date.Month;
527 sFieldDT.Date.Day = psField2->Date.Day;
528
529 if (0 > iSkip1)
530 iSkip1 = iTimeField;
531 else
532 iSkip2 = iTimeField;
533 }
534 }
535
536 goto Do_DateTime;
537 }
538
539 case OFTDateTime: // Date and Time
540 {
541 memcpy(&sFieldDT, poOgrFeat->GetRawFieldRef(i),
542 sizeof(OGRField));
543
544 Do_DateTime:
545 /***** timestamp *****/
546 if (EQUAL(name, oFC.tsfield))
547 {
548 char *const timebuf = OGRGetXMLDateTime(&sFieldDT);
549
550 TimeStampPtr poKmlTimeStamp =
551 poKmlFactory->CreateTimeStamp();
552 poKmlTimeStamp->set_when(timebuf);
553 poKmlFeature->set_timeprimitive(poKmlTimeStamp);
554 CPLFree(timebuf);
555
556 continue;
557 }
558
559 /***** begin *****/
560 if (EQUAL(name, oFC.beginfield))
561 {
562 char *const timebuf = OGRGetXMLDateTime(&sFieldDT);
563
564 if (!poKmlTimeSpan)
565 {
566 poKmlTimeSpan = poKmlFactory->CreateTimeSpan();
567 poKmlFeature->set_timeprimitive(poKmlTimeSpan);
568 }
569
570 poKmlTimeSpan->set_begin(timebuf);
571 CPLFree(timebuf);
572
573 continue;
574 }
575
576 /***** end *****/
577 else if (EQUAL(name, oFC.endfield))
578 {
579 char *const timebuf = OGRGetXMLDateTime(&sFieldDT);
580
581 if (!poKmlTimeSpan)
582 {
583 poKmlTimeSpan = poKmlFactory->CreateTimeSpan();
584 poKmlFeature->set_timeprimitive(poKmlTimeSpan);
585 }
586
587 poKmlTimeSpan->set_end(timebuf);
588 CPLFree(timebuf);
589
590 continue;
591 }
592
593 /***** other *****/
594 const char *pszVal =
595 type == OFTDateTime
596 ? poOgrFeat->GetFieldAsISO8601DateTime(i, nullptr)
597 : poOgrFeat->GetFieldAsString(i);
598 if (bUseSimpleField)
599 {
600 poKmlSimpleData = poKmlFactory->CreateSimpleData();
601 poKmlSimpleData->set_name(name);
602 poKmlSimpleData->set_text(pszVal);
603 }
604 else
605 {
606 poKmlData = poKmlFactory->CreateData();
607 poKmlData->set_name(name);
608 poKmlData->set_value(pszVal);
609 }
610
611 break;
612 }
613
614 case OFTInteger: // Simple 32bit integer
615 {
616 /***** extrude *****/
617 if (EQUAL(name, oFC.extrudefield))
618 {
619 if (poKmlFeature->IsA(kmldom::Type_Placemark))
620 {
621 PlacemarkPtr poKmlPlacemark = AsPlacemark(poKmlFeature);
622 if (poKmlPlacemark->has_geometry() &&
623 -1 < poOgrFeat->GetFieldAsInteger(i))
624 {
625 const int iExtrude =
626 poOgrFeat->GetFieldAsInteger(i);
627 if (iExtrude && !isGX &&
628 iAltitudeMode ==
629 kmldom::ALTITUDEMODE_CLAMPTOGROUND &&
630 CPLTestBool(CPLGetConfigOption(
631 "LIBKML_STRICT_COMPLIANCE", "TRUE")))
632 {
633 CPLError(CE_Warning, CPLE_NotSupported,
634 "altitudeMode=clampToGround "
635 "unsupported with "
636 "extrude=1");
637 }
638 else
639 {
640 GeometryPtr poKmlGeometry =
641 poKmlPlacemark->get_geometry();
642 ogr2extrude_rec(CPL_TO_BOOL(iExtrude),
643 poKmlGeometry);
644 }
645 }
646 }
647 continue;
648 }
649
650 /***** tessellate *****/
651 if (EQUAL(name, oFC.tessellatefield))
652 {
653 if (poKmlFeature->IsA(kmldom::Type_Placemark))
654 {
655 PlacemarkPtr poKmlPlacemark = AsPlacemark(poKmlFeature);
656 if (poKmlPlacemark->has_geometry() &&
657 -1 < poOgrFeat->GetFieldAsInteger(i))
658 {
659 const int iTessellate =
660 poOgrFeat->GetFieldAsInteger(i);
661 if (iTessellate &&
662 !(!isGX &&
663 static_cast<kmldom::AltitudeModeEnum>(
664 iAltitudeMode) ==
665 kmldom::ALTITUDEMODE_CLAMPTOGROUND) &&
666 !(isGX &&
667 static_cast<kmldom::GxAltitudeModeEnum>(
668 iAltitudeMode) ==
669 kmldom::
670 GX_ALTITUDEMODE_CLAMPTOSEAFLOOR) &&
671 CPLTestBool(CPLGetConfigOption(
672 "LIBKML_STRICT_COMPLIANCE", "TRUE")))
673 {
674 CPLError(CE_Warning, CPLE_NotSupported,
675 "altitudeMode!=clampToGround && "
676 "altitudeMode!=clampToSeaFloor "
677 "unsupported with tessellate=1");
678 }
679 else
680 {
681 GeometryPtr poKmlGeometry =
682 poKmlPlacemark->get_geometry();
683 ogr2tessellate_rec(CPL_TO_BOOL(iTessellate),
684 poKmlGeometry);
685 if (!isGX &&
686 iAltitudeMode ==
687 kmldom::ALTITUDEMODE_CLAMPTOGROUND)
688 ogr2altitudemode_rec(poKmlGeometry,
689 iAltitudeMode, isGX);
690 }
691 }
692 }
693
694 continue;
695 }
696
697 /***** visibility *****/
698 if (EQUAL(name, oFC.visibilityfield))
699 {
700 if (-1 < poOgrFeat->GetFieldAsInteger(i))
701 poKmlFeature->set_visibility(
702 CPL_TO_BOOL(poOgrFeat->GetFieldAsInteger(i)));
703
704 continue;
705 }
706 /***** other special fields *****/
707 else if (EQUAL(name, oFC.drawOrderfield) ||
708 EQUAL(name, oFC.networklink_refreshvisibility_field) ||
709 EQUAL(name, oFC.networklink_flytoview_field) ||
710 EQUAL(name, oFC.networklink_refreshInterval_field) ||
711 EQUAL(name, oFC.networklink_viewRefreshMode_field) ||
712 EQUAL(name, oFC.networklink_viewRefreshTime_field) ||
713 EQUAL(name, oFC.imagepyramid_tilesize_field) ||
714 EQUAL(name, oFC.imagepyramid_maxwidth_field) ||
715 EQUAL(name, oFC.imagepyramid_maxheight_field))
716 {
717 continue;
718 }
719
720 /***** other *****/
721 const char *value =
722 poOgrFieldDef->GetSubType() == OFSTBoolean
723 ? (poOgrFeat->GetFieldAsInteger(i) ? "true" : "false")
724 : poOgrFeat->GetFieldAsString(i);
725 if (bUseSimpleField)
726 {
727 poKmlSimpleData = poKmlFactory->CreateSimpleData();
728 poKmlSimpleData->set_name(name);
729 poKmlSimpleData->set_text(value);
730 }
731 else
732 {
733 poKmlData = poKmlFactory->CreateData();
734 poKmlData->set_name(name);
735 poKmlData->set_value(value);
736 }
737
738 break;
739 }
740
741 case OFTReal: // Double Precision floating point
742 {
743 if (EQUAL(name, oFC.headingfield) ||
744 EQUAL(name, oFC.tiltfield) || EQUAL(name, oFC.rollfield) ||
745 EQUAL(name, oFC.scalexfield) ||
746 EQUAL(name, oFC.scaleyfield) ||
747 EQUAL(name, oFC.scalezfield) ||
748 EQUAL(name, oFC.networklink_refreshInterval_field) ||
749 EQUAL(name, oFC.networklink_viewRefreshMode_field) ||
750 EQUAL(name, oFC.networklink_viewRefreshTime_field) ||
751 EQUAL(name, oFC.networklink_viewBoundScale_field) ||
752 EQUAL(name, oFC.camera_longitude_field) ||
753 EQUAL(name, oFC.camera_latitude_field) ||
754 EQUAL(name, oFC.camera_altitude_field) ||
755 EQUAL(name, oFC.leftfovfield) ||
756 EQUAL(name, oFC.rightfovfield) ||
757 EQUAL(name, oFC.bottomfovfield) ||
758 EQUAL(name, oFC.topfovfield) ||
759 EQUAL(name, oFC.nearfield) ||
760 EQUAL(name, oFC.camera_altitude_field))
761 {
762 continue;
763 }
764
765 char *pszStr = CPLStrdup(poOgrFeat->GetFieldAsString(i));
766
767 if (bUseSimpleField)
768 {
769 poKmlSimpleData = poKmlFactory->CreateSimpleData();
770 poKmlSimpleData->set_name(name);
771 poKmlSimpleData->set_text(pszStr);
772 }
773 else
774 {
775 poKmlData = poKmlFactory->CreateData();
776 poKmlData->set_name(name);
777 poKmlData->set_value(pszStr);
778 }
779
780 CPLFree(pszStr);
781
782 break;
783 }
784
785 case OFTStringList: // Array of strings
786 case OFTIntegerList: // List of 32bit integers
787 case OFTRealList: // List of doubles
788 case OFTBinary: // Raw Binary data
789 case OFTWideStringList: // deprecated
790 default:
791
792 /***** other *****/
793
794 if (bUseSimpleField)
795 {
796 poKmlSimpleData = poKmlFactory->CreateSimpleData();
797 poKmlSimpleData->set_name(name);
798 poKmlSimpleData->set_text(poOgrFeat->GetFieldAsString(i));
799 }
800 else
801 {
802 poKmlData = poKmlFactory->CreateData();
803 poKmlData->set_name(name);
804 poKmlData->set_value(poOgrFeat->GetFieldAsString(i));
805 }
806
807 break;
808 }
809
810 if (poKmlSimpleData)
811 {
812 poKmlSchemaData->add_simpledata(poKmlSimpleData);
813 }
814 else if (poKmlData)
815 {
816 if (!poKmlExtendedData)
817 poKmlExtendedData = poKmlFactory->CreateExtendedData();
818 #if defined(__GNUC__)
819 #pragma GCC diagnostic push
820 #pragma GCC diagnostic ignored "-Wnull-dereference"
821 #endif
822 poKmlExtendedData->add_data(poKmlData);
823 #if defined(__GNUC__)
824 #pragma GCC diagnostic pop
825 #endif
826 }
827 }
828
829 // Do not add it to the placemark unless there is data.
830 if (bUseSimpleField && poKmlSchemaData->get_simpledata_array_size() > 0)
831 {
832 poKmlExtendedData = poKmlFactory->CreateExtendedData();
833 poKmlExtendedData->add_schemadata(poKmlSchemaData);
834 }
835 if (poKmlExtendedData)
836 {
837 poKmlFeature->set_extendeddata(poKmlExtendedData);
838 }
839 }
840
841 /******************************************************************************
842 Recursive function to read altitude mode from the geometry.
843 ******************************************************************************/
844
845 static bool kml2altitudemode_rec(GeometryPtr poKmlGeometry, int *pnAltitudeMode,
846 int *pbIsGX)
847 {
848 switch (poKmlGeometry->Type())
849 {
850 case kmldom::Type_Point:
851 {
852 PointPtr poKmlPoint = AsPoint(poKmlGeometry);
853
854 if (poKmlPoint->has_altitudemode())
855 {
856 *pnAltitudeMode = poKmlPoint->get_altitudemode();
857 return true;
858 }
859 else if (poKmlPoint->has_gx_altitudemode())
860 {
861 *pnAltitudeMode = poKmlPoint->get_gx_altitudemode();
862 *pbIsGX = true;
863 return true;
864 }
865
866 break;
867 }
868 case kmldom::Type_LineString:
869 {
870 LineStringPtr poKmlLineString = AsLineString(poKmlGeometry);
871
872 if (poKmlLineString->has_altitudemode())
873 {
874 *pnAltitudeMode = poKmlLineString->get_altitudemode();
875 return true;
876 }
877 else if (poKmlLineString->has_gx_altitudemode())
878 {
879 *pnAltitudeMode = poKmlLineString->get_gx_altitudemode();
880 *pbIsGX = true;
881 return true;
882 }
883 break;
884 }
885 case kmldom::Type_LinearRing:
886 {
887 break;
888 }
889 case kmldom::Type_Polygon:
890 {
891 PolygonPtr poKmlPolygon = AsPolygon(poKmlGeometry);
892
893 if (poKmlPolygon->has_altitudemode())
894 {
895 *pnAltitudeMode = poKmlPolygon->get_altitudemode();
896 return true;
897 }
898 else if (poKmlPolygon->has_gx_altitudemode())
899 {
900 *pnAltitudeMode = poKmlPolygon->get_gx_altitudemode();
901 *pbIsGX = true;
902 return true;
903 }
904
905 break;
906 }
907 case kmldom::Type_MultiGeometry:
908 {
909 MultiGeometryPtr poKmlMultiGeometry =
910 AsMultiGeometry(poKmlGeometry);
911
912 const size_t nGeom = poKmlMultiGeometry->get_geometry_array_size();
913 for (size_t i = 0; i < nGeom; i++)
914 {
915 if (kml2altitudemode_rec(
916 poKmlMultiGeometry->get_geometry_array_at(i),
917 pnAltitudeMode, pbIsGX))
918 return true;
919 }
920
921 break;
922 }
923 default:
924 {
925 break;
926 }
927 }
928
929 return false;
930 }
931
932 /******************************************************************************
933 Recursive function to read extrude from the geometry.
934 ******************************************************************************/
935
936 static bool kml2extrude_rec(GeometryPtr poKmlGeometry, bool *pbExtrude)
937 {
938 switch (poKmlGeometry->Type())
939 {
940 case kmldom::Type_Point:
941 {
942 PointPtr poKmlPoint = AsPoint(poKmlGeometry);
943
944 if (poKmlPoint->has_extrude())
945 {
946 *pbExtrude = poKmlPoint->get_extrude();
947 return true;
948 }
949
950 break;
951 }
952 case kmldom::Type_LineString:
953 {
954 LineStringPtr poKmlLineString = AsLineString(poKmlGeometry);
955
956 if (poKmlLineString->has_extrude())
957 {
958 *pbExtrude = poKmlLineString->get_extrude();
959 return true;
960 }
961
962 break;
963 }
964 case kmldom::Type_LinearRing:
965 {
966 break;
967 }
968 case kmldom::Type_Polygon:
969 {
970 PolygonPtr poKmlPolygon = AsPolygon(poKmlGeometry);
971
972 if (poKmlPolygon->has_extrude())
973 {
974 *pbExtrude = poKmlPolygon->get_extrude();
975 return true;
976 }
977
978 break;
979 }
980 case kmldom::Type_MultiGeometry:
981 {
982 MultiGeometryPtr poKmlMultiGeometry =
983 AsMultiGeometry(poKmlGeometry);
984
985 const size_t nGeom = poKmlMultiGeometry->get_geometry_array_size();
986 for (size_t i = 0; i < nGeom; i++)
987 {
988 if (kml2extrude_rec(
989 poKmlMultiGeometry->get_geometry_array_at(i),
990 pbExtrude))
991 return true;
992 }
993
994 break;
995 }
996 default:
997 {
998 break;
999 }
1000 }
1001
1002 return false;
1003 }
1004
1005 /******************************************************************************
1006 Recursive function to read tessellate from the geometry.
1007 ******************************************************************************/
1008
1009 static bool kml2tessellate_rec(GeometryPtr poKmlGeometry, int *pnTessellate)
1010 {
1011 switch (poKmlGeometry->Type())
1012 {
1013 case kmldom::Type_Point:
1014 {
1015 break;
1016 }
1017 case kmldom::Type_LineString:
1018 {
1019 LineStringPtr poKmlLineString = AsLineString(poKmlGeometry);
1020
1021 if (poKmlLineString->has_tessellate())
1022 {
1023 *pnTessellate = poKmlLineString->get_tessellate();
1024 return true;
1025 }
1026
1027 break;
1028 }
1029 case kmldom::Type_LinearRing:
1030 {
1031 break;
1032 }
1033 case kmldom::Type_Polygon:
1034 {
1035 PolygonPtr poKmlPolygon = AsPolygon(poKmlGeometry);
1036
1037 if (poKmlPolygon->has_tessellate())
1038 {
1039 *pnTessellate = poKmlPolygon->get_tessellate();
1040 return true;
1041 }
1042
1043 break;
1044 }
1045 case kmldom::Type_MultiGeometry:
1046 {
1047 MultiGeometryPtr poKmlMultiGeometry =
1048 AsMultiGeometry(poKmlGeometry);
1049
1050 const size_t nGeom = poKmlMultiGeometry->get_geometry_array_size();
1051 for (size_t i = 0; i < nGeom; i++)
1052 {
1053 if (kml2tessellate_rec(
1054 poKmlMultiGeometry->get_geometry_array_at(i),
1055 pnTessellate))
1056 return true;
1057 }
1058
1059 break;
1060 }
1061 default:
1062 break;
1063 }
1064
1065 return false;
1066 }
1067
1068 /************************************************************************/
1069 /* ogrkmlSetAltitudeMode() */
1070 /************************************************************************/
1071
1072 static void ogrkmlSetAltitudeMode(OGRFeature *poOgrFeat, int iField,
1073 int nAltitudeMode, bool bIsGX)
1074 {
1075 if (!bIsGX)
1076 {
1077 switch (nAltitudeMode)
1078 {
1079 case kmldom::ALTITUDEMODE_CLAMPTOGROUND:
1080 poOgrFeat->SetField(iField, "clampToGround");
1081 break;
1082
1083 case kmldom::ALTITUDEMODE_RELATIVETOGROUND:
1084 poOgrFeat->SetField(iField, "relativeToGround");
1085 break;
1086
1087 case kmldom::ALTITUDEMODE_ABSOLUTE:
1088 poOgrFeat->SetField(iField, "absolute");
1089 break;
1090 }
1091 }
1092 else
1093 {
1094 switch (nAltitudeMode)
1095 {
1096 case kmldom::GX_ALTITUDEMODE_RELATIVETOSEAFLOOR:
1097 poOgrFeat->SetField(iField, "relativeToSeaFloor");
1098 break;
1099
1100 case kmldom::GX_ALTITUDEMODE_CLAMPTOSEAFLOOR:
1101 poOgrFeat->SetField(iField, "clampToSeaFloor");
1102 break;
1103 }
1104 }
1105 }
1106
1107 /************************************************************************/
1108 /* TrimSpaces() */
1109 /************************************************************************/
1110
1111 static const char *TrimSpaces(CPLString &oText)
1112 {
1113 // SerializePretty() adds a new line before the data
1114 // ands trailing spaces. I believe this is wrong
1115 // as it breaks round-tripping.
1116
1117 // Trim trailing spaces.
1118 while (!oText.empty() && oText.back() == ' ')
1119 oText.pop_back();
1120
1121 // Skip leading newline and spaces.
1122 const char *pszText = oText.c_str();
1123 if (pszText[0] == '\n')
1124 pszText++;
1125 while (pszText[0] == ' ')
1126 pszText++;
1127
1128 return pszText;
1129 }
1130
1131 /************************************************************************/
1132 /* kmldatetime2ogr() */
1133 /************************************************************************/
1134
1135 static void kmldatetime2ogr(OGRFeature *poOgrFeat, const char *pszOGRField,
1136 const std::string &osKmlDateTime)
1137 {
1138 const int iField = poOgrFeat->GetFieldIndex(pszOGRField);
1139
1140 if (iField > -1)
1141 {
1142 OGRField sField;
1143
1144 if (OGRParseXMLDateTime(osKmlDateTime.c_str(), &sField))
1145 poOgrFeat->SetField(iField, &sField);
1146 }
1147 }
1148
1149 /******************************************************************************
1150 function to read kml into ogr fields
1151 ******************************************************************************/
1152
1153 void kml2field(OGRFeature *poOgrFeat, FeaturePtr poKmlFeature,
1154 const fieldconfig &oFC,
1155 const std::map<std::string, int> &mapSimpleFieldNameToOgrFieldIx)
1156 {
1157 /***** id *****/
1158
1159 if (poKmlFeature->has_id())
1160 {
1161 const std::string oKmlId = poKmlFeature->get_id();
1162 int iField = poOgrFeat->GetFieldIndex(oFC.idfield);
1163
1164 if (iField > -1)
1165 poOgrFeat->SetField(iField, oKmlId.c_str());
1166 }
1167 /***** name *****/
1168
1169 if (poKmlFeature->has_name())
1170 {
1171 const std::string oKmlName = poKmlFeature->get_name();
1172 int iField = poOgrFeat->GetFieldIndex(oFC.namefield);
1173
1174 if (iField > -1)
1175 poOgrFeat->SetField(iField, oKmlName.c_str());
1176 }
1177
1178 /***** description *****/
1179
1180 if (poKmlFeature->has_description())
1181 {
1182 const std::string oKmlDesc = poKmlFeature->get_description();
1183 int iField = poOgrFeat->GetFieldIndex(oFC.descfield);
1184
1185 if (iField > -1)
1186 poOgrFeat->SetField(iField, oKmlDesc.c_str());
1187 }
1188
1189 if (poKmlFeature->has_timeprimitive())
1190 {
1191 TimePrimitivePtr poKmlTimePrimitive = poKmlFeature->get_timeprimitive();
1192
1193 /***** timestamp *****/
1194
1195 if (poKmlTimePrimitive->IsA(kmldom::Type_TimeStamp))
1196 {
1197 // Probably a libkml bug: AsTimeStamp should really return not NULL
1198 // on a gx:TimeStamp.
1199 TimeStampPtr poKmlTimeStamp = AsTimeStamp(poKmlTimePrimitive);
1200 if (!poKmlTimeStamp)
1201 poKmlTimeStamp = AsGxTimeStamp(poKmlTimePrimitive);
1202
1203 if (poKmlTimeStamp && poKmlTimeStamp->has_when())
1204 {
1205 const std::string oKmlWhen = poKmlTimeStamp->get_when();
1206 kmldatetime2ogr(poOgrFeat, oFC.tsfield, oKmlWhen);
1207 }
1208 }
1209
1210 /***** timespan *****/
1211
1212 if (poKmlTimePrimitive->IsA(kmldom::Type_TimeSpan))
1213 {
1214 // Probably a libkml bug: AsTimeSpan should really return not NULL
1215 // on a gx:TimeSpan.
1216 TimeSpanPtr poKmlTimeSpan = AsTimeSpan(poKmlTimePrimitive);
1217 if (!poKmlTimeSpan)
1218 poKmlTimeSpan = AsGxTimeSpan(poKmlTimePrimitive);
1219
1220 /***** begin *****/
1221
1222 if (poKmlTimeSpan && poKmlTimeSpan->has_begin())
1223 {
1224 const std::string oKmlWhen = poKmlTimeSpan->get_begin();
1225 kmldatetime2ogr(poOgrFeat, oFC.beginfield, oKmlWhen);
1226 }
1227
1228 /***** end *****/
1229
1230 if (poKmlTimeSpan && poKmlTimeSpan->has_end())
1231 {
1232 const std::string oKmlWhen = poKmlTimeSpan->get_end();
1233 kmldatetime2ogr(poOgrFeat, oFC.endfield, oKmlWhen);
1234 }
1235 }
1236 }
1237
1238 /***** placemark *****/
1239 PlacemarkPtr poKmlPlacemark = AsPlacemark(poKmlFeature);
1240 GroundOverlayPtr poKmlGroundOverlay = AsGroundOverlay(poKmlFeature);
1241 if (poKmlPlacemark && poKmlPlacemark->has_geometry())
1242 {
1243 GeometryPtr poKmlGeometry = poKmlPlacemark->get_geometry();
1244
1245 /***** altitudeMode *****/
1246 int bIsGX = false;
1247 int nAltitudeMode = -1;
1248
1249 int iField = poOgrFeat->GetFieldIndex(oFC.altitudeModefield);
1250
1251 if (iField > -1)
1252 {
1253 if (kml2altitudemode_rec(poKmlGeometry, &nAltitudeMode, &bIsGX))
1254 {
1255 ogrkmlSetAltitudeMode(poOgrFeat, iField, nAltitudeMode,
1256 CPL_TO_BOOL(bIsGX));
1257 }
1258 }
1259
1260 /***** tessellate *****/
1261 int nTessellate = -1;
1262
1263 kml2tessellate_rec(poKmlGeometry, &nTessellate);
1264
1265 iField = poOgrFeat->GetFieldIndex(oFC.tessellatefield);
1266 if (iField > -1)
1267 poOgrFeat->SetField(iField, nTessellate);
1268
1269 /***** extrude *****/
1270
1271 bool bExtrude = false;
1272
1273 kml2extrude_rec(poKmlGeometry, &bExtrude);
1274
1275 iField = poOgrFeat->GetFieldIndex(oFC.extrudefield);
1276 if (iField > -1)
1277 poOgrFeat->SetField(iField, bExtrude ? 1 : 0);
1278
1279 /***** special case for gx:Track ******/
1280 /* we set the first timestamp as begin and the last one as end */
1281 if (poKmlGeometry->Type() == kmldom::Type_GxTrack &&
1282 !poKmlFeature->has_timeprimitive())
1283 {
1284 GxTrackPtr poKmlGxTrack = AsGxTrack(poKmlGeometry);
1285 if (poKmlGxTrack)
1286 {
1287 const size_t nCoords = poKmlGxTrack->get_when_array_size();
1288 if (nCoords > 0)
1289 {
1290 kmldatetime2ogr(poOgrFeat, oFC.beginfield,
1291 poKmlGxTrack->get_when_array_at(0).c_str());
1292 kmldatetime2ogr(
1293 poOgrFeat, oFC.endfield,
1294 poKmlGxTrack->get_when_array_at(nCoords - 1).c_str());
1295 }
1296 }
1297 }
1298
1299 /***** special case for gx:MultiTrack ******/
1300 /* we set the first timestamp as begin and the last one as end */
1301 else if (poKmlGeometry->Type() == kmldom::Type_GxMultiTrack &&
1302 !poKmlFeature->has_timeprimitive())
1303 {
1304 GxMultiTrackPtr poKmlGxMultiTrack = AsGxMultiTrack(poKmlGeometry);
1305 if (poKmlGxMultiTrack)
1306 {
1307 const size_t nGeom =
1308 poKmlGxMultiTrack->get_gx_track_array_size();
1309 if (nGeom >= 1)
1310 {
1311 {
1312 GxTrackPtr poKmlGxTrack =
1313 poKmlGxMultiTrack->get_gx_track_array_at(0);
1314 const size_t nCoords =
1315 poKmlGxTrack->get_when_array_size();
1316 if (nCoords > 0)
1317 {
1318 kmldatetime2ogr(
1319 poOgrFeat, oFC.beginfield,
1320 poKmlGxTrack->get_when_array_at(0).c_str());
1321 }
1322 }
1323
1324 {
1325 GxTrackPtr poKmlGxTrack =
1326 poKmlGxMultiTrack->get_gx_track_array_at(nGeom - 1);
1327 const size_t nCoords =
1328 poKmlGxTrack->get_when_array_size();
1329 if (nCoords > 0)
1330 {
1331 kmldatetime2ogr(
1332 poOgrFeat, oFC.endfield,
1333 poKmlGxTrack->get_when_array_at(nCoords - 1)
1334 .c_str());
1335 }
1336 }
1337 }
1338 }
1339 }
1340 }
1341
1342 /***** camera *****/
1343
1344 else if (poKmlPlacemark && poKmlPlacemark->has_abstractview() &&
1345 poKmlPlacemark->get_abstractview()->IsA(kmldom::Type_Camera))
1346 {
1347 const CameraPtr &camera = AsCamera(poKmlPlacemark->get_abstractview());
1348
1349 if (camera->has_heading())
1350 {
1351 int iField = poOgrFeat->GetFieldIndex(oFC.headingfield);
1352 if (iField > -1)
1353 poOgrFeat->SetField(iField, camera->get_heading());
1354 }
1355
1356 if (camera->has_tilt())
1357 {
1358 int iField = poOgrFeat->GetFieldIndex(oFC.tiltfield);
1359 if (iField > -1)
1360 poOgrFeat->SetField(iField, camera->get_tilt());
1361 }
1362
1363 if (camera->has_roll())
1364 {
1365 int iField = poOgrFeat->GetFieldIndex(oFC.rollfield);
1366 if (iField > -1)
1367 poOgrFeat->SetField(iField, camera->get_roll());
1368 }
1369
1370 int iField = poOgrFeat->GetFieldIndex(oFC.altitudeModefield);
1371
1372 if (iField > -1)
1373 {
1374 if (camera->has_altitudemode())
1375 {
1376 const int nAltitudeMode = camera->get_altitudemode();
1377 ogrkmlSetAltitudeMode(poOgrFeat, iField, nAltitudeMode, false);
1378 }
1379 else if (camera->has_gx_altitudemode())
1380 {
1381 const int nAltitudeMode = camera->get_gx_altitudemode();
1382 ogrkmlSetAltitudeMode(poOgrFeat, iField, nAltitudeMode, true);
1383 }
1384 }
1385 }
1386 /***** ground overlay *****/
1387 else if (poKmlGroundOverlay)
1388 {
1389 /***** icon *****/
1390 int iField = poOgrFeat->GetFieldIndex(oFC.iconfield);
1391 if (iField > -1)
1392 {
1393 if (poKmlGroundOverlay->has_icon())
1394 {
1395 IconPtr icon = poKmlGroundOverlay->get_icon();
1396 if (icon->has_href())
1397 {
1398 poOgrFeat->SetField(iField, icon->get_href().c_str());
1399 }
1400 }
1401 }
1402
1403 /***** drawOrder *****/
1404 iField = poOgrFeat->GetFieldIndex(oFC.drawOrderfield);
1405 if (iField > -1)
1406 {
1407 if (poKmlGroundOverlay->has_draworder())
1408 {
1409 poOgrFeat->SetField(iField,
1410 poKmlGroundOverlay->get_draworder());
1411 }
1412 }
1413
1414 /***** altitudeMode *****/
1415
1416 iField = poOgrFeat->GetFieldIndex(oFC.altitudeModefield);
1417
1418 if (iField > -1)
1419 {
1420 if (poKmlGroundOverlay->has_altitudemode())
1421 {
1422 switch (poKmlGroundOverlay->get_altitudemode())
1423 {
1424 case kmldom::ALTITUDEMODE_CLAMPTOGROUND:
1425 poOgrFeat->SetField(iField, "clampToGround");
1426 break;
1427
1428 case kmldom::ALTITUDEMODE_RELATIVETOGROUND:
1429 poOgrFeat->SetField(iField, "relativeToGround");
1430 break;
1431
1432 case kmldom::ALTITUDEMODE_ABSOLUTE:
1433 poOgrFeat->SetField(iField, "absolute");
1434 break;
1435 }
1436 }
1437 else if (poKmlGroundOverlay->has_gx_altitudemode())
1438 {
1439 switch (poKmlGroundOverlay->get_gx_altitudemode())
1440 {
1441 case kmldom::GX_ALTITUDEMODE_RELATIVETOSEAFLOOR:
1442 poOgrFeat->SetField(iField, "relativeToSeaFloor");
1443 break;
1444
1445 case kmldom::GX_ALTITUDEMODE_CLAMPTOSEAFLOOR:
1446 poOgrFeat->SetField(iField, "clampToSeaFloor");
1447 break;
1448 }
1449 }
1450 }
1451 }
1452
1453 /***** visibility *****/
1454 const int nVisibility =
1455 poKmlFeature->has_visibility() ? poKmlFeature->get_visibility() : -1;
1456
1457 int iField = poOgrFeat->GetFieldIndex(oFC.visibilityfield);
1458
1459 if (iField > -1)
1460 poOgrFeat->SetField(iField, nVisibility);
1461
1462 /***** snippet *****/
1463
1464 if (poKmlFeature->has_snippet())
1465 {
1466 CPLString oText = poKmlFeature->get_snippet()->get_text();
1467
1468 iField = poOgrFeat->GetFieldIndex(oFC.snippetfield);
1469
1470 if (iField > -1)
1471 poOgrFeat->SetField(iField, TrimSpaces(oText));
1472 }
1473
1474 /***** extended schema *****/
1475 ExtendedDataPtr poKmlExtendedData = nullptr;
1476
1477 if (poKmlFeature->has_extendeddata())
1478 {
1479 poKmlExtendedData = poKmlFeature->get_extendeddata();
1480
1481 /***** loop over the schemadata_arrays *****/
1482
1483 const size_t nSchemaData =
1484 poKmlExtendedData->get_schemadata_array_size();
1485
1486 for (size_t iSchemaData = 0; iSchemaData < nSchemaData; iSchemaData++)
1487 {
1488 SchemaDataPtr poKmlSchemaData =
1489 poKmlExtendedData->get_schemadata_array_at(iSchemaData);
1490
1491 /***** loop over the simpledata array *****/
1492
1493 const size_t nSimpleData =
1494 poKmlSchemaData->get_simpledata_array_size();
1495
1496 for (size_t iSimpleData = 0; iSimpleData < nSimpleData;
1497 iSimpleData++)
1498 {
1499 SimpleDataPtr poKmlSimpleData =
1500 poKmlSchemaData->get_simpledata_array_at(iSimpleData);
1501
1502 /***** find the field index *****/
1503
1504 iField = -1;
1505
1506 if (poKmlSimpleData->has_name())
1507 {
1508 const string oName = poKmlSimpleData->get_name();
1509 const auto oIter =
1510 mapSimpleFieldNameToOgrFieldIx.find(oName);
1511 if (oIter != mapSimpleFieldNameToOgrFieldIx.end())
1512 iField = oIter->second;
1513 }
1514
1515 /***** if it has trxt set the field *****/
1516
1517 if (iField > -1 && poKmlSimpleData->has_text())
1518 {
1519 CPLString oText = poKmlSimpleData->get_text();
1520
1521 poOgrFeat->SetField(iField, TrimSpaces(oText));
1522 }
1523 }
1524 }
1525
1526 if (nSchemaData == 0 && poKmlExtendedData->get_data_array_size() > 0)
1527 {
1528 const bool bLaunderFieldNames = CPLTestBool(
1529 CPLGetConfigOption("LIBKML_LAUNDER_FIELD_NAMES", "YES"));
1530 const size_t nDataArraySize =
1531 poKmlExtendedData->get_data_array_size();
1532 for (size_t i = 0; i < nDataArraySize; i++)
1533 {
1534 const DataPtr &data = poKmlExtendedData->get_data_array_at(i);
1535 if (data->has_name() && data->has_value())
1536 {
1537 CPLString osName = std::string(data->get_name());
1538 if (bLaunderFieldNames)
1539 osName = OGRLIBKMLLayer::LaunderFieldNames(osName);
1540 iField = poOgrFeat->GetFieldIndex(osName);
1541 if (iField >= 0)
1542 {
1543 poOgrFeat->SetField(iField, data->get_value().c_str());
1544 }
1545 }
1546 }
1547 }
1548 }
1549 }
1550
1551 /******************************************************************************
1552 function create a simplefield from a FieldDefn
1553 ******************************************************************************/
1554
1555 SimpleFieldPtr FieldDef2kml(const OGRFieldDefn *poOgrFieldDef,
1556 KmlFactory *poKmlFactory, bool bApproxOK,
1557 const fieldconfig &oFC)
1558 {
1559 const char *pszFieldName = poOgrFieldDef->GetNameRef();
1560
1561 if (EQUAL(pszFieldName, oFC.idfield) ||
1562 EQUAL(pszFieldName, oFC.namefield) ||
1563 EQUAL(pszFieldName, oFC.descfield) ||
1564 EQUAL(pszFieldName, oFC.tsfield) ||
1565 EQUAL(pszFieldName, oFC.beginfield) ||
1566 EQUAL(pszFieldName, oFC.endfield) ||
1567 EQUAL(pszFieldName, oFC.altitudeModefield) ||
1568 EQUAL(pszFieldName, oFC.tessellatefield) ||
1569 EQUAL(pszFieldName, oFC.extrudefield) ||
1570 EQUAL(pszFieldName, oFC.visibilityfield) ||
1571 EQUAL(pszFieldName, oFC.drawOrderfield) ||
1572 EQUAL(pszFieldName, oFC.iconfield) ||
1573 EQUAL(pszFieldName, oFC.headingfield) ||
1574 EQUAL(pszFieldName, oFC.tiltfield) ||
1575 EQUAL(pszFieldName, oFC.rollfield) ||
1576 EQUAL(pszFieldName, oFC.snippetfield) ||
1577 EQUAL(pszFieldName, oFC.modelfield) ||
1578 EQUAL(pszFieldName, oFC.scalexfield) ||
1579 EQUAL(pszFieldName, oFC.scaleyfield) ||
1580 EQUAL(pszFieldName, oFC.scalezfield) ||
1581 EQUAL(pszFieldName, oFC.networklinkfield) ||
1582 EQUAL(pszFieldName, oFC.networklink_refreshvisibility_field) ||
1583 EQUAL(pszFieldName, oFC.networklink_flytoview_field) ||
1584 EQUAL(pszFieldName, oFC.networklink_refreshMode_field) ||
1585 EQUAL(pszFieldName, oFC.networklink_refreshInterval_field) ||
1586 EQUAL(pszFieldName, oFC.networklink_viewRefreshMode_field) ||
1587 EQUAL(pszFieldName, oFC.networklink_viewRefreshTime_field) ||
1588 EQUAL(pszFieldName, oFC.networklink_viewBoundScale_field) ||
1589 EQUAL(pszFieldName, oFC.networklink_viewFormat_field) ||
1590 EQUAL(pszFieldName, oFC.networklink_httpQuery_field) ||
1591 EQUAL(pszFieldName, oFC.camera_longitude_field) ||
1592 EQUAL(pszFieldName, oFC.camera_latitude_field) ||
1593 EQUAL(pszFieldName, oFC.camera_altitude_field) ||
1594 EQUAL(pszFieldName, oFC.camera_altitudemode_field) ||
1595 EQUAL(pszFieldName, oFC.photooverlayfield) ||
1596 EQUAL(pszFieldName, oFC.leftfovfield) ||
1597 EQUAL(pszFieldName, oFC.rightfovfield) ||
1598 EQUAL(pszFieldName, oFC.bottomfovfield) ||
1599 EQUAL(pszFieldName, oFC.topfovfield) ||
1600 EQUAL(pszFieldName, oFC.nearfield) ||
1601 EQUAL(pszFieldName, oFC.photooverlay_shape_field) ||
1602 EQUAL(pszFieldName, oFC.imagepyramid_tilesize_field) ||
1603 EQUAL(pszFieldName, oFC.imagepyramid_maxwidth_field) ||
1604 EQUAL(pszFieldName, oFC.imagepyramid_maxheight_field) ||
1605 EQUAL(pszFieldName, oFC.imagepyramid_gridorigin_field))
1606 {
1607 return nullptr;
1608 }
1609
1610 SimpleFieldPtr poKmlSimpleField = poKmlFactory->CreateSimpleField();
1611 poKmlSimpleField->set_name(pszFieldName);
1612
1613 switch (poOgrFieldDef->GetType())
1614 {
1615 case OFTInteger:
1616 case OFTIntegerList:
1617 poKmlSimpleField->set_type(
1618 poOgrFieldDef->GetSubType() == OFSTBoolean ? "bool" : "int");
1619 return poKmlSimpleField;
1620
1621 case OFTReal:
1622 case OFTRealList:
1623 poKmlSimpleField->set_type(
1624 poOgrFieldDef->GetSubType() == OFSTFloat32 ? "float"
1625 : "double");
1626 return poKmlSimpleField;
1627
1628 case OFTString:
1629 case OFTStringList:
1630 poKmlSimpleField->set_type("string");
1631 return poKmlSimpleField;
1632
1633 case OFTInteger64:
1634 if (bApproxOK)
1635 {
1636 poKmlSimpleField->set_type("string");
1637 return poKmlSimpleField;
1638 }
1639 break;
1640
1641 /***** kml has these types but as timestamp/timespan *****/
1642 case OFTDate:
1643 case OFTTime:
1644 case OFTDateTime:
1645 if (bApproxOK)
1646 {
1647 poKmlSimpleField->set_type("string");
1648 return poKmlSimpleField;
1649 }
1650 break;
1651
1652 default:
1653 poKmlSimpleField->set_type("string");
1654 return poKmlSimpleField;
1655 }
1656
1657 return nullptr;
1658 }
1659
1660 /******************************************************************************
1661 function to add the simpleFields in a schema to a featuredefn
1662 ******************************************************************************/
1663
1664 void kml2FeatureDef(SchemaPtr poKmlSchema, OGRFeatureDefn *poOgrFeatureDefn,
1665 std::map<std::string, int> &mapSimpleFieldNameToOgrFieldIx,
1666 std::set<std::string> &oSetSimpleFields)
1667 {
1668 const size_t nSimpleFields = poKmlSchema->get_simplefield_array_size();
1669
1670 for (size_t iSimpleField = 0; iSimpleField < nSimpleFields; iSimpleField++)
1671 {
1672 SimpleFieldPtr poKmlSimpleField =
1673 poKmlSchema->get_simplefield_array_at(iSimpleField);
1674
1675 const char *pszType = "string";
1676 string osType;
1677
1678 if (poKmlSimpleField->has_type())
1679 {
1680 osType = poKmlSimpleField->get_type();
1681
1682 pszType = osType.c_str();
1683 }
1684
1685 std::string osName;
1686 if (poKmlSimpleField->has_name())
1687 {
1688 osName = poKmlSimpleField->get_name();
1689 }
1690 else
1691 continue;
1692
1693 int counter = 2;
1694 const std::string osNameRadix = osName;
1695 const CPLString osNameLower = CPLString(osName).tolower();
1696 if (cpl::contains(oSetSimpleFields, osNameLower))
1697 continue;
1698 oSetSimpleFields.insert(osNameLower);
1699
1700 mapSimpleFieldNameToOgrFieldIx[osNameRadix] =
1701 poOgrFeatureDefn->GetFieldCount();
1702
1703 while (poOgrFeatureDefn->GetFieldIndex(osName.c_str()) >= 0)
1704 {
1705 osName = osNameRadix + std::to_string(counter);
1706 ++counter;
1707 }
1708
1709 if (EQUAL(pszType, "bool") || EQUAL(pszType, "boolean"))
1710 {
1711 OGRFieldDefn ogrFieldDefn(osName.c_str(), OFTInteger);
1712 ogrFieldDefn.SetSubType(OFSTBoolean);
1713 poOgrFeatureDefn->AddFieldDefn(&ogrFieldDefn);
1714 }
1715 else if (EQUAL(pszType, "int") || EQUAL(pszType, "short") ||
1716 EQUAL(pszType, "ushort"))
1717 {
1718 OGRFieldDefn ogrFieldDefn(osName.c_str(), OFTInteger);
1719 poOgrFeatureDefn->AddFieldDefn(&ogrFieldDefn);
1720 }
1721 else if (EQUAL(pszType, "uint"))
1722 {
1723 OGRFieldDefn ogrFieldDefn(osName.c_str(), OFTInteger64);
1724 poOgrFeatureDefn->AddFieldDefn(&ogrFieldDefn);
1725 }
1726 else if (EQUAL(pszType, "float") || EQUAL(pszType, "double"))
1727 {
1728 // We write correctly 'double' for 64-bit since GDAL 3.11.1.
1729 // In prior versions we wrote 'float', so it is premature
1730 // on reading to set OFSTFloat32 when reading 'float'
1731 OGRFieldDefn ogrFieldDefn(osName.c_str(), OFTReal);
1732 poOgrFeatureDefn->AddFieldDefn(&ogrFieldDefn);
1733 }
1734 else // string, or any other unrecognized type.
1735 {
1736 OGRFieldDefn ogrFieldDefn(osName.c_str(), OFTString);
1737 poOgrFeatureDefn->AddFieldDefn(&ogrFieldDefn);
1738 }
1739 }
1740 }
1741
1742 /*******************************************************************************
1743 * function to fetch the field config options
1744 *
1745 *******************************************************************************/
1746
1747 void get_fieldconfig(struct fieldconfig *oFC)
1748 {
1749 oFC->idfield = CPLGetConfigOption("LIBKML_ID_FIELD", "id");
1750 oFC->namefield = CPLGetConfigOption("LIBKML_NAME_FIELD", "Name");
1751 oFC->descfield =
1752 CPLGetConfigOption("LIBKML_DESCRIPTION_FIELD", "description");
1753 oFC->tsfield = CPLGetConfigOption("LIBKML_TIMESTAMP_FIELD", "timestamp");
1754 oFC->beginfield = CPLGetConfigOption("LIBKML_BEGIN_FIELD", "begin");
1755 oFC->endfield = CPLGetConfigOption("LIBKML_END_FIELD", "end");
1756 oFC->altitudeModefield =
1757 CPLGetConfigOption("LIBKML_ALTITUDEMODE_FIELD", "altitudeMode");
1758 oFC->tessellatefield =
1759 CPLGetConfigOption("LIBKML_TESSELLATE_FIELD", "tessellate");
1760 oFC->extrudefield = CPLGetConfigOption("LIBKML_EXTRUDE_FIELD", "extrude");
1761 oFC->visibilityfield =
1762 CPLGetConfigOption("LIBKML_VISIBILITY_FIELD", "visibility");
1763 oFC->drawOrderfield =
1764 CPLGetConfigOption("LIBKML_DRAWORDER_FIELD", "drawOrder");
1765 oFC->iconfield = CPLGetConfigOption("LIBKML_ICON_FIELD", "icon");
1766 oFC->headingfield = CPLGetConfigOption("LIBKML_HEADING_FIELD", "heading");
1767 oFC->tiltfield = CPLGetConfigOption("LIBKML_TILT_FIELD", "tilt");
1768 oFC->rollfield = CPLGetConfigOption("LIBKML_ROLL_FIELD", "roll");
1769 oFC->snippetfield = CPLGetConfigOption("LIBKML_SNIPPET_FIELD", "snippet");
1770 oFC->modelfield = CPLGetConfigOption("LIBKML_MODEL_FIELD", "model");
1771 oFC->scalexfield = CPLGetConfigOption("LIBKML_SCALE_X_FIELD", "scale_x");
1772 oFC->scaleyfield = CPLGetConfigOption("LIBKML_SCALE_Y_FIELD", "scale_y");
1773 oFC->scalezfield = CPLGetConfigOption("LIBKML_SCALE_Z_FIELD", "scale_z");
1774 oFC->networklinkfield =
1775 CPLGetConfigOption("LIBKML_NETWORKLINK_FIELD", "networklink");
1776 oFC->networklink_refreshvisibility_field =
1777 CPLGetConfigOption("LIBKML_NETWORKLINK_REFRESHVISIBILITY_FIELD",
1778 "networklink_refreshvisibility");
1779 oFC->networklink_flytoview_field = CPLGetConfigOption(
1780 "LIBKML_NETWORKLINK_FLYTOVIEW_FIELD", "networklink_flytoview");
1781 oFC->networklink_refreshMode_field = CPLGetConfigOption(
1782 "LIBKML_NETWORKLINK_REFRESHMODE_FIELD", "networklink_refreshmode");
1783 oFC->networklink_refreshInterval_field =
1784 CPLGetConfigOption("LIBKML_NETWORKLINK_REFRESHINTERVAL_FIELD",
1785 "networklink_refreshinterval");
1786 oFC->networklink_viewRefreshMode_field =
1787 CPLGetConfigOption("LIBKML_NETWORKLINK_VIEWREFRESHMODE_FIELD",
1788 "networklink_viewrefreshmode");
1789 oFC->networklink_viewRefreshTime_field =
1790 CPLGetConfigOption("LIBKML_NETWORKLINK_VIEWREFRESHTIME_FIELD",
1791 "networklink_viewrefreshtime");
1792 oFC->networklink_viewBoundScale_field =
1793 CPLGetConfigOption("LIBKML_NETWORKLINK_VIEWBOUNDSCALE_FIELD",
1794 "networklink_viewboundscale");
1795 oFC->networklink_viewFormat_field = CPLGetConfigOption(
1796 "LIBKML_NETWORKLINK_VIEWFORMAT_FIELD", "networklink_viewformat");
1797 oFC->networklink_httpQuery_field = CPLGetConfigOption(
1798 "LIBKML_NETWORKLINK_HTTPQUERY_FIELD", "networklink_httpquery");
1799 oFC->camera_longitude_field =
1800 CPLGetConfigOption("LIBKML_CAMERA_LONGITUDE_FIELD", "camera_longitude");
1801 oFC->camera_latitude_field =
1802 CPLGetConfigOption("LIBKML_CAMERA_LATITUDE_FIELD", "camera_latitude");
1803 oFC->camera_altitude_field =
1804 CPLGetConfigOption("LIBKML_CAMERA_ALTITUDE_FIELD", "camera_altitude");
1805 oFC->camera_altitudemode_field = CPLGetConfigOption(
1806 "LIBKML_CAMERA_ALTITUDEMODE_FIELD", "camera_altitudemode");
1807 oFC->photooverlayfield =
1808 CPLGetConfigOption("LIBKML_PHOTOOVERLAY_FIELD", "photooverlay");
1809 oFC->leftfovfield = CPLGetConfigOption("LIBKML_LEFTFOV_FIELD", "leftfov");
1810 oFC->rightfovfield =
1811 CPLGetConfigOption("LIBKML_RIGHTFOV_FIELD", "rightfov");
1812 oFC->bottomfovfield =
1813 CPLGetConfigOption("LIBKML_BOTTOMFOV_FIELD", "bottomfov");
1814 oFC->topfovfield = CPLGetConfigOption("LIBKML_TOPFOV_FIELD", "topfov");
1815 oFC->nearfield = CPLGetConfigOption("LIBKML_NEARFOV_FIELD", "near");
1816 oFC->photooverlay_shape_field = CPLGetConfigOption(
1817 "LIBKML_PHOTOOVERLAY_SHAPE_FIELD", "photooverlay_shape");
1818 oFC->imagepyramid_tilesize_field = CPLGetConfigOption(
1819 "LIBKML_IMAGEPYRAMID_TILESIZE", "imagepyramid_tilesize");
1820 oFC->imagepyramid_maxwidth_field = CPLGetConfigOption(
1821 "LIBKML_IMAGEPYRAMID_MAXWIDTH", "imagepyramid_maxwidth");
1822 oFC->imagepyramid_maxheight_field = CPLGetConfigOption(
1823 "LIBKML_IMAGEPYRAMID_MAXHEIGHT", "imagepyramid_maxheight");
1824 oFC->imagepyramid_gridorigin_field = CPLGetConfigOption(
1825 "LIBKML_IMAGEPYRAMID_GRIDORIGIN", "imagepyramid_gridorigin");
1826 }
1827
1828 /************************************************************************/
1829 /* kmlAltitudeModeFromString() */
1830 /************************************************************************/
1831
1832 int kmlAltitudeModeFromString(const char *pszAltitudeMode, int &isGX)
1833 {
1834 isGX = FALSE;
1835 int iAltitudeMode = static_cast<int>(kmldom::ALTITUDEMODE_CLAMPTOGROUND);
1836
1837 if (EQUAL(pszAltitudeMode, "clampToGround"))
1838 {
1839 iAltitudeMode = static_cast<int>(kmldom::ALTITUDEMODE_CLAMPTOGROUND);
1840 }
1841 else if (EQUAL(pszAltitudeMode, "relativeToGround"))
1842 {
1843 iAltitudeMode = static_cast<int>(kmldom::ALTITUDEMODE_RELATIVETOGROUND);
1844 }
1845 else if (EQUAL(pszAltitudeMode, "absolute"))
1846 {
1847 iAltitudeMode = static_cast<int>(kmldom::ALTITUDEMODE_ABSOLUTE);
1848 }
1849 else if (EQUAL(pszAltitudeMode, "relativeToSeaFloor"))
1850 {
1851 iAltitudeMode =
1852 static_cast<int>(kmldom::GX_ALTITUDEMODE_RELATIVETOSEAFLOOR);
1853 isGX = TRUE;
1854 }
1855 else if (EQUAL(pszAltitudeMode, "clampToSeaFloor"))
1856 {
1857 iAltitudeMode =
1858 static_cast<int>(kmldom::GX_ALTITUDEMODE_CLAMPTOSEAFLOOR);
1859 isGX = TRUE;
1860 }
1861 else
1862 {
1863 CPLError(CE_Warning, CPLE_NotSupported,
1864 "Unrecognized value for altitudeMode: %s", pszAltitudeMode);
1865 }
1866
1867 return iAltitudeMode;
1868 }