10#include <gdal/ogr_spatialref.h>
12#include <gdal/gdal_priv.h>
13#include <gdal/ogr_core.h>
17template<geometry::OGRLayerGeometry G>
20 constexpr static std::array<const char *, 1> DEFAULT_OPEN_OPTIONS = {
"ADJUST_TYPE=YES"};
21 std::vector<std::string> gdalOpenOptions;
27 for(
const auto & opt : DEFAULT_OPEN_OPTIONS) {
28 this->gdalOpenOptions.push_back(opt);
33 for(
auto && opt : openOptions) {
34 this->gdalOpenOptions.push_back(std::move(opt));
41 return std::unexpected(
"Shapefile does not exists, could not read from File: \"" + shapefile.
getPath().string() +
"\"");
43 std::vector<const char*> openOptionsVec;
44 for (
const auto& opt : gdalOpenOptions) {
45 openOptionsVec.push_back(opt.c_str());
47 openOptionsVec.push_back(
nullptr);
48 const char** openOptions = openOptionsVec.data();
49 auto * ds = (GDALDataset *) GDALOpenEx(shapefile.
getPath().c_str(), GDAL_OF_VECTOR,
nullptr, openOptions,
nullptr);
51 return std::unexpected(
"Could not open Shapefile: \"" + shapefile.
getPath().string() +
"\" with GDAL");
58template<geometry::OGRWritableGeometry G>
61 std::vector<std::string> options;
66 for(
auto && opt : options) {
67 this->options.push_back(std::move(opt));
72 GDALDriver * driver = GetGDALDriverManager()->GetDriverByName(
"ESRI Shapefile");
73 if (driver ==
nullptr) {
74 return std::unexpected(
"Could not find GDAL driver for ESRI Shapefile");
77 GDALDataset * outputDataset = driver->Create(output.
getPath().c_str(),0,0,0,GDT_Unknown,0);
78 const char *
const options[] = {
"SPATIAL_INDEX=YES",
nullptr};
81 outputLayer->SyncToDisk();
82 GDALClose(outputDataset);
83 return result.transform([&output](
const auto & _) {
89static_assert(VectorLayerReader<ShapefileReader<geometry::OGRPolygonAdapter>, Shapefile, geometry::OGRPolygonAdapter>,
"ShapefileReader must satisfy VectorLayerReader concept");
90static_assert(VectorLayerWriter<ShapefileWriter<geometry::OGRPolygonAdapter>, geometry::OGRPolygonAdapter, Shapefile>,
"ShapefileWriter must satisfy VectorLayerWriter concept");
const std::filesystem::path & getPath() const noexcept
Definition GISFile.hpp:65
static void init()
Definition GDALInitializer.hpp:15
Reads and writes a VectorLayer whose geometries are backed by the OGR data source.
Definition OGRLayerAdapter.hpp:20
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
Definition ShapefileIO.hpp:18
ShapefileReader()
Definition ShapefileIO.hpp:26
G geometry_type
Definition ShapefileIO.hpp:23
ShapefileReader(fishnet::util::forward_range_of< std::string > auto &&openOptions)
Definition ShapefileIO.hpp:32
Either< VectorLayer< G >, std::string > operator()(const Shapefile &shapefile) const
Definition ShapefileIO.hpp:38
Definition ShapefileIO.hpp:59
ShapefileWriter()=default
Either< Shapefile, std::string > operator()(const VectorLayer< G > &layer, const Shapefile &output) const
Definition ShapefileIO.hpp:70
ShapefileWriter(fishnet::util::forward_range_of< std::string > auto &&options)
Definition ShapefileIO.hpp:65
Shapefile handle implementation.
Definition Shapefile.hpp:14
bool remove() const noexcept override
Removes the file from the filesystem.
Definition Shapefile.cpp:19
bool exists() const noexcept
Definition Shapefile.hpp:40
Stores the geometries, wrapped in features (which hold the field values / attributes) Keeps track of ...
Definition VectorLayer.hpp:28
constexpr const OGRSpatialReference & getSpatialReference() const noexcept
Definition VectorLayer.hpp:91
Definition CollectionConcepts.hpp:27
Definition BFSClustering.hpp:5