1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
|
#ifndef SQL_GIS_GEOMETRIES_H_INCLUDED
#define SQL_GIS_GEOMETRIES_H_INCLUDED
// Copyright (c) 2017, 2025, Oracle and/or its affiliates.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program is designed to work with certain software (including
// but not limited to OpenSSL) that is licensed under separate terms,
// as designated in a particular file or component or in included license
// documentation. The authors of MySQL hereby grant you an additional
// permission to link the program and your derivative works with the
// separately licensed software that they have either included with
// the program or referenced in the documentation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License, version 2.0, for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
/// @file
///
/// This file declares the geometry class hierarchy used by the server as the
/// internal representation of geometries.
///
/// The hierarchy is closely modelled after the hierarchy in SFA-CA
/// (OGC 06-103r4), but since Boost.Geometry depends on type traits to
/// know if a geometry is in a Cartesian or geographic SRS, there are
/// Cartesian and geographic specializations of each instantiable type
/// in the SFA-CA. These are defined in geometries_cs.h.
///
/// Because of Boost.Geometry, iterators have to be of coordinate system
/// specific types. It is therefore not possible to have the standard begin()
/// and end() iterators in the common superclass. Instead, operator[] is
/// provided as a coordinate system agnostic option.
///
/// @see geometries_cs.h
#include <cmath>
#include <cstdint>
namespace gis {
/// Types of geometry objects. Not all are instantiable.
///
/// The values and the storage type are the same as used in WKB.
enum class Geometry_type : std::uint32_t {
kGeometry = 0,
kPoint = 1,
kLinestring = 2,
kPolygon = 3,
kMultipoint = 4,
kMultilinestring = 5,
kMultipolygon = 6,
kGeometrycollection = 7
// kMulticurve = 11,
// kMultisurface = 12,
// kCurve = 13,
// kSurface = 14,
};
/// Types of coordinate systems.
enum class Coordinate_system {
/// A Cartesian plane with the same unit in both directions.
kCartesian = 0,
/// An ellipsoidal system with longitude and latitude coordinates,
/// both in the same unit.
kGeographic = 1
};
/// Direction of a ring.
enum class Ring_direction {
/// Clockwise.
kCW = 0,
/// Counterclockwise.
kCCW = 1,
/// Unknown.
///
/// Used only as an output value when the function is unable to determine the
/// ring direction, e.g., if the ring contains only repetitions of the same
/// point, or if there is a spike in the ring.
kUnknown = 2
};
class Geometry_visitor;
/// Abstract superclass for all geometric objects.
///
/// Geometry is a non-instantiable type in SQL.
///
/// The hierarchy is first divided into abstract classes according to the SFA-CA
/// geometry type hierarchy, and then divided into concrete subclasses for each
/// coordinate system.
class Geometry {
public:
Geometry() = default;
virtual ~Geometry() = default;
Geometry(const Geometry &) = default;
Geometry &operator=(const Geometry &) = default;
// Returns a copy of an instantiable subclass of Geometry.
virtual Geometry *clone() const = 0;
/// Gets the geometry type of the object.
///
/// @return The type of this object
virtual Geometry_type type() const = 0;
/// Gets the coordinate system.
///
/// @return The coordinate system type.
virtual Coordinate_system coordinate_system() const = 0;
/// Applies a hierarchical visitor to this geometry.
///
/// @see gis::Geometry_visitor
///
/// @param v A hierarchical visitor.
///
/// @retval true The execution was aborted by the visitor.
/// @retval false The execution completed.
virtual bool accept(Geometry_visitor *v) = 0;
/// Check if this is an empty geometry.
///
/// The definition of empty geometries is the one in SFA-CA (OGC 06-103r4,
/// Sect. 6.1.2.2), i.e., an empty point set.
///
/// @note This is different from the function "empty", which returns true if a
/// geometry contains no subgeometries. E.g., a geometry collection may
/// contain another geometry collection which is empty. In this case, the
/// "empty" function would return false on the outermost geometry collection,
/// while "is_empty" would return true.
///
/// @retval true The geometry represents the empty point set.
/// @retval false The geometry represent a non-empty point set.
virtual bool is_empty() const = 0;
};
/// A 2d point.
///
/// Point is an instantiable type in SQL.
class Point : public Geometry {
public:
Point() : m_x(std::nan("")), m_y(std::nan("")) {}
Point(double x, double y) : m_x(x), m_y(y) {}
Geometry_type type() const override { return Geometry_type::kPoint; }
bool accept(Geometry_visitor *v) override;
bool is_empty() const override {
return (std::isnan(m_x) || std::isnan(m_y));
}
// Returns a copy of a subclass of Point.
Point *clone() const override = 0;
/// Gets a coordinate value.
///
/// This function signature must match the expectations of Boost.Geometry.
///
/// @tparam K Coordinate number, zero indexed.
/// @return The coordinate value.
template <std::size_t K>
double get() const;
/// Gets the first coordinate value.
///
/// For geographic points, the first coordinate is longitude.
///
/// @return The first coordinate value.
double x() const;
/// Gets the second coordinate value.
///
/// For geographic points, the second coordinate is latitude.
///
/// @return The second coordinate value.
double y() const;
/// Sets a coordinate.
///
/// This function signature must match the expectations of Boost.Geometry.
///
/// @tparam K Coordinate number, zero indexed.
/// @param d The coordinate value.
template <std::size_t K>
void set(double d);
/// Sets the first coordinate value.
///
/// For geographic points, the first coordinate is longitude.
///
/// @param d The coordinate value.
void x(double d);
/// Sets the second coordinate value.
///
/// For geographic points, the second coordinate is latitude.
///
/// @param d The coordinate value.
void y(double d);
private:
/// First coordinate (X or longitude).
///
/// Geographic coordinates are in radians, positive to the East of
/// Greenwich. Cartesian coordinates are in the SRSs length unit.
double m_x;
/// Second coordinate (Y or latitude).
///
/// Geographic coordinates are in radians, positive to the North of the
/// Equator. Cartesian coordinates are in the SRSs length unit.
double m_y;
};
/// Compares two points.
///
/// The point with the lowest X coordinate is the smaller point. If X
/// coordinates are equal, the point with the lowest Y coordinate is the
/// smaller.
///
/// @param lhs Left hand side.
/// @param rhs Right hand side.
///
/// @retval true Left hand side sorts before right hand side.
/// @retval false Left hand side does not sort before right hand side.
inline bool operator<(const Point &lhs, const Point &rhs) {
return (lhs.x() < rhs.x()) || (lhs.x() == rhs.x() && lhs.y() < rhs.y());
}
/// An abstract 2d curve.
///
/// Curve is a non-instantiable type in SQL.
class Curve : public Geometry {
public:
Geometry_type type() const override = 0;
bool accept(Geometry_visitor *v) override = 0;
};
/// A string of connected line segments.
///
/// Linestring is an instantiable type in SQL.
///
/// According to the SFA-CA, linestrings have a linear interpolation between
/// points. In MySQL, a linestring represents the geodesic. On a plane, this is
/// linear interpolation, but on an ellipsoid, it's the shortest path along the
/// ellipsoid surface.
class Linestring : public Curve {
public:
Geometry_type type() const override { return Geometry_type::kLinestring; }
bool accept(Geometry_visitor *v) override = 0;
bool is_empty() const override { return empty(); }
/// Creates a subclass of Linestring from a Coordinate_system
///
/// @param[in] coordinate_system Coordinate system to create a Linestring for.
/// @return A pointer to a Linestring that caller must free when it is done
/// with it.
static Linestring *create_linestring(Coordinate_system coordinate_system);
// Returns a copy of a subclass of Linestring.
Linestring *clone() const override = 0;
/// Adds a point to the end of the linestring.
///
/// @param pt The point to add.
virtual void push_back(const Point &pt) = 0;
virtual void push_back(Point &&pt) = 0;
/// Removes a point from the front of the linestring.
virtual void pop_front() = 0;
/// Checks if the linestring is empty.
///
/// Here, the definition of empty is that the linestring does not contain any
/// points. An invalid linestring with only one coordinate is not empty.
///
/// @retval true The linestring is empty.
/// @retval false The linestring is not empty.
virtual bool empty() const = 0;
/// Returns the size of (number of points in) the linestring.
///
/// @return Number of points in the linestring.
virtual std::size_t size() const = 0;
/// Removes all points from the linestring.
virtual void clear() noexcept = 0;
/// Returns the last point of the linestring.
///
/// @return Last point of linestring
virtual Point &back() = 0;
virtual const Point &back() const = 0;
/// Returns the first point of the linestring.
///
/// @return First point of linestring
virtual Point &front() = 0;
virtual const Point &front() const = 0;
virtual Point &operator[](std::size_t i) = 0;
virtual const Point &operator[](std::size_t i) const = 0;
};
/// A ring-shaped linestring.
///
/// Linearring is a non-instantiable type in SQL. It is used to represent
/// polygon rings.
///
/// The start and end point of the linestring must be the same (assumed, but not
/// enforced, by the implementation).
class Linearring : public Linestring {
public:
bool accept(Geometry_visitor *v) override = 0;
/// Creates a subclass of Linearring from a Coordinate_system
///
/// @param[in] coordinate_system Coordinate system to create a Linearring for.
/// @return A pointer to a Linearring that caller must free when it is done
/// with it.
static Linearring *create_linearring(Coordinate_system coordinate_system);
};
/// An abstract 2d surface.
///
/// Surface is a non-instantiable type in SQL.
class Surface : public Geometry {
public:
bool accept(Geometry_visitor *v) override = 0;
Geometry_type type() const override = 0;
};
/// A polygon consisting of an outer ring and zero or more interior rings
/// defining holes in the polygon.
///
/// Polygon is an instantiable type in SQL.
///
/// The interior rings must be inside the exterior ring (not enforced by the
/// implementation).
class Polygon : public Surface {
public:
Geometry_type type() const override { return Geometry_type::kPolygon; }
bool accept(Geometry_visitor *v) override = 0;
bool is_empty() const override { return empty(); }
/// Creates a subclass of Polygon from a Coordinate_system
///
/// @param[in] coordinate_system Coordinate system to create a Polygon for.
/// @return A pointer to a Polygon that caller must free when it is done
/// with it.
static Polygon *create_polygon(Coordinate_system coordinate_system);
// Returns a copy of a subclass of Polygon.
Polygon *clone() const override = 0;
/// Adds a linear ring to the polygon.
///
/// The first ring will become the exterior ring. All following rings will be
/// interior rings (holes).
///
/// @param lr The linear ring to add.
virtual void push_back(const Linearring &lr) = 0;
virtual void push_back(Linearring &&lr) = 0;
/// Checks if the polygon is empty.
///
/// The polygon is considered empty if it has no rings.
///
/// @retval true The polygon is empty.
/// @retval false The polygon is not empty.
virtual bool empty() const = 0;
/// Returns the size of the polygon.
///
/// @return Number of rings in the polygon (exterior + interior).
virtual std::size_t size() const = 0;
/// Returns the exterior ring of the polygon.
///
/// @note If the polygon currently has no exterior ring, an empty one is
/// added.
///
/// @return The exterior ring.
virtual Linearring &exterior_ring() = 0;
/// Returns an interior ring of the polygon.
///
/// @param n Ring number, zero indexed.
///
/// @return The interior ring.
virtual Linearring &interior_ring(std::size_t n) = 0;
};
/// A collection of geometries.
///
/// Geometrycollection is an instantiable type in SQL. It is the only
/// instantiable non-leaf geometry type.
///
/// The Geometrycollection class places no restrictions on type, overlapping,
/// etc. Subclasses do.
class Geometrycollection : public Geometry {
public:
Geometry_type type() const override {
return Geometry_type::kGeometrycollection;
}
bool accept(Geometry_visitor *v) override = 0;
/// Creates a subclass of Geometrycollection from a Coordinate_system
///
/// @param[in] coordinate_system Coordinate system to create a
/// Geometrycollection for.
/// @return A pointer to a Geometrycollection that caller must free when it is
/// done with it.
static Geometrycollection *CreateGeometrycollection(
Coordinate_system coordinate_system);
/// Adds a geometry to the collection.
///
/// @param g The geometry to add.
virtual void push_back(const Geometry &g) = 0;
virtual void push_back(Geometry &&g) = 0;
/// Removes a geometry from the front of the collection.
virtual void pop_front() = 0;
/// Checks if the collection is empty.
///
/// @retval true The polygon is empty.
/// @retval false The polygon is not empty.
virtual bool empty() const = 0;
/// Returns the size of the geometrycollection.
///
/// @return Number of geometries in the geometrycollection.
virtual std::size_t size() const = 0;
/// Resizes the geometrycollection to contain a given number of elements.
///
/// If the new size is smaller than the current size, the remaining geometries
/// are discarded.
///
/// @param[in] count The new number of geometries.
virtual void resize(std::size_t count) = 0;
/// Removes all geometries from the geometrycollection.
virtual void clear() noexcept = 0;
/// Returns the first geometry of the collection.
///
/// @return First geometry of the collection
virtual Geometry &front() = 0;
virtual const Geometry &front() const = 0;
virtual Geometry &operator[](std::size_t i) = 0;
virtual const Geometry &operator[](std::size_t i) const = 0;
/// Clone pattern to easily duplicate a Geometrycollection.
///
/// @return A pointer to a copy of the Geometrycollection that caller
/// must free when it is done with it.
Geometrycollection *clone() const override = 0;
/// Creates a subclass from a Coordinate_system
///
/// @param[in] coordinate_system Coordinate system to create a
/// geometrycollection for
/// @return A pointer to a Geometrycollection that caller must free when it is
/// done with it.
static Geometrycollection *create_geometrycollection(
Coordinate_system coordinate_system);
};
/// A collection of points.
///
/// Multipoint is an instantiable type in SQL.
class Multipoint : public Geometrycollection {
public:
Geometry_type type() const override { return Geometry_type::kMultipoint; }
bool accept(Geometry_visitor *v) override = 0;
/// Clone pattern to easily duplicate a Multipoint.
///
/// @return A pointer to a copy of the Multipoint that caller
/// must free when it is done with it.
Multipoint *clone() const override = 0;
/// Returns the first point of the Multipoint.
///
/// @return First point of the Multipoint
Point &front() override = 0;
const Point &front() const override = 0;
/// Creates a subclass of Multipoint from a Coordinate_system
///
/// @param[in] coordinate_system Coordinate system to create a Multipoint for.
/// @return A pointer to a Multipoint that caller must free when it is done
/// with it.
static Multipoint *create_multipoint(Coordinate_system coordinate_system);
};
/// An abstract collection of curves.
///
/// Multicurve is a non-instantiable type in SQL.
class Multicurve : public Geometrycollection {
public:
Geometry_type type() const override = 0;
bool accept(Geometry_visitor *v) override = 0;
};
/// A collection of linestrings.
///
/// Multilinestring is an instantiable type in SQL.
class Multilinestring : public Multicurve {
public:
Geometry_type type() const override {
return Geometry_type::kMultilinestring;
}
bool accept(Geometry_visitor *v) override = 0;
/// Clone pattern to easily duplicate a Multilinestring.
///
/// @return A pointer to a copy of the Multilinestring that caller
/// must free when it is done with it.
Multilinestring *clone() const override = 0;
/// Returns the first linestring of the Multilinestring.
///
/// @return First linestring of the Multilinestring
Linestring &front() override = 0;
const Linestring &front() const override = 0;
/// Creates a subclass of Multilinestring from a Coordinate_system
///
/// @param[in] coordinate_system Coordinate system to create a Multilinestring
/// for.
/// @return A pointer to a Multilinestring that caller must free when it is
/// done with it.
static Multilinestring *create_multilinestring(
Coordinate_system coordinate_system);
};
/// An abstract collection of surfaces.
///
/// Multisurface is a non-instantiable type in SQL.
class Multisurface : public Geometrycollection {
public:
Geometry_type type() const override = 0;
bool accept(Geometry_visitor *v) override = 0;
};
/// A collection of polygons.
///
/// Multipolygon is an instantiable type in SQL.
class Multipolygon : public Multisurface {
public:
Geometry_type type() const override { return Geometry_type::kMultipolygon; }
bool accept(Geometry_visitor *v) override = 0;
/// Clone pattern to easily duplicate a Multipolygon.
///
/// @return A pointer to a copy of the Multipolygon that caller
/// must free when it is done with it.
Multipolygon *clone() const override = 0;
/// Returns the first polygon of the Multipolygon.
///
/// @return First polygon of the Multipolygon
Polygon &front() override = 0;
const Polygon &front() const override = 0;
/// Creates a subclass of Multipolygon from a Coordinate_system
///
/// @param[in] coordinate_system Coordinate system to create a Multipolygon
/// for.
/// @return A pointer to a Multipolygon that caller must free when it is done
/// with it.
static Multipolygon *create_multipolygon(Coordinate_system coordinate_system);
};
/// Get the type name string corresponding to a geometry type.
///
/// @param type The geometry type.
///
/// @return A string containing the type name (in uppercase).
const char *type_to_name(Geometry_type type);
} // namespace gis
#endif // SQL_GIS_GEOMETRIES_H_INCLUDED
|