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)
12{
13 init();
14 char const* opts[] = {"MSSQLSPATIAL_USE_BCP=NO", nullptr};
15 auto ret = dataset_ptr{GDALOpenEx(file, 0, 0, opts, 0)};
16 boat::check(!!ret, error_or(concat("GDALOpenEx ", file)));
17 return ret;
18}
19
20inline dataset_ptr create(char const* file, char const* driver)
21{
22 init();
23 char const* opts[] = {"SPATIALITE=YES", nullptr};
24 auto drv = GDALGetDriverByName(driver);
25 boat::check(!!drv, error_or(concat("GDALGetDriverByName ", driver)));
26 auto ret = dataset_ptr{GDALCreate(drv, file, 0, 0, 0, GDT_Unknown, opts)};
27 boat::check(!!ret, error_or(concat("GDALCreate ", file)));
28 return ret;
29}
30
32inline dataset_ptr create( //
33 char const* file,
34 char const* driver,
35 db::raster const& rast)
36{
37 init();
38 auto drv = GDALGetDriverByName(driver);
39 boat::check(!!drv, error_or(concat("GDALGetDriverByName ", driver)));
40 auto ret = dataset_ptr{GDALCreate( //
41 drv,
42 file,
43 rast.width,
44 rast.height,
45 static_cast<int>(rast.bands.size()),
46 GDALGetDataTypeByName(rast.bands.at(0).type_name.data()),
47 0)};
48 boat::check(!!ret, error_or(concat("GDALCreate ", file)));
49 auto a = std::array{
50 rast.xorig,
51 rast.xscale,
52 rast.xskew,
53 rast.yorig,
54 rast.yskew,
55 rast.yscale,
56 };
57 check(GDALSetGeoTransform(ret.get(), a.data()));
58 auto crs = make_srs(rast.epsg, rast.wkt, rast.proj4);
59 check(GDALSetSpatialRef(ret.get(), crs.get()));
60 for (int i{}, n = static_cast<int>(rast.bands.size()); i < n; ++i) {
61 auto b = GDALGetRasterBand(ret.get(), i + 1);
62 auto ci =
63 GDALGetColorInterpretationByName(rast.bands[i].color_name.data());
64 boat::check(ci != GCI_Undefined, rast.bands[i].color_name);
65 check(GDALSetRasterColorInterpretation(b, ci));
66 }
67 return ret;
68}
69
70} // namespace boat::gdal
71
72#endif // BOAT_GDAL_DATASET_HPP
Definition catalog.hpp:12
dataset_ptr open(char const *file)
Definition dataset.hpp:11
dataset_ptr create(char const *file, char const *driver)
Definition dataset.hpp:20
Definition meta.hpp:98
std::string proj4
Definition meta.hpp:114
double xorig
Definition meta.hpp:105
double yskew
Definition meta.hpp:110
int height
Definition meta.hpp:104
std::vector< band > bands
Definition meta.hpp:102
double xscale
Definition meta.hpp:107
double yorig
Definition meta.hpp:106
int width
Definition meta.hpp:103
int epsg
Definition meta.hpp:112
std::string wkt
Definition meta.hpp:113
double yscale
Definition meta.hpp:108
double xskew
Definition meta.hpp:109
Definition drivers.hpp:15