-
Notifications
You must be signed in to change notification settings - Fork 105
/
gdal_template.cpp
84 lines (68 loc) · 2.44 KB
/
gdal_template.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Copyright 2019 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gdal_template.h"
#include <cpl_conv.h>
#include <gdal.h>
#include <ogr_api.h>
#include <ogr_srs_api.h>
#include <QString>
namespace OpenOrienteering {
// ## GdalTemplate::RasterGeoreferencing
// static
GdalTemplate::RasterGeoreferencing GdalTemplate::RasterGeoreferencing::fromGDALDataset(GDALDatasetH dataset)
{
RasterGeoreferencing raster_georef;
if (dataset != nullptr)
{
auto driver = GDALGetDatasetDriver(dataset);
raster_georef.driver = GDALGetDriverShortName(driver);
raster_georef.spec = GDALGetProjectionRef(dataset);
auto const result = GDALGetGeoTransform(dataset, raster_georef.geo_transform.data());
raster_georef.valid = result == CE_None;
}
return raster_georef;
}
GdalTemplate::RasterGeoreferencing::operator QTransform() const
{
return { geo_transform[1], geo_transform[2], geo_transform[4], geo_transform[5], geo_transform[0], geo_transform[3] };
}
// static
QByteArray GdalTemplate::RasterGeoreferencing::toProjSpec(const QByteArray& gdal_spec)
{
auto const spatial_reference = OSRNewSpatialReference(gdal_spec);
char* proj_spec_cstring;
auto const ogr_error = OSRExportToProj4(spatial_reference, &proj_spec_cstring);
auto const result = QByteArray(ogr_error == OGRERR_NONE ? proj_spec_cstring : nullptr);
CPLFree(proj_spec_cstring);
OSRDestroySpatialReference(spatial_reference);
return result;
}
// ## GdalTemplate
// static
GdalTemplate::RasterGeoreferencing GdalTemplate::tryReadProjection(const QString& filepath)
{
if (auto dataset = GDALOpen(filepath.toUtf8(), GA_ReadOnly))
{
auto raster_georef = RasterGeoreferencing::fromGDALDataset(dataset);
GDALClose(dataset);
return raster_georef;
}
return {};
}
} // namespace OpenOrienteering