Geometry.h
Go to the documentation of this file.
1 //=============================================================================
2 // Copyright (C) 2021-2024 Wageningen University - All Rights Reserved
3 // Author: Gonzalo Mier
4 // BSD-3 License
5 //=============================================================================
6 
7 #pragma once
8 #ifndef FIELDS2COVER_TYPES_GEOMETRY_H_
9 #define FIELDS2COVER_TYPES_GEOMETRY_H_
10 
11 #include <gdal/ogr_geometry.h>
12 #include <gdal/ogr_core.h>
13 #include <functional>
14 #include <memory>
15 #include <vector>
16 #include <string>
17 #include <utility>
18 #include <iostream>
19 #include <boost/math/constants/constants.hpp>
20 
21 namespace f2c::types {
22 
23 class EmptyDestructor {};
24 
25 template <class T, OGRwkbGeometryType R>
26 struct Geometry {
27  public:
28  Geometry();
29  explicit Geometry(const T& g);
30  explicit Geometry(std::shared_ptr<T> g);
31  explicit Geometry(T* g, EmptyDestructor);
32  explicit Geometry(const T* g);
33  explicit Geometry(OGRGeometry* g, EmptyDestructor);
34  explicit Geometry(const OGRGeometry* g);
35  ~Geometry();
36  explicit Geometry(const Geometry& g);
37  Geometry(Geometry&& g);
39  Geometry& operator=(const Geometry& g);
40 
41  std::shared_ptr<T> operator->();
42  std::shared_ptr<const T> operator->() const;
43  T* get();
44  const T* get() const;
45 
46 
47  bool operator !=(const Geometry<T, R>& geom2) const;
48  bool operator ==(const Geometry<T, R>& geom2) const;
49 
51  double getDimMinX() const;
52 
54  double getDimMaxX() const;
55 
57  double getDimMinY() const;
58 
60  double getDimMaxY() const;
61 
63  double getHeight() const;
64 
66  double getWidth() const;
67 
72  double getMinSafeLength() const;
73 
75  template <class T2, OGRwkbGeometryType R2>
76  double distance(const Geometry<T2, R2>& p) const;
77 
79  template <class T2, OGRwkbGeometryType R2>
80  bool disjoint(const Geometry<T2, R2>& geom) const;
81 
83  template <class T2, OGRwkbGeometryType R2>
84  bool crosses(const Geometry<T2, R2>& geom) const;
85 
87  template <class T2, OGRwkbGeometryType R2>
88  bool touches(const Geometry<T2, R2>& geom) const;
89 
91  template <class T2, OGRwkbGeometryType R2>
92  bool within(const Geometry<T2, R2>& geom) const;
93 
95  template <class T2, OGRwkbGeometryType R2>
96  bool intersects(const Geometry<T2, R2>& geom) const;
97 
101  static double mod_2pi(double val);
102  static double mod(double a, double b);
103 
104  static double getAngContinuity(double prev_val, double val);
105  static std::vector<double> getAngContinuity(const std::vector<double>& val);
106 
111  static double getAngleDiffAbs(double a, double b);
112 
114  static double getAngleAvg(double a, double b);
115 
116  bool isEmpty() const;
117 
118  std::string exportToWkt() const;
119  void importFromWkt(const std::string& text);
120  std::string exportToGML() const;
121  std::string exportToKML() const;
122  std::string exportToJson() const;
123 
124  // Code adapted from:
125  // https://github.com/OSGeo/gdal/blob/717dcc0eed252e2f78c142b1f7866e49c5511224/ogr/ogrgeometry.cpp#L4309
126  OGRGeometry* OGRBuffer(double dfDist, int side = 0) const;
127 
128  protected:
129  std::shared_ptr<T> data_;
130 
131  protected:
132  // Code adapted from:
133  // https://github.com/OSGeo/gdal/blob/b0aa6065a39b252cb8306e9c2e2535d6dda0fb55/port/cpl_conv.h#L397
134  template <typename To, typename From>
135  inline To downCast(From *f) const;
136 
137  template<typename RetType>
138  static RetType destroyResGeom(OGRGeometry*);
139 
140  private:
141  // Code extracted from:
142  // https://github.com/OSGeo/gdal/blob/717dcc0eed252e2f78c142b1f7866e49c5511224/ogr/ogrgeometry.cpp#L4309
143  OGRGeometry* buildGeometryFromGEOS(
144  GEOSContextHandle_t hGEOSCtxt, GEOSGeom hGeosProduct,
145  const OGRGeometry *poSelf, const OGRGeometry *poOtherGeom) const;
146  // Code extracted from:
147  // https://github.com/OSGeo/gdal/blob/717dcc0eed252e2f78c142b1f7866e49c5511224/ogr/ogrgeometry.cpp#L4309
148  OGRGeometry* OGRGeometryRebuildCurves(const OGRGeometry *poGeom,
149  const OGRGeometry *poOtherGeom, OGRGeometry *poOGRProduct) const;
150 };
151 
152 
153 } // namespace f2c::types
154 
156 
157 #endif // FIELDS2COVER_TYPES_GEOMETRY_H_
f2c::types::Geometry::within
bool within(const Geometry< T2, R2 > &geom) const
Check if this geometry is inside another geometry.
Definition: Geometry_impl.hpp:163
f2c::types::Geometry::touches
bool touches(const Geometry< T2, R2 > &geom) const
Check if this and another geometry touch each other.
Definition: Geometry_impl.hpp:157
f2c::types::Geometry::~Geometry
~Geometry()
f2c::types
Types used by fields2cover library.
Definition: Cell.h:20
f2c::types::Geometry
Definition: Geometry.h:26
f2c::types::Geometry::intersects
bool intersects(const Geometry< T2, R2 > &geom) const
Check if this and another geometry intersects.
Definition: Geometry_impl.hpp:169
f2c::types::Geometry::exportToJson
std::string exportToJson() const
Definition: Geometry_impl.hpp:252
f2c::types::Geometry::getAngleAvg
static double getAngleAvg(double a, double b)
Get the angle that is between a and b in the shortest direction.
Definition: Geometry_impl.hpp:210
f2c::types::Geometry::getHeight
double getHeight() const
Get the height of the geometry.
Definition: Geometry_impl.hpp:116
f2c::types::Geometry::exportToKML
std::string exportToKML() const
Definition: Geometry_impl.hpp:244
f2c::types::Geometry::getMinSafeLength
double getMinSafeLength() const
Definition: Geometry_impl.hpp:130
f2c::types::Geometry::destroyResGeom
static RetType destroyResGeom(OGRGeometry *)
Definition: Geometry_impl.hpp:281
f2c::types::Geometry::operator=
Geometry & operator=(Geometry &&g)
f2c::types::Geometry::operator!=
bool operator!=(const Geometry< T, R > &geom2) const
Definition: Geometry_impl.hpp:77
f2c::types::Geometry::OGRBuffer
OGRGeometry * OGRBuffer(double dfDist, int side=0) const
Definition: Geometry_impl.hpp:291
f2c::types::Geometry::isEmpty
bool isEmpty() const
Definition: Geometry_impl.hpp:222
f2c::types::Geometry::crosses
bool crosses(const Geometry< T2, R2 > &geom) const
Check if this and another geometry cross.
Definition: Geometry_impl.hpp:151
f2c::types::Geometry::buildGeometryFromGEOS
OGRGeometry * buildGeometryFromGEOS(GEOSContextHandle_t hGEOSCtxt, GEOSGeom hGeosProduct, const OGRGeometry *poSelf, const OGRGeometry *poOtherGeom) const
Definition: Geometry_impl.hpp:335
f2c::types::Geometry::get
T * get()
Definition: Geometry_impl.hpp:71
f2c::types::Geometry::operator==
bool operator==(const Geometry< T, R > &geom2) const
Definition: Geometry_impl.hpp:82
f2c::types::Geometry::getDimMaxX
double getDimMaxX() const
Get the maximum x value of the geometry.
Definition: Geometry_impl.hpp:95
f2c::types::Geometry::exportToWkt
std::string exportToWkt() const
Definition: Geometry_impl.hpp:227
f2c::types::Geometry::mod_2pi
static double mod_2pi(double val)
Definition: Geometry_impl.hpp:174
f2c::types::Geometry::getAngContinuity
static double getAngContinuity(double prev_val, double val)
Definition: Geometry_impl.hpp:179
f2c::types::Geometry::OGRGeometryRebuildCurves
OGRGeometry * OGRGeometryRebuildCurves(const OGRGeometry *poGeom, const OGRGeometry *poOtherGeom, OGRGeometry *poOGRProduct) const
Definition: Geometry_impl.hpp:321
f2c::types::Geometry::operator->
std::shared_ptr< T > operator->()
Definition: Geometry_impl.hpp:65
f2c::types::Geometry::getAngleDiffAbs
static double getAngleDiffAbs(double a, double b)
Definition: Geometry_impl.hpp:204
f2c::types::Geometry::downCast
To downCast(From *f) const
Definition: Geometry_impl.hpp:270
f2c::types::Geometry::distance
double distance(const Geometry< T2, R2 > &p) const
Compute shortest distance between this and another geometry.
Definition: Geometry_impl.hpp:139
Geometry_impl.hpp
f2c::types::Geometry::getDimMinX
double getDimMinX() const
Get the minimum x value of the geometry.
Definition: Geometry_impl.hpp:88
f2c::types::Geometry::data_
std::shared_ptr< T > data_
Definition: Geometry.h:129
f2c::types::Geometry::importFromWkt
void importFromWkt(const std::string &text)
Definition: Geometry_impl.hpp:260
f2c::types::Geometry::exportToGML
std::string exportToGML() const
Definition: Geometry_impl.hpp:236
f2c::types::Geometry::getDimMaxY
double getDimMaxY() const
Get the maximum y value of the geometry.
Definition: Geometry_impl.hpp:109
f2c::types::Geometry::getWidth
double getWidth() const
Get the width of the geometry.
Definition: Geometry_impl.hpp:123
f2c::types::EmptyDestructor
Definition: Geometry.h:23
f2c::types::Geometry::Geometry
Geometry()
Definition: Geometry_impl.hpp:21
f2c::types::Geometry::mod
static double mod(double a, double b)
Definition: Geometry_impl.hpp:358
f2c::types::Geometry::getDimMinY
double getDimMinY() const
Get the minimum y value of the geometry.
Definition: Geometry_impl.hpp:102
f2c::types::Geometry::disjoint
bool disjoint(const Geometry< T2, R2 > &geom) const
Check if this and another geometry are disjoint.
Definition: Geometry_impl.hpp:145


fields2cover
Author(s):
autogenerated on Fri Apr 25 2025 02:18:31