From e77a9c3b13c1ccc9846dd5ce37dfb95af32f7b9a Mon Sep 17 00:00:00 2001 From: Matthias Kuehlewein Date: Sun, 5 Feb 2023 11:29:52 +0100 Subject: [PATCH] OgrFileImport: Improve DXF import of text objects Text object properties like the text itsef, alignment and rotation were previously imported by putting them into a string and transporting the string via the description property of the related text symbol. Besides being an awkward design the approach suffers from multiple deficiencies: the auxiliary string was not removed from the description field, the rotation angle was rounded (e.g., -17.2 became 20) and the way of constructing and parsing the string was unnecessary complex. This commit uses a static QVariantHash object to transport the object properties. As only a single object is imported at a time it's not required to pass a reference to this QVariantHash object to the functions that return the related symbol and retrieve the object properties. --- src/gdal/ogr_file_format.cpp | 37 +++++++++++++----------------------- src/gdal/ogr_file_format_p.h | 4 +++- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/gdal/ogr_file_format.cpp b/src/gdal/ogr_file_format.cpp index 37ac197cd..6e7fd8a83 100644 --- a/src/gdal/ogr_file_format.cpp +++ b/src/gdal/ogr_file_format.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 Kai Pastor + * Copyright 2016-2023 Kai Pastor * * This file is part of OpenOrienteering. * @@ -1166,6 +1166,7 @@ OgrFileImport::ObjectList OgrFileImport::importGeometryCollection(OGRFeatureH fe Object* OgrFileImport::importPointGeometry(OGRFeatureH feature, OGRGeometryH geometry) { + object_properties.clear(); auto style = OGR_F_GetStyleString(feature); auto symbol = getSymbol(Symbol::Point, style); if (symbol->getType() == Symbol::Point) @@ -1175,15 +1176,9 @@ Object* OgrFileImport::importPointGeometry(OGRFeatureH feature, OGRGeometryH geo return object; } - if (symbol->getType() == Symbol::Text) + if (symbol->getType() == Symbol::Text && !object_properties.isEmpty()) { - const auto& description = symbol->getDescription(); - auto length = description.length(); - auto split = description.indexOf(QLatin1Char(' ')); - FILEFORMAT_ASSERT(split > 0); - FILEFORMAT_ASSERT(split < length); - - auto label = description.right(length - split - 1); + auto label = object_properties.value(QLatin1String("label")).toString(); if (label.startsWith(QLatin1Char{'{'}) && label.endsWith(QLatin1Char{'}'})) { label.remove(0, 1); @@ -1204,13 +1199,13 @@ Object* OgrFileImport::importPointGeometry(OGRFeatureH feature, OGRGeometryH geo object->setText(label); bool ok; - auto anchor = QStringRef(&description, 1, 2).toInt(&ok); + auto anchor = object_properties.value(QLatin1String("anchor"), 1).toInt(&ok); if (ok) { applyLabelAnchor(anchor, object); } - - auto angle = QStringRef(&description, 3, split-3).toDouble(&ok); + + auto angle = object_properties.value(QLatin1String("angle"), 0.0).toDouble(&ok); if (ok) { object->setRotation(qDegreesToRadians(angle)); @@ -1676,21 +1671,15 @@ TextSymbol* OgrFileImport::getSymbolForLabel(OGRStyleToolH tool, const QByteArra map->addSymbol(copy.release(), map->getNumSymbols()); } + object_properties.insert(QLatin1String("label"), QVariant(QString::fromUtf8(label_string))); + auto anchor = qBound(1, OGR_ST_GetParamNum(tool, OGRSTLabelAnchor, &is_null), 12); - if (is_null) - anchor = 1; + if (!is_null) + object_properties.insert(QLatin1String("anchor"), QVariant(anchor)); auto angle = OGR_ST_GetParamDbl(tool, OGRSTLabelAngle, &is_null); - if (is_null) - angle = 0.0; - - QString description; - description.reserve(int(qstrlen(label_string) + 100)); - description.append(QString::number(100 + anchor)); - description.append(QString::number(angle, 'g', 1)); - description.append(QLatin1Char(' ')); - description.append(QString::fromUtf8(label_string)); - text_symbol->setDescription(description); + if (!is_null) + object_properties.insert(QLatin1String("angle"), QVariant(angle)); return text_symbol; } diff --git a/src/gdal/ogr_file_format_p.h b/src/gdal/ogr_file_format_p.h index 79d797bd2..055228702 100644 --- a/src/gdal/ogr_file_format_p.h +++ b/src/gdal/ogr_file_format_p.h @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 Kai Pastor + * Copyright 2016-2023 Kai Pastor * * This file is part of OpenOrienteering. * @@ -356,6 +356,8 @@ class OgrFileImport : public Importer QHash colors; MapColor* default_pen_color; + QVariantHash object_properties; + MapCoordConstructor to_map_coord; ogr::unique_srs map_srs;