1 #ifndef OSMIUM_AREA_PROBLEM_REPORTER_OGR_HPP
2 #define OSMIUM_AREA_PROBLEM_REPORTER_OGR_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2020 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 /**
37  * @file
38  *
39  * This file contains code for reporting problems through OGR when
40  * assembling multipolygons.
41  *
42  * @attention If you include this file, you'll need to link with `libgdal`.
43  */
44 
45 #include <osmium/area/problem_reporter.hpp>
46 #include <osmium/geom/factory.hpp>
47 #include <osmium/geom/ogr.hpp>
48 #include <osmium/osm/item_type.hpp>
49 #include <osmium/osm/location.hpp>
50 #include <osmium/osm/node_ref.hpp>
51 #include <osmium/osm/node_ref_list.hpp>
52 #include <osmium/osm/types.hpp>
53 #include <osmium/osm/way.hpp>
54 
55 #include <gdalcpp.hpp>
56 
57 #include <memory>
58 
59 namespace osmium {
60 
61     namespace area {
62 
63         /**
64          * Report problems when assembling areas by adding them to
65          * layers in an OGR datasource.
66          */
67         class ProblemReporterOGR : public ProblemReporter {
68 
69             osmium::geom::OGRFactory<> m_ogr_factory;
70 
71             gdalcpp::Layer m_layer_perror;
72             gdalcpp::Layer m_layer_lerror;
73             gdalcpp::Layer m_layer_ways;
74 
set_object(gdalcpp::Feature & feature)75             void set_object(gdalcpp::Feature& feature) {
76                 const char t[2] = {osmium::item_type_to_char(m_object_type), '\0'};
77                 feature.set_field("obj_type", t);
78                 feature.set_field("obj_id", int32_t(m_object_id));
79                 feature.set_field("nodes", int32_t(m_nodes));
80             }
81 
write_point(const char * problem_type,osmium::object_id_type id1,osmium::object_id_type id2,osmium::Location location)82             void write_point(const char* problem_type, osmium::object_id_type id1, osmium::object_id_type id2, osmium::Location location) {
83                 gdalcpp::Feature feature{m_layer_perror, m_ogr_factory.create_point(location)};
84                 set_object(feature);
85                 feature.set_field("id1", double(id1));
86                 feature.set_field("id2", double(id2));
87                 feature.set_field("problem", problem_type);
88                 feature.add_to_layer();
89             }
90 
write_line(const char * problem_type,osmium::object_id_type id1,osmium::object_id_type id2,osmium::Location loc1,osmium::Location loc2)91             void write_line(const char* problem_type, osmium::object_id_type id1, osmium::object_id_type id2, osmium::Location loc1, osmium::Location loc2) {
92                 auto ogr_linestring = std::unique_ptr<OGRLineString>{new OGRLineString{}};
93                 ogr_linestring->addPoint(loc1.lon(), loc1.lat());
94                 ogr_linestring->addPoint(loc2.lon(), loc2.lat());
95 
96                 gdalcpp::Feature feature{m_layer_lerror, std::move(ogr_linestring)};
97                 set_object(feature);
98                 feature.set_field("id1", static_cast<double>(id1));
99                 feature.set_field("id2", static_cast<double>(id2));
100                 feature.set_field("problem", problem_type);
101                 feature.add_to_layer();
102             }
103 
104         public:
105 
ProblemReporterOGR(gdalcpp::Dataset & dataset)106             explicit ProblemReporterOGR(gdalcpp::Dataset& dataset) :
107                 m_layer_perror(dataset, "perrors", wkbPoint),
108                 m_layer_lerror(dataset, "lerrors", wkbLineString),
109                 m_layer_ways(dataset, "ways", wkbLineString) {
110 
111                 // 64bit integers are not supported in GDAL < 2, so we
112                 // are using a workaround here in fields where we expect
113                 // node IDs, we use real numbers.
114                 m_layer_perror
115                     .add_field("obj_type", OFTString, 1)
116                     .add_field("obj_id", OFTInteger, 10)
117                     .add_field("nodes", OFTInteger, 8)
118                     .add_field("id1", OFTReal, 12, 1)
119                     .add_field("id2", OFTReal, 12, 1)
120                     .add_field("problem", OFTString, 30)
121                 ;
122 
123                 m_layer_lerror
124                     .add_field("obj_type", OFTString, 1)
125                     .add_field("obj_id", OFTInteger, 10)
126                     .add_field("nodes", OFTInteger, 8)
127                     .add_field("id1", OFTReal, 12, 1)
128                     .add_field("id2", OFTReal, 12, 1)
129                     .add_field("problem", OFTString, 30)
130                 ;
131 
132                 m_layer_ways
133                     .add_field("obj_type", OFTString, 1)
134                     .add_field("obj_id", OFTInteger, 10)
135                     .add_field("way_id", OFTInteger, 10)
136                     .add_field("nodes", OFTInteger, 8)
137                 ;
138             }
139 
report_duplicate_node(osmium::object_id_type node_id1,osmium::object_id_type node_id2,osmium::Location location)140             void report_duplicate_node(osmium::object_id_type node_id1, osmium::object_id_type node_id2, osmium::Location location) override {
141                 write_point("duplicate_node", node_id1, node_id2, location);
142             }
143 
report_touching_ring(osmium::object_id_type node_id,osmium::Location location)144             void report_touching_ring(osmium::object_id_type node_id, osmium::Location location) override {
145                 write_point("touching_ring", node_id, 0, location);
146             }
147 
report_intersection(osmium::object_id_type way1_id,osmium::Location way1_seg_start,osmium::Location way1_seg_end,osmium::object_id_type way2_id,osmium::Location way2_seg_start,osmium::Location way2_seg_end,osmium::Location intersection)148             void report_intersection(osmium::object_id_type way1_id, osmium::Location way1_seg_start, osmium::Location way1_seg_end,
149                                      osmium::object_id_type way2_id, osmium::Location way2_seg_start, osmium::Location way2_seg_end, osmium::Location intersection) override {
150                 write_point("intersection", way1_id, way2_id, intersection);
151                 write_line("intersection", way1_id, way2_id, way1_seg_start, way1_seg_end);
152                 write_line("intersection", way2_id, way1_id, way2_seg_start, way2_seg_end);
153             }
154 
report_duplicate_segment(const osmium::NodeRef & nr1,const osmium::NodeRef & nr2)155             void report_duplicate_segment(const osmium::NodeRef& nr1, const osmium::NodeRef& nr2) override {
156                 write_line("duplicate_segment", nr1.ref(), nr2.ref(), nr1.location(), nr2.location());
157             }
158 
report_overlapping_segment(const osmium::NodeRef & nr1,const osmium::NodeRef & nr2)159             void report_overlapping_segment(const osmium::NodeRef& nr1, const osmium::NodeRef& nr2) override {
160                 write_line("overlapping_segment", nr1.ref(), nr2.ref(), nr1.location(), nr2.location());
161             }
162 
report_ring_not_closed(const osmium::NodeRef & nr,const osmium::Way * way)163             void report_ring_not_closed(const osmium::NodeRef& nr, const osmium::Way* way) override {
164                 write_point("ring_not_closed", nr.ref(), way ? way->id() : 0, nr.location());
165             }
166 
report_role_should_be_outer(osmium::object_id_type way_id,osmium::Location seg_start,osmium::Location seg_end)167             void report_role_should_be_outer(osmium::object_id_type way_id, osmium::Location seg_start, osmium::Location seg_end) override {
168                 write_line("role_should_be_outer", way_id, 0, seg_start, seg_end);
169             }
170 
report_role_should_be_inner(osmium::object_id_type way_id,osmium::Location seg_start,osmium::Location seg_end)171             void report_role_should_be_inner(osmium::object_id_type way_id, osmium::Location seg_start, osmium::Location seg_end) override {
172                 write_line("role_should_be_inner", way_id, 0, seg_start, seg_end);
173             }
174 
report_way_in_multiple_rings(const osmium::Way & way)175             void report_way_in_multiple_rings(const osmium::Way& way) override {
176                 if (way.nodes().size() < 2) {
177                     return;
178                 }
179                 try {
180                     gdalcpp::Feature feature{m_layer_lerror, m_ogr_factory.create_linestring(way)};
181                     set_object(feature);
182                     feature.set_field("id1", int32_t(way.id()));
183                     feature.set_field("id2", 0);
184                     feature.set_field("problem", "way_in_multiple_rings");
185                     feature.add_to_layer();
186                 } catch (const osmium::geometry_error&) {
187                     // XXX
188                 }
189             }
190 
report_inner_with_same_tags(const osmium::Way & way)191             void report_inner_with_same_tags(const osmium::Way& way) override {
192                 if (way.nodes().size() < 2) {
193                     return;
194                 }
195                 try {
196                     gdalcpp::Feature feature{m_layer_lerror, m_ogr_factory.create_linestring(way)};
197                     set_object(feature);
198                     feature.set_field("id1", int32_t(way.id()));
199                     feature.set_field("id2", 0);
200                     feature.set_field("problem", "inner_with_same_tags");
201                     feature.add_to_layer();
202                 } catch (const osmium::geometry_error&) {
203                     // XXX
204                 }
205             }
206 
report_duplicate_way(const osmium::Way & way)207             void report_duplicate_way(const osmium::Way& way) override {
208                 if (way.nodes().size() < 2) {
209                     return;
210                 }
211                 try {
212                     gdalcpp::Feature feature{m_layer_lerror, m_ogr_factory.create_linestring(way)};
213                     set_object(feature);
214                     feature.set_field("id1", int32_t(way.id()));
215                     feature.set_field("id2", 0);
216                     feature.set_field("problem", "duplicate_way");
217                     feature.add_to_layer();
218                 } catch (const osmium::geometry_error&) {
219                     // XXX
220                 }
221             }
222 
report_way(const osmium::Way & way)223             void report_way(const osmium::Way& way) override {
224                 if (way.nodes().empty()) {
225                     return;
226                 }
227                 if (way.nodes().size() == 1) {
228                     const auto& first_nr = way.nodes()[0];
229                     write_point("single_node_in_way", way.id(), first_nr.ref(), first_nr.location());
230                     return;
231                 }
232                 try {
233                     gdalcpp::Feature feature{m_layer_ways, m_ogr_factory.create_linestring(way)};
234                     set_object(feature);
235                     feature.set_field("way_id", int32_t(way.id()));
236                     feature.add_to_layer();
237                 } catch (const osmium::geometry_error&) {
238                     // XXX
239                 }
240             }
241 
242         }; // class ProblemReporterOGR
243 
244     } // namespace area
245 
246 } // namespace osmium
247 
248 #endif // OSMIUM_AREA_PROBLEM_REPORTER_OGR_HPP
249