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

时间:2013-12-03 22:21:29

标签: c++ xml kml google-earth

我需要输入从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 ++文件输入数据?它们被声明为双浮动

1 个答案:

答案 0 :(得分:3)

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

(另请参阅live demo。)

#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();