mirror of
https://github.com/boostorg/geometry.git
synced 2026-02-13 12:32:09 +00:00
182 lines
9.3 KiB
HTML
182 lines
9.3 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>Boost.Geometry (aka GGL, Generic Geometry Library)</title>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css">
|
|
<link href="tabs.css" rel="stylesheet" type="text/css">
|
|
</head>
|
|
|
|
<table cellpadding="2" width="100%">
|
|
<tbody>
|
|
<tr>
|
|
<td valign="top">
|
|
<img alt="Boost.Geometry" src="images/ggl-logo-big.png" height="80" width="200">
|
|
|
|
</td>
|
|
<td valign="top" align="right">
|
|
<a href="http://www.boost.org">
|
|
<img alt="Boost C++ Libraries" src="images/accepted_by_boost.png" height="80" width="230" border="0">
|
|
</a>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<!-- Generated by Doxygen 1.5.9 -->
|
|
<div class="navigation" id="top">
|
|
<div class="tabs">
|
|
<ul>
|
|
<li><a href="index.html"><span>Main Page</span></a></li>
|
|
<li><a href="pages.html"><span>Related Pages</span></a></li>
|
|
<li><a href="modules.html"><span>Modules</span></a></li>
|
|
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
|
|
<li><a href="annotated.html"><span>Classes</span></a></li>
|
|
<li><a href="files.html"><span>Files</span></a></li>
|
|
<li><a href="examples.html"><span>Examples</span></a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="contents">
|
|
<h1>x03_c_soci_example.cpp</h1>Example showing how to get polygons from PostGIS using SOCI and use them in GGL through WKB<p>
|
|
<div class="fragment"><pre class="fragment"><span class="comment">// Boost.Geometry (aka GGL, Generic Geometry Library)</span>
|
|
<span class="comment">//</span>
|
|
<span class="comment">// Copyright Mateusz Loskot 2009, mateusz@loskot.net</span>
|
|
<span class="comment">//</span>
|
|
<span class="comment">// Use, modification and distribution is subject to the Boost Software License,</span>
|
|
<span class="comment">// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at</span>
|
|
<span class="comment">// http://www.boost.org/LICENSE_1_0.txt)</span>
|
|
<span class="comment">// Boost.Geometry (aka GGL, Generic Geometry Library)</span>
|
|
<span class="comment">// SOCI example</span>
|
|
|
|
<span class="comment">// c: using WKB to retrieve geometries</span>
|
|
|
|
<span class="comment">// SOCI is a generic C++ template interface to access relational databases</span>
|
|
|
|
<span class="comment">// To build and run this example, see comments in example a</span>
|
|
<span class="comment">// Alternatively compile composing and executing compiler command directoy in examples directory,</span>
|
|
<span class="comment">// for example using GCC compiler:</span>
|
|
<span class="comment">// g++ -I../../../boost -I/home/mloskot/usr/include/soci \</span>
|
|
<span class="comment">// -I /home/mloskot/usr/include/soci/postgresql -I/usr/include/postgresql \</span>
|
|
<span class="comment">// -L/home/mloskot/usr/lib -lsoci_core-gcc-3_0 -lsoci_postgresql-gcc-3_0 x03_c_soci_example.cpp</span>
|
|
|
|
<span class="preprocessor">#include <soci.h></span>
|
|
<span class="preprocessor">#include <soci-postgresql.h></span>
|
|
|
|
<span class="preprocessor">#include <exception></span>
|
|
<span class="preprocessor">#include <iostream></span>
|
|
<span class="preprocessor">#include <iterator></span>
|
|
<span class="preprocessor">#include <string></span>
|
|
<span class="preprocessor">#include <vector></span>
|
|
|
|
<span class="preprocessor">#include <boost/geometry/geometry.hpp></span>
|
|
<span class="preprocessor">#include <<a class="code" href="geometries_8hpp.html">boost/geometry/geometries/geometries.hpp</a>></span>
|
|
<span class="preprocessor">#include <boost/geometry/extensions/gis/io/wkb/read_wkb.hpp></span>
|
|
<span class="preprocessor">#include <boost/geometry/extensions/gis/io/wkb/utility.hpp></span>
|
|
<span class="preprocessor">#include <boost/geometry/extensions/gis/io/wkt/wkt.hpp></span>
|
|
|
|
<span class="comment">// user-defined type with GGL geometry</span>
|
|
<span class="keyword">struct </span>tree
|
|
{
|
|
<span class="keywordtype">int</span> id;
|
|
<a name="_a0"></a><a class="code" href="classboost_1_1geometry_1_1point.html" title="Basic point class, having coordinates defined in a neutral way.">boost::geometry::point<float, 2, boost::geometry::cs::geographic<boost::geometry::degree></a> > location;
|
|
};
|
|
|
|
<span class="comment">// conversion of row of result to user-defined type - performs WKB parsing</span>
|
|
<span class="keyword">namespace </span>soci
|
|
{
|
|
<span class="keyword">template</span> <>
|
|
<span class="keyword">struct </span>type_conversion<tree>
|
|
{
|
|
<span class="keyword">typedef</span> soci::values base_type;
|
|
|
|
<span class="keyword">static</span> <span class="keywordtype">void</span> from_base(base_type <span class="keyword">const</span>& v, soci::indicator ind, tree& value)
|
|
{
|
|
<span class="keywordflow">try</span>
|
|
{
|
|
value.id = v.get<<span class="keywordtype">int</span>>(<span class="stringliteral">"id"</span>);
|
|
|
|
<span class="comment">// intermediate step: hex-encoded binary string to raw WKB</span>
|
|
std::string <span class="keyword">const</span>& hex = v.get<std::string>(<span class="stringliteral">"wkb"</span>);
|
|
std::vector<unsigned char> wkb;
|
|
<span class="keywordflow">if</span> (!boost::geometry::hex2wkb(hex, std::back_inserter(wkb)))
|
|
<span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"hex2wkb translation failed"</span>);
|
|
|
|
<span class="comment">// parse WKB and construct point geometry</span>
|
|
<span class="keywordflow">if</span> (!boost::geometry::read_wkb(wkb.begin(), wkb.end(), value.location))
|
|
<span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"read_wkb failed"</span>);
|
|
}
|
|
<span class="keywordflow">catch</span>(<span class="keyword">const</span> std::exception& e)
|
|
{
|
|
std::cout << e.what() << std::endl;
|
|
}
|
|
}
|
|
|
|
<span class="keyword">static</span> <span class="keywordtype">void</span> to_base(tree <span class="keyword">const</span>& value, base_type& v, soci::indicator& ind)
|
|
{
|
|
<span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"todo: wkb writer not yet implemented"</span>);
|
|
}
|
|
};
|
|
}
|
|
|
|
<span class="keywordtype">int</span> main()
|
|
{
|
|
<span class="keywordflow">try</span>
|
|
{
|
|
<span class="comment">// establish database connection</span>
|
|
soci::session sql(soci::postgresql, <span class="stringliteral">"dbname=ggl user=ggl password=ggl"</span>);
|
|
|
|
<span class="comment">// construct schema of table for trees (point geometries)</span>
|
|
sql << <span class="stringliteral">"DELETE FROM geometry_columns WHERE f_table_name = 'trees'"</span>;
|
|
sql << <span class="stringliteral">"DROP TABLE IF EXISTS trees CASCADE"</span>;
|
|
sql << <span class="stringliteral">"CREATE TABLE trees (id INTEGER)"</span>;
|
|
sql << <span class="stringliteral">"SELECT AddGeometryColumn('trees', 'geom', -1, 'POINT', 2)"</span>;
|
|
|
|
<span class="comment">// insert sample data using plain WKT input</span>
|
|
sql << <span class="stringliteral">"INSERT INTO trees VALUES(1, ST_GeomFromText('POINT(1.23 2.34)', -1))"</span>;
|
|
sql << <span class="stringliteral">"INSERT INTO trees VALUES(2, ST_GeomFromText('POINT(3.45 4.56)', -1))"</span>;
|
|
sql << <span class="stringliteral">"INSERT INTO trees VALUES(3, ST_GeomFromText('POINT(5.67 6.78)', -1))"</span>;
|
|
sql << <span class="stringliteral">"INSERT INTO trees VALUES(4, ST_GeomFromText('POINT(7.89 9.01)', -1))"</span>;
|
|
|
|
<span class="comment">// query data in WKB form and read to geometry object</span>
|
|
<span class="keyword">typedef</span> std::vector<tree> trees_t;
|
|
soci::rowset<tree> rows = (sql.prepare << <span class="stringliteral">"SELECT id, encode(ST_AsBinary(geom), 'hex') AS wkb FROM trees"</span>);
|
|
trees_t trees;
|
|
std::copy(rows.begin(), rows.end(), std::back_inserter(trees));
|
|
|
|
<span class="comment">// print trees output</span>
|
|
<span class="keywordflow">for</span> (trees_t::const_iterator it = trees.begin(); it != trees.end(); ++it)
|
|
{
|
|
std::cout << <span class="stringliteral">"Tree #"</span> << it->id << <span class="stringliteral">" located at\t"</span> << boost::geometry::wkt(it->location) << std::endl;
|
|
}
|
|
}
|
|
<span class="keywordflow">catch</span> (std::exception <span class="keyword">const</span> &e)
|
|
{
|
|
std::cerr << <span class="stringliteral">"Error: "</span> << e.what() << <span class="charliteral">'\n'</span>;
|
|
}
|
|
<span class="keywordflow">return</span> 0;
|
|
}
|
|
|
|
</pre></div> </div>
|
|
<hr size="1">
|
|
<table width="100%">
|
|
<tbody>
|
|
<tr>
|
|
<td align="left"><small>
|
|
<p>December 1, 2009</p>
|
|
</small></td>
|
|
<td align="right">
|
|
<small>Copyright © 1995-2009 Barend Gehrels, Geodan, Amsterdam<br>
|
|
Copyright © 2008-2009 Bruno Lalande, Paris<br>
|
|
Copyright © 2009 Mateusz Loskot, Cadcorp, London<br>
|
|
</small>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<address style="text-align: right;"><small>
|
|
Documentation is generated by <a href="http://www.doxygen.org/index.html">Doxygen</a>
|
|
</small></address>
|
|
</body>
|
|
</html>
|