Skip to content

Commit d901e78

Browse files
authored
Merge GH-1822 (KMZ export improvements)
2 parents db05e82 + dab6081 commit d901e78

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

Diff for: src/gdal/kmz_groundoverlay_export.cpp

+21-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "kmz_groundoverlay_export.h"
2121

2222
#include <algorithm>
23+
#include <cmath>
2324
#include <iterator>
2425
#include <vector>
2526

@@ -147,8 +148,10 @@ KmzGroundOverlayExport::Metrics KmzGroundOverlayExport::makeMetrics(qreal const
147148
KmzGroundOverlayExport::~KmzGroundOverlayExport() = default;
148149

149150
KmzGroundOverlayExport::KmzGroundOverlayExport(const QString& path, const Map& map)
150-
: map(map)
151-
, is_kmz(path.endsWith(QLatin1String(".kmz"), Qt::CaseInsensitive))
151+
: map(map)
152+
, overlap(std::max(2 * (std::nexttoward(180.0, 181.0) - 180.0), 0.000000000000001))
153+
, precision(std::max(std::ceil(log(overlap)/log(0.1) + 0.5), 12.0))
154+
, is_kmz(path.endsWith(QLatin1String(".kmz"), Qt::CaseInsensitive))
152155
{
153156
auto const fileinfo = QFileInfo(path);
154157
if (is_kmz)
@@ -299,14 +302,19 @@ std::vector<KmzGroundOverlayExport::Tile> KmzGroundOverlayExport::makeTiles(cons
299302
auto const bounding_box_map = boundingBoxMap(georef, bounding_box_lonlat);
300303

301304
auto eastwards = QLineF::fromPolar(metrics.tile_size_mm, georef.getDeclination()).p2();
302-
auto southwards = QLineF::fromPolar(metrics.tile_size_mm, georef.getDeclination() + 270).p2();
305+
auto northwards = QLineF::fromPolar(metrics.tile_size_mm, georef.getDeclination() + 90).p2();
303306

304307
auto const start_map = georef.toMapCoordF(fromLonLat(bounding_box_lonlat.topLeft()));
305308
auto tile_lonlat = QRectF(bounding_box_lonlat.topLeft(),
306-
toLonLat(georef.toGeographicCoords(MapCoordF(start_map + eastwards + southwards)))).normalized();
307-
while (tile_lonlat.top() < bounding_box_lonlat.bottom())
309+
toLonLat(georef.toGeographicCoords(MapCoordF(start_map + eastwards + northwards))))
310+
.normalized();
311+
auto const delta = QPointF(tile_lonlat.width() - overlap, tile_lonlat.height() - overlap);
312+
313+
auto const last_y = int(std::ceil((bounding_box_lonlat.height() - overlap) / delta.y()));
314+
for (int y = 0; y < last_y; ++y)
308315
{
309-
while (tile_lonlat.left() < bounding_box_lonlat.right())
316+
auto const last_x = int(std::ceil((bounding_box_lonlat.width() - overlap) / delta.x()));
317+
for (int x = 0; x < last_x; ++x)
310318
{
311319
MapCoordF tile_map[] = {
312320
georef.toMapCoordF(fromLonLat(tile_lonlat.topLeft())),
@@ -320,15 +328,15 @@ std::vector<KmzGroundOverlayExport::Tile> KmzGroundOverlayExport::makeTiles(cons
320328
&& std::any_of(begin(tile_map), end(tile_map), [&bounding_box_map](auto& p) { return p.y() >= bounding_box_map.top(); })
321329
&& std::any_of(begin(tile_map), end(tile_map), [&bounding_box_map](auto& p) { return p.y() <= bounding_box_map.bottom(); }))
322330
{
323-
auto const name = QByteArray(QByteArray::number(int(tiles.size())) + ".jpg");
331+
auto const name = QByteArray("tile_" + QByteArray::number(x) + '_' + QByteArray::number(y) + ".jpg");
324332
auto const filepath = QByteArray("files/" + name);
325333
tiles.push_back({name, filepath, tile_lonlat, boundingBox(tile_map[0], tile_map[1], tile_map[2], tile_map[3])});
326334
}
327335

328-
tile_lonlat.moveLeft(tile_lonlat.right());
336+
tile_lonlat.moveLeft(tile_lonlat.left() + delta.x());
329337
}
330338
tile_lonlat.moveLeft(bounding_box_lonlat.left());
331-
tile_lonlat.moveTop(tile_lonlat.bottom());
339+
tile_lonlat.moveTop(tile_lonlat.top() + delta.y());
332340
}
333341
return tiles;
334342
}
@@ -352,10 +360,10 @@ void KmzGroundOverlayExport::writeKml(QByteArray& buffer, const std::vector<KmzG
352360
" <href>").append(tile.filepath).append("</href>\n"
353361
" </Icon>\n"
354362
" <LatLonBox>\n"
355-
" <north>").append(QByteArray::number(tile.rect_lonlat.bottom(), 'f', 10)).append("</north>\n"
356-
" <south>").append(QByteArray::number(tile.rect_lonlat.top(), 'f', 10)).append("</south>\n"
357-
" <east>").append(QByteArray::number(tile.rect_lonlat.right(), 'f', 10)).append("</east>\n"
358-
" <west>").append(QByteArray::number(tile.rect_lonlat.left(), 'f', 10)).append("</west>\n"
363+
" <north>").append(QByteArray::number(tile.rect_lonlat.bottom(), 'f', precision)).append("</north>\n"
364+
" <south>").append(QByteArray::number(tile.rect_lonlat.top(), 'f', precision)).append("</south>\n"
365+
" <east>").append(QByteArray::number(tile.rect_lonlat.right(), 'f', precision)).append("</east>\n"
366+
" <west>").append(QByteArray::number(tile.rect_lonlat.left(), 'f', precision)).append("</west>\n"
359367
" <rotation>0</rotation>\n"
360368
" </LatLonBox>\n"
361369
" </GroundOverlay>\n"

Diff for: src/gdal/kmz_groundoverlay_export.h

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class KmzGroundOverlayExport
111111
QByteArray basepath_utf8;
112112
QByteArray doc_filepath_utf8;
113113
QString error_message;
114+
qreal overlap = 0.000000000001;
115+
int precision = 13;
114116
bool is_kmz = false;
115117

116118
};

0 commit comments

Comments
 (0)