boat
C++23 geospatial library
Loading...
Searching...
No Matches
dataset.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_GDAL_DATASET_HPP
4#define BOAT_GDAL_DATASET_HPP
5
6#include <boat/db/meta.hpp>
7#include <boat/gdal/detail/utility.hpp>
8
9namespace boat::gdal {
10
11inline dataset_ptr open(char const* file, bool update = false)
12{
13 init();
14 char const* opts[] = {"MSSQLSPATIAL_USE_BCP=NO", nullptr};
15 auto flags = update ? GDAL_OF_UPDATE : GDAL_OF_READONLY;
16 auto ret = dataset_ptr{GDALOpenEx(file, flags, 0, opts, 0)};
17 boat::check(!!ret, error_or(concat("GDALOpenEx ", file)));
18 return ret;
19}
20
21inline dataset_ptr create(char const* file, char const* driver)
22{
23 init();
24 char const* opts[] = {"SPATIALITE=YES", nullptr};
25 auto drv = GDALGetDriverByName(driver);
26 boat::check(!!drv, error_or(concat("GDALGetDriverByName ", driver)));
27 auto ret = dataset_ptr{GDALCreate(drv, file, 0, 0, 0, GDT_Unknown, opts)};
28 boat::check(!!ret, error_or(concat("GDALCreate ", file)));
29 return ret;
30}
31
33inline dataset_ptr
34create(char const* file, char const* driver, db::raster const& rast)
35{
36 init();
37 auto drv = GDALGetDriverByName(driver);
38 boat::check(!!drv, error_or(concat("GDALGetDriverByName ", driver)));
39 auto ret = dataset_ptr{GDALCreate(
40 drv,
41 file,
42 rast.extent.x(),
43 rast.extent.y(),
44 static_cast<int>(rast.bands.size()),
45 GDALGetDataTypeByName(rast.bands.at(0).type_name.data()),
46 0)};
47 boat::check(!!ret, error_or(concat("GDALCreate ", file)));
48 auto a = std::array{
49 rast.xorig,
50 rast.xscale,
51 rast.xskew,
52 rast.yorig,
53 rast.yskew,
54 rast.yscale};
55 check(GDALSetGeoTransform(ret.get(), a.data()));
56 auto crs = make_srs(rast.epsg, rast.wkt, rast.proj4);
57 check(GDALSetSpatialRef(ret.get(), crs.get()));
58 for (auto [i, band] : std::views::enumerate(rast.bands)) {
59 auto b = GDALGetRasterBand(ret.get(), int(i + 1));
60 auto ci = GDALGetColorInterpretationByName(band.color_name.data());
61 boat::check(ci != GCI_Undefined, band.color_name);
62 check(GDALSetRasterColorInterpretation(b, ci));
63 }
64 return ret;
65}
66
67} // namespace boat::gdal
68
69#endif // BOAT_GDAL_DATASET_HPP
Definition catalog.hpp:12
dataset_ptr create(char const *file, char const *driver)
Definition dataset.hpp:21
dataset_ptr open(char const *file, bool update=false)
Definition dataset.hpp:11
Definition meta.hpp:93
std::string proj4
Definition meta.hpp:108
double xorig
Definition meta.hpp:99
double yskew
Definition meta.hpp:104
geometry::raster::point extent
Definition meta.hpp:98
std::vector< band > bands
Definition meta.hpp:97
double xscale
Definition meta.hpp:101
double yorig
Definition meta.hpp:100
int epsg
Definition meta.hpp:106
std::string wkt
Definition meta.hpp:107
double yscale
Definition meta.hpp:102
double xskew
Definition meta.hpp:103
Definition drivers.hpp:15