x03_d_soci_example.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. //
  3. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  4. //
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // Boost.Geometry (aka GGL, Generic Geometry Library)
  9. // SOCI example
  10. // d: using WKB to retrieve geometries
  11. // SOCI is a generic C++ template interface to access relational databases
  12. // To build and run this example, see comments in example a
  13. // Alternatively compile composing and executing compiler command directoy in examples directory,
  14. // for example using GCC compiler:
  15. // g++ -I../../../boost -I/home/mloskot/usr/include/soci \
  16. // -I /home/mloskot/usr/include/soci/postgresql -I/usr/include/postgresql \
  17. // -L/home/mloskot/usr/lib -lsoci_core-gcc-3_0 -lsoci_postgresql-gcc-3_0 x03_c_soci_example.cpp
  18. #include <soci.h>
  19. #include <soci-postgresql.h>
  20. #include <exception>
  21. #include <iostream>
  22. #include <iterator>
  23. #include <string>
  24. #include <vector>
  25. #include <boost/geometry.hpp>
  26. #include <boost/geometry/geometries/geometries.hpp>
  27. #include <boost/geometry/geometries/point_xy.hpp>
  28. #include <boost/geometry/extensions/gis/io/wkb/read_wkb.hpp>
  29. #include <boost/geometry/extensions/gis/io/wkb/utility.hpp>
  30. int main()
  31. {
  32. try
  33. {
  34. // establish database connection
  35. soci::session sql(soci::postgresql, "dbname=ggl user=ggl password=ggl");
  36. // construct schema of table for trees (point geometries)
  37. sql << "DELETE FROM geometry_columns WHERE f_table_name = 'parcels'";
  38. sql << "DROP TABLE IF EXISTS parcels CASCADE";
  39. sql << "CREATE TABLE parcels (id INTEGER)";
  40. sql << "SELECT AddGeometryColumn('parcels', 'geom', -1, 'GEOMETRY', 2)";
  41. // insert sample data using plain WKT input
  42. sql << "INSERT INTO parcels VALUES(1, ST_GeomFromText('POLYGON ((10 10, 10 20, 20 20, 20 15, 10 10))', -1))";
  43. sql << "INSERT INTO parcels VALUES(2, ST_GeomFromText('POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))', -1))";
  44. sql << "INSERT INTO parcels VALUES(3, ST_GeomFromText('POLYGON((1 1,2 1,2 2,1 2,1 1))', -1))";
  45. // query data in WKB form and read to geometry object
  46. soci::rowset<std::string> rows = (sql.prepare << "SELECT encode(ST_AsBinary(geom), 'hex') AS wkb FROM parcels");
  47. // calculate area of each parcel
  48. for (soci::rowset<std::string>::iterator it = rows.begin(); it != rows.end(); ++it)
  49. {
  50. // parse WKB and construct geometry object
  51. std::string const& hex = *it;
  52. std::vector<unsigned char> wkb;
  53. if (!boost::geometry::hex2wkb(*it, std::back_inserter(wkb)))
  54. throw std::runtime_error("hex2wkb translation failed");
  55. boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > parcel;
  56. if (!boost::geometry::read_wkb(wkb.begin(), wkb.end(), parcel))
  57. throw std::runtime_error("read_wkb failed");
  58. double a = boost::geometry::area(parcel);
  59. std::cout << "Parcel geometry: " << boost::geometry::wkt(parcel) << std::endl
  60. << "\thas area is " << a << " in coordinate units" << std::endl;
  61. }
  62. }
  63. catch (std::exception const &e)
  64. {
  65. std::cerr << "Error: " << e.what() << '\n';
  66. }
  67. return 0;
  68. }