x03_a_soci_example.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. //
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // SOCI example
  8. // a: using boost::tuple to retrieve points
  9. // SOCI is a generic C++ template interface to access relational databases
  10. // To build and run this example:
  11. // 1) download SOCI from http://soci.sourceforge.net/
  12. // 2) put it in contrib/soci-3.0.0 (or another version/folder, but then update this VCPROJ)
  13. // 3) adapt your makefile or use this VCPROJ file
  14. // (note that SOCI sources are included directly, building SOCI is not necessary)
  15. // 4) load the demo-data, see script data/cities.sql (for PostgreSQL)
  16. #include <soci.h>
  17. #include <soci-postgresql.h>
  18. #include <boost/algorithm/string.hpp>
  19. #include <boost/optional.hpp>
  20. #include <boost/timer.hpp>
  21. #include <boost/random.hpp>
  22. #include <boost/tuple/tuple.hpp>
  23. #include <iostream>
  24. #include <istream>
  25. #include <ostream>
  26. #include <sstream>
  27. #include <string>
  28. #include <exception>
  29. #include <boost/geometry/geometry.hpp>
  30. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  31. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian);
  32. int main()
  33. {
  34. try
  35. {
  36. soci::session sql(soci::postgresql, "dbname=ggl user=ggl password=ggl");
  37. int count;
  38. sql << "select count(*) from cities", soci::into(count);
  39. std::cout << "# Capitals: " << count << std::endl;
  40. typedef std::vector<boost::tuple<double, double> > V;
  41. soci::rowset<boost::tuple<double, double> > rows
  42. = sql.prepare << "select x(location),y(location) from cities";
  43. V vec;
  44. std::copy(rows.begin(), rows.end(), std::back_inserter(vec));
  45. for (V::const_iterator it = vec.begin(); it != vec.end(); ++it)
  46. {
  47. std::cout << it->get<0>() << " " << it->get<1>() << std::endl;
  48. }
  49. // Calculate distances
  50. for (V::const_iterator it1 = vec.begin(); it1 != vec.end(); ++it1)
  51. {
  52. for (V::const_iterator it2 = vec.begin(); it2 != vec.end(); ++it2)
  53. {
  54. std::cout << boost::geometry::dsv(*it1) << " " << boost::geometry::distance(*it1, *it2) << std::endl;
  55. }
  56. }
  57. }
  58. catch (std::exception const &e)
  59. {
  60. std::cerr << "Error: " << e.what() << '\n';
  61. }
  62. return 0;
  63. }