Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: Spheroid classes 4 : * Purpose: Provide spheroid lookup table base classes. 5 : * Author: Gillian Walter 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 1999, Frank Warmerdam 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #include "atlsci_spheroid.h" 14 : #include "cpl_string.h" 15 : 16 : /**********************************************************************/ 17 : /* ================================================================== */ 18 : /* Spheroid definitions */ 19 : /* ================================================================== */ 20 : /**********************************************************************/ 21 : 22 1792 : SpheroidItem::SpheroidItem() 23 : : spheroid_name(nullptr), equitorial_radius(-1.0), polar_radius(-1.0), 24 1792 : inverse_flattening(-1.0) 25 : { 26 1792 : } 27 : 28 3584 : SpheroidItem::~SpheroidItem() 29 : { 30 1792 : CPLFree(spheroid_name); 31 1792 : } 32 : 33 108 : void SpheroidItem::SetValuesByRadii(const char *spheroidname, double eq_radius, 34 : double p_radius) 35 : { 36 108 : spheroid_name = CPLStrdup(spheroidname); 37 108 : equitorial_radius = eq_radius; 38 108 : polar_radius = p_radius; 39 108 : inverse_flattening = 40 108 : eq_radius == polar_radius ? 0 : eq_radius / (eq_radius - polar_radius); 41 108 : } 42 : 43 58 : void SpheroidItem::SetValuesByEqRadiusAndInvFlattening(const char *spheroidname, 44 : double eq_radius, 45 : double inverseflattening) 46 : { 47 58 : spheroid_name = CPLStrdup(spheroidname); 48 58 : equitorial_radius = eq_radius; 49 58 : inverse_flattening = inverseflattening; 50 116 : polar_radius = inverse_flattening == 0 51 58 : ? eq_radius 52 58 : : eq_radius * (1.0 - (1.0 / inverse_flattening)); 53 58 : } 54 : 55 1799 : SpheroidList::SpheroidList() : num_spheroids(0), epsilonR(0.0), epsilonI(0.0) 56 : { 57 7 : } 58 : 59 1799 : SpheroidList::~SpheroidList() 60 : { 61 7 : } 62 : 63 0 : char *SpheroidList::GetSpheroidNameByRadii(double eq_radius, 64 : double polar_radius) 65 : { 66 0 : for (int index = 0; index < num_spheroids; index++) 67 : { 68 0 : const double er = spheroids[index].equitorial_radius; 69 0 : const double pr = spheroids[index].polar_radius; 70 0 : if ((fabs(er - eq_radius) < epsilonR) && 71 0 : (fabs(pr - polar_radius) < epsilonR)) 72 0 : return CPLStrdup(spheroids[index].spheroid_name); 73 : } 74 : 75 0 : return nullptr; 76 : } 77 : 78 0 : char *SpheroidList::GetSpheroidNameByEqRadiusAndInvFlattening( 79 : double eq_radius, double inverse_flattening) 80 : { 81 0 : for (int index = 0; index < num_spheroids; index++) 82 : { 83 0 : const double er = spheroids[index].equitorial_radius; 84 0 : const double invf = spheroids[index].inverse_flattening; 85 0 : if ((fabs(er - eq_radius) < epsilonR) && 86 0 : (fabs(invf - inverse_flattening) < epsilonI)) 87 0 : return CPLStrdup(spheroids[index].spheroid_name); 88 : } 89 : 90 0 : return nullptr; 91 : } 92 : 93 6 : double SpheroidList::GetSpheroidEqRadius(const char *spheroid_name) 94 : { 95 38 : for (int index = 0; index < num_spheroids; index++) 96 : { 97 38 : if (EQUAL(spheroids[index].spheroid_name, spheroid_name)) 98 6 : return spheroids[index].equitorial_radius; 99 : } 100 : 101 0 : return -1.0; 102 : } 103 : 104 4 : int SpheroidList::SpheroidInList(const char *spheroid_name) 105 : { 106 : /* Return 1 if the spheroid name is recognized; 0 otherwise */ 107 26 : for (int index = 0; index < num_spheroids; index++) 108 : { 109 26 : if (EQUAL(spheroids[index].spheroid_name, spheroid_name)) 110 4 : return 1; 111 : } 112 : 113 0 : return 0; 114 : } 115 : 116 6 : double SpheroidList::GetSpheroidInverseFlattening(const char *spheroid_name) 117 : { 118 38 : for (int index = 0; index < num_spheroids; index++) 119 : { 120 38 : if (EQUAL(spheroids[index].spheroid_name, spheroid_name)) 121 6 : return spheroids[index].inverse_flattening; 122 : } 123 : 124 0 : return -1.0; 125 : } 126 : 127 0 : double SpheroidList::GetSpheroidPolarRadius(const char *spheroid_name) 128 : { 129 0 : for (int index = 0; index < num_spheroids; index++) 130 : { 131 0 : if (strcmp(spheroids[index].spheroid_name, spheroid_name) == 0) 132 0 : return spheroids[index].polar_radius; 133 : } 134 : 135 0 : return -1.0; 136 : }