Files
geometry/doc/doxygen_output/html/x03__d__soci__example_8cpp-example.html
Barend Gehrels 363580fbf6 Added old doxygen docs
[SVN r59777]
2010-02-20 15:57:12 +00:00

148 lines
8.0 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">
&nbsp;&nbsp;
</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&nbsp;Page</span></a></li>
<li><a href="pages.html"><span>Related&nbsp;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_d_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">// d: 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 &lt;soci.h&gt;</span>
<span class="preprocessor">#include &lt;soci-postgresql.h&gt;</span>
<span class="preprocessor">#include &lt;exception&gt;</span>
<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;iterator&gt;</span>
<span class="preprocessor">#include &lt;string&gt;</span>
<span class="preprocessor">#include &lt;vector&gt;</span>
<span class="preprocessor">#include &lt;boost/geometry/geometry.hpp&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="algorithms_2area_8hpp.html">boost/geometry/algorithms/area.hpp</a>&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="cartesian2d_8hpp.html">boost/geometry/geometries/cartesian2d.hpp</a>&gt;</span>
<span class="preprocessor">#include &lt;<a class="code" href="geometries_8hpp.html">boost/geometry/geometries/geometries.hpp</a>&gt;</span>
<span class="preprocessor">#include &lt;boost/geometry/extensions/gis/io/wkb/read_wkb.hpp&gt;</span>
<span class="preprocessor">#include &lt;boost/geometry/extensions/gis/io/wkb/utility.hpp&gt;</span>
<span class="preprocessor">#include &lt;boost/geometry/extensions/gis/io/wkt/wkt.hpp&gt;</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 &lt;&lt; <span class="stringliteral">"DELETE FROM geometry_columns WHERE f_table_name = 'parcels'"</span>;
sql &lt;&lt; <span class="stringliteral">"DROP TABLE IF EXISTS parcels CASCADE"</span>;
sql &lt;&lt; <span class="stringliteral">"CREATE TABLE parcels (id INTEGER)"</span>;
sql &lt;&lt; <span class="stringliteral">"SELECT AddGeometryColumn('parcels', 'geom', -1, 'GEOMETRY', 2)"</span>;
<span class="comment">// insert sample data using plain WKT input</span>
sql &lt;&lt; <span class="stringliteral">"INSERT INTO parcels VALUES(1, ST_GeomFromText('POLYGON ((10 10, 10 20, 20 20, 20 15, 10 10))', -1))"</span>;
sql &lt;&lt; <span class="stringliteral">"INSERT INTO parcels VALUES(2, ST_GeomFromText('POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))', -1))"</span>;
sql &lt;&lt; <span class="stringliteral">"INSERT INTO parcels VALUES(3, ST_GeomFromText('POLYGON((1 1,2 1,2 2,1 2,1 1))', -1))"</span>;
<span class="comment">// query data in WKB form and read to geometry object</span>
soci::rowset&lt;std::string&gt; rows = (sql.prepare &lt;&lt; <span class="stringliteral">"SELECT encode(ST_AsBinary(geom), 'hex') AS wkb FROM parcels"</span>);
<span class="comment">// calculate area of each parcel</span>
<span class="keywordflow">for</span> (soci::rowset&lt;std::string&gt;::iterator it = rows.begin(); it != rows.end(); ++it)
{
<span class="comment">// parse WKB and construct geometry object</span>
std::string <span class="keyword">const</span>&amp; hex = *it;
std::vector&lt;unsigned char&gt; wkb;
<span class="keywordflow">if</span> (!boost::geometry::hex2wkb(*it, std::back_inserter(wkb)))
<span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"hex2wkb translation failed"</span>);
<a name="_a0"></a><a class="code" href="classboost_1_1geometry_1_1polygon.html" title="The polygon contains an outer ring and zero or more inner rings.">boost::geometry::polygon_2d</a> parcel;
<span class="keywordflow">if</span> (!boost::geometry::read_wkb(wkb.begin(), wkb.end(), parcel))
<span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">"read_wkb failed"</span>);
<span class="keywordtype">double</span> a = <a name="a1"></a><a class="code" href="group__area.html#gd0e9e99685a9d45895162bd1fd96a136" title="Calculate area of a geometry.">boost::geometry::area</a>(parcel);
std::cout &lt;&lt; <span class="stringliteral">"Parcel geometry: "</span> &lt;&lt; boost::geometry::wkt(parcel) &lt;&lt; std::endl
&lt;&lt; <span class="stringliteral">"\thas area is "</span> &lt;&lt; a &lt;&lt; <span class="stringliteral">" in coordinate units"</span> &lt;&lt; std::endl;
}
}
<span class="keywordflow">catch</span> (std::exception <span class="keyword">const</span> &amp;e)
{
std::cerr &lt;&lt; <span class="stringliteral">"Error: "</span> &lt;&lt; e.what() &lt;&lt; <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&nbsp;<a href="http://www.doxygen.org/index.html">Doxygen</a>
</small></address>
</body>
</html>