Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
GeoPackageIO.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 std::vector<std::string> gdalOpenOptions;
21public:
22 using geometry_type = G;
24
25 GeoPackageReader() = default;
26
28 for(auto && opt : openOptions) {
29 this->gdalOpenOptions.push_back(std::move(opt));
30 }
31 }
32
33 Either<VectorLayer<G>,std::string> operator()(const GeoPackage & geopackage) const {
35 if(not geopackage.exists())
36 return std::unexpected("GeoPackage does not exist, could not read from File: \"" + geopackage.getPath().string() + "\"");
37 std::vector<const char*> openOptionsVec;
38 for (const auto& opt : gdalOpenOptions) {
39 openOptionsVec.push_back(opt.c_str());
40 }
41 openOptionsVec.push_back(nullptr);
42 const char** openOptions = openOptionsVec.data();
43 auto * ds = (GDALDataset *) GDALOpenEx(geopackage.getPath().c_str(), GDAL_OF_VECTOR, nullptr, openOptions, nullptr);
44 if(ds == nullptr)
45 return std::unexpected("Could not open GeoPackage: \"" + geopackage.getPath().string() + "\" with GDAL");
46 auto layer = OGRLayerAdapter<G>::fromOGR(ds->GetLayer(0));
47 GDALClose(ds);
48 return layer;
49 }
50};
51
52template<geometry::OGRWritableGeometry G>
54private:
55 bool createSpatialIndex = true;
56 std::vector<std::string> options;
57public:
58 GeoPackageWriter() = default;
59
60 GeoPackageWriter(bool createSpatialIndex) : createSpatialIndex(createSpatialIndex) {
61 if(not createSpatialIndex)
62 this->options.push_back("SPATIAL_INDEX=NO");
63 }
64
65 GeoPackageWriter(bool createSpatialIndex, fishnet::util::forward_range_of<std::string> auto && options) : createSpatialIndex(createSpatialIndex) {
66 for(auto && opt : options) {
67 this->options.push_back(std::move(opt));
68 }
69 }
70
73 GDALDriver * driver = GetGDALDriverManager()->GetDriverByName("GPKG");
74 if (driver == nullptr) {
75 return std::unexpected("Could not find GDAL driver for GeoPackage");
76 }
77 output.remove();
78 GDALDataset * outputDataset = driver->Create(output.getPath().c_str(), 0, 0, 0, GDT_Unknown, nullptr);
79 if (outputDataset == nullptr) {
80 return std::unexpected("Could not create GeoPackage dataset: \"" + output.getPath().string() + "\"");
81 }
82 const char * const createOptions[] = {nullptr};
83 OGRLayer * outputLayer = outputDataset->CreateLayer(
84 output.getPath().stem().c_str(),
85 layer.getSpatialReference().Clone(),
87 const_cast<char **>(createOptions)
88 );
89 if (outputLayer == nullptr) {
90 GDALClose(outputDataset);
91 return std::unexpected("Could not create layer in GeoPackage: \"" + output.getPath().string() + "\"");
92 }
93 OGRErr err = outputDataset->StartTransaction();
94 if (err != OGRERR_NONE) {
95 return std::unexpected("Could not start transaction for GeoPackage: \"" + output.getPath().string() + "\"");
96 }
97 auto result = OGRLayerAdapter<G>::toOGR(layer, outputLayer);
98 outputDataset->CommitTransaction();
99 // outputLayer->SyncToDisk();
100 GDALClose(outputDataset);
101 return result.transform([&output](const auto & _) {
102 return output;
103 });
104 }
105};
106
107static_assert(VectorLayerReader<GeoPackageReader<geometry::OGRPolygonAdapter>, GeoPackage, geometry::OGRPolygonAdapter>, "GeoPackageReader must satisfy VectorLayerReader concept");
108static_assert(VectorLayerWriter<GeoPackageWriter<geometry::OGRPolygonAdapter>, geometry::OGRPolygonAdapter, GeoPackage>, "GeoPackageWriter must satisfy VectorLayerWriter concept");
109
110} // namespace fishnet
bool exists() const noexcept
Tests if the GISFile exists on the filesystem.
Definition GISFile.hpp:79
const std::filesystem::path & getPath() const noexcept
Definition GISFile.hpp:65
Definition Either.hpp:11
static void init()
Definition GDALInitializer.hpp:15
Definition GeoPackageIO.hpp:18
G geometry_type
Definition GeoPackageIO.hpp:22
Either< VectorLayer< G >, std::string > operator()(const GeoPackage &geopackage) const
Definition GeoPackageIO.hpp:33
GeoPackageReader(fishnet::util::forward_range_of< std::string > auto &&openOptions)
Definition GeoPackageIO.hpp:27
Definition GeoPackageIO.hpp:53
GeoPackageWriter(bool createSpatialIndex, fishnet::util::forward_range_of< std::string > auto &&options)
Definition GeoPackageIO.hpp:65
GeoPackageWriter(bool createSpatialIndex)
Definition GeoPackageIO.hpp:60
Either< GeoPackage, std::string > operator()(const VectorLayer< G > &layer, const GeoPackage &output) const
Definition GeoPackageIO.hpp:71
GeoPackage handle.
Definition GeoPackage.hpp:9
bool remove() const noexcept override
Removes the file from the filesystem.
Definition GeoPackage.cpp:7
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
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