| 3004 | */ |
| 3005 | |
| 3006 | OGRErr OGRSpatialReference::SetTargetLinearUnits(const char *pszTargetKey, |
| 3007 | const char *pszUnitsName, |
| 3008 | double dfInMeters, |
| 3009 | const char *pszUnitAuthority, |
| 3010 | const char *pszUnitCode) |
| 3011 | |
| 3012 | { |
| 3013 | TAKE_OPTIONAL_LOCK(); |
| 3014 | |
| 3015 | if (dfInMeters <= 0.0) |
| 3016 | return OGRERR_FAILURE; |
| 3017 | |
| 3018 | d->refreshProjObj(); |
| 3019 | pszTargetKey = d->nullifyTargetKeyIfPossible(pszTargetKey); |
| 3020 | if (pszTargetKey == nullptr) |
| 3021 | { |
| 3022 | if (!d->m_pj_crs) |
| 3023 | return OGRERR_FAILURE; |
| 3024 | |
| 3025 | d->demoteFromBoundCRS(); |
| 3026 | if (d->m_pjType == PJ_TYPE_PROJECTED_CRS) |
| 3027 | { |
| 3028 | d->setPjCRS(proj_crs_alter_parameters_linear_unit( |
| 3029 | d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters, |
| 3030 | pszUnitAuthority, pszUnitCode, false)); |
| 3031 | } |
| 3032 | d->setPjCRS(proj_crs_alter_cs_linear_unit( |
| 3033 | d->getPROJContext(), d->m_pj_crs, pszUnitsName, dfInMeters, |
| 3034 | pszUnitAuthority, pszUnitCode)); |
| 3035 | d->undoDemoteFromBoundCRS(); |
| 3036 | |
| 3037 | d->m_osLinearUnits = pszUnitsName; |
| 3038 | d->dfToMeter = dfInMeters; |
| 3039 | |
| 3040 | return OGRERR_NONE; |
| 3041 | } |
| 3042 | |
| 3043 | OGR_SRSNode *poCS = GetAttrNode(pszTargetKey); |
| 3044 | |
| 3045 | if (poCS == nullptr) |
| 3046 | return OGRERR_FAILURE; |
| 3047 | |
| 3048 | char szValue[128] = {'\0'}; |
| 3049 | if (dfInMeters < std::numeric_limits<int>::max() && |
| 3050 | dfInMeters > std::numeric_limits<int>::min() && |
| 3051 | dfInMeters == static_cast<int>(dfInMeters)) |
| 3052 | snprintf(szValue, sizeof(szValue), "%d", static_cast<int>(dfInMeters)); |
| 3053 | else |
| 3054 | OGRsnPrintDouble(szValue, sizeof(szValue), dfInMeters); |
| 3055 | |
| 3056 | OGR_SRSNode *poUnits = nullptr; |
| 3057 | if (poCS->FindChild("UNIT") >= 0) |
| 3058 | { |
| 3059 | poUnits = poCS->GetChild(poCS->FindChild("UNIT")); |
| 3060 | if (poUnits->GetChildCount() < 2) |
| 3061 | return OGRERR_FAILURE; |
| 3062 | poUnits->GetChild(0)->SetValue(pszUnitsName); |
| 3063 | poUnits->GetChild(1)->SetValue(szValue); |