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