Libosmium  2.2.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
problem_reporter_ogr.hpp
Go to the documentation of this file.
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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 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 
45 #ifdef _MSC_VER
46 # pragma warning(push)
47 # pragma warning(disable : 4458)
48 #else
49 # pragma GCC diagnostic push
50 # ifdef __clang__
51 # pragma GCC diagnostic ignored "-Wdocumentation-unknown-command"
52 # endif
53 # pragma GCC diagnostic ignored "-Wfloat-equal"
54 # pragma GCC diagnostic ignored "-Wold-style-cast"
55 # pragma GCC diagnostic ignored "-Wpadded"
56 # pragma GCC diagnostic ignored "-Wredundant-decls"
57 # pragma GCC diagnostic ignored "-Wshadow"
58 #endif
59 
60 #include <ogr_api.h>
61 #include <ogrsf_frmts.h>
62 
63 #ifdef _MSC_VER
64 # pragma warning(pop)
65 #else
66 # pragma GCC diagnostic pop
67 #endif
68 
69 #include <memory>
70 #include <stdexcept>
71 
73 #include <osmium/geom/ogr.hpp>
74 #include <osmium/osm/location.hpp>
75 #include <osmium/osm/types.hpp>
76 
77 namespace osmium {
78 
79  namespace area {
80 
86 
88 
89  OGRDataSource* m_data_source;
90 
91  OGRLayer* m_layer_perror;
92  OGRLayer* m_layer_lerror;
93 
94  void write_point(const char* problem_type, osmium::object_id_type id1, osmium::object_id_type id2, osmium::Location location) {
95  OGRFeature* feature = OGRFeature::CreateFeature(m_layer_perror->GetLayerDefn());
96  std::unique_ptr<OGRPoint> ogr_point = m_ogr_factory.create_point(location);
97  feature->SetGeometry(ogr_point.get());
98  feature->SetField("id1", static_cast<double>(id1));
99  feature->SetField("id2", static_cast<double>(id2));
100  feature->SetField("problem_type", problem_type);
101 
102  if (m_layer_perror->CreateFeature(feature) != OGRERR_NONE) {
103  std::runtime_error("Failed to create feature on layer 'perrors'");
104  }
105 
106  OGRFeature::DestroyFeature(feature);
107  }
108 
109  void write_line(const char* problem_type, osmium::object_id_type id1, osmium::object_id_type id2, osmium::Location loc1, osmium::Location loc2) {
110  std::unique_ptr<OGRPoint> ogr_point1 = m_ogr_factory.create_point(loc1);
111  std::unique_ptr<OGRPoint> ogr_point2 = m_ogr_factory.create_point(loc2);
112  std::unique_ptr<OGRLineString> ogr_linestring = std::unique_ptr<OGRLineString>(new OGRLineString());
113  ogr_linestring->addPoint(ogr_point1.get());
114  ogr_linestring->addPoint(ogr_point2.get());
115  OGRFeature* feature = OGRFeature::CreateFeature(m_layer_lerror->GetLayerDefn());
116  feature->SetGeometry(ogr_linestring.get());
117  feature->SetField("id1", static_cast<double>(id1));
118  feature->SetField("id2", static_cast<double>(id2));
119  feature->SetField("problem_type", problem_type);
120 
121  if (m_layer_lerror->CreateFeature(feature) != OGRERR_NONE) {
122  std::runtime_error("Failed to create feature on layer 'lerrors'");
123  }
124 
125  OGRFeature::DestroyFeature(feature);
126  }
127 
128  public:
129 
130  explicit ProblemReporterOGR(OGRDataSource* data_source) :
131  m_data_source(data_source) {
132 
133  OGRSpatialReference sparef;
134  sparef.SetWellKnownGeogCS("WGS84");
135 
136  m_layer_perror = m_data_source->CreateLayer("perrors", &sparef, wkbPoint, nullptr);
137  if (!m_layer_perror) {
138  std::runtime_error("Layer creation failed for layer 'perrors'");
139  }
140 
141  OGRFieldDefn layer_perror_field_id1("id1", OFTReal);
142  layer_perror_field_id1.SetWidth(10);
143 
144  if (m_layer_perror->CreateField(&layer_perror_field_id1) != OGRERR_NONE) {
145  std::runtime_error("Creating field 'id1' failed for layer 'perrors'");
146  }
147 
148  OGRFieldDefn layer_perror_field_id2("id2", OFTReal);
149  layer_perror_field_id2.SetWidth(10);
150 
151  if (m_layer_perror->CreateField(&layer_perror_field_id2) != OGRERR_NONE) {
152  std::runtime_error("Creating field 'id2' failed for layer 'perrors'");
153  }
154 
155  OGRFieldDefn layer_perror_field_problem_type("problem_type", OFTString);
156  layer_perror_field_problem_type.SetWidth(30);
157 
158  if (m_layer_perror->CreateField(&layer_perror_field_problem_type) != OGRERR_NONE) {
159  std::runtime_error("Creating field 'problem_type' failed for layer 'perrors'");
160  }
161 
162  /**************/
163 
164  m_layer_lerror = m_data_source->CreateLayer("lerrors", &sparef, wkbLineString, nullptr);
165  if (!m_layer_lerror) {
166  std::runtime_error("Layer creation failed for layer 'lerrors'");
167  }
168 
169  OGRFieldDefn layer_lerror_field_id1("id1", OFTReal);
170  layer_lerror_field_id1.SetWidth(10);
171 
172  if (m_layer_lerror->CreateField(&layer_lerror_field_id1) != OGRERR_NONE) {
173  std::runtime_error("Creating field 'id1' failed for layer 'lerrors'");
174  }
175 
176  OGRFieldDefn layer_lerror_field_id2("id2", OFTReal);
177  layer_lerror_field_id2.SetWidth(10);
178 
179  if (m_layer_lerror->CreateField(&layer_lerror_field_id2) != OGRERR_NONE) {
180  std::runtime_error("Creating field 'id2' failed for layer 'lerrors'");
181  }
182 
183  OGRFieldDefn layer_lerror_field_problem_type("problem_type", OFTString);
184  layer_lerror_field_problem_type.SetWidth(30);
185 
186  if (m_layer_lerror->CreateField(&layer_lerror_field_problem_type) != OGRERR_NONE) {
187  std::runtime_error("Creating field 'problem_type' failed for layer 'lerrors'");
188  }
189  }
190 
191  virtual ~ProblemReporterOGR() = default;
192 
194  write_point("duplicate_node", node_id1, node_id2, location);
195  }
196 
198  osmium::object_id_type way2_id, osmium::Location way2_seg_start, osmium::Location way2_seg_end, osmium::Location intersection) override {
199  write_point("intersection", m_object_id, 0, intersection);
200  write_line("intersection", m_object_id, way1_id, way1_seg_start, way1_seg_end);
201  write_line("intersection", m_object_id, way2_id, way2_seg_start, way2_seg_end);
202  }
203 
205  write_point("ring_not_closed", m_object_id, 0, end1);
206  write_point("ring_not_closed", m_object_id, 0, end2);
207  }
208 
210  write_line("role_should_be_outer", m_object_id, way_id, seg_start, seg_end);
211  }
212 
214  write_line("role_should_be_inner", m_object_id, way_id, seg_start, seg_end);
215  }
216 
217  }; // class ProblemReporterOGR
218 
219  } // namespace area
220 
221 } // namespace osmium
222 
223 #endif // OSMIUM_AREA_PROBLEM_REPORTER_OGR_HPP
void report_role_should_be_outer(osmium::object_id_type way_id, osmium::Location seg_start, osmium::Location seg_end) override
Definition: problem_reporter_ogr.hpp:209
Definition: factory.hpp:146
void write_point(const char *problem_type, osmium::object_id_type id1, osmium::object_id_type id2, osmium::Location location)
Definition: problem_reporter_ogr.hpp:94
ProblemReporterOGR(OGRDataSource *data_source)
Definition: problem_reporter_ogr.hpp:130
osmium::object_id_type m_object_id
Definition: problem_reporter.hpp:63
point_type create_point(const osmium::Location location) const
Definition: factory.hpp:201
void 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) override
Definition: problem_reporter_ogr.hpp:197
Definition: entity_bits.hpp:67
OGRLayer * m_layer_perror
Definition: problem_reporter_ogr.hpp:91
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
Definition: problem_reporter.hpp:55
void report_duplicate_node(osmium::object_id_type node_id1, osmium::object_id_type node_id2, osmium::Location location) override
Definition: problem_reporter_ogr.hpp:193
void write_line(const char *problem_type, osmium::object_id_type id1, osmium::object_id_type id2, osmium::Location loc1, osmium::Location loc2)
Definition: problem_reporter_ogr.hpp:109
Definition: problem_reporter_ogr.hpp:85
OGRLayer * m_layer_lerror
Definition: problem_reporter_ogr.hpp:92
Definition: location.hpp:79
osmium::geom::OGRFactory m_ogr_factory
Definition: problem_reporter_ogr.hpp:87
virtual ~ProblemReporterOGR()=default
OGRDataSource * m_data_source
Definition: problem_reporter_ogr.hpp:89
void report_role_should_be_inner(osmium::object_id_type way_id, osmium::Location seg_start, osmium::Location seg_end) override
Definition: problem_reporter_ogr.hpp:213
void report_ring_not_closed(osmium::Location end1, osmium::Location end2) override
Definition: problem_reporter_ogr.hpp:204