static ContainerPtr MyGetContainerFromRoot ( KmlFactory *m_poKmlFactory, ElementPtr poKmlRoot ) { ContainerPtr poKmlContainer = NULL; if ( poKmlRoot ) { /***** skip over the <kml> we want the container *****/ if ( poKmlRoot->IsA ( kmldom::Type_kml ) ) { KmlPtr poKmlKml = AsKml ( poKmlRoot ); if ( poKmlKml->has_feature ( ) ) { FeaturePtr poKmlFeat = poKmlKml->get_feature ( ); if ( poKmlFeat->IsA ( kmldom::Type_Container ) ) poKmlContainer = AsContainer ( poKmlFeat ); else if ( poKmlFeat->IsA ( kmldom::Type_Placemark ) ) { poKmlContainer = m_poKmlFactory->CreateDocument ( ); poKmlContainer->add_feature ( kmldom::AsFeature(kmlengine::Clone(poKmlFeat)) ); } } } else if ( poKmlRoot->IsA ( kmldom::Type_Container ) ) poKmlContainer = AsContainer ( poKmlRoot ); } return poKmlContainer; }
bool UAVFlightPlan::exportToKMLFile(const std::string &f, int begin_, int end_) const { bool ret_val = true; if (end_ < 0 || end_ > size()) { end_ = size(); } if (begin_ < 0 || begin_ > size()) { begin_ = 0; } const_iterator it = begin(); // Advance to the first waypoint to save int i = 0; for (; i < begin_; i++, it++); KmlFactory* factory = KmlFactory::GetFactory(); kmldom::DocumentPtr doc = factory->CreateDocument(); for (int j = 1; i < end_ && it != end(); j++,i++, it++) { ostringstream name_; name_ << "Waypoint " << j; // Create a <Point> with <coordinates> from the given Vec3. kmlbase::Vec3 v(it->getLongitude(), it->getLatitude(), it->getAltitude()); kmldom::PointPtr point = kmlconvenience::CreatePointFromVec3(v); PlacemarkPtr place = factory->CreatePlacemark(); place->set_geometry(point); doc->add_feature(place); } // Finally create the kml KmlPtr kml = factory->CreateKml(); kml->set_feature(doc); // Then the file KmlFilePtr kmlfile = KmlFile::CreateFromImport(kml); if (!kmlfile) { cerr << "error: could not create kml file" << endl; return false; } // And write it std::string kml_data; kmlfile->SerializeToString(&kml_data); if (!kmlbase::File::WriteStringToFile(kml_data, f.c_str())) { cerr << "error: write of " << f << " failed" << endl; ret_val = false; } return ret_val; }
/** * @brief KmlExport::exportToKML Triggers logfile export to KML. */ bool KmlExport::exportToKML() { bool ret = open(); if (!ret) { qDebug () << "Logfile failed to open during KML export"; return false; } // Parses logfile and generates KML document ret = preparseLogFile(); if (!ret) { qDebug () << "Logfile preparsing failed"; return false; } // Call parser. parseLogFile(); // Add track to <Document> document->add_feature(trackFolder); // Add timespans to <Document> document->add_feature(timestampFolder); // Add ground track to <Document> { LineStringPtr linestring = factory->CreateLineString(); linestring->set_extrude(false); // Do not extrude to ground linestring->set_altitudemode(kmldom::ALTITUDEMODE_CLAMPTOGROUND); linestring->set_coordinates(wallAxes[0]); MultiGeometryPtr multiGeometry = factory->CreateMultiGeometry(); multiGeometry->add_geometry(linestring); PlacemarkPtr placemark = factory->CreatePlacemark(); placemark->set_geometry(multiGeometry); placemark->set_styleurl("#ts_2_tb"); placemark->set_name("Ground track"); document->add_feature(placemark); } // Add wall axes to <Document> FolderPtr folder = factory->CreateFolder(); for (int i=0; i<numberOfWallAxes; i++) { LineStringPtr linestring = factory->CreateLineString(); linestring->set_extrude(false); // Do not extrude to ground linestring->set_altitudemode(kmldom::ALTITUDEMODE_ABSOLUTE); linestring->set_coordinates(wallAxes[i]); MultiGeometryPtr multiGeometry = factory->CreateMultiGeometry(); multiGeometry->add_geometry(linestring); PlacemarkPtr placemark = factory->CreatePlacemark(); placemark->set_geometry(multiGeometry); placemark->set_styleurl("#ts_1_tb"); folder->add_feature(placemark); folder->set_name("Wall axes"); } document->add_feature(folder); // Create <kml> and give it <Document>. KmlPtr kml = factory->CreateKml(); kml->set_feature(document); // kml takes ownership. // Serialize to XML std::string kml_data = kmldom::SerializePretty(kml); // Save to file if (QFileInfo(outputFileName).suffix().toLower() == "kmz") { if (!kmlengine::KmzFile::WriteKmz(outputFileName.toStdString().c_str(), kml_data)) { qDebug() << "KMZ write failed: " << outputFileName; QMessageBox::critical(new QWidget(),"KMZ write failed", "Failed to write KMZ file."); return false; } } else if (QFileInfo(outputFileName).suffix().toLower() == "kml") { if (!kmlbase::File::WriteStringToFile(kml_data, outputFileName.toStdString())) { qDebug() << "KML write failed: " << outputFileName; QMessageBox::critical(new QWidget(),"KML write failed", "Failed to write KML file."); return false; } } else { qDebug() << "Write failed. Invalid file name:" << outputFileName; QMessageBox::critical(new QWidget(),"Write failed", "Failed to write file. Invalid filename"); return false; } return true; }