Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
OGRAdapterBase.hpp
Go to the documentation of this file.
1#pragma once
2#include <gdal/ogr_geometry.h>
3#include <memory>
4#include <cassert>
6
7namespace fishnet::geometry::__impl{
8
12enum class OGROwnership {
13 geometry,
14 shellOnly,
15 none
16};
17
27struct OGRGeometryDeleter {
28 OGROwnership ownership = OGROwnership::geometry;
29
30 void operator()(OGRGeometry* geom) const {
31 if(not geom || ownership == OGROwnership::none)
32 return;
33 if(ownership == OGROwnership::shellOnly){
34 // hand the borrowed rings back before the shell around them is destroyed, otherwise
35 // the shell would take the rings of their actual owner with it
36 auto * shell = geom->toPolygon();
37 for(int i = shell->getNumInteriorRings(); i >= 0; --i)
38 shell->removeRing(i, false);
39 }
40 OGRGeometryFactory::destroyGeometry(geom);
41 }
42};
43} // namespace fishnet::geometry::__impl
44
45namespace fishnet::geometry{
46
47template<typename T>
48using OGRUniquePtr = std::unique_ptr<T, __impl::OGRGeometryDeleter>;
49
50namespace __impl{
51
57template<typename T>
58static OGRUniquePtr<T> borrow(T * geometry) noexcept {
59 return OGRUniquePtr<T>(geometry, OGRGeometryDeleter{OGROwnership::none});
60}
61
70static OGRUniquePtr<OGRPolygon> borrowRingAsPolygon(OGRLinearRing * ring) noexcept {
71 auto * shell = static_cast<OGRPolygon*>(OGRGeometryFactory::createGeometry(wkbPolygon));
72 shell->addRingDirectly(ring);
73 return OGRUniquePtr<OGRPolygon>(shell, OGRGeometryDeleter{OGROwnership::shellOnly});
74}
75} // namespace __impl
76
82template<typename T> requires std::is_base_of_v<OGRGeometry, T>
84protected:
86public:
87 using numeric_type = double;
88
90 assert(this->geomPtr != nullptr);
91 }
92
93 OGRAdapterBase(OGRAdapterBase && other) noexcept : geomPtr(std::move(other.geomPtr)) {}
94
95 OGRAdapterBase(const OGRAdapterBase & other): geomPtr(OGRUniquePtr<T>(static_cast<T*>(other.geomPtr->clone()))) {}
96
98 if(this != &other){
99 geomPtr = OGRUniquePtr<T>(static_cast<T*>(other.geomPtr->clone()));
100 }
101 return *this;
102 }
104 if(this != &other){
105 geomPtr = std::move(other.geomPtr);
106 }
107 return *this;
108 }
109
110 bool operator==(const OGRAdapterBase & other) const noexcept{
111 if(this->geomPtr == other.geomPtr) return true; // same pointer, same object
112 return geomPtr->Equals(other.geomPtr.get());
113 }
114
115 T * raw() const noexcept{
116 return geomPtr.get();
117 }
118
119 size_t hash() const noexcept {
120 size_t nSize = geomPtr->WkbSize();
121 std::vector<unsigned char> pabyData(nSize);
122 geomPtr->exportToWkb(pabyData.data());
123 std::string_view bytes(reinterpret_cast<const char*>(pabyData.data()), nSize);
124 return std::hash<std::string_view>{}(bytes);
125 }
126
127 std::string toString() const{
128 return geomPtr->exportToWkt();
129 }
130};
136public:
137 static OGRwkbGeometryType toWKB(GeometryType type){
138 switch (type)
139 {
140 case geometry::GeometryType::POINT: return wkbPoint;
141 case geometry::GeometryType::POLYGON: return wkbPolygon;
142 case geometry::GeometryType::RING: return wkbLinearRing;
143 case geometry::GeometryType::MULTIPOLYGON: return wkbMultiPolygon;
144 default:
145 throw std::invalid_argument("Geometry type: "+std::to_string(type)+" could not be converted into wkb format");
146 }
147 }
148 static geometry::GeometryType fromWKB(OGRwkbGeometryType type){
149 switch(type){
150 case wkbPoint: return geometry::GeometryType::POINT;
151 // OGR has no WKB type of its own for a linear ring and reports it as a line string
152 case wkbLinearRing:
153 case wkbLineString: return geometry::GeometryType::RING;
154 case wkbPolygon: return geometry::GeometryType::POLYGON;
155 case wkbMultiPolygon: return geometry::GeometryType::MULTIPOLYGON;
156 default: throw std::invalid_argument("wkbGeometryType could not be converted into a Fishnet geometry type");
157 }
158 }
159};
160} // namespace fishnet::geometry
Convert fishnet::geometry::GeometryType <-> OGRwkbGeometryType.
Definition OGRAdapterBase.hpp:135
static OGRwkbGeometryType toWKB(GeometryType type)
Definition OGRAdapterBase.hpp:137
static geometry::GeometryType fromWKB(OGRwkbGeometryType type)
Definition OGRAdapterBase.hpp:148
Base class for OGR geometry adapters, which owns an OGR geometry.
Definition OGRAdapterBase.hpp:83
OGRAdapterBase(OGRAdapterBase &&other) noexcept
Definition OGRAdapterBase.hpp:93
std::string toString() const
Definition OGRAdapterBase.hpp:127
OGRAdapterBase(const OGRAdapterBase &other)
Definition OGRAdapterBase.hpp:95
T * raw() const noexcept
Definition OGRAdapterBase.hpp:115
size_t hash() const noexcept
Definition OGRAdapterBase.hpp:119
double numeric_type
Definition OGRAdapterBase.hpp:87
OGRAdapterBase & operator=(const OGRAdapterBase &other)
Definition OGRAdapterBase.hpp:97
bool operator==(const OGRAdapterBase &other) const noexcept
Definition OGRAdapterBase.hpp:110
OGRAdapterBase(OGRUniquePtr< T > &&geomPtr)
Definition OGRAdapterBase.hpp:89
OGRUniquePtr< T > geomPtr
Definition OGRAdapterBase.hpp:85
OGRAdapterBase & operator=(OGRAdapterBase &&other) noexcept
Definition OGRAdapterBase.hpp:103
Definition BoundingBoxWrapper.hpp:5
std::unique_ptr< T, __impl::OGRGeometryDeleter > OGRUniquePtr
Definition OGRAdapterBase.hpp:48
GeometryType
Definition GeometryBase.hpp:6
STL namespace.
static string to_string(fishnet::geometry::GeometryType type) noexcept
Definition GeometryBase.hpp:22