Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
ShapefileIO.hpp
Go to the documentation of this file.
1#pragma once
9
10#include <gdal/ogr_spatialref.h>
11#include <gdal/gdal.h>
12#include <gdal/gdal_priv.h>
13#include <gdal/ogr_core.h>
14
15namespace fishnet {
16
17template<geometry::OGRLayerGeometry G>
19private:
20 constexpr static std::array<const char *, 1> DEFAULT_OPEN_OPTIONS = { "ADJUST_TYPE=YES"};
21 std::vector<std::string> gdalOpenOptions;
22public:
23 using geometry_type = G;
25
27 for(const auto & opt : DEFAULT_OPEN_OPTIONS) {
28 this->gdalOpenOptions.push_back(opt);
29 }
30 }
31
33 for(auto && opt : openOptions) {
34 this->gdalOpenOptions.push_back(std::move(opt));
35 }
36 }
37
38 Either<VectorLayer<G>,std::string> operator()(const Shapefile & shapefile) const {
40 if(not shapefile.exists())
41 return std::unexpected("Shapefile does not exists, could not read from File: \"" + shapefile.getPath().string() + "\"");
42 // Prepare GDAL open options
43 std::vector<const char*> openOptionsVec;
44 for (const auto& opt : gdalOpenOptions) {
45 openOptionsVec.push_back(opt.c_str());
46 }
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);
50 if(ds == nullptr)
51 return std::unexpected("Could not open Shapefile: \"" + shapefile.getPath().string() + "\" with GDAL");
52 auto layer = OGRLayerAdapter<G>::fromOGR(ds->GetLayer(0));
53 GDALClose(ds);
54 return layer;
55 }
56};
57
58template<geometry::OGRWritableGeometry G>
60private:
61 std::vector<std::string> options;
62public:
63 ShapefileWriter() = default;
64
66 for(auto && opt : options) {
67 this->options.push_back(std::move(opt));
68 }
69 }
72 GDALDriver * driver = GetGDALDriverManager()->GetDriverByName("ESRI Shapefile");
73 if (driver == nullptr) {
74 return std::unexpected("Could not find GDAL driver for ESRI Shapefile");
75 }
76 output.remove(); // delete already existing files, if present
77 GDALDataset * outputDataset = driver->Create(output.getPath().c_str(),0,0,0,GDT_Unknown,0);
78 const char * const options[] = {"SPATIAL_INDEX=YES",nullptr};
79 OGRLayer * outputLayer = outputDataset->CreateLayer(output.getPath().c_str(),layer.getSpatialReference().Clone(),OGRLayerAdapter<G>::layerGeometryType(),const_cast<char **>(options));
80 auto result = OGRLayerAdapter<G>::toOGR(layer,outputLayer);
81 outputLayer->SyncToDisk();
82 GDALClose(outputDataset);
83 return result.transform([&output](const auto & _) {
84 return output;
85 });
86 }
87};
88
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");
91} // namespace fishnet
const std::filesystem::path & getPath() const noexcept
Definition GISFile.hpp:65
Definition Either.hpp:11
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
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