|
| 1 | +/** |
| 2 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + * |
| 4 | + * This file is part of osm2pgsql (https://osm2pgsql.org/). |
| 5 | + * |
| 6 | + * Copyright (C) 2006-2022 by the osm2pgsql developer community. |
| 7 | + * For a full list of authors see the git log. |
| 8 | + */ |
| 9 | + |
| 10 | +#include "geom-from-osm.hpp" |
| 11 | +#include "osmtypes.hpp" |
| 12 | + |
| 13 | +#include <osmium/area/geom_assembler.hpp> |
| 14 | +#include <osmium/osm/way.hpp> |
| 15 | + |
| 16 | +#include <utility> |
| 17 | + |
| 18 | +namespace geom { |
| 19 | + |
| 20 | +geometry_t create_point(osmium::Node const &node) |
| 21 | +{ |
| 22 | + return geometry_t{point_t{node.location()}}; |
| 23 | +} |
| 24 | + |
| 25 | +static void fill_point_list(point_list_t *list, |
| 26 | + osmium::NodeRefList const &nodes) |
| 27 | +{ |
| 28 | + osmium::Location last{}; |
| 29 | + |
| 30 | + for (auto const &node : nodes) { |
| 31 | + auto const loc = node.location(); |
| 32 | + if (loc.valid() && loc != last) { |
| 33 | + list->emplace_back(loc); |
| 34 | + last = loc; |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +geometry_t create_linestring(osmium::Way const &way) |
| 40 | +{ |
| 41 | + geometry_t geom{linestring_t{}}; |
| 42 | + auto &line = geom.get<linestring_t>(); |
| 43 | + |
| 44 | + fill_point_list(&line, way.nodes()); |
| 45 | + |
| 46 | + // Return nullgeom_t if the line geometry is invalid |
| 47 | + if (line.size() <= 1U) { |
| 48 | + geom.reset(); |
| 49 | + } |
| 50 | + |
| 51 | + return geom; |
| 52 | +} |
| 53 | + |
| 54 | +geometry_t create_polygon(osmium::Way const &way) |
| 55 | +{ |
| 56 | + geometry_t geom{polygon_t{}}; |
| 57 | + |
| 58 | + // A closed way with less than 4 nodes can never be a valid polygon |
| 59 | + if (way.nodes().size() < 4U) { |
| 60 | + geom.reset(); |
| 61 | + return geom; |
| 62 | + } |
| 63 | + |
| 64 | + osmium::area::AssemblerConfig area_config; |
| 65 | + area_config.ignore_invalid_locations = true; |
| 66 | + osmium::area::GeomAssembler assembler{area_config}; |
| 67 | + osmium::memory::Buffer area_buffer{1024}; |
| 68 | + |
| 69 | + if (!assembler(way, area_buffer)) { |
| 70 | + geom.reset(); |
| 71 | + return geom; |
| 72 | + } |
| 73 | + |
| 74 | + auto const &area = area_buffer.get<osmium::Area>(0); |
| 75 | + auto const &ring = *area.begin<osmium::OuterRing>(); |
| 76 | + |
| 77 | + fill_point_list(&geom.get<polygon_t>().outer(), ring); |
| 78 | + |
| 79 | + return geom; |
| 80 | +} |
| 81 | + |
| 82 | +geometry_t create_multilinestring(osmium::memory::Buffer const &ways) |
| 83 | +{ |
| 84 | + geometry_t geom{multilinestring_t{}}; |
| 85 | + auto &mls = geom.get<multilinestring_t>(); |
| 86 | + |
| 87 | + for (auto const &way : ways.select<osmium::Way>()) { |
| 88 | + linestring_t line; |
| 89 | + fill_point_list(&line, way.nodes()); |
| 90 | + if (line.size() >= 2U) { |
| 91 | + mls.add_geometry(std::move(line)); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (mls.num_geometries() == 0) { |
| 96 | + geom.reset(); |
| 97 | + } |
| 98 | + |
| 99 | + return geom; |
| 100 | +} |
| 101 | + |
| 102 | +static void fill_polygon(polygon_t *polygon, osmium::Area const &area, |
| 103 | + osmium::OuterRing const &outer_ring) |
| 104 | +{ |
| 105 | + assert(polygon->inners().empty()); |
| 106 | + |
| 107 | + for (auto const &nr : outer_ring) { |
| 108 | + polygon->outer().emplace_back(nr.location()); |
| 109 | + } |
| 110 | + |
| 111 | + for (auto const &inner_ring : area.inner_rings(outer_ring)) { |
| 112 | + auto &ring = polygon->inners().emplace_back(); |
| 113 | + for (auto const &nr : inner_ring) { |
| 114 | + ring.emplace_back(nr.location()); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +geometry_t create_multipolygon(osmium::Relation const &relation, |
| 120 | + osmium::memory::Buffer const &way_buffer) |
| 121 | +{ |
| 122 | + geometry_t geom{}; |
| 123 | + |
| 124 | + osmium::area::AssemblerConfig area_config; |
| 125 | + area_config.ignore_invalid_locations = true; |
| 126 | + osmium::area::GeomAssembler assembler{area_config}; |
| 127 | + osmium::memory::Buffer area_buffer{1024}; |
| 128 | + |
| 129 | + if (!assembler(relation, way_buffer, area_buffer)) { |
| 130 | + return geom; |
| 131 | + } |
| 132 | + |
| 133 | + auto const &area = area_buffer.get<osmium::Area>(0); |
| 134 | + |
| 135 | + if (area.is_multipolygon()) { |
| 136 | + auto &multipolygon = geom.set<multipolygon_t>(); |
| 137 | + |
| 138 | + for (auto const &outer : area.outer_rings()) { |
| 139 | + auto &polygon = multipolygon.add_geometry(); |
| 140 | + fill_polygon(&polygon, area, outer); |
| 141 | + } |
| 142 | + } else { |
| 143 | + auto &polygon = geom.set<polygon_t>(); |
| 144 | + fill_polygon(&polygon, area, *area.outer_rings().begin()); |
| 145 | + } |
| 146 | + |
| 147 | + return geom; |
| 148 | +} |
| 149 | + |
| 150 | +} // namespace geom |
0 commit comments