diff --git a/example/05_a_overlay_polygon_example.cpp b/example/05_a_overlay_polygon_example.cpp index 40d7b4633..9d74b8273 100644 --- a/example/05_a_overlay_polygon_example.cpp +++ b/example/05_a_overlay_polygon_example.cpp @@ -21,7 +21,9 @@ #include #include -#include +#if defined(HAVE_SVG) +# include +#endif int main(void) @@ -32,8 +34,10 @@ int main(void) typedef bg::model::polygon polygon_2d; +#if defined(HAVE_SVG) std::ofstream stream("05_a_intersection_polygon_example.svg"); bg::svg_mapper svg(stream, 500, 500); +#endif // Define a polygons and fill the outer rings. polygon_2d a; @@ -45,7 +49,6 @@ int main(void) } bg::correct(a); std::cout << "A: " << bg::dsv(a) << std::endl; - svg.add(a); polygon_2d b; { @@ -56,10 +59,13 @@ int main(void) } bg::correct(b); std::cout << "B: " << bg::dsv(b) << std::endl; +#if defined(HAVE_SVG) + svg.add(a); svg.add(b); svg.map(a, "opacity:0.6;fill:rgb(0,255,0);"); svg.map(b, "opacity:0.6;fill:rgb(0,0,255);"); +#endif // Calculate interesection(s) @@ -70,7 +76,9 @@ int main(void) BOOST_FOREACH(polygon_2d const& polygon, intersection) { std::cout << bg::dsv(polygon) << std::endl; +#if defined(HAVE_SVG) svg.map(polygon, "opacity:0.5;fill:none;stroke:rgb(255,0,0);stroke-width:6"); +#endif } return 0; diff --git a/example/05_b_overlay_linestring_polygon_example.cpp b/example/05_b_overlay_linestring_polygon_example.cpp index 93cc98861..16eff548e 100644 --- a/example/05_b_overlay_linestring_polygon_example.cpp +++ b/example/05_b_overlay_linestring_polygon_example.cpp @@ -21,7 +21,9 @@ #include #include -#include +#if defined(HAVE_SVG) +# include +#endif int main(void) @@ -43,6 +45,7 @@ int main(void) } bg::correct(p); +#if defined(HAVE_SVG) // Create SVG-mapper std::ofstream stream("05_b_overlay_linestring_polygon_example.svg"); bg::svg_mapper svg(stream, 500, 500); @@ -52,7 +55,7 @@ int main(void) // Map geometries svg.map(ls, "opacity:0.6;stroke:rgb(255,0,0);stroke-width:2;"); svg.map(p, "opacity:0.6;fill:rgb(0,0,255);"); - +#endif // Calculate intersection points (turn points) typedef bg::detail::overlay::turn_info turn_info; @@ -76,8 +79,10 @@ int main(void) } std::cout << action << " polygon at " << bg::dsv(turn.point) << std::endl; +#if defined(HAVE_SVG) svg.map(turn.point, "fill:rgb(255,128,0);stroke:rgb(0,0,100);stroke-width:1"); svg.text(turn.point, action, "fill:rgb(0,0,0);font-family:Arial;font-size:10px"); +#endif } return 0; diff --git a/example/06_b_transformation_example.cpp b/example/06_b_transformation_example.cpp index f31c7fd13..0b6a0542b 100644 --- a/example/06_b_transformation_example.cpp +++ b/example/06_b_transformation_example.cpp @@ -19,9 +19,12 @@ #include #include #include -#include #include +#if defined(HAVE_SVG) +# include +#endif + #include #include #include @@ -84,8 +87,9 @@ struct svg_output void put(G const& g, std::string const& label) { std::string style_str(style.fill(opacity) + style.stroke(5, opacity)); - os << ::boost::geometry::svg(g, style_str) << std::endl; - +#if defined(HAVE_SVG) + os << boost::geometry::svg(g, style_str) << std::endl; +#endif if (!label.empty()) { typename point_type::type c; @@ -101,6 +105,7 @@ private: random_style style; }; + int main() { using namespace boost::geometry::strategy::transform; diff --git a/example/07_a_graph_route_example.cpp b/example/07_a_graph_route_example.cpp index a41575a25..4ea9c5ae8 100644 --- a/example/07_a_graph_route_example.cpp +++ b/example/07_a_graph_route_example.cpp @@ -30,10 +30,12 @@ // Yes, this example currently uses some extensions: // For output: - #include + #if defined(HAVE_SVG) + # include + #endif // For distance-calculations over the Earth: - #include + //#include @@ -260,10 +262,11 @@ inline void build_route(Graph const& graph, int main() { - // Define a point in the Geographic coordinate system + // Define a point in the Geographic coordinate system (currently Spherical) + // (geographic calculations are in an extension; for sample it makes no difference) typedef boost::geometry::model::point < - double, 2, boost::geometry::cs::geographic + double, 2, boost::geometry::cs::spherical > point_type; typedef boost::geometry::model::linestring line_type; @@ -302,8 +305,12 @@ int main() double const km = 1000.0; std::cout << "distances, all in KM" << std::endl << std::fixed << std::setprecision(0); + + // To calculate distance, declare and construct a strategy with average earth radius + boost::geometry::strategy::distance::haversine haversine(6372795.0); // Main functionality: calculate shortest routes from/to all cities + // For the first one, the complete route is stored as a linestring bool first = true; @@ -329,7 +336,7 @@ int main() if (! boost::equals(city1.get<1>(), city2.get<1>())) { double distance = costs[city2.get<2>()] / km; - double acof = boost::geometry::distance(city1.get<0>(), city2.get<0>()) / km; + double acof = boost::geometry::distance(city1.get<0>(), city2.get<0>(), haversine) / km; std::cout << std::setiosflags (std::ios_base::left) << std::setw(15) @@ -351,6 +358,7 @@ int main() } } +#if defined(HAVE_SVG) // Create the SVG std::ofstream stream("routes.svg"); boost::geometry::svg_mapper mapper(stream, 600, 600); @@ -378,6 +386,7 @@ int main() mapper.text(city.get<0>(), city.get<1>(), "fill:rgb(0,0,0);font-family:Arial;font-size:10px", 5, 5); } +#endif return 0; } diff --git a/example/07_b_graph_route_example.cpp b/example/07_b_graph_route_example.cpp index 7ab224433..64f4844c9 100644 --- a/example/07_b_graph_route_example.cpp +++ b/example/07_b_graph_route_example.cpp @@ -34,10 +34,12 @@ // Yes, this example currently uses some extensions: // For output: - #include + #if defined(HAVE_SVG) + # include + #endif // For distance-calculations over the Earth: - #include + //#include @@ -246,10 +248,11 @@ inline void build_route(Graph const& graph, int main() { - // Define a point in the Geographic coordinate system + // Define a point in the Geographic coordinate system (currently Spherical) + // (geographic calculations are in an extension; for sample it makes no difference) typedef boost::geometry::model::point < - double, 2, boost::geometry::cs::geographic + double, 2, boost::geometry::cs::spherical > point_type; typedef boost::geometry::model::linestring line_type; @@ -291,6 +294,9 @@ int main() std::cout << "distances, all in KM" << std::endl << std::fixed << std::setprecision(0); + // To calculate distance, declare and construct a strategy with average earth radius + boost::geometry::strategy::distance::haversine haversine(6372795.0); + // Main functionality: calculate shortest routes from/to all cities // For the first one, the complete route is stored as a linestring @@ -317,7 +323,7 @@ int main() if (! boost::equals(city1.get<1>(), city2.get<1>())) { double distance = costs[city2.get<2>()] / km; - double acof = boost::geometry::distance(city1.get<0>(), city2.get<0>()) / km; + double acof = boost::geometry::distance(city1.get<0>(), city2.get<0>(), haversine) / km; std::cout << std::setiosflags (std::ios_base::left) << std::setw(15) @@ -339,6 +345,7 @@ int main() } } +#if defined(HAVE_SVG) // Create the SVG std::ofstream stream("routes.svg"); boost::geometry::svg_mapper mapper(stream, 600, 600); @@ -366,6 +373,7 @@ int main() mapper.text(city.get<0>(), city.get<1>(), "fill:rgb(0,0,0);font-family:Arial;font-size:10px", 5, 5); } +#endif return 0; } diff --git a/example/Jamfile.v2 b/example/Jamfile.v2 index 141ee1537..8f12d888a 100644 --- a/example/Jamfile.v2 +++ b/example/Jamfile.v2 @@ -26,7 +26,7 @@ exe 07_b_graph_route_example : 07_b_graph_route_example.cpp ; 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 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 c06_custom_polygon_example : c06_custom_polygon_example.cpp ; diff --git a/example/c03_custom_linestring_example.cpp b/example/c03_custom_linestring_example.cpp deleted file mode 100644 index 118bbaced..000000000 --- a/example/c03_custom_linestring_example.cpp +++ /dev/null @@ -1,88 +0,0 @@ -// Boost.Geometry (aka GGL, Generic Geometry Library) - -// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands. -// Copyright (c) 2008-2011 Bruno Lalande, Paris, France. -// Copyright (c) 2009-2011 Mateusz Loskot, London, UK. - -// 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) -// -// Custom Linestring Example - -#include -#include -#include - -#include -#include -#include - -// To register the 'geographic' distance function to calculate distance over the earth: -#include -#include - -// Define a GPS point with coordinates in latitude/longitude and some additional values -struct gps_point -{ - double latitude, longitude, height; - double speed; - // Date/time, heading, etc could be added - - // The default constructor is required if being used in a vector - gps_point() {} - - // Define a constructor to create the point in one line. Order of latitude/longitude - // does not matter as long as "E", "N", etc are included - gps_point(std::string const& c1, std::string const& c2, double h, double s) - : height(h) - , speed(s) - { - boost::geometry::parse(*this, c1, c2); - } -}; - -// Declare a custom linestring which will have the GPS points -struct gps_track : std::vector -{ - std::string owner; - int route_identifier; - // etc - - gps_track(int i, std::string const& o) - : owner(o) - , route_identifier(i) - {} -}; - - -// Register this point as being a recognizable point by Boost.Geometry -BOOST_GEOMETRY_REGISTER_POINT_2D(gps_point, double, cs::geographic, longitude, latitude) - - -// Register the track as well, as being a "linestring" -BOOST_GEOMETRY_REGISTER_LINESTRING(gps_track) - - -int main() -{ - // Declare a "GPS Track" and add some GPS points - gps_track track(23, "Mister G"); - track.push_back(gps_point("52 22 23 N", "4 53 32 E", 50, 180)); - track.push_back(gps_point("52 10 00 N", "4 59 59 E", 110, 170)); - track.push_back(gps_point("52 5 20 N", "5 6 56 E", 0, 90)); - - std::cout - << "track: " << track.route_identifier << std::endl - << "from: " << track.owner << std::endl - << "as wkt: " << boost::geometry::dsv(track) << std::endl - << "length: " << boost::geometry::length(track)/1000.0 << " km" << std::endl; - - // Above gives the idea, shows that custom linestrings can be useful. - // We could of course do anything with this track which the library can handle, e.g.: - // - simplify it - // - calculate distance of point-to-line - // - project it to UTM, then transform it to a GIF image (see p03_example) - - return 0; -} diff --git a/example/with_external_libs/x02_gd_example.cpp b/example/with_external_libs/x02_gd_example.cpp deleted file mode 100644 index e51e6d09f..000000000 --- a/example/with_external_libs/x02_gd_example.cpp +++ /dev/null @@ -1,146 +0,0 @@ -// Boost.Geometry (aka GGL, Generic Geometry Library) -// -// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands. -// 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) -// -// GD example - -// GD is a well-known and often used graphic library to write GIF (and other formats) - -// To build and run this example: -// 1) download GD from http://www.libgd.org (currently gd-2.0.35 is assumed) -// 2) add 11 files -// gd.c, gd_gd.c, gd_gif_out.c, gd_io*.c, gd_security.c, gd_topal.c, gdhelpers.c, gdtables.c -// to the project or makefile or jamfile -// 3) for windows, add define NONDLL to indicate GD is not used as a DLL -// (Note that steps 2 and 3 are done in the MSVC gd_example project file and property sheets) - -#include -#include -#include - -#include -#include - - -#include - -#include -#include -#include -#include -#include - -#include - - -#include - -namespace bg = boost::geometry; - - -// ---------------------------------------------------------------------------- -// Read an ASCII file containing WKT's -// (note this function is shared by various examples) -// ---------------------------------------------------------------------------- -template -inline void read_wkt(std::string const& filename, std::vector& geometries) -{ - std::ifstream cpp_file(filename.c_str()); - if (cpp_file.is_open()) - { - while (! cpp_file.eof() ) - { - std::string line; - std::getline(cpp_file, line); - if (! line.empty()) - { - Geometry geometry; - bg::read_wkt(line, geometry); - geometries.push_back(geometry); - } - } - } -} - - -int main() -{ - // Adapt if necessary - std::string filename = "../data/world.wkt"; - - - // The world is measured in latlong (degrees), so latlong is appropriate. - // We ignore holes in this sample (below) - typedef bg::model::ll::point point_type; - typedef bg::model::polygon polygon_type; - typedef bg::model::multi_polygon country_type; - - std::vector countries; - - // Read (for example) world countries - read_wkt(filename, countries); - - - // Create a GD image. - // A world map, as world.shp, is usually mapped in latitude-longitude (-180..180 and -90..90) - // For this example we use a very simple "transformation" - // mapping to 0..720 and 0..360 - const double factor = 2.0; - gdImagePtr im = gdImageCreateTrueColor(int(360 * factor), int(180 * factor)); - - // Allocate three colors - int blue = gdImageColorResolve(im, 0, 52, 255); - int green = gdImageColorResolve(im, 0, 255, 0); - int black = gdImageColorResolve(im, 0, 0, 0); - - // Paint background in blue - gdImageFilledRectangle(im, 0, 0, im->sx, im->sy, blue); - - // Paint all countries in green - BOOST_FOREACH(country_type const& country, countries) - { - BOOST_FOREACH(polygon_type const& polygon, country) - { - // Ignore holes, so take only exterior ring - bg::model::ring const& ring = bg::exterior_ring(polygon); - - // If wished, suppress too small polygons. - // (Note that even in latlong, area is calculated in square meters) - double const a = bg::area(ring); - if (std::fabs(a) > 5000.0e6) - { - int const n = ring.size(); - gdPoint* points = new gdPoint[n]; - - for (int i = 0; i < n; i++) - { - // Translate lon/lat or x/y to GD x/y points - points[i].x = int(factor * (bg::get<0>(ring[i]) + 180.0)); - points[i].y = im->sy - int(factor * (bg::get<1>(ring[i]) + 90.0)); - } - - // Draw the polygon... - gdImageFilledPolygon(im, points, n, green); - // .. and the outline in black... - gdImagePolygon(im, points, n, black); - - delete[] points; - } - } - } - - // Use GD to create a GIF file - std::FILE* out = std::fopen("world.gif", "wb"); - if (out != NULL) - { - gdImageGif(im, out); - std::fclose(out); - } - - gdImageDestroy(im); - - return 0; -} diff --git a/example/with_external_libs/x03_c_soci_example.cpp b/example/with_external_libs/x03_c_soci_example.cpp deleted file mode 100644 index d2310ba75..000000000 --- a/example/with_external_libs/x03_c_soci_example.cpp +++ /dev/null @@ -1,118 +0,0 @@ -// Boost.Geometry (aka GGL, Generic Geometry Library) -// -// Copyright (c) 2009-2011 Mateusz Loskot, London, UK. -// -// 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) -// Boost.Geometry (aka GGL, Generic Geometry Library) -// SOCI example - -// c: 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 -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -// user-defined type with GGL geometry -struct tree -{ - int id; - boost::geometry::model::point > location; -}; - -// conversion of row of result to user-defined type - performs WKB parsing -namespace soci -{ - template <> - struct type_conversion - { - typedef soci::values base_type; - - static void from_base(base_type const& v, soci::indicator ind, tree& value) - { - try - { - value.id = v.get("id"); - - // intermediate step: hex-encoded binary string to raw WKB - std::string const& hex = v.get("wkb"); - std::vector wkb; - if (!boost::geometry::hex2wkb(hex, std::back_inserter(wkb))) - throw std::runtime_error("hex2wkb translation failed"); - - // parse WKB and construct point geometry - if (!boost::geometry::read_wkb(wkb.begin(), wkb.end(), value.location)) - throw std::runtime_error("read_wkb failed"); - } - catch(const std::exception& e) - { - std::cout << e.what() << std::endl; - } - } - - static void to_base(tree const& value, base_type& v, soci::indicator& ind) - { - throw std::runtime_error("todo: wkb writer not yet implemented"); - } - }; -} - -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 = '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 trees_t; - soci::rowset 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)); - - // print trees output - for (trees_t::const_iterator it = trees.begin(); it != trees.end(); ++it) - { - std::cout << "Tree #" << it->id << " located at\t" << boost::geometry::wkt(it->location) << std::endl; - } - } - catch (std::exception const &e) - { - std::cerr << "Error: " << e.what() << '\n'; - } - return 0; -} - diff --git a/example/with_external_libs/x03_d_soci_example.cpp b/example/with_external_libs/x03_d_soci_example.cpp deleted file mode 100644 index 448bc3658..000000000 --- a/example/with_external_libs/x03_d_soci_example.cpp +++ /dev/null @@ -1,83 +0,0 @@ -// Boost.Geometry (aka GGL, Generic Geometry Library) -// -// Copyright (c) 2009-2011 Mateusz Loskot, London, UK. -// -// 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) -// Boost.Geometry (aka GGL, 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 -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -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 rows = (sql.prepare << "SELECT encode(ST_AsBinary(geom), 'hex') AS wkb FROM parcels"); - - // calculate area of each parcel - for (soci::rowset::iterator it = rows.begin(); it != rows.end(); ++it) - { - // parse WKB and construct geometry object - std::string const& hex = *it; - std::vector wkb; - if (!boost::geometry::hex2wkb(*it, std::back_inserter(wkb))) - throw std::runtime_error("hex2wkb translation failed"); - - boost::geometry::model::d2::polygon parcel; - if (!boost::geometry::read_wkb(wkb.begin(), wkb.end(), parcel)) - throw std::runtime_error("read_wkb failed"); - - double a = boost::geometry::area(parcel); - std::cout << "Parcel geometry: " << boost::geometry::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; -} -