Skip to content

Commit 15172fa

Browse files
authored
Merge GH-1410
GDAL/OGR template/import improvement
2 parents a5a12bb + 32aef5f commit 15172fa

File tree

5 files changed

+65
-48
lines changed

5 files changed

+65
-48
lines changed

src/gdal/gdal_manager.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 Kai Pastor
2+
* Copyright 2016-2019 Kai Pastor
33
*
44
* This file is part of OpenOrienteering.
55
*
@@ -68,6 +68,9 @@ class GdalManager::GdalManagerPrivate
6868
}
6969

7070
GdalManagerPrivate(const GdalManagerPrivate&) = delete;
71+
GdalManagerPrivate(GdalManagerPrivate&&) = delete;
72+
GdalManagerPrivate& operator=(const GdalManagerPrivate&) = delete;
73+
GdalManagerPrivate&& operator=(GdalManagerPrivate&&) = delete;
7174

7275
void configure()
7376
{

src/gdal/gdal_settings_page.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 Kai Pastor
2+
* Copyright 2016-2019 Kai Pastor
33
*
44
* This file is part of OpenOrienteering.
55
*
@@ -94,10 +94,8 @@ GdalSettingsPage::GdalSettingsPage(QWidget* parent)
9494
connect(parameters, &QTableWidget::cellChanged, this, &GdalSettingsPage::cellChange);
9595
}
9696

97-
GdalSettingsPage::~GdalSettingsPage()
98-
{
99-
// nothing, not inlined
100-
}
97+
GdalSettingsPage::~GdalSettingsPage() = default;
98+
10199

102100
QString GdalSettingsPage::title() const
103101
{

src/gdal/ogr_file_format.cpp

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 Kai Pastor
2+
* Copyright 2016-2019 Kai Pastor
33
*
44
* This file is part of OpenOrienteering.
55
*
@@ -21,6 +21,7 @@
2121
#include "ogr_file_format_p.h" // IWYU pragma: associated
2222

2323
#include <algorithm>
24+
#include <cmath>
2425
#include <cstddef>
2526
#include <functional>
2627
#include <iterator>
@@ -413,7 +414,7 @@ namespace {
413414
}
414415

415416

416-
class AverageLatLon
417+
class AverageCoords
417418
{
418419
private:
419420
double x = 0;
@@ -452,14 +453,8 @@ namespace {
452453
}
453454

454455
public:
455-
AverageLatLon(OGRDataSourceH data_source)
456+
AverageCoords(OGRDataSourceH data_source, OGRDataSourceH srs)
456457
{
457-
auto geo_srs = ogr::unique_srs { OSRNewSpatialReference(nullptr) };
458-
OSRSetWellKnownGeogCS(geo_srs.get(), "WGS84");
459-
#if GDAL_VERSION_MAJOR >= 3
460-
OSRSetAxisMappingStrategy(geo_srs.get(), OAMS_TRADITIONAL_GIS_ORDER);
461-
#endif
462-
463458
auto num_layers = OGR_DS_GetLayerCount(data_source);
464459
for (int i = 0; i < num_layers; ++i)
465460
{
@@ -469,7 +464,7 @@ namespace {
469464
if (!spatial_reference)
470465
continue;
471466

472-
auto transformation = ogr::unique_transformation{ OCTNewCoordinateTransformation(spatial_reference, geo_srs.get()) };
467+
auto transformation = ogr::unique_transformation{ OCTNewCoordinateTransformation(spatial_reference, srs) };
473468
if (!transformation)
474469
continue;
475470

@@ -490,9 +485,9 @@ namespace {
490485
}
491486
}
492487

493-
operator LatLon() const
488+
operator QPointF() const
494489
{
495-
return num_coords ? LatLon{ y / num_coords, x / num_coords } : LatLon{};
490+
return num_coords ? QPointF{ x / num_coords, y / num_coords } : QPointF{};
496491
}
497492

498493
};
@@ -546,7 +541,6 @@ OgrFileImport::OgrFileImport(const QString& path, Map* map, MapView* view, UnitT
546541
: Importer(path, map, view)
547542
, manager{ OGR_SM_Create(nullptr) }
548543
, unit_type{ unit_type }
549-
, georeferencing_import_enabled{ true }
550544
{
551545
GdalManager().configure();
552546

@@ -826,13 +820,16 @@ ogr::unique_srs OgrFileImport::importGeoreferencing(OGRDataSourceH data_source)
826820
if (projected_srs_spec)
827821
{
828822
// Found a suitable projected SRS
823+
auto center = calcAverageCoords(data_source, suitable_srs.get());
829824
auto georef = map->getGeoreferencing(); // copy
830825
georef.setProjectedCRS(QStringLiteral("PROJ.4"), QString::fromLatin1(projected_srs_spec));
826+
georef.setProjectedRefPoint({std::round(center.x()), std::round(center.y())});
831827
map->setGeoreferencing(georef);
832828
CPLFree(projected_srs_spec);
833829
return suitable_srs;
834830
}
835-
else if (suitable_srs)
831+
832+
if (suitable_srs)
836833
{
837834
// Found a suitable SRS but it is not projected.
838835
// Setting up a local orthographic projection.
@@ -850,18 +847,17 @@ ogr::unique_srs OgrFileImport::importGeoreferencing(OGRDataSourceH data_source)
850847
map->setGeoreferencing(ortho_georef);
851848
return srsFromMap();
852849
}
853-
else if (local_srs || no_srs)
850+
851+
if (local_srs || no_srs)
854852
{
855853
auto georef = Georeferencing();
856854
georef.setScaleDenominator(int(map->getScaleDenominator()));
857855
georef.setDeclination(map->getGeoreferencing().getDeclination());
858856
map->setGeoreferencing(georef);
859857
return local_srs ? std::move(local_srs) : srsFromMap();
860858
}
861-
else
862-
{
863-
throw FileFormatException(tr("The geospatial data has no suitable spatial reference."));
864-
}
859+
860+
throw FileFormatException(tr("The geospatial data has no suitable spatial reference."));
865861
}
866862

867863

@@ -1003,7 +999,8 @@ Object* OgrFileImport::importPointGeometry(OGRFeatureH feature, OGRGeometryH geo
1003999
object->setPosition(toMapCoord(OGR_G_GetX(geometry, 0), OGR_G_GetY(geometry, 0)));
10041000
return object;
10051001
}
1006-
else if (symbol->getType() == Symbol::Text)
1002+
1003+
if (symbol->getType() == Symbol::Text)
10071004
{
10081005
const auto& description = symbol->getDescription();
10091006
auto length = description.length();
@@ -1038,7 +1035,7 @@ Object* OgrFileImport::importPointGeometry(OGRFeatureH feature, OGRGeometryH geo
10381035
applyLabelAnchor(anchor, object);
10391036
}
10401037

1041-
auto angle = QStringRef(&description, 3, split-3).toFloat(&ok);
1038+
auto angle = QStringRef(&description, 3, split-3).toDouble(&ok);
10421039
if (ok)
10431040
{
10441041
object->setRotation(qDegreesToRadians(angle));
@@ -1124,7 +1121,7 @@ PathObject* OgrFileImport::importPolygonGeometry(OGRFeatureH feature, OGRGeometr
11241121

11251122
Symbol* OgrFileImport::getSymbol(Symbol::Type type, const char* raw_style_string)
11261123
{
1127-
auto style_string = QByteArray::fromRawData(raw_style_string, qstrlen(raw_style_string));
1124+
auto style_string = QByteArray::fromRawData(raw_style_string, int(qstrlen(raw_style_string)));
11281125
Symbol* symbol = nullptr;
11291126
switch (type)
11301127
{
@@ -1167,7 +1164,7 @@ Symbol* OgrFileImport::getSymbol(Symbol::Type type, const char* raw_style_string
11671164

11681165
MapColor* OgrFileImport::makeColor(OGRStyleToolH tool, const char* color_string)
11691166
{
1170-
auto key = QByteArray::fromRawData(color_string, qstrlen(color_string));
1167+
auto key = QByteArray::fromRawData(color_string, int(qstrlen(color_string)));
11711168
auto color = colors.value(key);
11721169
if (!color)
11731170
{
@@ -1372,7 +1369,7 @@ PointSymbol* OgrFileImport::getSymbolForOgrSymbol(OGRStyleToolH tool, const QByt
13721369
break;
13731370
default:
13741371
return nullptr;
1375-
};
1372+
}
13761373

13771374
int is_null;
13781375
auto color_string = OGR_ST_GetParamStr(tool, color_key, &is_null);
@@ -1415,7 +1412,7 @@ TextSymbol* OgrFileImport::getSymbolForLabel(OGRStyleToolH tool, const QByteArra
14151412

14161413
// Don't use the style string as a key: The style contains the label.
14171414
QByteArray key;
1418-
key.reserve(qstrlen(color_string) + qstrlen(font_size_string) + 1);
1415+
key.reserve(int(qstrlen(color_string) + qstrlen(font_size_string) + 1));
14191416
key.append(color_string);
14201417
key.append(font_size_string);
14211418
auto text_symbol = static_cast<TextSymbol*>(text_symbols.value(key));
@@ -1449,7 +1446,7 @@ TextSymbol* OgrFileImport::getSymbolForLabel(OGRStyleToolH tool, const QByteArra
14491446
angle = 0.0;
14501447

14511448
QString description;
1452-
description.reserve(qstrlen(label_string) + 100);
1449+
description.reserve(int(qstrlen(label_string) + 100));
14531450
description.append(QString::number(100 + anchor));
14541451
description.append(QString::number(angle, 'g', 1));
14551452
description.append(QLatin1Char(' '));
@@ -1464,7 +1461,7 @@ LineSymbol* OgrFileImport::getSymbolForPen(OGRStyleToolH tool, const QByteArray&
14641461
Q_ASSERT(OGR_ST_GetType(tool) == OGRSTCPen);
14651462

14661463
auto raw_tool_key = OGR_ST_GetStyleString(tool);
1467-
auto tool_key = QByteArray::fromRawData(raw_tool_key, qstrlen(raw_tool_key));
1464+
auto tool_key = QByteArray::fromRawData(raw_tool_key, int(qstrlen(raw_tool_key)));
14681465
auto symbol = line_symbols.value(tool_key);
14691466
if (symbol && symbol->getType() == Symbol::Line)
14701467
return static_cast<LineSymbol*>(symbol);
@@ -1496,7 +1493,7 @@ AreaSymbol* OgrFileImport::getSymbolForBrush(OGRStyleToolH tool, const QByteArra
14961493
Q_ASSERT(OGR_ST_GetType(tool) == OGRSTCBrush);
14971494

14981495
auto raw_tool_key = OGR_ST_GetStyleString(tool);
1499-
auto tool_key = QByteArray::fromRawData(raw_tool_key, qstrlen(raw_tool_key));
1496+
auto tool_key = QByteArray::fromRawData(raw_tool_key, int(qstrlen(raw_tool_key)));
15001497
auto symbol = area_symbols.value(tool_key);
15011498
if (symbol && symbol->getType() == Symbol::Area)
15021499
return static_cast<AreaSymbol*>(symbol);
@@ -1608,9 +1605,24 @@ LatLon OgrFileImport::calcAverageLatLon(const QString& path)
16081605
// static
16091606
LatLon OgrFileImport::calcAverageLatLon(OGRDataSourceH data_source)
16101607
{
1611-
return AverageLatLon(data_source);
1608+
auto geo_srs = ogr::unique_srs { OSRNewSpatialReference(nullptr) };
1609+
OSRSetWellKnownGeogCS(geo_srs.get(), "WGS84");
1610+
#if GDAL_VERSION_MAJOR >= 3
1611+
OSRSetAxisMappingStrategy(geo_srs.get(), OAMS_TRADITIONAL_GIS_ORDER);
1612+
#endif
1613+
1614+
auto const average = calcAverageCoords(data_source, geo_srs.get());
1615+
return {average.y(), average.x()};
16121616
}
16131617

1618+
1619+
// static
1620+
QPointF OgrFileImport::calcAverageCoords(OGRDataSourceH data_source, OGRDataSourceH srs)
1621+
{
1622+
return QPointF{AverageCoords(data_source, srs)};
1623+
}
1624+
1625+
16141626
// ### OgrFileExport ###
16151627

16161628
OgrFileExport::OgrFileExport(const QString& path, const Map* map, const MapView* view)
@@ -1997,7 +2009,7 @@ void OgrFileExport::addTextToLayer(OGRLayerH layer, const std::function<bool (co
19972009
{
19982010
// There is no label field, or the text is too long.
19992011
// Put the text directly in the label.
2000-
text.replace(QRegularExpression(QLatin1String("([\"\\\\])"), QRegularExpression::MultilineOption), QLatin1String("\\\\1"));
2012+
text.replace(QRegularExpression(QLatin1String(R"((["\\]))"), QRegularExpression::MultilineOption), QLatin1String("\\\\1"));
20012013
style.replace("{Name}", text.toUtf8());
20022014
}
20032015
OGR_F_SetStyleString(po_feature.get(), style);
@@ -2016,7 +2028,7 @@ void OgrFileExport::addLinesToLayer(OGRLayerH layer, const std::function<bool (c
20162028
auto add_feature = [&](const Object* object) {
20172029
const auto* symbol = object->getSymbol();
20182030
const auto* path = object->asPath();
2019-
if (path->parts().size() < 1)
2031+
if (path->parts().empty())
20202032
return;
20212033

20222034
QString sym_name = symbol->getPlainTextName();
@@ -2057,7 +2069,7 @@ void OgrFileExport::addAreasToLayer(OGRLayerH layer, const std::function<bool (c
20572069
auto add_feature = [&](const Object* object) {
20582070
const auto* symbol = object->getSymbol();
20592071
const auto* path = object->asPath();
2060-
if (path->parts().size() < 1)
2072+
if (path->parts().empty())
20612073
return;
20622074

20632075
auto po_feature = ogr::unique_feature(OGR_F_Create(OGR_L_GetLayerDefn(layer)));

src/gdal/ogr_file_format_p.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 Kai Pastor
2+
* Copyright 2016-2019 Kai Pastor
33
*
44
* This file is part of OpenOrienteering.
55
*
@@ -42,6 +42,8 @@
4242
#include "core/symbols/symbol.h"
4343
#include "fileformats/file_import_export.h"
4444

45+
class QPointF;
46+
4547
namespace OpenOrienteering {
4648

4749
class AreaSymbol;
@@ -291,6 +293,8 @@ class OgrFileImport : public Importer
291293

292294
static LatLon calcAverageLatLon(OGRDataSourceH data_source);
293295

296+
static QPointF calcAverageCoords(OGRDataSourceH data_source, OGRDataSourceH srs);
297+
294298

295299
private:
296300
Symbol* getSymbolForPointGeometry(const QByteArray& style_string);
@@ -323,15 +327,15 @@ class OgrFileImport : public Importer
323327

324328
ogr::unique_stylemanager manager;
325329

326-
int empty_geometries;
327-
int no_transformation;
328-
int failed_transformation;
329-
int unsupported_geometry_type;
330-
int too_few_coordinates;
330+
int empty_geometries = 0;
331+
int no_transformation = 0;
332+
int failed_transformation = 0;
333+
int unsupported_geometry_type = 0;
334+
int too_few_coordinates = 0;
331335

332336
UnitType unit_type;
333337

334-
bool georeferencing_import_enabled;
338+
bool georeferencing_import_enabled = true;
335339
};
336340

337341

src/gdal/ogr_template.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 Kai Pastor
2+
* Copyright 2016-2019 Kai Pastor
33
*
44
* This file is part of OpenOrienteering.
55
*
@@ -328,7 +328,7 @@ try
328328

329329
if (!explicit_georef)
330330
{
331-
explicit_georef.reset(new Georeferencing());
331+
explicit_georef = std::make_unique<Georeferencing>();
332332
explicit_georef->setScaleDenominator(int(map_georef.getScaleDenominator()));
333333
explicit_georef->setProjectedCRS(QString{}, projected_crs_spec);
334334
explicit_georef->setProjectedRefPoint({}, false);
@@ -506,7 +506,7 @@ bool OgrTemplate::loadTypeSpecificTemplateConfiguration(QXmlStreamReader& xml)
506506
{
507507
if (xml.name() == literal::georeferencing)
508508
{
509-
explicit_georef.reset(new Georeferencing());
509+
explicit_georef = std::make_unique<Georeferencing>();
510510
explicit_georef->load(xml, false);
511511
}
512512
else if (xml.name() == literal::crs_spec)

0 commit comments

Comments
 (0)