-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
171 lines (150 loc) · 5.34 KB
/
main.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <fstream>
#include <mapnik/datasource.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/geometry/boost_adapters.hpp>
#include <boost/geometry/algorithms/within.hpp>
#include <boost/geometry/algorithms/distance.hpp>
#include <boost/geometry/algorithms/comparable_distance.hpp>
#include <boost/geometry/extensions/algorithms/closest_point.hpp>
#include <boost/geometry/io/svg/svg_mapper.hpp>
#include <mapnik/util/geometry_to_wkt.hpp>
namespace mapnik { namespace geometry {
struct closest_point
{
using result_type = boost::geometry::closest_point_result<mapnik::geometry::point<double>>;
closest_point(mapnik::geometry::point<double> const& pt)
: pt_(pt) {}
result_type operator() (mapnik::geometry::geometry_empty const&) const
{
return result_type(); // FIXME: consider std::optional<result_type>
}
result_type operator() (mapnik::geometry::point<double> const& pt) const
{
result_type info;
boost::geometry::closest_point(pt_ ,pt, info);
return info;
}
result_type operator() (mapnik::geometry::line_string<double> const& line) const
{
result_type info;
boost::geometry::closest_point(pt_ ,line, info);
return info;
}
result_type operator() (mapnik::geometry::polygon<double> const& poly) const
{
result_type info;
if (boost::geometry::within(pt_, poly))
{
info.closest_point = pt_;
info.distance = 0.0;
return info;
}
bool first = true;
for (auto const& ring : poly)
{
result_type ring_info;
boost::geometry::closest_point(pt_ ,ring, ring_info);
if (first)
{
first = false;
info = std::move(ring_info);
}
else if (ring_info.distance < info.distance)
{
info = std::move(ring_info);
}
}
return info;
}
// Multi* + GeometryCollection
result_type operator() (mapnik::geometry::geometry<double> const& geom) const
{
return mapnik::util::apply_visitor(*this, geom);
}
template <typename T>
result_type operator() (T const& multi_geom) const
{
result_type info;
bool first = true;
for (auto const& geom : multi_geom)
{
if (first)
{
first = false;
info = std::move(operator()(geom));
}
else
{
auto sub_info = operator()(geom);
if (sub_info.distance < info.distance)
{
info = std::move(sub_info);
}
}
}
return info;
}
mapnik::geometry::point<double> pt_;
};
}}
int main(int argc, char** argv)
{
std::cerr << "Comparable distance test" << std::endl;
mapnik::datasource_cache::instance().register_datasources("/opt/mapnik/lib/mapnik/input");
if (argc != 4)
{
std::cerr << "Usage: <geojson-filename> <x> <y>" << std::endl;
return EXIT_FAILURE;
}
mapnik::parameters params;
params["type"] = "geojson";
params["file"] = std::string(argv[1]);
double x = std::stod(argv[2]);
double y = std::stod(argv[3]);
auto ds = mapnik::datasource_cache::instance().create(params);
if (ds == nullptr)
{
std::cerr << "Failed to create datasource" << std::endl;
return EXIT_FAILURE;
}
mapnik::query q(ds->envelope());
auto features = ds->features(q);
auto feature = features->next();
mapnik::geometry::point<double> pt(x, y);
std::ofstream svg("point_to_geometry_distance.svg");
boost::geometry::svg_mapper<mapnik::geometry::point<double>> mapper(svg, 600, 400,
"width=\"800\" height=\"600\" fill=\"white\"");
std::array<std::string, 4> four_colors = {{"red", "blue", "green", "yellow"}};
std::size_t counter = 0;
while (feature != nullptr)
{
auto const& geom = feature->get_geometry();
if (geom.is<mapnik::geometry::polygon<double>>())
{
auto const& poly = geom.get<mapnik::geometry::polygon<double>>();
mapper.add(poly);
std::string style = "stroke:blue;stroke-width:1; fill-opacity:0.5;fill:" + four_colors[counter % four_colors.size()];
++counter;
mapper.map(poly, style);
}
auto result = mapnik::util::apply_visitor(mapnik::geometry::closest_point(pt), geom);
std::string wkt;
mapnik::util::to_wkt(wkt, result.closest_point);
std::cerr << "closest point:" << wkt
<< " distance=" << result.distance << std::endl;
mapnik::geometry::line_string<double> line;
line.push_back(pt);
line.push_back(result.closest_point);
mapper.add(line);
mapper.map(line,"stroke:red;stroke-width:1;stroke-dasharray:4,4;comp-op:color-burn");
mapper.add(result.closest_point);
mapper.map(result.closest_point,"fill-opacity:1.0;fill:yellow;stroke:blue;stroke-width:1");
feature = features->next();
}
mapper.add(pt);
mapper.map(pt,"fill-opacity:1.0;fill:salmon;stroke:blue;stroke-width:1");
std::cerr << "Done" << std::endl;
return EXIT_SUCCESS;
}