1 /* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
22
23 #include "sql/gis/st_units_of_measure.h"
24 #include <m_ctype.h>
25 #include "mysqld_error.h"
26 namespace gis {
27
units()28 collation_unordered_map<std::string, Unit> units() {
29 collation_unordered_map<std::string, Unit> units(
30 &my_charset_utf8mb4_0900_ai_ci, PSI_INSTRUMENT_ME);
31
32 units[std::string("millimetre")] = Unit(Unit_Type::kLinear, 0.001);
33 units[std::string("centimetre")] = Unit(Unit_Type::kLinear, 0.01);
34 units[std::string("metre")] = Unit(Unit_Type::kLinear, 1);
35 units[std::string("foot")] = Unit(Unit_Type::kLinear, 0.3048);
36 units[std::string("US survey foot")] =
37 Unit(Unit_Type::kLinear, 0.30480060960121924);
38 units[std::string("Clarke's foot")] = Unit(Unit_Type::kLinear, 0.3047972654);
39 units[std::string("fathom")] = Unit(Unit_Type::kLinear, 1.8288);
40 units[std::string("nautical mile")] = Unit(Unit_Type::kLinear, 1852);
41 units[std::string("German legal metre")] =
42 Unit(Unit_Type::kLinear, 1.0000135965);
43 units[std::string("US survey chain")] =
44 Unit(Unit_Type::kLinear, 20.11684023368047);
45 units[std::string("US survey link")] =
46 Unit(Unit_Type::kLinear, 0.2011684023368047);
47 units[std::string("US survey mile")] =
48 Unit(Unit_Type::kLinear, 1609.3472186944375);
49 units[std::string("kilometre")] = Unit(Unit_Type::kLinear, 1000);
50 units[std::string("Clarke's yard")] = Unit(Unit_Type::kLinear, 0.9143917962);
51 units[std::string("Clarke's chain")] =
52 Unit(Unit_Type::kLinear, 20.1166195164);
53 units[std::string("Clarke's link")] =
54 Unit(Unit_Type::kLinear, 0.201166195164);
55 units[std::string("British yard (Sears 1922)")] =
56 Unit(Unit_Type::kLinear, 0.9143984146160287);
57 units[std::string("British foot (Sears 1922)")] =
58 Unit(Unit_Type::kLinear, 0.3047994715386762);
59 units[std::string("British chain (Sears 1922)")] =
60 Unit(Unit_Type::kLinear, 20.116765121552632);
61 units[std::string("British link (Sears 1922)")] =
62 Unit(Unit_Type::kLinear, 0.2011676512155263);
63 units[std::string("British yard (Benoit 1895 A)")] =
64 Unit(Unit_Type::kLinear, 0.9143992);
65 units[std::string("British foot (Benoit 1895 A)")] =
66 Unit(Unit_Type::kLinear, 0.3047997333333333);
67 units[std::string("British chain (Benoit 1895 A)")] =
68 Unit(Unit_Type::kLinear, 20.1167824);
69 units[std::string("British link (Benoit 1895 A)")] =
70 Unit(Unit_Type::kLinear, 0.201167824);
71 units[std::string("British yard (Benoit 1895 B)")] =
72 Unit(Unit_Type::kLinear, 0.9143992042898124);
73 units[std::string("British foot (Benoit 1895 B)")] =
74 Unit(Unit_Type::kLinear, 0.30479973476327077);
75 units[std::string("British chain (Benoit 1895 B)")] =
76 Unit(Unit_Type::kLinear, 20.116782494375872);
77 units[std::string("British link (Benoit 1895 B)")] =
78 Unit(Unit_Type::kLinear, 0.2011678249437587);
79 units[std::string("British foot (1865)")] =
80 Unit(Unit_Type::kLinear, 0.30480083333333335);
81 units[std::string("Indian foot")] =
82 Unit(Unit_Type::kLinear, 0.30479951024814694);
83 units[std::string("Indian foot (1937)")] =
84 Unit(Unit_Type::kLinear, 0.30479841);
85 units[std::string("Indian foot (1962)")] =
86 Unit(Unit_Type::kLinear, 0.3047996);
87 units[std::string("Indian foot (1975)")] =
88 Unit(Unit_Type::kLinear, 0.3047995);
89 units[std::string("Indian yard")] =
90 Unit(Unit_Type::kLinear, 0.9143985307444408);
91 units[std::string("Indian yard (1937)")] =
92 Unit(Unit_Type::kLinear, 0.91439523);
93 units[std::string("Indian yard (1962)")] =
94 Unit(Unit_Type::kLinear, 0.9143988);
95 units[std::string("Indian yard (1975)")] =
96 Unit(Unit_Type::kLinear, 0.9143985);
97 units[std::string("Statute mile")] = Unit(Unit_Type::kLinear, 1609.344);
98 units[std::string("Gold Coast foot")] =
99 Unit(Unit_Type::kLinear, 0.3047997101815088);
100 units[std::string("British foot (1936)")] =
101 Unit(Unit_Type::kLinear, 0.3048007491);
102 units[std::string("yard")] = Unit(Unit_Type::kLinear, 0.9144);
103 units[std::string("chain")] = Unit(Unit_Type::kLinear, 20.1168);
104 units[std::string("link")] = Unit(Unit_Type::kLinear, 0.201168);
105 units[std::string("British yard (Sears 1922 truncated)")] =
106 Unit(Unit_Type::kLinear, 0.914398);
107 units[std::string("British foot (Sears 1922 truncated)")] =
108 Unit(Unit_Type::kLinear, 0.30479933333333337);
109 units[std::string("British chain (Sears 1922 truncated)")] =
110 Unit(Unit_Type::kLinear, 20.116756);
111 units[std::string("British link (Sears 1922 truncated)")] =
112 Unit(Unit_Type::kLinear, 0.20116756);
113
114 return units;
115 }
116
get_conversion_factor(const std::string & unit,double * conversion_factor)117 bool get_conversion_factor(const std::string &unit, double *conversion_factor) {
118 DBUG_ASSERT(conversion_factor);
119 collation_unordered_map<std::string, Unit> unit_table = units();
120 collation_unordered_map<std::string, Unit>::iterator unit_it =
121 unit_table.find(unit);
122 if (unit_it == unit_table.end()) {
123 my_error(ER_UNIT_NOT_FOUND, myf(0), unit.c_str());
124 return true;
125 }
126 *conversion_factor = unit_it->second.conversion_factor;
127 return false;
128 }
129
130 } // namespace gis
131