MySQL 8.0.33
Source Code Documentation
spatial_reference_system_impl.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2023, Oracle and/or its affiliates.
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#ifndef DD__SPATIAL_REFERENCE_SYSTEM_IMPL_INCLUDED
24#define DD__SPATIAL_REFERENCE_SYSTEM_IMPL_INCLUDED
25
26#include <assert.h>
27#include <stdio.h>
28
29#include <cstddef> // std::nullptr_t
30#include <memory> // std::unique_ptr
31#include <new>
32#include <optional>
33
34#include "my_inttypes.h"
35#include "sql/dd/impl/types/entity_object_impl.h" // dd::Entity_object_impl
37#include "sql/dd/object_id.h"
38#include "sql/dd/sdi_fwd.h"
39#include "sql/dd/string_type.h"
40#include "sql/dd/types/spatial_reference_system.h" // dd:Spatial_reference_system
42#include "sql/gis/geometries.h" // gis::Coordinate_system
43#include "sql/gis/srid.h" // srid_t
44#include "sql/gis/srs/srs.h" // gis::srs::Spatial_reference_...
45#include "sql/sql_time.h" // gmt_time_to_local_time
46
47class THD;
48
49namespace dd {
50
51///////////////////////////////////////////////////////////////////////////
52
53class Open_dictionary_tables_ctx;
54class Raw_record;
55class Sdi_rcontext;
56class Sdi_wcontext;
57class Object_table;
58
59///////////////////////////////////////////////////////////////////////////
60
63 public:
65 : m_created(0),
71 m_description() {}
72
73 private:
75 : Weak_object(srs),
84
85 public:
86 const Object_table &object_table() const override;
87
88 bool validate() const override;
89
90 bool store_attributes(Raw_record *r) override;
91
92 bool restore_attributes(const Raw_record &r) override;
93
94 void serialize(Sdi_wcontext *wctx, Sdi_writer *w) const;
95
96 bool deserialize(Sdi_rcontext *rctx, const RJ_Value &val);
97
98 /// Parse the SRS definition string.
99 ///
100 /// Used internally. Made public to make it easier to write unit tests.
101 bool parse_definition();
102
103 public:
105
106 /////////////////////////////////////////////////////////////////////////
107 // created
108 /////////////////////////////////////////////////////////////////////////
109
110 ulonglong created(bool convert_time) const override {
111 return convert_time ? gmt_time_to_local_time(m_created) : m_created;
112 }
113
115
116 /////////////////////////////////////////////////////////////////////////
117 // last_altered
118 /////////////////////////////////////////////////////////////////////////
119
120 ulonglong last_altered(bool convert_time) const override {
121 return convert_time ? gmt_time_to_local_time(m_last_altered)
123 }
124
127 }
128
129 /////////////////////////////////////////////////////////////////////////
130 // organization
131 /////////////////////////////////////////////////////////////////////////
132
133 const std::optional<String_type> &organization() const override {
134 return m_organization;
135 }
136
138 m_organization = std::optional<String_type>(organization);
139 }
140
141 void set_organization(std::nullptr_t) override {
142 m_organization = std::optional<String_type>();
143 }
144
145 /////////////////////////////////////////////////////////////////////////
146 // organization_coordsys_id
147 /////////////////////////////////////////////////////////////////////////
148
149 const std::optional<gis::srid_t> &organization_coordsys_id() const override {
151 }
152
156 std::optional<gis::srid_t>(organization_coordsys_id);
157 }
158
159 void set_organization_coordsys_id(std::nullptr_t) override {
160 m_organization_coordsys_id = std::optional<gis::srid_t>();
161 }
162
163 /////////////////////////////////////////////////////////////////////////
164 // definition
165 /////////////////////////////////////////////////////////////////////////
166
167 const String_type &definition() const override { return m_definition; }
168
169 void set_definition(const String_type &definition) override {
171 }
172
174 // Work around bugs in Developer Studio 12.5 on Solaris by casting the enum
175 // to int. Otherwise the default case, and only the default case, is always
176 // executed. This happens regardless of SRS type value.
177 switch (static_cast<int>(m_parsed_definition->srs_type())) {
178 case static_cast<int>(gis::srs::Srs_type::PROJECTED):
180 case static_cast<int>(gis::srs::Srs_type::GEOGRAPHIC):
182 default:
183 /* purecov: begin deadcode */
184 assert(false);
186 /* purecov: end */
187 }
188 }
189
190 bool is_projected() const override {
192 }
193
194 bool is_geographic() const override {
196 }
197
198 bool is_cartesian() const override {
200 }
201
202 bool is_lat_long() const override;
203
204 double semi_major_axis() const override {
205 if (is_geographic()) {
206 return static_cast<gis::srs::Geographic_srs *>(m_parsed_definition.get())
207 ->semi_major_axis();
208 } else {
209 return 0.0;
210 }
211 }
212
213 double semi_minor_axis() const override {
214 if (is_geographic()) {
216 static_cast<gis::srs::Geographic_srs *>(m_parsed_definition.get());
217 if (srs->inverse_flattening() == 0.0)
218 return srs->semi_major_axis();
219 else
220 return srs->semi_major_axis() * (1 - 1 / srs->inverse_flattening());
221 } else {
222 return 0.0;
223 }
224 }
225
226 double linear_unit() const override {
227 return m_parsed_definition->linear_unit();
228 }
229
230 double angular_unit() const override {
231 return m_parsed_definition->angular_unit();
232 }
233
234 double prime_meridian() const override {
235 return m_parsed_definition->prime_meridian();
236 }
237
238 bool positive_east() const override {
239 if (is_lat_long()) {
240 return (m_parsed_definition->axis_direction(1) ==
242 } else {
243 return (m_parsed_definition->axis_direction(0) ==
245 }
246 }
247
248 bool positive_north() const override {
249 if (is_lat_long()) {
250 return (m_parsed_definition->axis_direction(0) ==
252 } else {
253 return (m_parsed_definition->axis_direction(1) ==
255 }
256 }
257
258 bool missing_towgs84() const override {
259 return (!m_parsed_definition->is_wgs84_based() &&
260 !m_parsed_definition->has_towgs84());
261 }
262
263 double to_radians(double d) const override {
264 assert(is_geographic());
265 assert(angular_unit() > 0.0);
266 return d * angular_unit();
267 }
268
269 double from_radians(double d) const override {
270 assert(is_geographic());
271 assert(angular_unit() > 0.0);
272 return d / angular_unit();
273 }
274
275 double to_normalized_latitude(double d) const override {
276 double latitude = to_radians(d);
277 if (!positive_north()) latitude *= -1.0;
278 return latitude;
279 }
280
281 double from_normalized_latitude(double d) const override {
282 double latitude = from_radians(d);
283 if (!positive_north()) latitude *= -1.0;
284 return latitude;
285 }
286
287 double to_normalized_longitude(double d) const override {
288 double longitude = d;
289 if (!positive_east()) longitude *= -1.0;
290 longitude += prime_meridian();
291 longitude *= angular_unit();
292 return longitude;
293 }
294
295 double from_normalized_longitude(double d) const override {
296 double longitude = d;
297 longitude /= angular_unit();
298 longitude -= prime_meridian();
299 if (!positive_east()) longitude *= -1.0;
300 return longitude;
301 }
302
303 bool can_be_modified_to(const Spatial_reference_system &srs) const override {
304 return m_parsed_definition->can_be_modified_to(
305 *static_cast<const Spatial_reference_system_impl &>(srs)
307 }
308
309 String_type proj4_parameters() const override {
310 return m_parsed_definition->proj4_parameters().c_str();
311 }
312
313 /////////////////////////////////////////////////////////////////////////
314 // description
315 /////////////////////////////////////////////////////////////////////////
316
317 const std::optional<String_type> &description() const override {
318 return m_description;
319 }
320
322 m_description = std::optional<String_type>(description);
323 }
324
325 void set_description(std::nullptr_t) override {
326 m_description = std::optional<String_type>();
327 }
328
329 // Fix "inherits ... via dominance" warnings
331 const Entity_object_impl *impl() const override {
333 }
334 Object_id id() const override { return Entity_object_impl::id(); }
335 bool is_persistent() const override {
337 }
338 const String_type &name() const override {
340 }
341 void set_name(const String_type &name) override {
343 }
344
345 public:
346 void debug_print(String_type &outb) const override {
347 char outbuf[1024];
348 sprintf(outbuf,
349 "SPATIAL REFERENCE SYSTEM OBJECT: id= {OID: %lld}, "
350 "name= %s, m_created= %llu, m_last_altered= %llu",
351 id(), name().c_str(), m_created, m_last_altered);
352 outb = String_type(outbuf);
353 }
354
355 private:
356 // Fields
359 std::optional<String_type> m_organization;
360 std::optional<gis::srid_t> m_organization_coordsys_id;
362 std::unique_ptr<gis::srs::Spatial_reference_system> m_parsed_definition;
363 std::optional<String_type> m_description;
364
365 Spatial_reference_system *clone() const override {
366 return new Spatial_reference_system_impl(*this);
367 }
368
370 /*
371 Even though we don't drop SRSes en masse we still create slimmed
372 down version for consistency sake.
373 */
374 Spatial_reference_system_impl *placeholder =
376 placeholder->set_id(id());
377 placeholder->set_name(name());
378 return placeholder;
379 }
380};
381
382///////////////////////////////////////////////////////////////////////////
383
384} // namespace dd
385
386#endif // DD__SPATIAL_REFERENCE_SYSTEM_IMPL_INCLUDED
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
Definition: entity_object_impl.h:43
void set_name(const String_type &name) override
Definition: entity_object_impl.h:61
void set_id(Object_id id)
Definition: entity_object_impl.h:50
Object_id id() const override
The unique dictionary object id.
Definition: entity_object_impl.h:48
const String_type & name() const override
Definition: entity_object_impl.h:59
Entity_object_impl * impl() override
Definition: entity_object_impl.h:67
bool is_persistent() const override
Is dictionary object persistent in dictionary tables ?
Definition: entity_object_impl.h:56
This class represents all data dictionary table like mysql.tables, mysql.columns and more.
Definition: object_table.h:71
Auxiliary class for opening dictionary tables.
Definition: transaction_impl.h:75
Definition: raw_record.h:45
Opaque context which keeps reusable resoureces needed during deserialization.
Definition: sdi.cc:230
Opaque context which keeps reusable resources needed during serialization.
Definition: sdi.cc:128
Definition: spatial_reference_system_impl.h:62
bool missing_towgs84() const override
Checks whether the SRS definition is missing a TOWGS84 clause.
Definition: spatial_reference_system_impl.h:258
std::optional< String_type > m_organization
Definition: spatial_reference_system_impl.h:359
Spatial_reference_system * clone_dropped_object_placeholder() const override
Allocate a new object which can serve as a placeholder for the original object in the Dictionary_clie...
Definition: spatial_reference_system_impl.h:369
std::unique_ptr< gis::srs::Spatial_reference_system > m_parsed_definition
Definition: spatial_reference_system_impl.h:362
bool deserialize(Sdi_rcontext *rctx, const RJ_Value &val)
Definition: spatial_reference_system_impl.cc:144
ulonglong m_created
Definition: spatial_reference_system_impl.h:357
void set_organization_coordsys_id(std::nullptr_t) override
Definition: spatial_reference_system_impl.h:159
double semi_minor_axis() const override
Definition: spatial_reference_system_impl.h:213
double linear_unit() const override
Definition: spatial_reference_system_impl.h:226
std::optional< gis::srid_t > m_organization_coordsys_id
Definition: spatial_reference_system_impl.h:360
Spatial_reference_system_impl(const Spatial_reference_system_impl &srs)
Definition: spatial_reference_system_impl.h:74
double angular_unit() const override
Definition: spatial_reference_system_impl.h:230
double to_radians(double d) const override
Converts a coordinate value from the SRS unit to radians.
Definition: spatial_reference_system_impl.h:263
const Object_table & object_table() const override
Definition: spatial_reference_system_impl.cc:204
const std::optional< gis::srid_t > & organization_coordsys_id() const override
Definition: spatial_reference_system_impl.h:149
void set_definition(const String_type &definition) override
Definition: spatial_reference_system_impl.h:169
bool restore_attributes(const Raw_record &r) override
Definition: spatial_reference_system_impl.cc:70
void set_organization(const String_type &organization) override
Definition: spatial_reference_system_impl.h:137
ulonglong created(bool convert_time) const override
Definition: spatial_reference_system_impl.h:110
void serialize(Sdi_wcontext *wctx, Sdi_writer *w) const
Definition: spatial_reference_system_impl.cc:119
Spatial_reference_system_impl()
Definition: spatial_reference_system_impl.h:64
void set_description(const String_type &description) override
Definition: spatial_reference_system_impl.h:321
bool validate() const override
Definition: spatial_reference_system_impl.cc:55
double prime_meridian() const override
Definition: spatial_reference_system_impl.h:234
bool is_lat_long() const override
Check whether an SRS has latitude-longitude axis ordering.
Definition: spatial_reference_system_impl.cc:61
void debug_print(String_type &outb) const override
Definition: spatial_reference_system_impl.h:346
ulonglong last_altered(bool convert_time) const override
Definition: spatial_reference_system_impl.h:120
const std::optional< String_type > & organization() const override
Definition: spatial_reference_system_impl.h:133
static void register_tables(Open_dictionary_tables_ctx *otx)
Definition: spatial_reference_system_impl.cc:210
gis::Coordinate_system cs_type() const override
Definition: spatial_reference_system_impl.h:173
bool is_projected() const override
Definition: spatial_reference_system_impl.h:190
void set_organization_coordsys_id(gis::srid_t organization_coordsys_id) override
Definition: spatial_reference_system_impl.h:153
double from_normalized_longitude(double d) const override
Converts a longitude value from the in-memory representation of longitude (radians,...
Definition: spatial_reference_system_impl.h:295
double to_normalized_latitude(double d) const override
Converts a latitude value from the SRS unit and direction to the in-memory representation of latitude...
Definition: spatial_reference_system_impl.h:275
String_type m_definition
Definition: spatial_reference_system_impl.h:361
double from_radians(double d) const override
Converts a coordinate value from radians to the SRS unit.
Definition: spatial_reference_system_impl.h:269
bool is_persistent() const override
Is dictionary object persistent in dictionary tables ?
Definition: spatial_reference_system_impl.h:335
bool positive_east() const override
Definition: spatial_reference_system_impl.h:238
Entity_object_impl * impl() override
Definition: spatial_reference_system_impl.h:330
std::optional< String_type > m_description
Definition: spatial_reference_system_impl.h:363
void set_description(std::nullptr_t) override
Definition: spatial_reference_system_impl.h:325
String_type proj4_parameters() const override
Gets the proj4 parameters for this SRS.
Definition: spatial_reference_system_impl.h:309
Object_id id() const override
The unique dictionary object id.
Definition: spatial_reference_system_impl.h:334
Spatial_reference_system * clone() const override
Allocate a new object and invoke the copy constructor.
Definition: spatial_reference_system_impl.h:365
bool is_cartesian() const override
Definition: spatial_reference_system_impl.h:198
double semi_major_axis() const override
Definition: spatial_reference_system_impl.h:204
void set_created(ulonglong created) override
Definition: spatial_reference_system_impl.h:114
const String_type & name() const override
Definition: spatial_reference_system_impl.h:338
bool parse_definition()
Parse the SRS definition string.
Definition: spatial_reference_system_impl.cc:175
void set_name(const String_type &name) override
Definition: spatial_reference_system_impl.h:341
double from_normalized_latitude(double d) const override
Converts a latitude value from the in-memory representation of latitude (radians, positive North) to ...
Definition: spatial_reference_system_impl.h:281
bool can_be_modified_to(const Spatial_reference_system &srs) const override
Checks if this SRS can be changed to another SRS definition without changing any computations.
Definition: spatial_reference_system_impl.h:303
const std::optional< String_type > & description() const override
Definition: spatial_reference_system_impl.h:317
ulonglong m_last_altered
Definition: spatial_reference_system_impl.h:358
bool store_attributes(Raw_record *r) override
Definition: spatial_reference_system_impl.cc:90
double to_normalized_longitude(double d) const override
Converts a longitude value from the SRS unit, direction and meridian to the in-memory representation ...
Definition: spatial_reference_system_impl.h:287
const Entity_object_impl * impl() const override
Definition: spatial_reference_system_impl.h:331
bool positive_north() const override
Definition: spatial_reference_system_impl.h:248
void set_last_altered(ulonglong last_altered) override
Definition: spatial_reference_system_impl.h:125
const String_type & definition() const override
Definition: spatial_reference_system_impl.h:167
void set_organization(std::nullptr_t) override
Definition: spatial_reference_system_impl.h:141
bool is_geographic() const override
Definition: spatial_reference_system_impl.h:194
Definition: spatial_reference_system.h:52
Base class for all data dictionary objects.
Definition: weak_object.h:41
A geographic (longitude-latitude) spatial reference system.
Definition: srs.h:209
double semi_major_axis() const
Definition: srs.h:270
double inverse_flattening() const
Definition: srs.h:272
This file declares the geometry class hierarchy used by the server as the internal representation of ...
ulonglong gmt_time_to_local_time(ulonglong gmt_time)
This function gets GMT time and adds value of time_zone to get the local time.
Definition: sql_time.cc:840
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:42
RJ_Writer Sdi_writer
Alias for the rapidjson Writer type to use in serialization.
Definition: sdi_fwd.h:63
unsigned long long Object_id
Definition: object_id.h:30
rapidjson::GenericValue< RJ_Encoding, RJ_Allocator > RJ_Value
Definition: sdi_fwd.h:48
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:50
Coordinate_system
Types of coordinate systems.
Definition: geometries.h:68
@ kCartesian
A Cartesian plane with the same unit in both directions.
@ kGeographic
An ellipsoidal system with longitude and latitude coordinates, both in the same unit.
std::uint32_t srid_t
A spatial reference system ID (SRID).
Definition: srid.h:32
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
This header provides Rapidjson Type Aliases.
Interface for server time utilities.