当前位置: 首页 > 工具软件 > libkml > 使用案例 >

c++ 解析生成kml的库 libkml

李俊雅
2023-12-01

libkml是google解析生成kml的库,很好用。

地址:安装源码下载 https://github.com/google/libkml/wiki/Building-and-installing-libkml

库下载:https://cbs.centos.org/koji/buildinfo?buildID=13972

使用例子:下面是输出点的例子

  kmldom::KmlFactory* factory = kmldom::KmlFactory::GetFactory();
     kmldom::DocumentPtr document = factory->CreateDocument();

     kmldom::StylePtr oneStyle = factory->CreateStyle();
     oneStyle->set_id("point style");
     kmldom::IconStylePtr iconStyle = factory->CreateIconStyle();
     iconStyle->set_color((kmlbase::Color32)aValue.styleColorLine);
     iconStyle->set_scale(aValue.scale);

     kmldom::IconStyleIconPtr icon = factory->CreateIconStyleIcon();
     icon->set_href("http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png");
     iconStyle->set_icon(icon);
     kmldom::ListStylePtr listStyle = factory->CreateListStyle();
     listStyle->set_bgcolor((kmlbase::Color32)aValue.styleColorLine);
     oneStyle->set_liststyle(listStyle);
     kmldom::LabelStylePtr labelStyle = factory->CreateLabelStyle();

     string::size_type position;
     position = aValue.placeName.find("speed");
     if (position != aValue.placeName.npos)
     {
         labelStyle->set_scale(0.7);
     }
     else
     {
         labelStyle->set_scale(0);
     }

     oneStyle->set_labelstyle(labelStyle);
     oneStyle->set_iconstyle(iconStyle);

     document->add_styleselector(oneStyle);

     for(auto item : aVecPoints)
     {
         kmldom::PlacemarkPtr placemark = factory->CreatePlacemark();
         kmldom::CoordinatesPtr coordinates = factory->CreateCoordinates();
         coordinates->add_latlngalt(item.lat,item.lon,item.elevation);
         kmldom::PointPtr point = factory->CreatePoint();
         point->set_coordinates(coordinates);
         placemark->set_name(item.name);
         placemark->set_description(item.description);
         placemark->set_geometry(point);
         placemark->set_styleurl("point style");
         document->add_feature(placemark);
     }

     kmldom::KmlPtr kml = factory->CreateKml();
     kml->set_feature(document);
     kmlengine::KmlFilePtr kml_file =  kmlengine::KmlFile::CreateFromImport(kml);
     std::string xml1;
     kml_file->SerializeToString(&xml1);

     std::ofstream	OsWrite(aSaveFileName,std::ofstream::out);
     OsWrite<<xml1;
     OsWrite<<std::endl;
     OsWrite.close();

写线:

kmldom::KmlFactory* factory = kmldom::KmlFactory::GetFactory();
    kmldom::DocumentPtr document = factory->CreateDocument();

    kmldom::StylePtr oneStyle = factory->CreateStyle();
    oneStyle->set_id("line_style");
    kmldom::LineStylePtr lineStyle = factory->CreateLineStyle();
    lineStyle->set_color((kmlbase::Color32)aValue.styleColorLine);
    lineStyle->set_width(aValue.styleWidth);
    kmldom::PolyStylePtr polyStyle = factory->CreatePolyStyle();
    polyStyle->set_color((kmlbase::Color32)aValue.styleColorPoly);
    oneStyle->set_linestyle(lineStyle);
    oneStyle->set_polystyle(polyStyle);
    document->add_styleselector(oneStyle);

    for(auto vecItem : pointsList)
    {
        kmldom::PlacemarkPtr placemark = factory->CreatePlacemark();
        placemark->set_name("line mark");
        placemark->set_description("line mark description");
        placemark->set_styleurl("line_style");
        kmldom::LineStringPtr lineString = factory->CreateLineString();
        lineString->set_tessellate(true);
        lineString->set_extrude(true);

        if(vecItem.size() > 0)
        {
            placemark->set_name(vecItem[vecItem.size() -1].name);
            placemark->set_description(vecItem[vecItem.size() -1].description);
        }
        else
        {
            continue;
        }

        kmldom::CoordinatesPtr coordinates = factory->CreateCoordinates();
        for(auto item : vecItem)
        {
            coordinates->add_latlngalt(item.lat,item.lon,item.elevation);
            lineString->set_coordinates(coordinates);
        }

        placemark->set_geometry(lineString);
        document->add_feature(placemark);
    }

    kmldom::KmlPtr kml = factory->CreateKml();
    kml->set_feature(document);
    kmlengine::KmlFilePtr kml_file =  kmlengine::KmlFile::CreateFromImport(kml);
    std::string xml1;
    kml_file->SerializeToString(&xml1);

    std::ofstream	OsWrite(aSaveFileName,std::ofstream::out);
    OsWrite<<xml1;
    OsWrite<<std::endl;
    OsWrite.close();

 

 类似资料: