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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/******************************************************************************
*
* Purpose: Implementation of access to a PCIDSK GCP2 Segment
*
******************************************************************************
* Copyright (c) 2009
* PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "segment/cpcidskgcp2segment.h"
#include "pcidsk_gcp.h"
#include "pcidsk_exception.h"
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
using namespace PCIDSK;
struct CPCIDSKGCP2Segment::PCIDSKGCP2SegInfo
{
std::vector<PCIDSK::GCP> gcps;
unsigned int num_gcps;
PCIDSKBuffer seg_data;
std::string map_units; ///< PCI mapunits string
std::string proj_parms; ///< Additional projection parameters
unsigned int num_proj;
bool changed;
};
CPCIDSKGCP2Segment::CPCIDSKGCP2Segment(PCIDSKFile *file, int segment, const char *segment_pointer)
: CPCIDSKSegment(file, segment, segment_pointer), loaded_(false)
{
pimpl_ = new PCIDSKGCP2SegInfo;
pimpl_->gcps.clear();
pimpl_->changed = false;
Load();
}
CPCIDSKGCP2Segment::~CPCIDSKGCP2Segment()
{
RebuildSegmentData();
delete pimpl_;
}
void CPCIDSKGCP2Segment::Load()
{
if (loaded_) {
return;
}
// Read the the segment in. The first block has information about
// the structure of the GCP segment (how many, the projection, etc.)
pimpl_->seg_data.SetSize(data_size - 1024);
ReadFromFile(pimpl_->seg_data.buffer, 0, data_size - 1024);
// check for 'GCP2 ' in the first 8 bytes
if (std::strncmp(pimpl_->seg_data.buffer, "GCP2 ", 8) != 0) {
// Assume it's an empty segment, so we can mark loaded_ = true,
// write it out and return
pimpl_->changed = true;
pimpl_->map_units = "LAT/LONG D000";
pimpl_->proj_parms = "";
pimpl_->num_gcps = 0;
loaded_ = true;
return;
}
// Check the number of blocks field's validity
unsigned int num_blocks = pimpl_->seg_data.GetInt(8, 8);
if (((data_size - 1024 - 512) / 512) != num_blocks) {
//ThrowPCIDSKException("Calculated number of blocks (%d) does not match "
// "the value encoded in the GCP2 segment (%d).", ((data_size - 1024 - 512)/512),
// num_blocks);
// Something is messed up with how GDB generates these segments... nice.
}
pimpl_->num_gcps = pimpl_->seg_data.GetInt(16, 8);
// Extract the map units string:
pimpl_->map_units = std::string(pimpl_->seg_data.buffer + 24, 16);
// Extract the projection parameters string
pimpl_->proj_parms = std::string(pimpl_->seg_data.buffer + 256, 256);
// Get the number of alternative projections (should be 0!)
pimpl_->num_proj = pimpl_->seg_data.GetInt(40, 8);
if (pimpl_->num_proj != 0) {
ThrowPCIDSKException("There are alternative projections contained in this "
"GCP2 segment. This functionality is not supported in libpcidsk.");
}
// Load the GCPs into the vector of PCIDSK::GCPs
for (unsigned int i = 0; i < pimpl_->num_gcps; i++)
{
unsigned int offset = 512 + i * 256;
bool is_cp = pimpl_->seg_data.buffer[offset] == 'C';
double pixel = pimpl_->seg_data.GetDouble(offset + 6, 14);
double line = pimpl_->seg_data.GetDouble(offset + 20, 14);
double elev = pimpl_->seg_data.GetDouble(offset + 34, 12);
double x = pimpl_->seg_data.GetDouble(offset + 48, 22);
double y = pimpl_->seg_data.GetDouble(offset + 70, 22);
PCIDSK::GCP::EElevationDatum elev_datum = pimpl_->seg_data.buffer[offset + 47] != 'M' ?
GCP::EEllipsoidal : GCP::EMeanSeaLevel;
char elev_unit_c = pimpl_->seg_data.buffer[offset + 46];
PCIDSK::GCP::EElevationUnit elev_unit = elev_unit_c == 'M' ? GCP::EMetres :
elev_unit_c == 'F' ? GCP::EInternationalFeet :
elev_unit_c == 'A' ? GCP::EAmericanFeet : GCP::EUnknown;
double pix_err = pimpl_->seg_data.GetDouble(offset + 92, 10);
double line_err = pimpl_->seg_data.GetDouble(offset + 102, 10);
double elev_err = pimpl_->seg_data.GetDouble(offset + 112, 10);
double x_err = pimpl_->seg_data.GetDouble(offset + 122, 14);
double y_err = pimpl_->seg_data.GetDouble(offset + 136, 14);
std::string gcp_id(pimpl_->seg_data.buffer + offset + 192, 64);
PCIDSK::GCP gcp(x, y, elev,
line, pixel, gcp_id, pimpl_->map_units,
pimpl_->proj_parms,
x_err, y_err, elev_err,
line_err, pix_err);
gcp.SetElevationUnit(elev_unit);
gcp.SetElevationDatum(elev_datum);
gcp.SetCheckpoint(is_cp);
pimpl_->gcps.push_back(gcp);
}
loaded_ = true;
}
// Return all GCPs in the segment
std::vector<PCIDSK::GCP> const& CPCIDSKGCP2Segment::GetGCPs(void) const
{
return pimpl_->gcps;
}
// Write the given GCPs to the segment. If the segment already
// exists, it will be replaced with this one.
void CPCIDSKGCP2Segment::SetGCPs(std::vector<PCIDSK::GCP> const& gcps)
{
pimpl_->num_gcps = gcps.size();
pimpl_->gcps = gcps; // copy them in
pimpl_->changed = true;
RebuildSegmentData();
}
// Return the count of GCPs in the segment
unsigned int CPCIDSKGCP2Segment::GetGCPCount(void) const
{
return pimpl_->num_gcps;
}
void CPCIDSKGCP2Segment::RebuildSegmentData(void)
{
if (pimpl_->changed == false) {
return;
}
// Rebuild the segment data based on the contents of the struct
int num_blocks = (pimpl_->num_gcps + 1) / 2;
// This will have to change when we have proper projections support
if (pimpl_->gcps.size() > 0)
{
pimpl_->gcps[0].GetMapUnits(pimpl_->map_units,
pimpl_->proj_parms);
}
pimpl_->seg_data.SetSize(num_blocks * 512 + 512);
// Write out the first few fields
pimpl_->seg_data.Put("GCP2 ", 0, 8);
pimpl_->seg_data.Put(num_blocks, 8, 8);
pimpl_->seg_data.Put((int)pimpl_->gcps.size(), 16, 8);
pimpl_->seg_data.Put(pimpl_->map_units.c_str(), 24, 16);
pimpl_->seg_data.Put((int)0, 40, 8);
pimpl_->seg_data.Put(pimpl_->proj_parms.c_str(), 256, 256);
// Time to write GCPs out:
std::vector<PCIDSK::GCP>::const_iterator iter =
pimpl_->gcps.begin();
unsigned int id = 0;
while (iter != pimpl_->gcps.end()) {
std::size_t offset = 512 + id * 256;
if ((*iter).IsCheckPoint()) {
pimpl_->seg_data.Put("C", offset, 1);
} else {
pimpl_->seg_data.Put("G", offset, 1);
}
pimpl_->seg_data.Put("0", offset + 1, 5);
// Start writing out the GCP values
pimpl_->seg_data.Put((*iter).GetPixel(), offset + 6, 14, "%14.4f");
pimpl_->seg_data.Put((*iter).GetLine(), offset + 20, 14, "%14.4f");
pimpl_->seg_data.Put((*iter).GetZ(), offset + 34, 12, "%12.4f");
GCP::EElevationUnit unit;
GCP::EElevationDatum datum;
(*iter).GetElevationInfo(datum, unit);
char unit_c[2];
switch (unit)
{
case GCP::EMetres:
case GCP::EUnknown:
unit_c[0] = 'M';
break;
case GCP::EAmericanFeet:
unit_c[0] = 'A';
break;
case GCP::EInternationalFeet:
unit_c[0] = 'F';
break;
}
char datum_c[2];
switch(datum)
{
case GCP::EEllipsoidal:
datum_c[0] = 'E';
break;
case GCP::EMeanSeaLevel:
datum_c[0] = 'M';
break;
}
unit_c[1] = '\0';
datum_c[1] = '\0';
// Write out elevation information
pimpl_->seg_data.Put(unit_c, offset + 46, 1);
pimpl_->seg_data.Put(datum_c, offset + 47, 1);
pimpl_->seg_data.Put((*iter).GetX(), offset + 48, 22, "%22.14e");
pimpl_->seg_data.Put((*iter).GetY(), offset + 70, 22, "%22.14e");
pimpl_->seg_data.Put((*iter).GetPixelErr(), offset + 92, 10, "%10.4f");
pimpl_->seg_data.Put((*iter).GetLineErr(), offset + 102, 10, "%10.4f");
pimpl_->seg_data.Put((*iter).GetZErr(), offset + 112, 10, "%10.4f");
pimpl_->seg_data.Put((*iter).GetXErr(), offset + 122, 14, "%14.4e");
pimpl_->seg_data.Put((*iter).GetYErr(), offset + 136, 14, "%14.4e");
pimpl_->seg_data.Put((*iter).GetIDString(), offset + 192, 64, true );
id++;
iter++;
}
WriteToFile(pimpl_->seg_data.buffer, 0, pimpl_->seg_data.buffer_size);
pimpl_->changed = false;
}
// Clear a GCP Segment
void CPCIDSKGCP2Segment::ClearGCPs(void)
{
pimpl_->num_gcps = 0;
pimpl_->gcps.clear();
pimpl_->changed = true;
RebuildSegmentData();
}
|