x03_a_soci_example.cpp

First example showing how to get spatial data from a database using SOCI and put them into GGL

    //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

    //a:usingboost::tupletoretrievepoints

    //SOCIisagenericC++templateinterfacetoaccessrelationaldatabases

    //Tobuildandrunthisexample:
    //1)downloadSOCIfromhttp://soci.sourceforge.net/
    //2)putitincontrib/soci-3.0.0(oranotherversion/folder,butthenupdatethisVCPROJ)
    //3)adaptyourmakefileorusethisVCPROJfile
    //(notethatSOCIsourcesareincludeddirectly,buildingSOCIisnotnecessary)
    //4)loadthedemo-data,seescriptdata/cities.sql(forPostgreSQL)

    #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/adapted/boost_tuple.hpp>

    BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian);

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

    intcount;
    sql<<"selectcount(*)fromcities",soci::into(count);
    std::cout<<"#Capitals:"<<count<<std::endl;

    typedefstd::vector<boost::tuple<double,double>>V;

    soci::rowset<boost::tuple<double,double>>rows
    =sql.prepare<<"selectx(location),y(location)fromcities";
    Vvec;
    std::copy(rows.begin(),rows.end(),std::back_inserter(vec));

    for(V::const_iteratorit=vec.begin();it!=vec.end();++it)
    {
    std::cout<<it->get<0>()<<""<<it->get<1>()<<std::endl;
    }
    //Calculatedistances
    for(V::const_iteratorit1=vec.begin();it1!=vec.end();++it1)
    {
    for(V::const_iteratorit2=vec.begin();it2!=vec.end();++it2)
    {
    std::cout<<boost::geometry::dsv(*it1)<<""<<boost::geometry::distance(*it1,*it2)<<std::endl;
    }
    }
    }
    catch(std::exceptionconst&e)
    {
    std::cerr<<"Error:"<<e.what()<<'\n';
    }
    return0;
    }