forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoheif.cpp
223 lines (205 loc) · 5.98 KB
/
geoheif.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**********************************************************************
*
* Name: geoheif.cpp
* Project: GDAL
* Purpose: OGC GeoHEIF shared implementation.
* Author: Brad Hards <[email protected]>
*
**********************************************************************
* Copyright (c) 2024, Brad Hards
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "geoheif.h"
namespace gdal
{
//! @cond Doxygen_Suppress
GeoHEIF::GeoHEIF() : gcps(0)
{
}
GeoHEIF::~GeoHEIF()
{
}
bool GeoHEIF::has_SRS() const
{
return !m_oSRS.IsEmpty();
}
bool GeoHEIF::has_GCPs() const
{
return haveGCPs;
}
static double to_double(const uint8_t *data, uint32_t index)
{
double d = 0;
memcpy(&d, data + index, sizeof(d));
CPL_MSBPTR64(&d);
return d;
}
static double int_as_double(const uint8_t *data, uint32_t index)
{
uint32_t v = 0;
memcpy(&v, data + index, sizeof(uint32_t));
CPL_MSBPTR32(&v);
return static_cast<double>(v);
}
void GeoHEIF::setModelTransformation(const uint8_t *payload, size_t length)
{
// TODO: this only handles the 2D case.
if (length != (6 * 8 + 4))
{
return;
}
// Match version
if (payload[0] == 0x00)
{
uint32_t index = 0;
if (payload[index + 3] == 0x01)
{
index += 4;
modelTransform[1] = to_double(payload, index);
index += 8;
modelTransform[2] = to_double(payload, index);
index += 8;
modelTransform[0] = to_double(payload, index);
index += 8;
modelTransform[4] = to_double(payload, index);
index += 8;
modelTransform[5] = to_double(payload, index);
index += 8;
modelTransform[3] = to_double(payload, index);
}
}
else
{
CPLDebug("GeoHEIF", "Unsupported mtxf version %d", payload[0]);
}
}
CPLErr GeoHEIF::GetGeoTransform(double *padfTransform) const
{
padfTransform[1] = modelTransform[1];
padfTransform[2] = modelTransform[2];
padfTransform[0] = modelTransform[0];
padfTransform[4] = modelTransform[4];
padfTransform[5] = modelTransform[5];
padfTransform[3] = modelTransform[3];
return CE_None;
}
/************************************************************************/
/* GetSpatialRef() */
/************************************************************************/
const OGRSpatialReference *GeoHEIF::GetSpatialRef() const
{
return !m_oSRS.IsEmpty() ? &m_oSRS : nullptr;
}
void GeoHEIF::extractSRS(const uint8_t *payload, size_t length) const
{
// TODO: more sophisticated length checks
if (length < 12)
{
CPLDebug("GeoHEIF", "Infeasible length CRS payload %u",
static_cast<unsigned>(length));
return;
}
std::string crsEncoding(payload + 4, payload + 8);
std::string crs(payload + 8, payload + length);
if (crsEncoding == "wkt2")
{
m_oSRS.importFromWkt(crs.c_str());
}
else if (crsEncoding == "crsu")
{
m_oSRS.importFromCRSURL(crs.c_str());
}
else if (crsEncoding == "curi")
{
if ((crs.at(0) != '[') || (crs.at(crs.length() - 1) != ']'))
{
CPLDebug("GeoHEIF", "CRS CURIE is not a safe CURIE");
return;
}
std::string curie = crs.substr(1, crs.length() - 2);
size_t separatorPos = curie.find(':');
if (separatorPos == std::string::npos)
{
CPLDebug("GeoHEIF",
"CRS CURIE does not contain required separator");
return;
}
std::string authority = curie.substr(0, separatorPos);
std::string code = curie.substr(separatorPos + 1);
std::string osURL("http://www.opengis.net/def/crs/");
osURL.append(authority);
osURL += "/0/";
osURL.append(code);
m_oSRS.importFromCRSURL(osURL.c_str());
}
else
{
CPLDebug("GeoHEIF", "CRS encoding is not supported");
return;
}
}
void GeoHEIF::addGCPs(const uint8_t *data, size_t length)
{
constexpr size_t MIN_VALID_SIZE = sizeof(uint32_t) + sizeof(uint16_t) +
2 * sizeof(uint32_t) + 2 * sizeof(double);
if (length < MIN_VALID_SIZE)
{
CPLDebug("GeoHEIF", "GCP data length is too short");
return;
}
if (data[0] == 0x00)
{
uint32_t index = 0;
bool is_3D = (data[index + 3] == 0x00);
if (is_3D)
{
if (length < MIN_VALID_SIZE + sizeof(double))
{
CPLDebug("GeoHEIF", "GCP data length is too short for 3D");
return;
}
}
index += sizeof(uint32_t);
uint16_t count = (data[index] << 8) + (data[index + 1]);
index += sizeof(uint16_t);
for (uint16_t j = 0; (j < count) && (index < length); j++)
{
double dfGCPPixel = int_as_double(data, index);
index += sizeof(uint32_t);
double dfGCPLine = int_as_double(data, index);
index += sizeof(uint32_t);
double dfGCPX = to_double(data, index);
index += sizeof(double);
double dfGCPY = to_double(data, index);
index += sizeof(double);
double dfGCPZ = 0.0;
if (is_3D)
{
dfGCPZ = to_double(data, index);
index += sizeof(double);
}
gcps.emplace_back("", "", dfGCPPixel, dfGCPLine, dfGCPX, dfGCPY,
dfGCPZ);
haveGCPs = true;
}
}
else
{
CPLDebug("GeoHEIF", "Unsupported tiep version %d", data[0]);
}
}
int GeoHEIF::GetGCPCount() const
{
return static_cast<int>(gcps.size());
}
const GDAL_GCP *GeoHEIF::GetGCPs()
{
return gdal::GCP::c_ptr(gcps);
}
const OGRSpatialReference *GeoHEIF::GetGCPSpatialRef() const
{
return this->GetSpatialRef();
}
} // namespace gdal
//! @endcond