5#include "ogrsf_frmts.h"
6#include <gdal/ogr_core.h>
7#include <gdal/gdal_priv.h>
19template<geometry::OGRWritableGeometry G>
28 static void addOGRField(
VectorLayer<G> & layer,OGRFieldDefn * fieldDef,
int id){
29 std::string fieldName = fieldDef->GetNameRef();
30 switch (fieldDef->GetType()){
53 struct AddAttributeVisitor{
55 OGRFeature * ogrFeature;
59 if constexpr(std::same_as<T,int>)
62 else if constexpr(std::integral<T>)
65 else if constexpr(std::floating_point<T>)
68 else if constexpr(std::convertible_to<T,std::string>)
92 if(ogrLayer ==
nullptr)
93 return std::unexpected(
"Could not read from OGRLayer, pointer is null");
95 OGRFeatureDefn * layerDef = ogrLayer->GetLayerDefn();
96 for(
int i = 0; i < layerDef->GetFieldCount();i++) {
97 addOGRField(layer, layerDef->GetFieldDefn(i),i);
99 auto addFeatureIfPresent = [&layer](std::optional<G> && geometry, OGRFeature * ogrFeaturePtr) ->
void{
103 for(
const auto & [_,fieldDefinition]: layer.
getFieldsMap()){
104 std::visit(AddAttributeVisitor(&f,ogrFeaturePtr),fieldDefinition);
108 for(
const auto & ogrFeature: ogrLayer){
109 if (ogrFeature->GetGeometryRef() ==
nullptr)
116 addFeatureIfPresent(std::move(geometry).
template narrowTo<G>(), ogrFeature.get());
118 if (
auto * spatialRef = ogrLayer->GetSpatialRef(); spatialRef !=
nullptr)
130 for(
const auto & [fieldName,fieldDefinition] : layer.
getFieldsMap()) {
131 OGRFieldType fieldType;
133 std::visit([&fieldType](
auto && fieldVariant){
134 using T =
typename std::decay_t<
decltype(fieldVariant)>::value_type;
137 auto fieldDefn = OGRFieldDefn(fieldName.c_str(),fieldType);
138 fieldDefn.SetPrecision(20);
139 std::ignore = outputLayer->CreateField(&fieldDefn);
142 auto * feature =
new OGRFeature(outputLayer->GetLayerDefn());
147 feature->SetGeometry(f.getGeometry().raw());
148 }
else if constexpr (G::type == fishnet::geometry::GeometryType::POINT) {
149 OGRPoint point {f.getGeometry().getX(), f.getGeometry().getY()};
150 feature->SetGeometry(&point);
151 }
else if constexpr (std::same_as<G, fishnet::geometry::OGRRingAdapter>) {
153 feature->SetGeometry(f.getGeometry().raw()->getExteriorRing());
155 feature->SetGeometry(f.getGeometry().raw());
160 feature->SetGeometry(adapted.raw()->getExteriorRing());
163 feature->SetGeometry(adapted.raw());
166 feature->SetGeometry(adapted.raw());
168 static_assert(
false,
"toOGR has no case for this geometry: a type was added to "
169 "OGRWritableGeometry without saying how it is turned into an OGR geometry");
172 for(
const auto & [fieldName,fieldDefinition]: layer.
getFieldsMap()){
174 std::visit([&fieldName,&f,feature](
auto && var){
175 auto optionalAttribute = f.getAttribute(var);
176 if(optionalAttribute)
181 OGRErr success = outputLayer->CreateFeature(feature);
183 return std::unexpected(
"Could not write Geometry: "+f.getGeometry().toString());
Feature implementation storing a geometry and associated attributes.
Definition Feature.hpp:51
constexpr bool addAttribute(const FieldDef &fieldDefinition, math::convertible_without_loss< typename FieldDef::value_type > auto value) noexcept
Definition Feature.hpp:78
Field definition implementation. Defines name, id and data type of a field.
Definition FieldDefinition.hpp:43
constexpr int getFieldID() const noexcept
Definition FieldDefinition.hpp:63
static void setFieldValue(OGRFeature *feature, const std::string &fieldName, const T &value) noexcept
Definition OGRFieldAdapter.hpp:25
static OGRFieldType fromTypeIndex(const std::type_index &type) noexcept
Definition OGRFieldAdapter.hpp:14
Reads and writes a VectorLayer whose geometries are backed by the OGR data source.
Definition OGRLayerAdapter.hpp:20
static OGRwkbGeometryType layerGeometryType()
WKB type an output layer holding G has to be created with.
Definition OGRLayerAdapter.hpp:76
static Either< VectorLayer< G >, std::string > fromOGR(OGRLayer *ogrLayer)
Converts an OGRLayer to a fishnet::VectorLayer.
Definition OGRLayerAdapter.hpp:90
static Either< OGRLayer *, std::string > toOGR(const VectorLayer< G > &layer, OGRLayer *outputLayer)
Converts a fishnet::VectorLayer to an OGRLayer.
Definition OGRLayerAdapter.hpp:129
Stores the geometries, wrapped in features (which hold the field values / attributes) Keeps track of ...
Definition VectorLayer.hpp:28
constexpr void setSpatialReference(const OGRSpatialReference &spatialReference) noexcept
Definition VectorLayer.hpp:95
constexpr fishnet::Either< FieldDefinition< std::string >, error_type > addTextField(const std::string &fieldName, const std::optional< int > &fieldID=std::nullopt) noexcept
Definition VectorLayer.hpp:180
constexpr fishnet::Either< FieldDefinition< size_t >, error_type > addSizeField(const std::string &fieldName, const std::optional< int > &fieldID=std::nullopt) noexcept
Definition VectorLayer.hpp:184
constexpr fishnet::Either< FieldDefinition< double >, error_type > addDoubleField(const std::string &fieldName, const std::optional< int > &fieldID=std::nullopt) noexcept
Definition VectorLayer.hpp:176
constexpr void addFeature(Feature< G > &&feature) noexcept
Definition VectorLayer.hpp:129
constexpr const std::unordered_map< std::string, FieldDefinitionVariant > & getFieldsMap() const noexcept
Definition VectorLayer.hpp:239
constexpr fishnet::Either< FieldDefinition< int >, error_type > addIntegerField(const std::string &fieldName, const std::optional< int > &fieldID=std::nullopt) noexcept
Definition VectorLayer.hpp:172
constexpr util::view_of< Feature< G > > auto getFeatures() const noexcept
Definition VectorLayer.hpp:83
static OGRwkbGeometryType toWKB(GeometryType type)
Definition OGRAdapterBase.hpp:137
Adapter for an OGRGeometry of statically unknown type, as handed out by a data source.
Definition OGRGeometryAdapter.hpp:83
IMultiPolygon implementation backed by an OGRMultiPolygon.
Definition OGRMultiPolygonAdapter.hpp:19
IPolygon implementation backed by an OGRPolygon.
Definition OGRPolygonAdapter.hpp:22
IRing implementation backed by an OGRPolygon holding nothing but an exterior ring.
Definition OGRRingAdapter.hpp:30
A geometry which carries its type at runtime instead of statically.
Definition IGeometry.hpp:41
Interface for a multi-polygon A multi-polygon is a collection of polygons.
Definition IMultiPolygon.hpp:93
Interface for a polygon A polygon is a two-dimensional shape defined by a boundary and zero or more h...
Definition IPolygon.hpp:98
Interface for a ring A ring is a closed loop of segments, forming a boundary of a shape.
Definition IRing.hpp:143
A geometry an OGR layer can be read into and written from.
Definition OGRGeometryAdapter.hpp:26
std::unique_ptr< T, __impl::OGRGeometryDeleter > OGRUniquePtr
Definition OGRAdapterBase.hpp:48
Definition BFSClustering.hpp:5