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 create( //
34 char const* file,
35 char const* driver,
36 db::raster const& rast)
37{
38 init();
39 auto drv = GDALGetDriverByName(driver);
40 boat::check(!!drv, error_or(concat("GDALGetDriverByName ", driver)));
41 auto ret = dataset_ptr{GDALCreate( //
42 drv,
43 file,
44 rast.width,
45 rast.height,
46 static_cast<int>(rast.bands.size()),
47 GDALGetDataTypeByName(rast.bands.at(0).type_name.data()),
48 0)};
49 boat::check(!!ret, error_or(concat("GDALCreate ", file)));
50 auto a = std::array{
51 rast.xorig,
52 rast.xscale,
53 rast.xskew,
54 rast.yorig,
55 rast.yskew,
56 rast.yscale,
57 };
58 check(GDALSetGeoTransform(ret.get(), a.data()));
59 auto crs = make_srs(rast.epsg, rast.wkt, rast.proj4);
60 check(GDALSetSpatialRef(ret.get(), crs.get()));
61 for (int i{}, n = static_cast<int>(rast.bands.size()); i < n; ++i) {
62 auto b = GDALGetRasterBand(ret.get(), i + 1);
63 auto ci =
64 GDALGetColorInterpretationByName(rast.bands[i].color_name.data());
65 boat::check(ci != GCI_Undefined, rast.bands[i].color_name);
66 check(GDALSetRasterColorInterpretation(b, ci));
67 }
68 return ret;
69}
70
71} // namespace boat::gdal
72
73#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: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