Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
GISFile.hpp
Go to the documentation of this file.
1#pragma once
2#include <filesystem>
3#include <array>
4#include <fishnet/Option.hpp>
5#include <utility>
8
9namespace fishnet{
10
14
15[[maybe_unused]] static std::array<GISFileType,4> SUPPORTED_VECTOR_FORMATS{GISFileType::SHAPEFILE,GISFileType::GEOPACKAGE};
16[[maybe_unused]] static std::array<GISFileType,4> SUPPORTED_RASTER_FORMATS{GISFileType::GEOTIFF};
17
18static fishnet::Option<GISFileType> getGISFileType(const std::filesystem::path & path) {
19 const auto & ext = path.extension().string();
20 if(ext == ".shp" )
22 else if (ext == ".tif" || ext == ".tiff") {
24 }
25 else if (ext == ".gpkg") {
27 }
28 return std::nullopt;
29}
30
31static const char * getExtension(GISFileType type) {
32 switch(type) {
34 return ".shp";
36 return ".tif";
38 return ".gpkg";
39 default:
40 throw std::invalid_argument("Unsupported GISFileType");
41 }
42}
43
48protected:
49 std::filesystem::path pathToFile;
50
51 void supportsOrThrow(const std::filesystem::path & path) const {
52 if(not supports(path)){
53 throw std::invalid_argument("File type not supported: "+ path.string());
54 }
55 }
56
57private:
58 explicit AbstractGISFile(std::filesystem::path pathToFile):pathToFile(std::move(pathToFile)){}
59
60 explicit AbstractGISFile(const std::string& pathName):pathToFile(pathName){}
61
62 friend class AbstractVectorFile;
63 friend class AbstractRasterFile;
64public:
65 [[nodiscard]] const std::filesystem::path & getPath() const noexcept {
66 return pathToFile;
67 }
68
69 operator bool ()const noexcept {
70 return this->exists();
71 }
72
79 [[nodiscard]] bool exists() const noexcept {
80 return std::filesystem::exists(this->pathToFile);
81 }
82
90 [[nodiscard]] bool supports(const std::filesystem::path & path) const noexcept{
91 auto type = getGISFileType(path);
92 return type && type.value() == this->type();
93 }
94
100 void changeFilename(const std::string & filename) noexcept {
101 this->pathToFile = util::PathHelper::changeFilename(this->pathToFile, filename);
102 }
103
109 void appendToFilename(const std::string & suffix) noexcept {
110 changeFilename(this->pathToFile.stem().string() + suffix);
111 }
112
118 constexpr virtual GISFileType type() const noexcept = 0;
119
126 virtual bool remove() const noexcept = 0;
127
128 virtual ~AbstractGISFile() = default;
129};
130
132public:
133 AbstractVectorFile(const std::filesystem::path & pathToFile): AbstractGISFile(pathToFile){}
134 AbstractVectorFile(const std::string& pathName): AbstractGISFile(pathName){}
135 virtual ~AbstractVectorFile() = default;
136
137 constexpr GISFileType type() const noexcept override {
138 return GISFileType::VECTOR;
139 }
140
141 bool remove() const noexcept override {
142 return std::filesystem::remove(this->pathToFile);
143 }
144
145 AbstractVectorFile & move(const std::filesystem::path & newPath) {
146 std::filesystem::rename(this->pathToFile, newPath);
147 this->pathToFile = newPath;
148 return *this;
149 }
150
151 AbstractVectorFile copy(const std::filesystem::path & newPath) const {
152 std::filesystem::copy(this->pathToFile, newPath);
153 return AbstractVectorFile(newPath);
154 }
155
156 std::string toString() const noexcept {
157 return "VectorFile(" + this->pathToFile.string() + ")";
158 }
159};
160
161
163public:
164 explicit AbstractRasterFile(const std::filesystem::path & pathToFile): AbstractGISFile(pathToFile){}
165 explicit AbstractRasterFile(const std::string& pathName): AbstractGISFile(pathName){}
166 virtual ~AbstractRasterFile() = default;
167};
168
174template<typename File>
175concept GISFile = std::derived_from<File,AbstractGISFile> && fishnet::util::HasToString<File> && requires (const File & constF, File & file, std::filesystem::path p){
176 {file.move(p)}-> std::same_as<File &>;
177 {constF.copy(p)} -> std::same_as<File>;
178 {File(p)};
179};
180
186template<typename File>
187concept VectorGISFile = GISFile<File> && std::derived_from<File,AbstractVectorFile>;
188
189static_assert(VectorGISFile<AbstractVectorFile>, "AbstractVectorFile should be a VectorGISFile");
190
196template<typename File>
197concept RasterGISFile = GISFile<File> && std::derived_from<File,AbstractRasterFile>;
198}
Definition GISFile.hpp:47
virtual constexpr GISFileType type() const noexcept=0
Returns the type of GISFile.
bool supports(const std::filesystem::path &path) const noexcept
Checks if the supplied path is supported by the concrete GISFile implementation.
Definition GISFile.hpp:90
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
void changeFilename(const std::string &filename) noexcept
Modifies the path of the GISFile by changing the filename (extension is preserved).
Definition GISFile.hpp:100
std::filesystem::path pathToFile
Definition GISFile.hpp:49
void supportsOrThrow(const std::filesystem::path &path) const
Definition GISFile.hpp:51
void appendToFilename(const std::string &suffix) noexcept
Appends a suffix to the filename / stem of the GISFile (extension is preserved).
Definition GISFile.hpp:109
virtual bool remove() const noexcept=0
Removes the file from the filesystem.
friend class AbstractVectorFile
Definition GISFile.hpp:62
Definition GISFile.hpp:162
AbstractRasterFile(const std::filesystem::path &pathToFile)
Definition GISFile.hpp:164
virtual ~AbstractRasterFile()=default
AbstractRasterFile(const std::string &pathName)
Definition GISFile.hpp:165
Definition GISFile.hpp:131
AbstractVectorFile(const std::string &pathName)
Definition GISFile.hpp:134
AbstractVectorFile copy(const std::filesystem::path &newPath) const
Definition GISFile.hpp:151
std::string toString() const noexcept
Definition GISFile.hpp:156
AbstractVectorFile(const std::filesystem::path &pathToFile)
Definition GISFile.hpp:133
constexpr GISFileType type() const noexcept override
Returns the type of GISFile.
Definition GISFile.hpp:137
bool remove() const noexcept override
Removes the file from the filesystem.
Definition GISFile.hpp:141
virtual ~AbstractVectorFile()=default
AbstractVectorFile & move(const std::filesystem::path &newPath)
Definition GISFile.hpp:145
Definition Option.hpp:13
static std::filesystem::path changeFilename(const std::filesystem::path &path, std::string_view filename) noexcept
Definition PathHelper.cpp:24
Interface for GISFile types. Implementation have to inherit from AbstractGISFile and have to printabl...
Definition GISFile.hpp:175
Interface for all Raster GISFile types.
Definition GISFile.hpp:197
Interface for all Vector GISFile types.
Definition GISFile.hpp:187
Helper concept which checks whether a class has a toString method.
Definition ObjectConcepts.hpp:12
Definition BFSClustering.hpp:5
static std::array< GISFileType, 4 > SUPPORTED_VECTOR_FORMATS
Definition GISFile.hpp:15
static const char * getExtension(GISFileType type)
Definition GISFile.hpp:31
GISFileType
Definition GISFile.hpp:11
static std::array< GISFileType, 4 > SUPPORTED_RASTER_FORMATS
Definition GISFile.hpp:16
static fishnet::Option< GISFileType > getGISFileType(const std::filesystem::path &path)
Definition GISFile.hpp:18
STL namespace.