Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r56931 - sandbox/ggl/formal_review_request/libs/ggl/example
From: barend.gehrels_at_[hidden]
Date: 2009-10-16 15:56:54


Author: barendgehrels
Date: 2009-10-16 15:56:53 EDT (Fri, 16 Oct 2009)
New Revision: 56931
URL: http://svn.boost.org/trac/boost/changeset/56931

Log:
Added soci WKB samples
Added:
   sandbox/ggl/formal_review_request/libs/ggl/example/x03_d_soci_example.cpp (contents, props changed)
Text files modified:
   sandbox/ggl/formal_review_request/libs/ggl/example/Jamroot | 4 +-
   sandbox/ggl/formal_review_request/libs/ggl/example/soci_examples.sln | 6 ++++
   sandbox/ggl/formal_review_request/libs/ggl/example/x03_c_soci_example.cpp | 57 +++++++++++++++++++++++++++++++--------
   3 files changed, 53 insertions(+), 14 deletions(-)

Modified: sandbox/ggl/formal_review_request/libs/ggl/example/Jamroot
==============================================================================
--- sandbox/ggl/formal_review_request/libs/ggl/example/Jamroot (original)
+++ sandbox/ggl/formal_review_request/libs/ggl/example/Jamroot 2009-10-16 15:56:53 EDT (Fri, 16 Oct 2009)
@@ -20,7 +20,7 @@
 
 exe c01_custom_point_example : c01_custom_point_example.cpp ;
 exe c02_custom_box_example : c02_custom_box_example.cpp ;
+exe c03_custom_linestring_example : c03_custom_linestring_example.cpp ;
 exe c04_a_custom_triangle_example : c04_a_custom_triangle_example.cpp ;
 exe c04_b_custom_triangle_example : c04_b_custom_triangle_example.cpp ;
-exe c05_custom_point_pointer_example : c05_custom_point_pointer_example.cpp ;
-
+exe c05_custom_point_pointer_example`: c05_custom_point_pointer_example.cpp ;

Modified: sandbox/ggl/formal_review_request/libs/ggl/example/soci_examples.sln
==============================================================================
--- sandbox/ggl/formal_review_request/libs/ggl/example/soci_examples.sln (original)
+++ sandbox/ggl/formal_review_request/libs/ggl/example/soci_examples.sln 2009-10-16 15:56:53 EDT (Fri, 16 Oct 2009)
@@ -6,6 +6,8 @@
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "x03_c_soci_example", "x03_c_soci_example.vcproj", "{5EFD08FE-10CB-4D3E-9907-4E9A2F3AB1C1}"
 EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "x03_d_soci_example", "x03_d_soci_example.vcproj", "{5EFD08FE-10CB-4D3E-9917-4E9A2F3AB1C1}"
+EndProject
 Global
         GlobalSection(SolutionConfigurationPlatforms) = preSolution
                 Debug|Win32 = Debug|Win32
@@ -24,6 +26,10 @@
                 {5EFD08FE-10CB-4D3E-9907-4E9A2F3AB1C1}.Debug|Win32.Build.0 = Debug|Win32
                 {5EFD08FE-10CB-4D3E-9907-4E9A2F3AB1C1}.Release|Win32.ActiveCfg = Release|Win32
                 {5EFD08FE-10CB-4D3E-9907-4E9A2F3AB1C1}.Release|Win32.Build.0 = Release|Win32
+ {5EFD08FE-10CB-4D3E-9917-4E9A2F3AB1C1}.Debug|Win32.ActiveCfg = Debug|Win32
+ {5EFD08FE-10CB-4D3E-9917-4E9A2F3AB1C1}.Debug|Win32.Build.0 = Debug|Win32
+ {5EFD08FE-10CB-4D3E-9917-4E9A2F3AB1C1}.Release|Win32.ActiveCfg = Release|Win32
+ {5EFD08FE-10CB-4D3E-9917-4E9A2F3AB1C1}.Release|Win32.Build.0 = Release|Win32
         EndGlobalSection
         GlobalSection(SolutionProperties) = preSolution
                 HideSolutionNode = FALSE

Modified: sandbox/ggl/formal_review_request/libs/ggl/example/x03_c_soci_example.cpp
==============================================================================
--- sandbox/ggl/formal_review_request/libs/ggl/example/x03_c_soci_example.cpp (original)
+++ sandbox/ggl/formal_review_request/libs/ggl/example/x03_c_soci_example.cpp 2009-10-16 15:56:53 EDT (Fri, 16 Oct 2009)
@@ -1,6 +1,6 @@
 // Generic Geometry Library
 //
-// Copyright Mateusz Loskot 2009, mateusz @loskot.net
+// Copyright Mateusz Loskot 2009, mateusz_at_[hidden]
 //
 // Use, modification and distribution is subject to the Boost Software License,
 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
@@ -22,31 +22,48 @@
 #include <soci.h>
 #include <soci-postgresql.h>
 
-#include <iostream>
 #include <exception>
+#include <iostream>
+#include <iterator>
+#include <string>
+#include <vector>
 
 #include <ggl/ggl.hpp>
 #include <ggl/geometries/geometries.hpp>
 #include <ggl/extensions/gis/io/wkb/read_wkb.hpp>
 #include <ggl/extensions/gis/io/wkb/utility.hpp>
+#include <ggl/extensions/gis/io/wkt/wkt.hpp>
 
-
-
-struct road
+// user-defined type with GGL geometry
+struct tree
 {
+ int id;
+ ggl::point<float, 2, ggl::cs::geographic<ggl::degree> > location;
 };
 
+// conversion of row of result to user-defined type - performs WKB parsing
 namespace soci
 {
     template <>
- struct type_conversion<road>
+ struct type_conversion<tree>
     {
         typedef soci::values base_type;
 
- static void from_base(const base_type& v, soci::indicator ind, road& value)
+ static void from_base(base_type const& v, soci::indicator ind, tree& value)
         {
             try
             {
+ value.id = v.get<int>("id");
+
+ // intermediate step: hex-encoded binary string to raw WKB
+ std::string const& hex = v.get<std::string>("wkb");
+ std::vector<unsigned char> wkb;
+ if (!ggl::hex2wkb(hex, std::back_inserter(wkb)))
+ throw std::runtime_error("hex2wkb translation failed");
+
+ // parse WKB and construct point geometry
+ if (!ggl::read_wkb(wkb.begin(), wkb.end(), value.location))
+ throw std::runtime_error("read_wkb failed");
             }
             catch(const std::exception& e)
             {
@@ -54,8 +71,9 @@
             }
         }
 
- static void to_base(const road& value, base_type& v, soci::indicator& ind)
+ static void to_base(tree const& value, base_type& v, soci::indicator& ind)
         {
+ throw std::runtime_error("todo: wkb writer not yet implemented");
         }
     };
 }
@@ -64,17 +82,32 @@
 {
     try
     {
+ // establish database connection
         soci::session sql(soci::postgresql, "dbname=ggl user=ggl password=ggl");
 
+ // construct schema of table for trees (point geometries)
+ sql << "DELETE FROM geometry_columns WHERE f_table_name = 'trees'";
+ sql << "DROP TABLE IF EXISTS trees CASCADE";
+ sql << "CREATE TABLE trees (id INTEGER)";
+ sql << "SELECT AddGeometryColumn('trees', 'geom', -1, 'POINT', 2)";
 
         // insert sample data using plain WKT input
+ sql << "INSERT INTO trees VALUES(1, ST_GeomFromText('POINT(1.23 2.34)', -1))";
+ sql << "INSERT INTO trees VALUES(2, ST_GeomFromText('POINT(3.45 4.56)', -1))";
+ sql << "INSERT INTO trees VALUES(3, ST_GeomFromText('POINT(5.67 6.78)', -1))";
+ sql << "INSERT INTO trees VALUES(4, ST_GeomFromText('POINT(7.89 9.01)', -1))";
 
         // query data in WKB form and read to geometry object
+ typedef std::vector<tree> trees_t;
+ soci::rowset<tree> rows = (sql.prepare << "SELECT id, encode(ST_AsBinary(geom), 'hex') AS wkb FROM trees");
+ trees_t trees;
+ std::copy(rows.begin(), rows.end(), std::back_inserter(trees));
 
- // output geometry object to WKT
-
- // print input and result WKT texts
-
+ // print trees output
+ for (trees_t::const_iterator it = trees.begin(); it != trees.end(); ++it)
+ {
+ std::cout << "Tree #" << it->id << " located at\t" << ggl::wkt(it->location) << std::endl;
+ }
     }
     catch (std::exception const &e)
     {

Added: sandbox/ggl/formal_review_request/libs/ggl/example/x03_d_soci_example.cpp
==============================================================================
--- (empty file)
+++ sandbox/ggl/formal_review_request/libs/ggl/example/x03_d_soci_example.cpp 2009-10-16 15:56:53 EDT (Fri, 16 Oct 2009)
@@ -0,0 +1,84 @@
+// Generic Geometry Library
+//
+// Copyright Mateusz Loskot 2009, mateusz_at_[hidden]
+//
+// Use, modification and distribution is subject to the Boost Software License,
+// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+// Generic Geometry Library
+// SOCI example
+
+// d: using WKB to retrieve geometries
+
+// SOCI is a generic C++ template interface to access relational databases
+
+// To build and run this example, see comments in example a
+// Alternatively compile composing and executing compiler command directoy in examples directory,
+// for example using GCC compiler:
+// g++ -I../../../boost -I/home/mloskot/usr/include/soci \
+// -I /home/mloskot/usr/include/soci/postgresql -I/usr/include/postgresql \
+// -L/home/mloskot/usr/lib -lsoci_core-gcc-3_0 -lsoci_postgresql-gcc-3_0 x03_c_soci_example.cpp
+
+#include <soci.h>
+#include <soci-postgresql.h>
+
+#include <exception>
+#include <iostream>
+#include <iterator>
+#include <string>
+#include <vector>
+
+#include <ggl/ggl.hpp>
+#include <ggl/algorithms/area.hpp>
+#include <ggl/geometries/cartesian2d.hpp>
+#include <ggl/geometries/geometries.hpp>
+#include <ggl/extensions/gis/io/wkb/read_wkb.hpp>
+#include <ggl/extensions/gis/io/wkb/utility.hpp>
+#include <ggl/extensions/gis/io/wkt/wkt.hpp>
+
+int main()
+{
+ try
+ {
+ // establish database connection
+ soci::session sql(soci::postgresql, "dbname=ggl user=ggl password=ggl");
+
+ // construct schema of table for trees (point geometries)
+ sql << "DELETE FROM geometry_columns WHERE f_table_name = 'parcels'";
+ sql << "DROP TABLE IF EXISTS parcels CASCADE";
+ sql << "CREATE TABLE parcels (id INTEGER)";
+ sql << "SELECT AddGeometryColumn('parcels', 'geom', -1, 'GEOMETRY', 2)";
+
+ // insert sample data using plain WKT input
+ sql << "INSERT INTO parcels VALUES(1, ST_GeomFromText('POLYGON ((10 10, 10 20, 20 20, 20 15, 10 10))', -1))";
+ sql << "INSERT INTO parcels VALUES(2, ST_GeomFromText('POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))', -1))";
+ sql << "INSERT INTO parcels VALUES(3, ST_GeomFromText('POLYGON((1 1,2 1,2 2,1 2,1 1))', -1))";
+
+ // query data in WKB form and read to geometry object
+ soci::rowset<std::string> rows = (sql.prepare << "SELECT encode(ST_AsBinary(geom), 'hex') AS wkb FROM parcels");
+
+ // calculate area of each parcel
+ for (soci::rowset<std::string>::iterator it = rows.begin(); it != rows.end(); ++it)
+ {
+ // parse WKB and construct geometry object
+ std::string const& hex = *it;
+ std::vector<unsigned char> wkb;
+ if (!ggl::hex2wkb(*it, std::back_inserter(wkb)))
+ throw std::runtime_error("hex2wkb translation failed");
+
+ ggl::polygon_2d parcel;
+ if (!ggl::read_wkb(wkb.begin(), wkb.end(), parcel))
+ throw std::runtime_error("read_wkb failed");
+
+ double a = ggl::area(parcel);
+ std::cout << "Parcel geometry: " << ggl::wkt(parcel) << std::endl
+ << "\thas area is " << a << " in coordinate units" << std::endl;
+ }
+ }
+ catch (std::exception const &e)
+ {
+ std::cerr << "Error: " << e.what() << '\n';
+ }
+ return 0;
+}
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk