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; }
OGRErr OGRLIBKMLLayer::ICreateFeature ( OGRFeature * poOgrFeat ) { if ( !bUpdate ) return OGRERR_UNSUPPORTED_OPERATION; if( m_bRegionBoundsAuto && poOgrFeat->GetGeometryRef() != NULL && !(poOgrFeat->GetGeometryRef()->IsEmpty()) ) { OGREnvelope sEnvelope; poOgrFeat->GetGeometryRef()->getEnvelope(&sEnvelope); m_dfRegionMinX = MIN(m_dfRegionMinX, sEnvelope.MinX); m_dfRegionMinY = MIN(m_dfRegionMinY, sEnvelope.MinY); m_dfRegionMaxX = MAX(m_dfRegionMaxX, sEnvelope.MaxX); m_dfRegionMaxY = MAX(m_dfRegionMaxY, sEnvelope.MaxY); } FeaturePtr poKmlFeature = feat2kml ( m_poOgrDS, this, poOgrFeat, m_poOgrDS->GetKmlFactory ( ), m_bUseSimpleField ); if( m_poKmlLayer != NULL ) m_poKmlLayer->add_feature ( poKmlFeature ); else { CPLAssert( m_poKmlUpdate != NULL ); KmlFactory *poKmlFactory = m_poOgrDS->GetKmlFactory ( ); CreatePtr poCreate = poKmlFactory->CreateCreate(); ContainerPtr poContainer; if( m_bUpdateIsFolder ) poContainer = poKmlFactory->CreateFolder(); else poContainer = poKmlFactory->CreateDocument(); poContainer->set_targetid(OGRLIBKMLGetSanitizedNCName(GetName())); poContainer->add_feature ( poKmlFeature ); poCreate->add_container(poContainer); m_poKmlUpdate->add_updateoperation(poCreate); } /***** update the layer class count of features *****/ if( m_poKmlLayer != NULL ) { nFeatures++; const char* pszId = CPLSPrintf("%s.%d", OGRLIBKMLGetSanitizedNCName(GetName()).c_str(), nFeatures); poOgrFeat->SetFID(nFeatures); poKmlFeature->set_id(pszId); } else { if( poOgrFeat->GetFID() < 0 ) { static int bAlreadyWarned = FALSE; if( !bAlreadyWarned ) { bAlreadyWarned = TRUE; CPLError(CE_Warning, CPLE_AppDefined, "It is recommended to define a FID when calling CreateFeature() in a update document"); } } else { const char* pszId = CPLSPrintf("%s." CPL_FRMT_GIB, OGRLIBKMLGetSanitizedNCName(GetName()).c_str(), poOgrFeat->GetFID()); poOgrFeat->SetFID(nFeatures); poKmlFeature->set_id(pszId); } } /***** mark the layer as updated *****/ bUpdated = TRUE; m_poOgrDS->Updated ( ); return OGRERR_NONE; }