将数据从c++文件输入到KML文件

Enter data to KML file from c++ file

本文关键字:文件 输入 KML c++ 数据      更新时间:2023-10-16

我需要输入从c++文件到KML文件的坐标才能使用Google Earth运行,你会怎么做?KML文件为:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" 
 xmlns:gx="http://www.google.com/kml/ext/2.2"
 xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Path.kml</name>
<Style id="pathstyle">
    <LineStyle>
        <color>ff190cff</color>
        <width>2</width>
    </LineStyle>
</Style>
<Placemark>
    <name>Path</name>
    <description>This is the path between the 2 points</description>
    <styleUrl>#pathstyle</styleUrl>
    <LineString>
        <tessellate>1</tessellate>
        <coordinates>
            long1,lat1,0
            long2,lat2,0 
        </coordinates>
    </LineString>
</Placemark>

当纬度和经度输入到c++文件中时,我将如何输入该文件中的数据?它们被声明为双浮点

这里有一个我成功使用的策略:创建一系列函数来逐步构建KML/XML。例如,这里有一个函数来序列化KML:的Placemark部分

(另请参阅现场演示。)

#include <fstream>
#include <sstream>
#include <string>
std::string FormatPlacemark(double lat1, double long1, double lat2, double long2)
{
    std::ostringstream ss;
    ss << "<Placemark>n"
       << "<name>Path</name>n"
       << "<description>This is the path between the 2 points</description>n"
       << "<styleUrl>#pathstyle</styleUrl>n"
       << "<LineString>n"
       << "<tessellate>1</tessellate>n"
       << "<coordinates>"
       << long1 << "," << lat1 << ",0"
       << " "
       << long2 << "," << lat2 << ",0"
       << "</coordinates>n"
       << "</LineString>n"
       << "</Placemark>n";
    return ss.str();
}

以下是如何创建/打开您的KML文件并对其进行写入:

std::ofstream handle;
// http://www.cplusplus.com/reference/ios/ios/exceptions/
// Throw an exception on failure to open the file or on a write error.
handle.exceptions(std::ofstream::failbit | std::ofstream::badbit);
// Open the KML file for writing:
handle.open("C:/Output/Sample.kml");
// Write to the KML file:
handle << "<?xml version='1.0' encoding='utf-8'?>n";
handle << "<kml xmlns='http://www.opengis.net/kml/2.2'>n";
handle << FormatPlacemark(-76.2, 38.5, -76.1, 38.6);
handle << "</kml>n";
handle.close();