File: 05_b_overlay_linestring_polygon_example.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (97 lines) | stat: -rw-r--r-- 3,196 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Boost.Geometry (aka GGL, Generic Geometry Library)

// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
// Copyright (c) 2009-2012 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)
//
// Linestring Polygon Overlay Example

// NOTE: this example is obsolete. Boost.Geometry can now
// overlay linestrings/polygons.
// This sample will be removed in next version.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/c_array.hpp>

#if defined(HAVE_SVG)
#  include <boost/geometry/io/svg/svg_mapper.hpp>
#endif

BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian);


int main(void)
{
    namespace bg = boost::geometry;

    using point_2d = bg::model::d2::point_xy<double>;

    bg::model::linestring<point_2d> ls;
    {
        const double c[][2] = { {0, 1}, {2, 5}, {5, 3} };
        bg::assign_points(ls, c);
    }

    bg::model::polygon<point_2d> p;
    {
        const double c[][2] = { {3, 0}, {0, 3}, {4, 5}, {3, 0} };
        bg::assign_points(p, c);
    }
    bg::correct(p);

#if defined(HAVE_SVG)
    // Create SVG-mapper
    std::ofstream stream("05_b_overlay_linestring_polygon_example.svg");
    bg::svg_mapper<point_2d> svg(stream, 500, 500);
    // Determine extend by adding geometries
    svg.add(p);
    svg.add(ls);
    // 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)
    using segment_ratio = bg::segment_ratio_type<point_2d>::type;
    using turn_info = bg::detail::overlay::turn_info<point_2d, segment_ratio>;
    std::vector<turn_info> turns;
    bg::detail::get_turns::no_interrupt_policy policy;
    bg::strategies::relate::services::default_strategy<bg::model::linestring<point_2d>, bg::model::polygon<point_2d>>::type strategy;
    bg::get_turns<false, false, bg::detail::overlay::assign_null_policy>(ls, p, strategy, turns, policy);

    std::cout << "Intersection of linestring/polygon" << std::endl;
    for (turn_info const& turn : turns)
    {
        std::string action = "intersecting";
        if (turn.operations[0].operation
                == bg::detail::overlay::operation_intersection)
        {
            action = "entering";
        }
        else if (turn.operations[0].operation
                == bg::detail::overlay::operation_union)
        {
            action = "leaving";

        }
        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;
}