x03_b_soci_example.cpp

Second example showing how to get polygons from a database using SOCI and put them into GGL, using WKT.

    //Boost.Geometry(akaGGL,GenericGeometryLibrary)
    //
    //Copyright(c)2007-2012BarendGehrels,Amsterdam,theNetherlands.
    //Use,modificationanddistributionissubjecttotheBoostSoftwareLicense,
    //Version1.0.(SeeaccompanyingfileLICENSE_1_0.txtorcopyat
    //http://www.boost.org/LICENSE_1_0.txt)

    //SOCIexample

    //b:usingWKTtoretrievepoints

    //Tobuildandrunthisexample,seecommentsinexamplea

    #include<soci.h>
    #include<soci-postgresql.h>

    #include<boost/algorithm/string.hpp>
    #include<boost/optional.hpp>
    #include<boost/timer.hpp>
    #include<boost/random.hpp>
    #include<boost/tuple/tuple.hpp>

    #include<iostream>
    #include<istream>
    #include<ostream>
    #include<sstream>
    #include<string>
    #include<exception>

    #include<boost/geometry/geometry.hpp>
    #include<boost/geometry/geometries/geometries.hpp>

    #include<boost/geometry/io/wkt/wkt.hpp>

    structcity
    {
    boost::geometry::model::point<float, 2, boost::geometry::cs::geographic<boost::geometry::degree>>location;
    std::stringname;
    };

    namespacesoci
    {
    template<>
    structtype_conversion<city>
    {
    typedefsoci::valuesbase_type;

    staticvoidfrom_base(constbase_type&v,soci::indicatorind,city&value)
    {
    try
    {
    value.name=v.get<std::string>("name");
    boost::geometry::read_wkt(v.get<std::string>("wkt"),value.location);
    }
    catch(conststd::exception&e)
    {
    std::cout<<e.what()<<std::endl;
    }
    }

    staticvoidto_base(constcity&value,base_type&v,soci::indicator&ind)
    {
    v.set("name",value.name);
    std::ostringstreamout;
    out<<boost::geometry::wkt(value.location);
    v.set("wkt",out.str());
    ind=i_ok;
    }
    };
    }

    intmain()
    {
    try
    {
    soci::sessionsql(soci::postgresql,"dbname=ggluser=gglpassword=ggl");

    typedefstd::vector<city>V;

    soci::rowset<city>rows=sql.prepare<<"selectname,astext(location)aswktfromcities";
    Vvec;
    std::copy(rows.begin(),rows.end(),std::back_inserter(vec));

    for(V::const_iteratorit=vec.begin();it!=vec.end();++it)
    {
    staticconstdoublesqrkm=1000.0*1000.0;
    std::cout<<it->name
    <<""<<boost::geometry::dsv(it->location)
    //<<""<<boost::geometry::area(it->shape)/sqrkm<<"km2"
    <<std::endl;
    }
    }
    catch(std::exceptionconst&e)
    {
    std::cerr<<"Error:"<<e.what()<<'\n';
    }
    return0;
    }