Skip to content

Commit

Permalink
Merge remote-tracking branch 'osgeo/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kbevers committed Aug 26, 2019
2 parents 5a504f5 + bdaf9a8 commit f059b90
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 48 deletions.
13 changes: 13 additions & 0 deletions docs/source/development/reference/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,19 @@ Various
:type `direction`: PJ_DIRECTION
:returns: :c:type:`int` 1 if output units is expected in radians, otherwise 0
Cleanup
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.. c:function:: void proj_cleanup()
.. versionadded:: 6.2.0
This function frees global resources (grids, cache of +init files). It
should be called typically before process termination, and *after* having
freed PJ and PJ_CONTEXT objects.
C API for ISO-19111 functionality
+++++++++++++++++++++++++++++++++
Expand Down
4 changes: 2 additions & 2 deletions include/proj/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,8 @@ class PROJ_GCC_DLL PROJStringFormatter {
PROJ_INTERNAL void popOmitZUnitConversion();
PROJ_INTERNAL bool omitZUnitConversion() const;

PROJ_INTERNAL void setDropEarlyBindingsTerms(bool drop);
PROJ_INTERNAL bool getDropEarlyBindingsTerms() const;
PROJ_INTERNAL void setLegacyCRSToCRSContext(bool legacyContext);
PROJ_INTERNAL bool getLegacyCRSToCRSContext() const;

PROJ_INTERNAL const DatabaseContextPtr &databaseContext() const;

Expand Down
1 change: 1 addition & 0 deletions scripts/reference_exported_symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ proj_as_projjson
proj_as_proj_string
proj_assign_context
proj_as_wkt
proj_cleanup
proj_clone
proj_concatoperation_get_step
proj_concatoperation_get_step_count
Expand Down
9 changes: 8 additions & 1 deletion src/apply_gridshift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,14 @@ PJ_LP proj_hgrid_apply(PJ *P, PJ_LP lp, PJ_DIRECTION direction) {
ct = find_ctable(P->ctx, lp, P->gridlist_count, P->gridlist);

if (ct == nullptr || ct->cvs == nullptr) {
pj_ctx_set_errno( P->ctx, PJD_ERR_FAILED_TO_LOAD_GRID );
if( P->gridlist_count == 1 &&
strcmp(P->gridlist[0]->gridname, "null") == 0) {
// TODO: remove this particular case that is put there just to be
// able to handle longitudes outside of -180,180
out = lp;
} else {
pj_ctx_set_errno( P->ctx, PJD_ERR_FAILED_TO_LOAD_GRID );
}
return out;
}

Expand Down
2 changes: 1 addition & 1 deletion src/apps/cs2cs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ int main(int argc, char **argv) {

proj_destroy(transformation);

pj_deallocate_grids();
proj_cleanup();

exit(0); /* normal completion */
}
2 changes: 1 addition & 1 deletion src/iso19111/coordinateoperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12064,7 +12064,7 @@ CoordinateOperationFactory::Private::createOperations(
}
auto projFormatter = io::PROJStringFormatter::create();
projFormatter->setCRSExport(true);
projFormatter->setDropEarlyBindingsTerms(true);
projFormatter->setLegacyCRSToCRSContext(true);
projFormatter->startInversion();
sourceProjExportable->_exportToPROJString(projFormatter.get());
auto geogSrc =
Expand Down
42 changes: 33 additions & 9 deletions src/iso19111/crs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ void GeodeticCRS::addDatumInfoToPROJString(
const auto &nadgrids = formatter->getHDatumExtension();
const auto &l_datum = datum();
if (formatter->getCRSExport() && l_datum && TOWGS84Params.empty() &&
nadgrids.empty() && !formatter->getDropEarlyBindingsTerms()) {
nadgrids.empty()) {
if (l_datum->_isEquivalentTo(
datum::GeodeticReferenceFrame::EPSG_6326.get(),
util::IComparable::Criterion::EQUIVALENT)) {
Expand All @@ -1323,7 +1323,12 @@ void GeodeticCRS::addDatumInfoToPROJString(
datum::GeodeticReferenceFrame::EPSG_6269.get(),
util::IComparable::Criterion::EQUIVALENT)) {
datumWritten = true;
formatter->addParam("datum", "NAD83");
if (formatter->getLegacyCRSToCRSContext()) {
// We do not want datum=NAD83 to cause a useless towgs84=0,0,0
formatter->addParam("ellps", "GRS80");
} else {
formatter->addParam("datum", "NAD83");
}
}
}
if (!datumWritten) {
Expand Down Expand Up @@ -2083,8 +2088,31 @@ void GeographicCRS::_exportToPROJString(
primeMeridian()->longitude().getSIValue() != 0.0 ||
!formatter->getTOWGS84Parameters().empty() ||
!formatter->getHDatumExtension().empty()) {

formatter->addStep("longlat");
addDatumInfoToPROJString(formatter);
bool done = false;
if (formatter->getLegacyCRSToCRSContext() &&
formatter->getHDatumExtension().empty() &&
formatter->getTOWGS84Parameters().empty()) {
const auto &l_datum = datum();
if (l_datum &&
l_datum->_isEquivalentTo(
datum::GeodeticReferenceFrame::EPSG_6326.get(),
util::IComparable::Criterion::EQUIVALENT)) {
done = true;
formatter->addParam("ellps", "WGS84");
} else if (l_datum &&
l_datum->_isEquivalentTo(
datum::GeodeticReferenceFrame::EPSG_6269.get(),
util::IComparable::Criterion::EQUIVALENT)) {
done = true;
// We do not want datum=NAD83 to cause a useless towgs84=0,0,0
formatter->addParam("ellps", "GRS80");
}
}
if (!done) {
addDatumInfoToPROJString(formatter);
}
}
if (!formatter->getCRSExport()) {
addAngularUnitConvertAndAxisSwap(formatter);
Expand Down Expand Up @@ -3132,7 +3160,8 @@ void ProjectedCRS::addUnitConvertAndAxisSwap(io::PROJStringFormatter *formatter,
formatter->addParam("units", projUnit);
}
}
} else if (formatter->getCRSExport()) {
} else if (formatter->getCRSExport() &&
!formatter->getLegacyCRSToCRSContext()) {
formatter->addParam("units", "m");
}

Expand Down Expand Up @@ -4143,11 +4172,6 @@ void BoundCRS::_exportToPROJString(
"baseCRS of BoundCRS cannot be exported as a PROJ string");
}

if (formatter->getDropEarlyBindingsTerms()) {
crs_exportable->_exportToPROJString(formatter);
return;
}

auto vdatumProj4GridName = getVDatumPROJ4GRIDS();
if (!vdatumProj4GridName.empty()) {
formatter->setVDatumExtension(vdatumProj4GridName);
Expand Down
56 changes: 23 additions & 33 deletions src/iso19111/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6145,7 +6145,7 @@ struct PROJStringFormatter::Private {
bool addNoDefs_ = true;
bool coordOperationOptimizations_ = false;
bool crsExport_ = false;
bool dropEarlyBindingsTerms_ = false;
bool legacyCRSToCRSContext_ = false;

std::string result_{};

Expand Down Expand Up @@ -6723,7 +6723,7 @@ void PROJStringFormatter::Private::appendToResult(const char *str) {
static void
PROJStringSyntaxParser(const std::string &projString, std::vector<Step> &steps,
std::vector<Step::KeyValue> &globalParamValues,
std::string &title, bool dropEarlyBindingsTerms) {
std::string &title) {
const char *c_str = projString.c_str();
std::vector<std::string> tokens;

Expand Down Expand Up @@ -6811,27 +6811,14 @@ PROJStringSyntaxParser(const std::string &projString, std::vector<Step> &steps,
} else if (word != "step") {
const auto pos = word.find('=');
auto key = word.substr(0, pos);
if (!(dropEarlyBindingsTerms &&
(key == "towgs84" || key == "nadgrids" ||
key == "geoidgrids"))) {
auto pair = (pos != std::string::npos)
? Step::KeyValue(key, word.substr(pos + 1))
: Step::KeyValue(key);
if (dropEarlyBindingsTerms && key == "datum") {
const auto datums = pj_get_datums_ref();
for (int i = 0; datums[i].id != nullptr; i++) {
if (pair.value == datums[i].id) {
pair.key = "ellps";
pair.value = datums[i].ellipse_id;
break;
}
}
}
if (steps.empty()) {
globalParamValues.push_back(pair);
} else {
steps.back().paramValues.push_back(pair);
}

auto pair = (pos != std::string::npos)
? Step::KeyValue(key, word.substr(pos + 1))
: Step::KeyValue(key);
if (steps.empty()) {
globalParamValues.push_back(pair);
} else {
steps.back().paramValues.push_back(pair);
}
}
}
Expand Down Expand Up @@ -6906,8 +6893,7 @@ void PROJStringFormatter::ingestPROJString(
{
std::vector<Step> steps;
std::string title;
PROJStringSyntaxParser(str, steps, d->globalParamValues_, title,
d->dropEarlyBindingsTerms_);
PROJStringSyntaxParser(str, steps, d->globalParamValues_, title);
d->steps_.insert(d->steps_.end(), steps.begin(), steps.end());
}

Expand Down Expand Up @@ -7175,14 +7161,14 @@ bool PROJStringFormatter::omitZUnitConversion() const {

// ---------------------------------------------------------------------------

void PROJStringFormatter::setDropEarlyBindingsTerms(bool drop) {
d->dropEarlyBindingsTerms_ = drop;
void PROJStringFormatter::setLegacyCRSToCRSContext(bool legacyContext) {
d->legacyCRSToCRSContext_ = legacyContext;
}

// ---------------------------------------------------------------------------

bool PROJStringFormatter::getDropEarlyBindingsTerms() const {
return d->dropEarlyBindingsTerms_;
bool PROJStringFormatter::getLegacyCRSToCRSContext() const {
return d->legacyCRSToCRSContext_;
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -8269,6 +8255,7 @@ CRSNNPtr PROJStringParser::Private::buildProjectedCRS(
}

auto axisType = AxisType::REGULAR;
bool bWebMercator = false;

if (step.name == "tmerc" &&
((getParamValue(step, "axis") == "wsu" && iAxisSwap < 0) ||
Expand Down Expand Up @@ -8351,8 +8338,7 @@ CRSNNPtr PROJStringParser::Private::buildProjectedCRS(
}
}
if (getNumericValue(getParamValue(step, "a")) == 6378137) {
return createPseudoMercator(PropertyMap().set(
IdentifiedObject::NAME_KEY, "WGS 84 / Pseudo-Mercator"));
bWebMercator = true;
}
} else if (hasParamValue(step, "lat_ts")) {
mapping = getMapping(EPSG_CODE_METHOD_MERCATOR_VARIANT_B);
Expand Down Expand Up @@ -8613,7 +8599,11 @@ CRSNNPtr PROJStringParser::Private::buildProjectedCRS(

props.set("IMPLICIT_CS", true);

CRSNNPtr crs = ProjectedCRS::create(props, geogCRS, NN_NO_CHECK(conv), cs);
CRSNNPtr crs =
bWebMercator
? createPseudoMercator(props.set(IdentifiedObject::NAME_KEY,
"WGS 84 / Pseudo-Mercator"))
: ProjectedCRS::create(props, geogCRS, NN_NO_CHECK(conv), cs);

if (!hasParamValue(step, "geoidgrids") &&
(hasParamValue(step, "vunits") || hasParamValue(step, "vto_meter"))) {
Expand Down Expand Up @@ -8671,7 +8661,7 @@ PROJStringParser::createFromPROJString(const std::string &projString) {
d->globalParamValues_.clear();
d->projString_ = projString;
PROJStringSyntaxParser(projString, d->steps_, d->globalParamValues_,
d->title_, false);
d->title_);

if (d->steps_.empty()) {
const auto &vunits = d->getGlobalParamValue("vunits");
Expand Down
7 changes: 7 additions & 0 deletions src/malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,10 @@ PJ *pj_default_destructor (PJ *P, int errlev) { /* Destructor */
delete P;
return nullptr;
}

/*****************************************************************************/
void proj_cleanup() {
/*****************************************************************************/
pj_clear_initcache();
pj_deallocate_grids();
}
2 changes: 2 additions & 0 deletions src/proj.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ double PROJ_DLL proj_todeg (double angle_in_radians);
double PROJ_DLL proj_dmstor(const char *is, char **rs);
char PROJ_DLL * proj_rtodms(char *s, double r, int pos, int neg);

void PROJ_DLL proj_cleanup(void);

/*! @endcond */

/* ------------------------------------------------------------------------- */
Expand Down
12 changes: 12 additions & 0 deletions test/cli/testvarious
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,18 @@ $EXE -f %.7f -I +proj=longlat +ellps=GRS80 +to +proj=ob_tran +o_proj=longlat +lo
-122 46
EOF

echo "##############################################################" >> ${OUT}
echo "Check +init=epsg:4326 +over +to +init=epsg:3857 +over" >> ${OUT}
$EXE -f %.7f +init=epsg:4326 +over +to +init=epsg:3857 +over -E >>${OUT} <<EOF
-181 49
EOF

echo "##############################################################" >> ${OUT}
echo "Check +proj=longlat +over +datum=WGS84 +to proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +over" >> ${OUT}
$EXE -f %.7f +proj=longlat +over +datum=WGS84 +to proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +over -E >>${OUT} <<EOF
-181 49
EOF


# Done!
# do 'diff' with distribution results
Expand Down
6 changes: 6 additions & 0 deletions test/cli/tv_out.dist
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,9 @@ Check ob_tran with o_proj=longlat (#1525)
-122 46 -132.0000000 46.0000000 0.0000000
Test inverse handling
-122 46 -112.0000000 46.0000000 0.0000000
##############################################################
Check +init=epsg:4326 +over +to +init=epsg:3857 +over
-181 49 -20148827.8335825 6274861.3940066 0.0000000
##############################################################
Check +proj=longlat +over +datum=WGS84 +to proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +over
-181 49 -20148827.8335825 6274861.3940066 0.0000000
Loading

0 comments on commit f059b90

Please sign in to comment.