1
/******************************************************************************
3
* Purpose: Implementation of access to a PCIDSK GCP2 Segment
5
******************************************************************************
7
* PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
9
* Permission is hereby granted, free of charge, to any person obtaining a
10
* copy of this software and associated documentation files (the "Software"),
11
* to deal in the Software without restriction, including without limitation
12
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
13
* and/or sell copies of the Software, and to permit persons to whom the
14
* Software is furnished to do so, subject to the following conditions:
16
* The above copyright notice and this permission notice shall be included
17
* in all copies or substantial portions of the Software.
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25
* DEALINGS IN THE SOFTWARE.
26
****************************************************************************/
27
#include "segment/cpcidskgcp2segment.h"
29
#include "pcidsk_gcp.h"
30
#include "pcidsk_exception.h"
37
using namespace PCIDSK;
39
struct CPCIDSKGCP2Segment::PCIDSKGCP2SegInfo
41
std::vector<PCIDSK::GCP> gcps;
42
unsigned int num_gcps;
43
PCIDSKBuffer seg_data;
45
std::string map_units; ///< PCI mapunits string
46
std::string proj_parms; ///< Additional projection parameters
47
unsigned int num_proj;
51
CPCIDSKGCP2Segment::CPCIDSKGCP2Segment(PCIDSKFile *file, int segment, const char *segment_pointer)
52
: CPCIDSKSegment(file, segment, segment_pointer), loaded_(false)
54
pimpl_ = new PCIDSKGCP2SegInfo;
56
pimpl_->changed = false;
60
CPCIDSKGCP2Segment::~CPCIDSKGCP2Segment()
66
void CPCIDSKGCP2Segment::Load()
72
// Read the the segment in. The first block has information about
73
// the structure of the GCP segment (how many, the projection, etc.)
74
pimpl_->seg_data.SetSize(data_size - 1024);
75
ReadFromFile(pimpl_->seg_data.buffer, 0, data_size - 1024);
77
// check for 'GCP2 ' in the first 8 bytes
78
if (std::strncmp(pimpl_->seg_data.buffer, "GCP2 ", 8) != 0) {
79
// Assume it's an empty segment, so we can mark loaded_ = true,
80
// write it out and return
81
pimpl_->changed = true;
82
pimpl_->map_units = "LAT/LONG D000";
83
pimpl_->proj_parms = "";
89
// Check the number of blocks field's validity
90
unsigned int num_blocks = pimpl_->seg_data.GetInt(8, 8);
92
if (((data_size - 1024 - 512) / 512) != num_blocks) {
93
//ThrowPCIDSKException("Calculated number of blocks (%d) does not match "
94
// "the value encoded in the GCP2 segment (%d).", ((data_size - 1024 - 512)/512),
96
// Something is messed up with how GDB generates these segments... nice.
99
pimpl_->num_gcps = pimpl_->seg_data.GetInt(16, 8);
101
// Extract the map units string:
102
pimpl_->map_units = std::string(pimpl_->seg_data.buffer + 24, 16);
104
// Extract the projection parameters string
105
pimpl_->proj_parms = std::string(pimpl_->seg_data.buffer + 256, 256);
107
// Get the number of alternative projections (should be 0!)
108
pimpl_->num_proj = pimpl_->seg_data.GetInt(40, 8);
109
if (pimpl_->num_proj != 0) {
110
ThrowPCIDSKException("There are alternative projections contained in this "
111
"GCP2 segment. This functionality is not supported in libpcidsk.");
114
// Load the GCPs into the vector of PCIDSK::GCPs
115
for (unsigned int i = 0; i < pimpl_->num_gcps; i++)
117
unsigned int offset = 512 + i * 256;
118
bool is_cp = pimpl_->seg_data.buffer[offset] == 'C';
119
double pixel = pimpl_->seg_data.GetDouble(offset + 6, 14);
120
double line = pimpl_->seg_data.GetDouble(offset + 20, 14);
122
double elev = pimpl_->seg_data.GetDouble(offset + 34, 12);
123
double x = pimpl_->seg_data.GetDouble(offset + 48, 22);
124
double y = pimpl_->seg_data.GetDouble(offset + 70, 22);
126
PCIDSK::GCP::EElevationDatum elev_datum = pimpl_->seg_data.buffer[offset + 47] != 'M' ?
127
GCP::EEllipsoidal : GCP::EMeanSeaLevel;
129
char elev_unit_c = pimpl_->seg_data.buffer[offset + 46];
130
PCIDSK::GCP::EElevationUnit elev_unit = elev_unit_c == 'M' ? GCP::EMetres :
131
elev_unit_c == 'F' ? GCP::EInternationalFeet :
132
elev_unit_c == 'A' ? GCP::EAmericanFeet : GCP::EUnknown;
134
double pix_err = pimpl_->seg_data.GetDouble(offset + 92, 10);
135
double line_err = pimpl_->seg_data.GetDouble(offset + 102, 10);
136
double elev_err = pimpl_->seg_data.GetDouble(offset + 112, 10);
138
double x_err = pimpl_->seg_data.GetDouble(offset + 122, 14);
139
double y_err = pimpl_->seg_data.GetDouble(offset + 136, 14);
141
std::string gcp_id(pimpl_->seg_data.buffer + offset + 192, 64);
143
PCIDSK::GCP gcp(x, y, elev,
144
line, pixel, gcp_id, pimpl_->map_units,
146
x_err, y_err, elev_err,
148
gcp.SetElevationUnit(elev_unit);
149
gcp.SetElevationDatum(elev_datum);
150
gcp.SetCheckpoint(is_cp);
152
pimpl_->gcps.push_back(gcp);
158
// Return all GCPs in the segment
159
std::vector<PCIDSK::GCP> const& CPCIDSKGCP2Segment::GetGCPs(void) const
164
// Write the given GCPs to the segment. If the segment already
165
// exists, it will be replaced with this one.
166
void CPCIDSKGCP2Segment::SetGCPs(std::vector<PCIDSK::GCP> const& gcps)
168
pimpl_->num_gcps = gcps.size();
169
pimpl_->gcps = gcps; // copy them in
170
pimpl_->changed = true;
172
RebuildSegmentData();
175
// Return the count of GCPs in the segment
176
unsigned int CPCIDSKGCP2Segment::GetGCPCount(void) const
178
return pimpl_->num_gcps;
181
void CPCIDSKGCP2Segment::RebuildSegmentData(void)
183
if (pimpl_->changed == false) {
187
// Rebuild the segment data based on the contents of the struct
188
int num_blocks = (pimpl_->num_gcps + 1) / 2;
190
// This will have to change when we have proper projections support
192
if (pimpl_->gcps.size() > 0)
194
pimpl_->gcps[0].GetMapUnits(pimpl_->map_units,
198
pimpl_->seg_data.SetSize(num_blocks * 512 + 512);
200
// Write out the first few fields
201
pimpl_->seg_data.Put("GCP2 ", 0, 8);
202
pimpl_->seg_data.Put(num_blocks, 8, 8);
203
pimpl_->seg_data.Put((int)pimpl_->gcps.size(), 16, 8);
204
pimpl_->seg_data.Put(pimpl_->map_units.c_str(), 24, 16);
205
pimpl_->seg_data.Put((int)0, 40, 8);
206
pimpl_->seg_data.Put(pimpl_->proj_parms.c_str(), 256, 256);
208
// Time to write GCPs out:
209
std::vector<PCIDSK::GCP>::const_iterator iter =
210
pimpl_->gcps.begin();
213
while (iter != pimpl_->gcps.end()) {
214
std::size_t offset = 512 + id * 256;
216
if ((*iter).IsCheckPoint()) {
217
pimpl_->seg_data.Put("C", offset, 1);
219
pimpl_->seg_data.Put("G", offset, 1);
222
pimpl_->seg_data.Put("0", offset + 1, 5);
224
// Start writing out the GCP values
225
pimpl_->seg_data.Put((*iter).GetPixel(), offset + 6, 14, "%14.4f");
226
pimpl_->seg_data.Put((*iter).GetLine(), offset + 20, 14, "%14.4f");
227
pimpl_->seg_data.Put((*iter).GetZ(), offset + 34, 12, "%12.4f");
229
GCP::EElevationUnit unit;
230
GCP::EElevationDatum datum;
231
(*iter).GetElevationInfo(datum, unit);
241
case GCP::EAmericanFeet:
244
case GCP::EInternationalFeet:
253
case GCP::EEllipsoidal:
256
case GCP::EMeanSeaLevel:
264
// Write out elevation information
265
pimpl_->seg_data.Put(unit_c, offset + 46, 1);
266
pimpl_->seg_data.Put(datum_c, offset + 47, 1);
268
pimpl_->seg_data.Put((*iter).GetX(), offset + 48, 22, "%22.14e");
269
pimpl_->seg_data.Put((*iter).GetY(), offset + 70, 22, "%22.14e");
270
pimpl_->seg_data.Put((*iter).GetPixelErr(), offset + 92, 10, "%10.4f");
271
pimpl_->seg_data.Put((*iter).GetLineErr(), offset + 102, 10, "%10.4f");
272
pimpl_->seg_data.Put((*iter).GetZErr(), offset + 112, 10, "%10.4f");
273
pimpl_->seg_data.Put((*iter).GetXErr(), offset + 122, 14, "%14.4e");
274
pimpl_->seg_data.Put((*iter).GetYErr(), offset + 136, 14, "%14.4e");
275
pimpl_->seg_data.Put((*iter).GetIDString(), offset + 192, 64, true );
281
WriteToFile(pimpl_->seg_data.buffer, 0, pimpl_->seg_data.buffer_size);
283
pimpl_->changed = false;
286
// Clear a GCP Segment
287
void CPCIDSKGCP2Segment::ClearGCPs(void)
289
pimpl_->num_gcps = 0;
290
pimpl_->gcps.clear();
291
pimpl_->changed = true;
293
RebuildSegmentData();