|
| 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-2023 by the osm2pgsql developer community. |
| 7 | + * For a full list of authors see the git log. |
| 8 | + */ |
| 9 | + |
| 10 | +#include "canvas.hpp" |
| 11 | +#include "raster.hpp" |
| 12 | + |
| 13 | +cimg_library::CImg<int> canvas_t::create_pointlist(geom::point_list_t const &pl, |
| 14 | + tile_t const &tile) const |
| 15 | +{ |
| 16 | + cimg_library::CImg<int> points{static_cast<unsigned int>(pl.size()), 2}; |
| 17 | + |
| 18 | + int n = 0; |
| 19 | + for (auto const point : pl) { |
| 20 | + auto const tp = tile.to_tile_coords(point, m_extent); |
| 21 | + points(n, 0) = static_cast<int>(static_cast<double>(m_buffer) + tp.x()); |
| 22 | + points(n, 1) = |
| 23 | + static_cast<int>(static_cast<double>(m_buffer + m_extent) - tp.y()); |
| 24 | + ++n; |
| 25 | + } |
| 26 | + |
| 27 | + return points; |
| 28 | +} |
| 29 | + |
| 30 | +std::size_t canvas_t::draw_polygon(geom::polygon_t const &polygon, |
| 31 | + tile_t const &tile) |
| 32 | +{ |
| 33 | + if (polygon.inners().empty()) { |
| 34 | + m_rast.draw_polygon(create_pointlist(polygon.outer(), tile), &White); |
| 35 | + return polygon.outer().size(); |
| 36 | + } |
| 37 | + |
| 38 | + std::size_t num_points = polygon.outer().size(); |
| 39 | + m_temp.draw_polygon(create_pointlist(polygon.outer(), tile), &White); |
| 40 | + for (auto const &inner : polygon.inners()) { |
| 41 | + num_points += inner.size(); |
| 42 | + m_temp.draw_polygon(create_pointlist(inner, tile), &Black); |
| 43 | + } |
| 44 | + m_rast |= m_temp; |
| 45 | + |
| 46 | + return num_points; |
| 47 | +} |
| 48 | + |
| 49 | +std::size_t canvas_t::draw_linestring(geom::linestring_t const &linestring, |
| 50 | + tile_t const &tile) |
| 51 | +{ |
| 52 | + m_rast.draw_line(create_pointlist(linestring, tile), &White); |
| 53 | + return linestring.size(); |
| 54 | +} |
| 55 | + |
| 56 | +std::size_t canvas_t::draw(geom::geometry_t const &geometry, tile_t const &tile) |
| 57 | +{ |
| 58 | + if (geometry.is_linestring()) { |
| 59 | + auto const &linestring = geometry.get<geom::linestring_t>(); |
| 60 | + return draw_linestring(linestring, tile); |
| 61 | + } |
| 62 | + |
| 63 | + if (geometry.is_polygon()) { |
| 64 | + auto const &polygon = geometry.get<geom::polygon_t>(); |
| 65 | + return draw_polygon(polygon, tile); |
| 66 | + } |
| 67 | + |
| 68 | + if (geometry.is_multipolygon()) { |
| 69 | + auto const &mp = geometry.get<geom::multipolygon_t>(); |
| 70 | + std::size_t num_points = 0; |
| 71 | + for (auto const &p : mp) { |
| 72 | + num_points += draw_polygon(p, tile); |
| 73 | + } |
| 74 | + return num_points; |
| 75 | + } |
| 76 | + |
| 77 | + // XXX other geometry types? |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +void canvas_t::save(std::string const &filename) const |
| 83 | +{ |
| 84 | + m_rast.save(filename.c_str()); |
| 85 | +} |
| 86 | + |
| 87 | +std::string canvas_t::to_wkb(tile_t const &tile, double margin) const |
| 88 | +{ |
| 89 | + std::string wkb; |
| 90 | + wkb.reserve(61 + 2 + m_rast.size()); |
| 91 | + |
| 92 | + // header |
| 93 | + wkb_raster_header header{}; |
| 94 | + header.nBands = 1; |
| 95 | + header.scaleX = tile.extent() / static_cast<double>(m_extent); |
| 96 | + header.scaleY = -header.scaleX; |
| 97 | + header.ipX = tile.xmin(margin); |
| 98 | + header.ipY = tile.ymax(margin); |
| 99 | + header.width = m_extent + 2 * m_buffer; |
| 100 | + header.height = header.width; |
| 101 | + add_raster_header(&wkb, header); |
| 102 | + |
| 103 | + // band |
| 104 | + wkb_raster_band band{}; |
| 105 | + band.bits = 4; |
| 106 | + add_raster_band(&wkb, band); |
| 107 | + |
| 108 | + // rasterdata |
| 109 | + wkb.append(reinterpret_cast<char const *>(m_rast.data()), m_rast.size()); |
| 110 | + |
| 111 | + assert(wkb.size() == 61 + 2 + m_rast.size()); |
| 112 | + |
| 113 | + return wkb; |
| 114 | +} |
| 115 | + |
| 116 | +void canvas_t::merge(canvas_t const &other) { m_rast |= other.m_rast; } |
| 117 | + |
| 118 | +std::string to_hex(std::string const &in) |
| 119 | +{ |
| 120 | + std::string result; |
| 121 | + char const *const lookup_hex = "0123456789ABCDEF"; |
| 122 | + |
| 123 | + for (const auto c : in) { |
| 124 | + unsigned int const num = static_cast<unsigned char>(c); |
| 125 | + result += lookup_hex[(num >> 4U) & 0xfU]; |
| 126 | + result += lookup_hex[num & 0xfU]; |
| 127 | + } |
| 128 | + |
| 129 | + return result; |
| 130 | +} |
0 commit comments