Loading...
Searching...
No Matches
problem_reporter_ogr.hpp
Go to the documentation of this file.
#ifndef OSMIUM_AREA_PROBLEM_REPORTER_OGR_HPP
#define OSMIUM_AREA_PROBLEM_REPORTER_OGR_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/area/problem_reporter.hpp>
#include <osmium/geom/factory.hpp>
#include <osmium/geom/ogr.hpp>
#include <osmium/osm/item_type.hpp>
#include <osmium/osm/location.hpp>
#include <osmium/osm/node_ref.hpp>
#include <osmium/osm/node_ref_list.hpp>
#include <osmium/osm/types.hpp>
#include <osmium/osm/way.hpp>
#include <gdalcpp.hpp>
#include <memory>
namespace osmium {
namespace area {
class ProblemReporterOGR : public ProblemReporter {
osmium::geom::OGRFactory<> m_ogr_factory;
gdalcpp::Layer m_layer_perror;
gdalcpp::Layer m_layer_lerror;
gdalcpp::Layer m_layer_ways;
void set_object(gdalcpp::Feature& feature) {
const char t[2] = {osmium::item_type_to_char(m_object_type), '\0'};
feature.set_field("obj_type", t);
feature.set_field("obj_id", static_cast<int32_t>(m_object_id));
feature.set_field("nodes", static_cast<int32_t>(m_nodes));
}
void write_point(const char* problem_type, osmium::object_id_type id1, osmium::object_id_type id2, osmium::Location location) {
gdalcpp::Feature feature{m_layer_perror, m_ogr_factory.create_point(location)};
set_object(feature);
feature.set_field("id1", static_cast<double>(id1));
feature.set_field("id2", static_cast<double>(id2));
feature.set_field("problem", problem_type);
feature.add_to_layer();
}
void write_line(const char* problem_type, osmium::object_id_type id1, osmium::object_id_type id2, osmium::Location loc1, osmium::Location loc2) {
auto ogr_linestring = std::make_unique<OGRLineString>();
ogr_linestring->addPoint(loc1.lon(), loc1.lat());
ogr_linestring->addPoint(loc2.lon(), loc2.lat());
gdalcpp::Feature feature{m_layer_lerror, std::move(ogr_linestring)};
set_object(feature);
feature.set_field("id1", static_cast<double>(id1));
feature.set_field("id2", static_cast<double>(id2));
feature.set_field("problem", problem_type);
feature.add_to_layer();
}
public:
explicit ProblemReporterOGR(gdalcpp::Dataset& dataset) :
m_layer_perror(dataset, "perrors", wkbPoint),
m_layer_lerror(dataset, "lerrors", wkbLineString),
m_layer_ways(dataset, "ways", wkbLineString) {
// 64bit integers are not supported in GDAL < 2, so we
// are using a workaround here in fields where we expect
// node IDs, we use real numbers.
m_layer_perror
.add_field("obj_type", OFTString, 1)
.add_field("obj_id", OFTInteger, 10)
.add_field("nodes", OFTInteger, 8)
.add_field("id1", OFTReal, 12, 1)
.add_field("id2", OFTReal, 12, 1)
.add_field("problem", OFTString, 30);
m_layer_lerror
.add_field("obj_type", OFTString, 1)
.add_field("obj_id", OFTInteger, 10)
.add_field("nodes", OFTInteger, 8)
.add_field("id1", OFTReal, 12, 1)
.add_field("id2", OFTReal, 12, 1)
.add_field("problem", OFTString, 30);
m_layer_ways
.add_field("obj_type", OFTString, 1)
.add_field("obj_id", OFTInteger, 10)
.add_field("way_id", OFTInteger, 10)
.add_field("nodes", OFTInteger, 8);
}
void report_duplicate_node(osmium::object_id_type node_id1, osmium::object_id_type node_id2, osmium::Location location) override {
write_point("duplicate_node", node_id1, node_id2, location);
}
void report_touching_ring(osmium::object_id_type node_id, osmium::Location location) override {
write_point("touching_ring", node_id, 0, location);
}
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 {
write_point("intersection", way1_id, way2_id, intersection);
write_line("intersection", way1_id, way2_id, way1_seg_start, way1_seg_end);
write_line("intersection", way2_id, way1_id, way2_seg_start, way2_seg_end);
}
void report_duplicate_segment(const osmium::NodeRef& nr1, const osmium::NodeRef& nr2) override {
write_line("duplicate_segment", nr1.ref(), nr2.ref(), nr1.location(), nr2.location());
}
void report_overlapping_segment(const osmium::NodeRef& nr1, const osmium::NodeRef& nr2) override {
write_line("overlapping_segment", nr1.ref(), nr2.ref(), nr1.location(), nr2.location());
}
void report_ring_not_closed(const osmium::NodeRef& nr, const osmium::Way* way) override {
write_point("ring_not_closed", nr.ref(), way ? way->id() : 0, nr.location());
}
void report_role_should_be_outer(osmium::object_id_type way_id, osmium::Location seg_start, osmium::Location seg_end) override {
write_line("role_should_be_outer", way_id, 0, seg_start, seg_end);
}
void report_role_should_be_inner(osmium::object_id_type way_id, osmium::Location seg_start, osmium::Location seg_end) override {
write_line("role_should_be_inner", way_id, 0, seg_start, seg_end);
}
void report_way_in_multiple_rings(const osmium::Way& way) override {
if (way.nodes().size() < 2) {
return;
}
try {
gdalcpp::Feature feature{m_layer_lerror, m_ogr_factory.create_linestring(way)};
set_object(feature);
feature.set_field("id1", static_cast<int32_t>(way.id()));
feature.set_field("id2", 0);
feature.set_field("problem", "way_in_multiple_rings");
feature.add_to_layer();
} catch (const osmium::geometry_error&) {
// XXX
}
}
void report_inner_with_same_tags(const osmium::Way& way) override {
if (way.nodes().size() < 2) {
return;
}
try {
gdalcpp::Feature feature{m_layer_lerror, m_ogr_factory.create_linestring(way)};
set_object(feature);
feature.set_field("id1", static_cast<int32_t>(way.id()));
feature.set_field("id2", 0);
feature.set_field("problem", "inner_with_same_tags");
feature.add_to_layer();
} catch (const osmium::geometry_error&) {
// XXX
}
}
void report_duplicate_way(const osmium::Way& way) override {
if (way.nodes().size() < 2) {
return;
}
try {
gdalcpp::Feature feature{m_layer_lerror, m_ogr_factory.create_linestring(way)};
set_object(feature);
feature.set_field("id1", static_cast<int32_t>(way.id()));
feature.set_field("id2", 0);
feature.set_field("problem", "duplicate_way");
feature.add_to_layer();
} catch (const osmium::geometry_error&) {
// XXX
}
}
void report_way(const osmium::Way& way) override {
if (way.nodes().empty()) {
return;
}
if (way.nodes().size() == 1) {
const auto& first_nr = way.nodes()[0];
write_point("single_node_in_way", way.id(), first_nr.ref(), first_nr.location());
return;
}
try {
gdalcpp::Feature feature{m_layer_ways, m_ogr_factory.create_linestring(way)};
set_object(feature);
feature.set_field("way_id", static_cast<int32_t>(way.id()));
feature.add_to_layer();
} catch (const osmium::geometry_error&) {
// XXX
}
}
}; // class ProblemReporterOGR
} // namespace area
} // namespace osmium
#endif // OSMIUM_AREA_PROBLEM_REPORTER_OGR_HPP
osmium::Location
Definition location.hpp:271
osmium::Location::lon
double lon() const
Definition location.hpp:400
osmium::Location::lat
double lat() const
Definition location.hpp:419
osmium::NodeRef
Definition node_ref.hpp:50
osmium::NodeRef::ref
constexpr osmium::object_id_type ref() const noexcept
Definition node_ref.hpp:71
osmium::NodeRef::location
osmium::Location & location() noexcept
Definition node_ref.hpp:85
osmium::Way
Definition way.hpp:72
osmium::area::ProblemReporterOGR
Definition problem_reporter_ogr.hpp:67
osmium::area::ProblemReporterOGR::report_ring_not_closed
void report_ring_not_closed(const osmium::NodeRef &nr, const osmium::Way *way) override
Definition problem_reporter_ogr.hpp:160
osmium::area::ProblemReporterOGR::report_duplicate_segment
void report_duplicate_segment(const osmium::NodeRef &nr1, const osmium::NodeRef &nr2) override
Definition problem_reporter_ogr.hpp:152
osmium::area::ProblemReporterOGR::m_ogr_factory
osmium::geom::OGRFactory m_ogr_factory
Definition problem_reporter_ogr.hpp:69
osmium::area::ProblemReporterOGR::report_inner_with_same_tags
void report_inner_with_same_tags(const osmium::Way &way) override
Definition problem_reporter_ogr.hpp:188
osmium::area::ProblemReporterOGR::m_layer_ways
gdalcpp::Layer m_layer_ways
Definition problem_reporter_ogr.hpp:73
osmium::area::ProblemReporterOGR::report_overlapping_segment
void report_overlapping_segment(const osmium::NodeRef &nr1, const osmium::NodeRef &nr2) override
Definition problem_reporter_ogr.hpp:156
osmium::area::ProblemReporterOGR::report_role_should_be_outer
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:164
osmium::area::ProblemReporterOGR::write_point
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:82
osmium::area::ProblemReporterOGR::m_layer_lerror
gdalcpp::Layer m_layer_lerror
Definition problem_reporter_ogr.hpp:72
osmium::area::ProblemReporterOGR::report_duplicate_way
void report_duplicate_way(const osmium::Way &way) override
Definition problem_reporter_ogr.hpp:204
osmium::area::ProblemReporterOGR::ProblemReporterOGR
ProblemReporterOGR(gdalcpp::Dataset &dataset)
Definition problem_reporter_ogr.hpp:106
osmium::area::ProblemReporterOGR::report_way_in_multiple_rings
void report_way_in_multiple_rings(const osmium::Way &way) override
Definition problem_reporter_ogr.hpp:172
osmium::area::ProblemReporterOGR::write_line
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:91
osmium::area::ProblemReporterOGR::report_touching_ring
void report_touching_ring(osmium::object_id_type node_id, osmium::Location location) override
Definition problem_reporter_ogr.hpp:141
osmium::area::ProblemReporterOGR::m_layer_perror
gdalcpp::Layer m_layer_perror
Definition problem_reporter_ogr.hpp:71
osmium::area::ProblemReporterOGR::report_way
void report_way(const osmium::Way &way) override
Definition problem_reporter_ogr.hpp:220
osmium::area::ProblemReporterOGR::report_role_should_be_inner
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:168
osmium::area::ProblemReporterOGR::set_object
void set_object(gdalcpp::Feature &feature)
Definition problem_reporter_ogr.hpp:75
osmium::area::ProblemReporterOGR::report_intersection
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:145
osmium::area::ProblemReporterOGR::report_duplicate_node
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:137
osmium::area::ProblemReporter
Definition problem_reporter.hpp:60
osmium::area::ProblemReporter::m_object_id
osmium::object_id_type m_object_id
Definition problem_reporter.hpp:68
osmium::area::ProblemReporter::m_object_type
osmium::item_type m_object_type
Definition problem_reporter.hpp:65
osmium::area::ProblemReporter::m_nodes
size_t m_nodes
Definition problem_reporter.hpp:71
osmium::geom::GeometryFactory
Definition factory.hpp:149
osmium::geom::GeometryFactory::create_point
point_type create_point(const osmium::Location &location) const
Definition factory.hpp:211
osmium::geom::GeometryFactory::create_linestring
linestring_type create_linestring(const osmium::WayNodeList &wnl, use_nodes un=use_nodes::unique, direction dir=direction::forward)
Definition factory.hpp:266
osmium::geometry_error
Definition factory.hpp:60
factory.hpp
item_type.hpp
location.hpp
osmium::osm_entity_bits::area
@ area
Definition entity_bits.hpp:75
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition types.hpp:45
osmium::item_type::way
@ way
osmium::item_type_to_char
char item_type_to_char(const item_type type) noexcept
Definition item_type.hpp:122
node_ref.hpp
node_ref_list.hpp
ogr.hpp
problem_reporter.hpp
types.hpp
way.hpp