c43f8db Release v2.3.0 44c135f Update README to show dependencies used internally. ece54cd Add external licenses. 908cd5f Updated change log. 96dbf0e Change %-escape in OPL format. 98f6e27 Change write benchmark to interleave reading and writing. 39620ce Make writing of metadata configurable for XML and OPL output. e5a4e5e Add debug output format. 597390f Remove superfluous include and pragmas. ecc57b0 Update pbf reader/writer to use new protozero functions. 5d1e8d2 Update protozero from upstream. ef8746b Fix build on Windows. ddba46f Remove superfluous include. 098c57f Add some paranoia checks to pbf reader. 0f804c2 Try building with newer boost library on travis. 6f79d63 Use explicit return types on lambdas. 355f3b1 New PBF reader and writer based on protozero. 71d719b Add pbf writing benchmark. f014b4c Fix iwyu.sh script: Works now if build directory doesn't exist. a0ace49 Use utf8cpp header-only lib instead of boost for utf8 decoding. 796f18e Bugfix: Reading large XML files could block. 5a2bcbe Replace strcmp by std::string comparison in test. bc49e2c Bugfix: XML writer was not writing whitespace correctly. 61222f8 Fix 64bit byte swap. e56f090 Fix new CRC code on OSX and Windows. 70229aa Add low-level building blocks that allow calculating CRC of OSM data. 0968a66 Remove assert checking for unset version. 62e0261 Refactor test case. 4bfc7fc Allow instantiating osmium::geom::GEOSFactory with existing GEOS factory. e70af0c Remove calls to protobuf cleanup function im benchmarks and examples. 718518d Bugfix in OPL output. Relation member roles were not encoded. 759d5cb Rename parameter that had the same name as a type. 7054cab Provide (Typed)MemoryMapping constructors for backwards compatibility. d09f5d1 Fix typo. b4e578f Make memory mapping utility class more flexible. 633fa8e Travis build without sudo. 7ff23f1 Improved code setting file format from suffix/format argument. 90ef3b9 Remove some tests that didn't test much and failed on FreeBSD. af86273 Add some pragmas to disable warnings for GCC. efac7fd Fix some include problems found by IWYU. 79d2f4c Changed add_user() and add_role() in builders. Add add_member(). 9375d00 Add function to set tags from ptr + length. Improve TagBuilder tests. bafca20 Test helper: Use more const and have sub-builders in their own scope. f73c993 Simplify code. fee1710 Disable warning only when compiling with GCC. 74402f3 Merge pull request #98 from dforsi/master 2c4b449 Update to new upstream catch.hpp version. 1318732 Release v2.2.0 1873998 Add missing test. 2e5ea1d Do not add timestamp to html doc pages. 1b2ea89 Remove debug output. 0be9599 Improved parsing of ids, versions, uids, etc. from strings. 4308d80 Add second version of split_string utility function. f18c9e5 Move part of pbf.hpp into new pbf_type_conv.hpp. d201152 Use new DeltaEncode class in pbf writer. e205610 Add DeltaEncode/DeltaDecode utility classes. 32905d6 Bugfix: Actually throw the exception we are creating... d3e86d8 Add functions to convert item_type to zero-based index. daddf07 Bugfix: Programs writing OSM files can stall up to a second after writing. 00b0247 Add function to set the id of a relation member. f85316a Fix error message. 19bc6cc Fix name of travis install script. 719cd33 spatialite-bin package now available on travis cb03821 Shorten long test string (MSVC doesn't like it). c3440a6 Add BoolVector index class. da08073 Add min_op/max_op utility functions. 411d112 AppVeyor.yml: new links for binary deps 7d9095f add test for badly formatted timestamps a073f73 Add helper methods to DiffObject. 3b9819a Add GeoJSON factory using the RapidJSON library. 107bca5 Use a reference instead of a copy. a6943a4 Mark a few variables that are not changing as const. 51b7e53 Improved error message for geometry exceptions. 5c37a13 Some minor spelling fixes 8ae5723 Bugfix: Dense location store was written out only partially. 5994322 Add support for tiles. 2168bac Add has_map_type() method to map factory. a9634bd Add more tests for mercator projection. 3c13e4d Add functionality to create simple polygons from ways in geom factories. e8c5bb1 Use uint64_t as counter, so there can be no overflows. 07fc9b9 libsparsehash-dev now in travis package whitelist 820e112 Add coverage support to CMake config. 5e9f943 Bugfix: Use the right include to really allow any input file type. d4b48eb CMake: Make version string a cached variable. e6baccb Add (c)begin/end functions to TypedMemoryMapping. Removed get_addr(). 3e32710 Use size() from MemoryMapping in TypedMemoryMapping. 96390db Improve MemoryMapping class documentation. 60a6217 Do not round memory mapped files to page size boundaries. 4907cbe Bugfix: function name. cac01d8 Use _filelengthi64 on Windows instead of fstat(2). 6a25bdf Windows: Put invalid parameter handler into wrapper class. Re-enable test. 110df9b Add invalid parameter handler on Windows to test. 549ed5f Disable some tests (to find which one fails on appveyor). a5b8873 Use resize_file() in memory mapping test instead of ftruncate directly. 40e41d3 Use _chsize_s() instead of _chsize() on Windows. 048397e Refactoring: Use low-level util functions in DataFile. 6a033f9 Remove now unused Windows implementation of mmap. 3eccdbb Move dword_hi/lo functions into osmium::util namespace. be7351b Remove unused code. b859b18 Make dword_hi/lo functions inline. 2e3bc37 Simplify mmap_vector_base/anon/file. f819cf3 Always map full pages. Make sure files behind mapping are large enough. d0c84b6 Add some low-level helper functions for file system access. 62e8d91 Make DataFile constructor explicit. fba684c Fix memory mapping test for windows. 78a7fd5 Add constructor to DataFile to create tmp file with given size. f911893 Bugfix: typo. 1cf2739 Add AnonymousMemoryMapping class. 56eac30 Implement MemoryMapping::resize() function. 1a73262 Bugfix: Counter variables were too small. 1ade32c Fix include position. b03aec3 Fixed some bugs in new DataFile class/tests. f109534 Add DataFile utility class. 9ed3c43 Fix/cleanup some code. 4f326c9 Fix bug: Copy-and-paste error. 78a5b2f Use reinterpret_cast instead of static_cast to get HANDLE on Windows. 7baa318 Fix typo. e669069 Make huge value even huger to see if code reliable fails then. 66137ad Improved documentation of MemoryMapping and TypedMemoryMapping classes. 3121393 Add TypedMemoryMapping class. f45335e Default for get_addr() template type. 685bbaf Remove unused code from tests. ce65bd4 Fix some issue with new MemoryMapping class. e7b8e15 Added MemoryMapping wrapper class for mmap() and Windows equivalent. 6b1effe typo fixed 33d479d Refactored travis build. 4348522 Fix xml data test. 769b1e8 Bugfix: Better check for invalid locations. bba7e68 Appveyor: Disable test failing because of missing dependency. 3d40dc7 Link with /debug on MSVC, add note about LNK4099 warnings. 5ef051f Appveyor: Disable header builds, add benchmarks. ce7485e Reformat Appveyor config. c60e505 use shallow clones for faster git fetch 3b18bca Travis cleanups. b8dfac0 Cleanup travis build. 5f19838 Trying to fix travis gcc build... d4255a4 Remove -Wno-return-type from recommended options. 5f1a41b Add dump_as_array() function to maps. ff22f76 Add constructors and begin()/end() functions to VectorBasedSparseMultimap. c7e05dd Bugfix: Make REGISTER_MAP() macro work when called several time with same name parameter. abdc317 Bugfix: Mark cbegin() and cend() of mmap_vector_base as const functions. d81d439 Add close() function to mmap_vector_base class. d74cff2 Add function on Buffer to get iterator to specific offset. git-subtree-dir: third_party/libosmium git-subtree-split: c43f8db50d93912a8bec5cd9fea733f7fec05549
784 lines
33 KiB
C++
784 lines
33 KiB
C++
#ifndef OSMIUM_AREA_ASSEMBLER_HPP
|
|
#define OSMIUM_AREA_ASSEMBLER_HPP
|
|
|
|
/*
|
|
|
|
This file is part of Osmium (http://osmcode.org/libosmium).
|
|
|
|
Copyright 2013-2015 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 <algorithm>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
#include <list>
|
|
#include <map>
|
|
#include <vector>
|
|
|
|
#include <osmium/builder/osm_object_builder.hpp>
|
|
#include <osmium/memory/buffer.hpp>
|
|
#include <osmium/osm/area.hpp>
|
|
#include <osmium/osm/location.hpp>
|
|
#include <osmium/osm/relation.hpp>
|
|
#include <osmium/tags/filter.hpp>
|
|
|
|
#include <osmium/area/detail/proto_ring.hpp>
|
|
#include <osmium/area/detail/node_ref_segment.hpp>
|
|
#include <osmium/area/detail/segment_list.hpp>
|
|
#include <osmium/area/problem_reporter.hpp>
|
|
|
|
namespace osmium {
|
|
|
|
namespace area {
|
|
|
|
using osmium::area::detail::ProtoRing;
|
|
|
|
struct AssemblerConfig {
|
|
|
|
osmium::area::ProblemReporter* problem_reporter;
|
|
|
|
// Enables debug output to stderr
|
|
bool debug;
|
|
|
|
explicit AssemblerConfig(osmium::area::ProblemReporter* pr = nullptr, bool d = false) :
|
|
problem_reporter(pr),
|
|
debug(d) {
|
|
}
|
|
|
|
/**
|
|
* Enable or disable debug output to stderr. This is for Osmium
|
|
* developers only.
|
|
*/
|
|
void enable_debug_output(bool d = true) {
|
|
debug = d;
|
|
}
|
|
|
|
}; // struct AssemblerConfig
|
|
|
|
/**
|
|
* Assembles area objects from multipolygon relations and their
|
|
* members. This is called by the MultipolygonCollector object
|
|
* after all members have been collected.
|
|
*/
|
|
class Assembler {
|
|
|
|
const AssemblerConfig m_config;
|
|
|
|
// The way segments
|
|
osmium::area::detail::SegmentList m_segment_list;
|
|
|
|
// The rings we are building from the way segments
|
|
std::list<ProtoRing> m_rings;
|
|
|
|
std::vector<ProtoRing*> m_outer_rings;
|
|
std::vector<ProtoRing*> m_inner_rings;
|
|
|
|
int m_inner_outer_mismatches { 0 };
|
|
|
|
bool debug() const {
|
|
return m_config.debug;
|
|
}
|
|
|
|
/**
|
|
* Checks whether the given NodeRefs have the same location.
|
|
* Uses the actual location for the test, not the id. If both
|
|
* have the same location, but not the same id, a problem
|
|
* point will be added to the list of problem points.
|
|
*/
|
|
bool has_same_location(const osmium::NodeRef& nr1, const osmium::NodeRef& nr2) {
|
|
if (nr1.location() != nr2.location()) {
|
|
return false;
|
|
}
|
|
if (nr1.ref() != nr2.ref()) {
|
|
if (m_config.problem_reporter) {
|
|
m_config.problem_reporter->report_duplicate_node(nr1.ref(), nr2.ref(), nr1.location());
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void add_tags_to_area(osmium::builder::AreaBuilder& builder, const osmium::Way& way) const {
|
|
osmium::builder::TagListBuilder tl_builder(builder.buffer(), &builder);
|
|
for (const osmium::Tag& tag : way.tags()) {
|
|
tl_builder.add_tag(tag.key(), tag.value());
|
|
}
|
|
}
|
|
|
|
void add_common_tags(osmium::builder::TagListBuilder& tl_builder, std::set<const osmium::Way*>& ways) const {
|
|
std::map<std::string, size_t> counter;
|
|
for (const osmium::Way* way : ways) {
|
|
for (const auto& tag : way->tags()) {
|
|
std::string kv {tag.key()};
|
|
kv.append(1, '\0');
|
|
kv.append(tag.value());
|
|
++counter[kv];
|
|
}
|
|
}
|
|
|
|
size_t num_ways = ways.size();
|
|
for (const auto& t_c : counter) {
|
|
if (debug()) {
|
|
std::cerr << " tag " << t_c.first << " is used " << t_c.second << " times in " << num_ways << " ways\n";
|
|
}
|
|
if (t_c.second == num_ways) {
|
|
size_t len = std::strlen(t_c.first.c_str());
|
|
tl_builder.add_tag(t_c.first.c_str(), t_c.first.c_str() + len + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
struct MPFilter : public osmium::tags::KeyFilter {
|
|
|
|
MPFilter() : osmium::tags::KeyFilter(true) {
|
|
add(false, "type");
|
|
add(false, "created_by");
|
|
add(false, "source");
|
|
add(false, "note");
|
|
add(false, "test:id");
|
|
add(false, "test:section");
|
|
}
|
|
|
|
}; // struct MPFilter
|
|
|
|
static MPFilter& filter() {
|
|
static MPFilter filter;
|
|
return filter;
|
|
}
|
|
|
|
void add_tags_to_area(osmium::builder::AreaBuilder& builder, const osmium::Relation& relation) const {
|
|
const auto count = std::count_if(relation.tags().begin(), relation.tags().end(), filter());
|
|
|
|
if (debug()) {
|
|
std::cerr << " found " << count << " tags on relation (without ignored ones)\n";
|
|
}
|
|
|
|
if (count > 0) {
|
|
if (debug()) {
|
|
std::cerr << " use tags from relation\n";
|
|
}
|
|
|
|
// write out all tags except type=*
|
|
osmium::builder::TagListBuilder tl_builder(builder.buffer(), &builder);
|
|
for (const osmium::Tag& tag : relation.tags()) {
|
|
if (strcmp(tag.key(), "type")) {
|
|
tl_builder.add_tag(tag.key(), tag.value());
|
|
}
|
|
}
|
|
} else {
|
|
if (debug()) {
|
|
std::cerr << " use tags from outer ways\n";
|
|
}
|
|
std::set<const osmium::Way*> ways;
|
|
for (const auto& ring : m_outer_rings) {
|
|
ring->get_ways(ways);
|
|
}
|
|
if (ways.size() == 1) {
|
|
if (debug()) {
|
|
std::cerr << " only one outer way\n";
|
|
}
|
|
osmium::builder::TagListBuilder tl_builder(builder.buffer(), &builder);
|
|
for (const osmium::Tag& tag : (*ways.begin())->tags()) {
|
|
tl_builder.add_tag(tag.key(), tag.value());
|
|
}
|
|
} else {
|
|
if (debug()) {
|
|
std::cerr << " multiple outer ways, get common tags\n";
|
|
}
|
|
osmium::builder::TagListBuilder tl_builder(builder.buffer(), &builder);
|
|
add_common_tags(tl_builder, ways);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Go through all the rings and find rings that are not closed.
|
|
* Problems are reported through the problem reporter.
|
|
*
|
|
* @returns true if any rings were not closed, false otherwise
|
|
*/
|
|
bool check_for_open_rings() {
|
|
bool open_rings = false;
|
|
|
|
for (const auto& ring : m_rings) {
|
|
if (!ring.closed()) {
|
|
open_rings = true;
|
|
if (m_config.problem_reporter) {
|
|
m_config.problem_reporter->report_ring_not_closed(ring.get_segment_front().first().location(), ring.get_segment_back().second().location());
|
|
}
|
|
}
|
|
}
|
|
|
|
return open_rings;
|
|
}
|
|
|
|
/**
|
|
* Check whether there are any rings that can be combined with the
|
|
* given ring to one larger ring by appending the other ring to
|
|
* the end of this ring.
|
|
* If the rings can be combined they are and the function returns
|
|
* true.
|
|
*/
|
|
bool possibly_combine_rings_back(ProtoRing& ring) {
|
|
const osmium::NodeRef& nr = ring.get_segment_back().second();
|
|
|
|
if (debug()) {
|
|
std::cerr << " possibly_combine_rings_back()\n";
|
|
}
|
|
for (auto it = m_rings.begin(); it != m_rings.end(); ++it) {
|
|
if (&*it != &ring && !it->closed()) {
|
|
if (has_same_location(nr, it->get_segment_front().first())) {
|
|
if (debug()) {
|
|
std::cerr << " ring.last=it->first\n";
|
|
}
|
|
ring.merge_ring(*it, debug());
|
|
m_rings.erase(it);
|
|
return true;
|
|
}
|
|
if (has_same_location(nr, it->get_segment_back().second())) {
|
|
if (debug()) {
|
|
std::cerr << " ring.last=it->last\n";
|
|
}
|
|
ring.merge_ring_reverse(*it, debug());
|
|
m_rings.erase(it);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Check whether there are any rings that can be combined with the
|
|
* given ring to one larger ring by prepending the other ring to
|
|
* the start of this ring.
|
|
* If the rings can be combined they are and the function returns
|
|
* true.
|
|
*/
|
|
bool possibly_combine_rings_front(ProtoRing& ring) {
|
|
const osmium::NodeRef& nr = ring.get_segment_front().first();
|
|
|
|
if (debug()) {
|
|
std::cerr << " possibly_combine_rings_front()\n";
|
|
}
|
|
for (auto it = m_rings.begin(); it != m_rings.end(); ++it) {
|
|
if (&*it != &ring && !it->closed()) {
|
|
if (has_same_location(nr, it->get_segment_back().second())) {
|
|
if (debug()) {
|
|
std::cerr << " ring.first=it->last\n";
|
|
}
|
|
ring.swap_segments(*it);
|
|
ring.merge_ring(*it, debug());
|
|
m_rings.erase(it);
|
|
return true;
|
|
}
|
|
if (has_same_location(nr, it->get_segment_front().first())) {
|
|
if (debug()) {
|
|
std::cerr << " ring.first=it->first\n";
|
|
}
|
|
ring.reverse();
|
|
ring.merge_ring(*it, debug());
|
|
m_rings.erase(it);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void split_off_subring(osmium::area::detail::ProtoRing& ring, osmium::area::detail::ProtoRing::segments_type::iterator it, osmium::area::detail::ProtoRing::segments_type::iterator it_begin, osmium::area::detail::ProtoRing::segments_type::iterator it_end) {
|
|
if (debug()) {
|
|
std::cerr << " subring found at: " << *it << "\n";
|
|
}
|
|
ProtoRing new_ring(it_begin, it_end);
|
|
ring.remove_segments(it_begin, it_end);
|
|
if (debug()) {
|
|
std::cerr << " split into two rings:\n";
|
|
std::cerr << " " << new_ring << "\n";
|
|
std::cerr << " " << ring << "\n";
|
|
}
|
|
m_rings.push_back(std::move(new_ring));
|
|
}
|
|
|
|
bool has_closed_subring_back(ProtoRing& ring, const NodeRef& nr) {
|
|
if (ring.segments().size() < 3) {
|
|
return false;
|
|
}
|
|
if (debug()) {
|
|
std::cerr << " has_closed_subring_back()\n";
|
|
}
|
|
const auto end = ring.segments().end();
|
|
for (auto it = ring.segments().begin() + 1; it != end - 1; ++it) {
|
|
if (has_same_location(nr, it->first())) {
|
|
split_off_subring(ring, it, it, end);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool has_closed_subring_front(ProtoRing& ring, const NodeRef& nr) {
|
|
if (ring.segments().size() < 3) {
|
|
return false;
|
|
}
|
|
if (debug()) {
|
|
std::cerr << " has_closed_subring_front()\n";
|
|
}
|
|
const auto end = ring.segments().end();
|
|
for (auto it = ring.segments().begin() + 1; it != end - 1; ++it) {
|
|
if (has_same_location(nr, it->second())) {
|
|
split_off_subring(ring, it, ring.segments().begin(), it+1);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool check_for_closed_subring(ProtoRing& ring) {
|
|
if (debug()) {
|
|
std::cerr << " check_for_closed_subring()\n";
|
|
}
|
|
|
|
osmium::area::detail::ProtoRing::segments_type segments(ring.segments().size());
|
|
std::copy(ring.segments().begin(), ring.segments().end(), segments.begin());
|
|
std::sort(segments.begin(), segments.end());
|
|
const auto it = std::adjacent_find(segments.begin(), segments.end(), [this](const osmium::area::detail::NodeRefSegment& s1, const osmium::area::detail::NodeRefSegment& s2) {
|
|
return has_same_location(s1.first(), s2.first());
|
|
});
|
|
if (it == segments.end()) {
|
|
return false;
|
|
}
|
|
const auto r1 = std::find_first_of(ring.segments().begin(), ring.segments().end(), it, it+1);
|
|
assert(r1 != ring.segments().end());
|
|
const auto r2 = std::find_first_of(ring.segments().begin(), ring.segments().end(), it+1, it+2);
|
|
assert(r2 != ring.segments().end());
|
|
|
|
if (debug()) {
|
|
std::cerr << " found subring in ring " << ring << " at " << it->first() << "\n";
|
|
}
|
|
|
|
const auto m = std::minmax(r1, r2);
|
|
|
|
ProtoRing new_ring(m.first, m.second);
|
|
ring.remove_segments(m.first, m.second);
|
|
|
|
if (debug()) {
|
|
std::cerr << " split ring1=" << new_ring << "\n";
|
|
std::cerr << " split ring2=" << ring << "\n";
|
|
}
|
|
|
|
m_rings.emplace_back(new_ring);
|
|
|
|
return true;
|
|
}
|
|
|
|
void combine_rings_front(const osmium::area::detail::NodeRefSegment& segment, ProtoRing& ring) {
|
|
if (debug()) {
|
|
std::cerr << " => match at front of ring\n";
|
|
}
|
|
ring.add_segment_front(segment);
|
|
has_closed_subring_front(ring, segment.first());
|
|
if (possibly_combine_rings_front(ring)) {
|
|
check_for_closed_subring(ring);
|
|
}
|
|
}
|
|
|
|
void combine_rings_back(const osmium::area::detail::NodeRefSegment& segment, ProtoRing& ring) {
|
|
if (debug()) {
|
|
std::cerr << " => match at back of ring\n";
|
|
}
|
|
ring.add_segment_back(segment);
|
|
has_closed_subring_back(ring, segment.second());
|
|
if (possibly_combine_rings_back(ring)) {
|
|
check_for_closed_subring(ring);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Append each outer ring together with its inner rings to the
|
|
* area in the buffer.
|
|
*/
|
|
void add_rings_to_area(osmium::builder::AreaBuilder& builder) const {
|
|
for (const ProtoRing* ring : m_outer_rings) {
|
|
if (debug()) {
|
|
std::cerr << " ring " << *ring << " is outer\n";
|
|
}
|
|
{
|
|
osmium::builder::OuterRingBuilder ring_builder(builder.buffer(), &builder);
|
|
ring_builder.add_node_ref(ring->get_segment_front().first());
|
|
for (const auto& segment : ring->segments()) {
|
|
ring_builder.add_node_ref(segment.second());
|
|
}
|
|
}
|
|
for (ProtoRing* inner : ring->inner_rings()) {
|
|
osmium::builder::InnerRingBuilder ring_builder(builder.buffer(), &builder);
|
|
ring_builder.add_node_ref(inner->get_segment_front().first());
|
|
for (const auto& segment : inner->segments()) {
|
|
ring_builder.add_node_ref(segment.second());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool add_to_existing_ring(osmium::area::detail::NodeRefSegment segment) {
|
|
int n = 0;
|
|
for (auto& ring : m_rings) {
|
|
if (debug()) {
|
|
std::cerr << " check against ring " << n << " " << ring;
|
|
}
|
|
if (ring.closed()) {
|
|
if (debug()) {
|
|
std::cerr << " => ring CLOSED\n";
|
|
}
|
|
} else {
|
|
if (has_same_location(ring.get_segment_back().second(), segment.first())) {
|
|
combine_rings_back(segment, ring);
|
|
return true;
|
|
}
|
|
if (has_same_location(ring.get_segment_back().second(), segment.second())) {
|
|
segment.swap_locations();
|
|
combine_rings_back(segment, ring);
|
|
return true;
|
|
}
|
|
if (has_same_location(ring.get_segment_front().first(), segment.first())) {
|
|
segment.swap_locations();
|
|
combine_rings_front(segment, ring);
|
|
return true;
|
|
}
|
|
if (has_same_location(ring.get_segment_front().first(), segment.second())) {
|
|
combine_rings_front(segment, ring);
|
|
return true;
|
|
}
|
|
if (debug()) {
|
|
std::cerr << " => no match\n";
|
|
}
|
|
}
|
|
|
|
++n;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void check_inner_outer(ProtoRing& ring) {
|
|
const osmium::NodeRef& min_node = ring.min_node();
|
|
if (debug()) {
|
|
std::cerr << " check_inner_outer min_node=" << min_node << "\n";
|
|
}
|
|
|
|
int count = 0;
|
|
int above = 0;
|
|
|
|
for (auto it = m_segment_list.begin(); it != m_segment_list.end() && it->first().location().x() <= min_node.location().x(); ++it) {
|
|
if (!ring.contains(*it)) {
|
|
if (debug()) {
|
|
std::cerr << " segments for count: " << *it;
|
|
}
|
|
if (it->to_left_of(min_node.location())) {
|
|
++count;
|
|
if (debug()) {
|
|
std::cerr << " counted\n";
|
|
}
|
|
} else {
|
|
if (debug()) {
|
|
std::cerr << " not counted\n";
|
|
}
|
|
}
|
|
if (it->first().location() == min_node.location()) {
|
|
if (it->second().location().y() > min_node.location().y()) {
|
|
++above;
|
|
}
|
|
}
|
|
if (it->second().location() == min_node.location()) {
|
|
if (it->first().location().y() > min_node.location().y()) {
|
|
++above;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (debug()) {
|
|
std::cerr << " count=" << count << " above=" << above << "\n";
|
|
}
|
|
|
|
count += above % 2;
|
|
|
|
if (count % 2) {
|
|
ring.set_inner();
|
|
}
|
|
}
|
|
|
|
void check_inner_outer_roles() {
|
|
if (debug()) {
|
|
std::cerr << " check_inner_outer_roles\n";
|
|
}
|
|
|
|
for (const auto ringptr : m_outer_rings) {
|
|
for (const auto& segment : ringptr->segments()) {
|
|
if (!segment.role_outer()) {
|
|
++m_inner_outer_mismatches;
|
|
if (debug()) {
|
|
std::cerr << " segment " << segment << " from way " << segment.way()->id() << " should have role 'outer'\n";
|
|
}
|
|
if (m_config.problem_reporter) {
|
|
m_config.problem_reporter->report_role_should_be_outer(segment.way()->id(), segment.first().location(), segment.second().location());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (const auto ringptr : m_inner_rings) {
|
|
for (const auto& segment : ringptr->segments()) {
|
|
if (!segment.role_inner()) {
|
|
++m_inner_outer_mismatches;
|
|
if (debug()) {
|
|
std::cerr << " segment " << segment << " from way " << segment.way()->id() << " should have role 'inner'\n";
|
|
}
|
|
if (m_config.problem_reporter) {
|
|
m_config.problem_reporter->report_role_should_be_inner(segment.way()->id(), segment.first().location(), segment.second().location());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create rings from segments.
|
|
*/
|
|
bool create_rings() {
|
|
m_segment_list.sort();
|
|
m_segment_list.erase_duplicate_segments();
|
|
|
|
// Now we look for segments crossing each other. If there are
|
|
// any, the multipolygon is invalid.
|
|
// In the future this could be improved by trying to fix those
|
|
// cases.
|
|
if (m_segment_list.find_intersections(m_config.problem_reporter)) {
|
|
return false;
|
|
}
|
|
|
|
// Now iterator over all segments and add them to rings. Each segment
|
|
// is tacked on to either end of an existing ring if possible, or a
|
|
// new ring is started with it.
|
|
for (const auto& segment : m_segment_list) {
|
|
if (debug()) {
|
|
std::cerr << " checking segment " << segment << "\n";
|
|
}
|
|
if (!add_to_existing_ring(segment)) {
|
|
if (debug()) {
|
|
std::cerr << " new ring for segment " << segment << "\n";
|
|
}
|
|
m_rings.emplace_back(segment);
|
|
}
|
|
}
|
|
|
|
if (debug()) {
|
|
std::cerr << " Rings:\n";
|
|
for (const auto& ring : m_rings) {
|
|
std::cerr << " " << ring;
|
|
if (ring.closed()) {
|
|
std::cerr << " (closed)";
|
|
}
|
|
std::cerr << "\n";
|
|
}
|
|
}
|
|
|
|
if (check_for_open_rings()) {
|
|
if (debug()) {
|
|
std::cerr << " not all rings are closed\n";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
if (debug()) {
|
|
std::cerr << " Find inner/outer...\n";
|
|
}
|
|
|
|
if (m_rings.size() == 1) {
|
|
m_outer_rings.push_back(&m_rings.front());
|
|
} else {
|
|
for (auto& ring : m_rings) {
|
|
check_inner_outer(ring);
|
|
if (ring.outer()) {
|
|
if (!ring.is_cw()) {
|
|
ring.reverse();
|
|
}
|
|
m_outer_rings.push_back(&ring);
|
|
} else {
|
|
if (ring.is_cw()) {
|
|
ring.reverse();
|
|
}
|
|
m_inner_rings.push_back(&ring);
|
|
}
|
|
}
|
|
|
|
if (m_outer_rings.size() == 1) {
|
|
for (auto inner : m_inner_rings) {
|
|
m_outer_rings.front()->add_inner_ring(inner);
|
|
}
|
|
} else {
|
|
// sort outer rings by size, smallest first
|
|
std::sort(m_outer_rings.begin(), m_outer_rings.end(), [](ProtoRing* a, ProtoRing* b) {
|
|
return a->area() < b->area();
|
|
});
|
|
for (auto inner : m_inner_rings) {
|
|
for (auto outer : m_outer_rings) {
|
|
if (inner->is_in(outer)) {
|
|
outer->add_inner_ring(inner);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
check_inner_outer_roles();
|
|
|
|
return true;
|
|
}
|
|
|
|
public:
|
|
|
|
typedef osmium::area::AssemblerConfig config_type;
|
|
|
|
explicit Assembler(const config_type& config) :
|
|
m_config(config),
|
|
m_segment_list(config.debug) {
|
|
}
|
|
|
|
~Assembler() = default;
|
|
|
|
/**
|
|
* Assemble an area from the given way.
|
|
* The resulting area is put into the out_buffer.
|
|
*/
|
|
void operator()(const osmium::Way& way, osmium::memory::Buffer& out_buffer) {
|
|
if (m_config.problem_reporter) {
|
|
m_config.problem_reporter->set_object(osmium::item_type::way, way.id());
|
|
}
|
|
|
|
if (!way.ends_have_same_id()) {
|
|
if (m_config.problem_reporter) {
|
|
m_config.problem_reporter->report_duplicate_node(way.nodes().front().ref(), way.nodes().back().ref(), way.nodes().front().location());
|
|
}
|
|
}
|
|
|
|
m_segment_list.extract_segments_from_way(way, "outer");
|
|
|
|
if (debug()) {
|
|
std::cerr << "\nBuild way id()=" << way.id() << " segments.size()=" << m_segment_list.size() << "\n";
|
|
}
|
|
|
|
// Now create the Area object and add the attributes and tags
|
|
// from the relation.
|
|
{
|
|
osmium::builder::AreaBuilder builder(out_buffer);
|
|
builder.initialize_from_object(way);
|
|
|
|
if (create_rings()) {
|
|
add_tags_to_area(builder, way);
|
|
add_rings_to_area(builder);
|
|
}
|
|
}
|
|
out_buffer.commit();
|
|
}
|
|
|
|
/**
|
|
* Assemble an area from the given relation and its members.
|
|
* All members are to be found in the in_buffer at the offsets
|
|
* given by the members parameter.
|
|
* The resulting area is put into the out_buffer.
|
|
*/
|
|
void operator()(const osmium::Relation& relation, const std::vector<size_t>& members, const osmium::memory::Buffer& in_buffer, osmium::memory::Buffer& out_buffer) {
|
|
if (m_config.problem_reporter) {
|
|
m_config.problem_reporter->set_object(osmium::item_type::relation, relation.id());
|
|
}
|
|
|
|
m_segment_list.extract_segments_from_ways(relation, members, in_buffer);
|
|
|
|
if (debug()) {
|
|
std::cerr << "\nBuild relation id()=" << relation.id() << " members.size()=" << members.size() << " segments.size()=" << m_segment_list.size() << "\n";
|
|
}
|
|
|
|
size_t area_offset = out_buffer.committed();
|
|
|
|
// Now create the Area object and add the attributes and tags
|
|
// from the relation.
|
|
{
|
|
osmium::builder::AreaBuilder builder(out_buffer);
|
|
builder.initialize_from_object(relation);
|
|
|
|
if (create_rings()) {
|
|
add_tags_to_area(builder, relation);
|
|
add_rings_to_area(builder);
|
|
}
|
|
}
|
|
out_buffer.commit();
|
|
|
|
const osmium::TagList& area_tags = out_buffer.get<osmium::Area>(area_offset).tags(); // tags of the area we just built
|
|
|
|
// Find all closed ways that are inner rings and check their
|
|
// tags. If they are not the same as the tags of the area we
|
|
// just built, add them to a list and later build areas for
|
|
// them, too.
|
|
std::vector<const osmium::Way*> ways_that_should_be_areas;
|
|
if (m_inner_outer_mismatches == 0) {
|
|
auto memit = relation.members().begin();
|
|
for (size_t offset : members) {
|
|
if (!std::strcmp(memit->role(), "inner")) {
|
|
const osmium::Way& way = in_buffer.get<const osmium::Way>(offset);
|
|
if (way.is_closed() && way.tags().size() > 0) {
|
|
auto d = std::count_if(way.tags().begin(), way.tags().end(), filter());
|
|
if (d > 0) {
|
|
osmium::tags::KeyFilter::iterator way_fi_begin(filter(), way.tags().begin(), way.tags().end());
|
|
osmium::tags::KeyFilter::iterator way_fi_end(filter(), way.tags().end(), way.tags().end());
|
|
osmium::tags::KeyFilter::iterator area_fi_begin(filter(), area_tags.begin(), area_tags.end());
|
|
osmium::tags::KeyFilter::iterator area_fi_end(filter(), area_tags.end(), area_tags.end());
|
|
|
|
if (!std::equal(way_fi_begin, way_fi_end, area_fi_begin) || d != std::distance(area_fi_begin, area_fi_end)) {
|
|
ways_that_should_be_areas.push_back(&way);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
++memit;
|
|
}
|
|
}
|
|
|
|
// Now build areas for all ways found in the last step.
|
|
for (const osmium::Way* way : ways_that_should_be_areas) {
|
|
Assembler assembler(m_config);
|
|
assembler(*way, out_buffer);
|
|
}
|
|
}
|
|
|
|
}; // class Assembler
|
|
|
|
} // namespace area
|
|
|
|
} // namespace osmium
|
|
|
|
#endif // OSMIUM_AREA_ASSEMBLER_HPP
|