diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2020-10-16 00:10:05 +0200 |
|---|---|---|
| committer | Even Rouault <even.rouault@spatialys.com> | 2020-10-16 18:49:58 +0200 |
| commit | 149366116ad0ffb936eaa042823211731e9dcdee (patch) | |
| tree | 1258ceb40af7d348f12ed30c2eb4de1c80c2ace5 /src/iso19111/io.cpp | |
| parent | 82b496fb32df0b6705159cd5c626aab20c8e9d39 (diff) | |
| download | PROJ-149366116ad0ffb936eaa042823211731e9dcdee.tar.gz PROJ-149366116ad0ffb936eaa042823211731e9dcdee.zip | |
Add multi-line PROJ string export capability, and use it by default in projinfo (unless --single-line is specified) (fixes #1543)
Diffstat (limited to 'src/iso19111/io.cpp')
| -rw-r--r-- | src/iso19111/io.cpp | 78 |
1 files changed, 70 insertions, 8 deletions
diff --git a/src/iso19111/io.cpp b/src/iso19111/io.cpp index c464b724..7a107f96 100644 --- a/src/iso19111/io.cpp +++ b/src/iso19111/io.cpp @@ -6722,6 +6722,10 @@ struct PROJStringFormatter::Private { bool coordOperationOptimizations_ = false; bool crsExport_ = false; bool legacyCRSToCRSContext_ = false; + bool multiLine_ = false; + int indentWidth_ = 2; + int indentLevel_ = 0; + int maxLineLength_ = 80; std::string result_{}; @@ -6780,6 +6784,36 @@ void PROJStringFormatter::setUseApproxTMerc(bool flag) { // --------------------------------------------------------------------------- +/** \brief Whether to use multi line output or not. */ +PROJStringFormatter & +PROJStringFormatter::setMultiLine(bool multiLine) noexcept { + d->multiLine_ = multiLine; + return *this; +} + +// --------------------------------------------------------------------------- + +/** \brief Set number of spaces for each indentation level (defaults to 2). + */ +PROJStringFormatter & +PROJStringFormatter::setIndentationWidth(int width) noexcept { + d->indentWidth_ = width; + return *this; +} + +// --------------------------------------------------------------------------- + +/** \brief Set the maximum size of a line (when multiline output is enable). + * Can be set to 0 for unlimited length. + */ +PROJStringFormatter & +PROJStringFormatter::setMaxLineLength(int maxLineLength) noexcept { + d->maxLineLength_ = maxLineLength; + return *this; +} + +// --------------------------------------------------------------------------- + /** \brief Returns the PROJ string. */ const std::string &PROJStringFormatter::toString() const { @@ -7308,28 +7342,56 @@ const std::string &PROJStringFormatter::toString() const { pj_double_quote_string_param_if_needed(paramValue.value); } } + + if (d->multiLine_) { + d->indentLevel_++; + } } for (const auto &step : d->steps_) { + std::string curLine; if (!d->result_.empty()) { - d->appendToResult("+step"); + if (d->multiLine_) { + curLine = std::string(d->indentLevel_ * d->indentWidth_, ' '); + curLine += "+step"; + } else { + curLine = " +step"; + } } if (step.inverted) { - d->appendToResult("+inv"); + curLine += " +inv"; } if (!step.name.empty()) { - d->appendToResult(step.isInit ? "+init=" : "+proj="); - d->result_ += step.name; + if (!curLine.empty()) + curLine += ' '; + curLine += step.isInit ? "+init=" : "+proj="; + curLine += step.name; } for (const auto ¶mValue : step.paramValues) { - d->appendToResult("+"); - d->result_ += paramValue.key; + std::string newKV = "+"; + newKV += paramValue.key; if (!paramValue.value.empty()) { - d->result_ += '='; - d->result_ += + newKV += '='; + newKV += pj_double_quote_string_param_if_needed(paramValue.value); } + if (d->maxLineLength_ > 0 && d->multiLine_ && + curLine.size() + newKV.size() > + static_cast<size_t>(d->maxLineLength_)) { + if (d->multiLine_ && !d->result_.empty()) + d->result_ += '\n'; + d->result_ += curLine; + curLine = std::string( + d->indentLevel_ * d->indentWidth_ + strlen("+step "), ' '); + } else { + if (!curLine.empty()) + curLine += ' '; + } + curLine += newKV; } + if (d->multiLine_ && !d->result_.empty()) + d->result_ += '\n'; + d->result_ += curLine; } if (d->result_.empty()) { |
