Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export georeferencing to OCD more accurately #2096

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/fileformats/ocd_file_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,21 +943,22 @@ void OcdFileExport::exportGeoreferencing()
{
case MapGrid::MillimetersOnMap:
grid_spacing_map = spacing;
grid_spacing_real = spacing * georef.getScaleDenominator() / 1000;
grid_spacing_real = spacing * fields.m / 1000;
break;
case MapGrid::MetersInTerrain:
grid_spacing_map = spacing * 1000 / georef.getScaleDenominator();
grid_spacing_map = spacing * 1000 / fields.m;
grid_spacing_real = spacing;
break;
}

QString string_1039;
QTextStream out(&string_1039, QIODevice::Append);
out << fixed
out << qSetRealNumberPrecision(6)
<< "\tm" << fields.m
<< qSetRealNumberPrecision(4)
<< "\tg" << grid_spacing_map
<< "\tr" << fields.r
<< qSetRealNumberPrecision(9)
<< "\tx" << fields.x
<< "\ty" << fields.y
<< qSetRealNumberPrecision(8)
Expand Down
18 changes: 15 additions & 3 deletions src/fileformats/ocd_file_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,25 @@ void OcdFileImport::importGeoreferencing(const QString& param_string)
switch (parameters.key())
{
case 'm':
tryParamConvert(fields.m, param_value);
{
auto scale_to_grid = param_value.toDouble(&ok);
if (ok)
fields.m = scale_to_grid;
}
break;
case 'x':
tryParamConvert(fields.x, param_value);
{
auto real_offset_x = param_value.toDouble(&ok);
if (ok)
fields.x = real_offset_x;
}
break;
case 'y':
tryParamConvert(fields.y, param_value);
{
auto real_offset_y = param_value.toDouble(&ok);
if (ok)
fields.y = real_offset_y;
}
break;
case 'i':
tryParamConvert(fields.i, param_value);
Expand Down
22 changes: 13 additions & 9 deletions src/fileformats/ocd_georef_fields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,25 +838,29 @@ int toOcd(const MapperCrs crs_unique_id,
bool operator==(const OcdGeorefFields& lhs, const OcdGeorefFields& rhs)
{
return lhs.i == rhs.i
&& lhs.m == rhs.m
&& lhs.x == rhs.x
&& lhs.y == rhs.y
&& ((qIsNaN(lhs.m) && qIsNaN(rhs.m)) || qAbs(lhs.m - rhs.m) < 5e-9) // 8-digit precision or both NaN's
&& ((qIsNaN(lhs.x) && qIsNaN(rhs.x)) || qAbs(lhs.x - rhs.x) < 5e-9) // 8-digit precision or both NaN's
&& ((qIsNaN(lhs.y) && qIsNaN(rhs.y)) || qAbs(lhs.y - rhs.y) < 5e-9) // 8-digit precision or both NaN's
&& ((qIsNaN(lhs.a) && qIsNaN(rhs.a)) || qAbs(lhs.a - rhs.a) < 5e-9) // 8-digit precision or both NaN's
&& lhs.r == rhs.r;
}

void OcdGeorefFields::setupGeoref(Georeferencing& georef,
const std::function<void (const QString&)>& warning_handler) const
{
if (m > 0)
georef.setScaleDenominator(m);
auto map_scale = qRound(m);
if (map_scale > 0)
georef.setScaleDenominator(map_scale);

if (r)
applyGridAndZone(georef, i, warning_handler);

QPointF proj_ref_point(x, y);
georef.setProjectedRefPoint(proj_ref_point, false, false);
georef.setCombinedScaleFactor(1.0);
if (m > 0)
georef.setCombinedScaleFactor(m / georef.getScaleDenominator());
else
georef.setCombinedScaleFactor(1.0);
georef.setGrivation(qIsFinite(a) ? a : 0);
}

Expand All @@ -867,10 +871,10 @@ OcdGeorefFields OcdGeorefFields::fromGeoref(const Georeferencing& georef,

// store known values early, they can be useful even if CRS translation fails
retval.a = georef.getGrivation();
retval.m = georef.getScaleDenominator();
retval.m = georef.getScaleDenominator() * georef.getCombinedScaleFactor();
const QPointF offset(georef.toProjectedCoords(MapCoord{}));
retval.x = qRound(offset.x()); // OCD easting and northing is integer
retval.y = qRound(offset.y());
retval.x = offset.x();
retval.y = offset.y();

// attempt translation from Mapper CRS reference into OCD one
auto crs_id_string = georef.getProjectedCRSId();
Expand Down
12 changes: 6 additions & 6 deletions src/fileformats/ocd_georef_fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class Georeferencing;
*/
struct OcdGeorefFields
{
double a { 0 }; ///< Real world angle
int m { 15000 }, ///< Map scale
x { 0 }, ///< Real world offset easting
y { 0 }, ///< Real world offset northing
i { 1000 }, ///< Grid and zone
r { 0 }; ///< Real world coord (0 = paper, 1 = real world)
double a { 0 }, ///< Real world angle
m { 15000 }, ///< Map scale, relative to grid
x { 0 }, ///< Real world offset easting
y { 0 }; ///< Real world offset northing
int i { 1000 }, ///< Grid and zone
r { 0 }; ///< Real world coord (0 = paper, 1 = real world)

/**
* Fill in provided georef with data extracted from type 1039 string.
Expand Down
6 changes: 3 additions & 3 deletions test/georef_ocd_mapping_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ template<>
char* toString(const OcdGeorefFields &f) // NOLINT : declared parameter name is 't'
{
auto ret = QString(QStringLiteral("m:%1; x:%2; y:%3; a:%4; i:%5; r:%6"))
.arg(f.m)
.arg(f.x)
.arg(f.y)
.arg(f.m, 0, 'f', 3)
.arg(f.x, 0, 'f', 2)
.arg(f.y, 0, 'f', 2)
.arg(f.a, 0, 'f', 8)
.arg(f.i)
.arg(f.r);
Expand Down