Merge pull request #3931 from afabri/Stream_support-parseXML-GF

Stream_support:  Add example to read an xml file using boost::property_tree
This commit is contained in:
Laurent Rineau 2019-06-05 11:20:26 +02:00
commit 44ea54db97
5 changed files with 64 additions and 0 deletions

View File

@ -258,6 +258,24 @@ in those WKT types, namely:
- `CGAL::Point_3`
- `CGAL::Polygon_with_holes_2`
- random access range of the above types.
\section IOstreamXML Reading Adhoc XML
In case you have to read data in a simple xml file with a simple structure, without existing I/O libraries
the <a href="https://www.boost.org/doc/libs/release/libs/property_tree/">`boost::property_tree`</a> comes in handy.
The following small example shows how to parse a file
looking like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PolySet>
<Polygon>
<Point X="-715.8811978465" Y="-2729.9490000000" Z="-534.9000000000"/>
<Point X="-718.1905989232" Y="-2729.9490000000" Z="-538.9000000000"/>
<Point X="-722.8094010768" Y="-2729.9490000000" Z="-538.9000000000"/>
</Polygon>
</PolySet>
\cgalExample{Stream_support/read_xml.cpp}
*/
} /* namespace CGAL */

View File

@ -2,4 +2,5 @@
\example Stream_support/Linestring_WKT.cpp
\example Stream_support/Point_WKT.cpp
\example Stream_support/Polygon_WKT.cpp
\example Stream_support/read_xml.cpp
*/

View File

@ -25,6 +25,8 @@ if ( CGAL_FOUND )
create_single_source_cgal_program( "Polygon_WKT.cpp" )
create_single_source_cgal_program( "Linestring_WKT.cpp" )
create_single_source_cgal_program( "read_WKT.cpp" )
create_single_source_cgal_program( "read_xml.cpp" )
else ()
message(STATUS "This project requires the CGAL library, and will not be compiled.")

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PolySet>
<Polygon>
<Point X="-715.8811978465" Y="-2729.9490000000" Z="-534.9000000000"/>
<Point X="-718.1905989232" Y="-2729.9490000000" Z="-538.9000000000"/>
<Point X="-722.8094010768" Y="-2729.9490000000" Z="-538.9000000000"/>
</Polygon>
</PolySet>

View File

@ -0,0 +1,35 @@
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <vector>
#include <fstream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_3 Point_3;
int main(int argc, char* argv[]){
std::ifstream in( (argc>1)? argv[1] : "data/cloud.pol");
boost::property_tree::ptree tree;
boost::property_tree::read_xml(in, tree);
std::vector<Point_3> points;
for(boost::property_tree::ptree::value_type& node : tree.get_child("PolySet.Polygon")){
boost::property_tree::ptree subtree = node.second;
if( node.first == "Point" ){
for( boost::property_tree::ptree::value_type const& v : subtree.get_child( "" ) ) {
std::string label = v.first;
if ( label == "<xmlattr>" ) {
Point_3 p(subtree.get<double>( label+".X"),
subtree.get<double>( label+".Y"),
subtree.get<double>( label+".Z"));
points.push_back(p);
}
}
}
}
std::cout << points.size() << " points read"<< std::endl;
return 0;
}