LCOV - code coverage report
Current view: top level - frmts/pcidsk/sdk/segment - cpcidskgcp2segment.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 0 152 0.0 %
Date: 2024-11-21 22:18:42 Functions: 0 12 0.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Purpose: Implementation of access to a PCIDSK GCP2 Segment
       4             :  *
       5             :  ******************************************************************************
       6             :  * Copyright (c) 2009
       7             :  * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada.
       8             :  *
       9             :  * SPDX-License-Identifier: MIT
      10             :  ****************************************************************************/
      11             : #include "segment/cpcidskgcp2segment.h"
      12             : 
      13             : #include "pcidsk_gcp.h"
      14             : #include "pcidsk_exception.h"
      15             : #include "pcidsk_file.h"
      16             : #include "core/pcidsk_utils.h"
      17             : 
      18             : #include <cstring>
      19             : #include <iostream>
      20             : #include <vector>
      21             : #include <string>
      22             : 
      23             : using namespace PCIDSK;
      24             : 
      25           0 : CPCIDSKGCP2Segment::CPCIDSKGCP2Segment(PCIDSKFile *fileIn, int segmentIn, const char *segment_pointer)
      26           0 :     : CPCIDSKSegment(fileIn, segmentIn, segment_pointer), loaded_(false)
      27             : {
      28           0 :     pimpl_ = new PCIDSKGCP2SegInfo;
      29           0 :     pimpl_->gcps.clear();
      30           0 :     pimpl_->changed = false;
      31             :     try
      32             :     {
      33           0 :         Load();
      34             :     }
      35           0 :     catch( const PCIDSKException& )
      36             :     {
      37           0 :         delete pimpl_;
      38           0 :         pimpl_ = new PCIDSKGCP2SegInfo;
      39           0 :         pimpl_->gcps.clear();
      40           0 :         pimpl_->num_gcps = 0;
      41           0 :         pimpl_->changed = false;
      42           0 :         this->loaded_ = true;
      43             :     }
      44           0 : }
      45             : 
      46           0 : CPCIDSKGCP2Segment::~CPCIDSKGCP2Segment()
      47             : {
      48             :     try
      49             :     {
      50           0 :         RebuildSegmentData();
      51             :     }
      52           0 :     catch( const PCIDSKException& )
      53             :     {
      54             :         // TODO ?
      55             :     }
      56           0 :     delete pimpl_;
      57           0 : }
      58             : 
      59           0 : void CPCIDSKGCP2Segment::Load()
      60             : {
      61           0 :     if (loaded_) {
      62           0 :         return;
      63             :     }
      64             : 
      65             :     // Read the segment in. The first block has information about
      66             :     // the structure of the GCP segment (how many, the projection, etc.)
      67           0 :     pimpl_->seg_data.SetSize(static_cast<int>(data_size) - 1024);
      68           0 :     ReadFromFile(pimpl_->seg_data.buffer, 0, data_size - 1024);
      69             : 
      70             :     // check for 'GCP2    ' in the first 8 bytes
      71           0 :     if (!STARTS_WITH(pimpl_->seg_data.buffer, "GCP2    ")) {
      72             :         // Assume it is an empty segment, so we can mark loaded_ = true,
      73             :         // write it out and return
      74           0 :         pimpl_->changed = true;
      75           0 :         pimpl_->map_units = "LAT/LONG D000";
      76           0 :         pimpl_->proj_parms = "";
      77           0 :         pimpl_->num_gcps = 0;
      78           0 :         loaded_ = true;
      79           0 :         return;
      80             :     }
      81             : 
      82             :     // Check the number of blocks field's validity
      83           0 :     unsigned int num_blocks = pimpl_->seg_data.GetInt(8, 8);
      84             : 
      85           0 :     if (((data_size - 1024 - 512) / 512) != num_blocks) {
      86             :         //ThrowPCIDSKException("Calculated number of blocks (%d) does not match "
      87             :         //    "the value encoded in the GCP2 segment (%d).", ((data_size - 1024 - 512)/512),
      88             :         //    num_blocks);
      89             :         // Something is messed up with how GDB generates these segments... nice.
      90             :     }
      91             : 
      92           0 :     pimpl_->num_gcps = pimpl_->seg_data.GetInt(16, 8);
      93             : 
      94             :     // Extract the map units string:
      95           0 :     pimpl_->map_units = std::string(pimpl_->seg_data.buffer + 24, 16);
      96             : 
      97             :     // Extract the projection parameters string
      98           0 :     pimpl_->proj_parms = std::string(pimpl_->seg_data.buffer + 256, 256);
      99             : 
     100             :     // Get the number of alternative projections (should be 0!)
     101           0 :     pimpl_->num_proj = pimpl_->seg_data.GetInt(40, 8);
     102           0 :     if (pimpl_->num_proj != 0) {
     103           0 :         return ThrowPCIDSKException("There are alternative projections contained in this "
     104           0 :             "GCP2 segment. This functionality is not supported in libpcidsk.");
     105             :     }
     106             : 
     107             :     // Load the GCPs into the vector of PCIDSK::GCPs
     108           0 :     for (unsigned int i = 0; i < pimpl_->num_gcps; i++)
     109             :     {
     110           0 :         unsigned int offset = 512 + i * 256;
     111           0 :         bool is_cp = pimpl_->seg_data.buffer[offset] == 'C';
     112           0 :         bool is_active = pimpl_->seg_data.buffer[offset] != 'I';
     113           0 :         double pixel = pimpl_->seg_data.GetDouble(offset + 6, 14);
     114           0 :         double line = pimpl_->seg_data.GetDouble(offset + 20, 14);
     115             : 
     116           0 :         double elev = pimpl_->seg_data.GetDouble(offset + 34, 12);
     117           0 :         double x = pimpl_->seg_data.GetDouble(offset + 48, 22);
     118           0 :         double y = pimpl_->seg_data.GetDouble(offset + 70, 22);
     119             : 
     120           0 :         char cElevDatum = (char)toupper(static_cast<unsigned char>(pimpl_->seg_data.buffer[offset + 47]));
     121           0 :         PCIDSK::GCP::EElevationDatum elev_datum = cElevDatum != 'M' ?
     122             :             GCP::EEllipsoidal : GCP::EMeanSeaLevel;
     123             : 
     124           0 :         char elev_unit_c = (char)toupper(static_cast<unsigned char>(pimpl_->seg_data.buffer[offset + 46]));
     125           0 :         PCIDSK::GCP::EElevationUnit elev_unit = elev_unit_c == 'M' ? GCP::EMetres :
     126           0 :             elev_unit_c == 'F' ? GCP::EInternationalFeet :
     127           0 :             elev_unit_c == 'A' ? GCP::EAmericanFeet : GCP::EUnknown;
     128             : 
     129           0 :         double pix_err = pimpl_->seg_data.GetDouble(offset + 92, 10);
     130           0 :         double line_err = pimpl_->seg_data.GetDouble(offset + 102, 10);
     131           0 :         double elev_err = pimpl_->seg_data.GetDouble(offset + 112, 10);
     132             : 
     133           0 :         double x_err = pimpl_->seg_data.GetDouble(offset + 122, 14);
     134           0 :         double y_err = pimpl_->seg_data.GetDouble(offset + 136, 14);
     135             : 
     136           0 :         std::string gcp_id(pimpl_->seg_data.buffer + offset + 192, 64);
     137             : 
     138             :         PCIDSK::GCP gcp(x, y, elev,
     139           0 :                         line, pixel, gcp_id, pimpl_->map_units,
     140           0 :                         pimpl_->proj_parms,
     141             :                         x_err, y_err, elev_err,
     142           0 :                         line_err, pix_err);
     143           0 :         gcp.SetElevationUnit(elev_unit);
     144           0 :         gcp.SetElevationDatum(elev_datum);
     145           0 :         gcp.SetActive(is_active);
     146           0 :         gcp.SetCheckpoint(is_cp);
     147             : 
     148           0 :         pimpl_->gcps.push_back(gcp);
     149             :     }
     150             : 
     151           0 :     loaded_ = true;
     152             : }
     153             : 
     154             :  // Return all GCPs in the segment
     155           0 : std::vector<PCIDSK::GCP> const& CPCIDSKGCP2Segment::GetGCPs(void) const
     156             : {
     157           0 :     return pimpl_->gcps;
     158             : }
     159             : 
     160             : // Write the given GCPs to the segment. If the segment already
     161             : // exists, it will be replaced with this one.
     162           0 : void CPCIDSKGCP2Segment::SetGCPs(std::vector<PCIDSK::GCP> const& gcps)
     163             : {
     164           0 :     pimpl_->num_gcps = static_cast<unsigned int>(gcps.size());
     165           0 :     pimpl_->gcps = gcps; // copy them in
     166           0 :     pimpl_->changed = true;
     167             : 
     168           0 :     RebuildSegmentData();
     169           0 : }
     170             : 
     171             : // Return the count of GCPs in the segment
     172           0 : unsigned int  CPCIDSKGCP2Segment::GetGCPCount(void) const
     173             : {
     174           0 :     return pimpl_->num_gcps;
     175             : }
     176             : 
     177           0 : void CPCIDSKGCP2Segment::Synchronize()
     178             : {
     179           0 :     if( pimpl_ != nullptr )
     180             :     {
     181           0 :         RebuildSegmentData();
     182             :     }
     183           0 : }
     184             : 
     185           0 : void CPCIDSKGCP2Segment::RebuildSegmentData(void)
     186             : {
     187           0 :     if (pimpl_->changed == false || !this->file->GetUpdatable()) {
     188           0 :         return;
     189             :     }
     190           0 :     pimpl_->changed = false;
     191             : 
     192             :     // Rebuild the segment data based on the contents of the struct
     193           0 :     int num_blocks = (pimpl_->num_gcps + 1) / 2;
     194             : 
     195             :     // This will have to change when we have proper projections support
     196             : 
     197           0 :     if (!pimpl_->gcps.empty())
     198             :     {
     199           0 :         pimpl_->gcps[0].GetMapUnits(pimpl_->map_units,
     200           0 :             pimpl_->proj_parms);
     201             :     }
     202             : 
     203           0 :     pimpl_->seg_data.SetSize(num_blocks * 512 + 512);
     204             : 
     205             :     // Write out the first few fields
     206           0 :     pimpl_->seg_data.Put("GCP2    ", 0, 8);
     207           0 :     pimpl_->seg_data.Put(num_blocks, 8, 8);
     208           0 :     pimpl_->seg_data.Put((int)pimpl_->gcps.size(), 16, 8);
     209           0 :     pimpl_->seg_data.Put(pimpl_->map_units.c_str(), 24, 16);
     210           0 :     pimpl_->seg_data.Put((int)0, 40, 8);
     211           0 :     pimpl_->seg_data.Put(pimpl_->proj_parms.c_str(), 256, 256);
     212             : 
     213             :     // Time to write GCPs out:
     214             :     std::vector<PCIDSK::GCP>::const_iterator iter =
     215           0 :         pimpl_->gcps.begin();
     216             : 
     217           0 :     int id = 0;
     218           0 :     while (iter != pimpl_->gcps.end()) {
     219           0 :         int offset = 512 + id * 256;
     220             : 
     221           0 :         if ((*iter).IsCheckPoint()) {
     222           0 :             pimpl_->seg_data.Put("C", offset, 1);
     223             :         }
     224           0 :         else if ((*iter).IsActive())
     225             :         {
     226           0 :             pimpl_->seg_data.Put("G", offset, 1);
     227             :         }
     228             :         else
     229             :         {
     230           0 :             pimpl_->seg_data.Put("I", offset, 1);
     231             :         }
     232             : 
     233           0 :         pimpl_->seg_data.Put("0", offset + 1, 5);
     234             : 
     235             :         // Start writing out the GCP values
     236           0 :         pimpl_->seg_data.Put((*iter).GetPixel(), offset + 6, 14, "%14.4f");
     237           0 :         pimpl_->seg_data.Put((*iter).GetLine(), offset + 20, 14, "%14.4f");
     238           0 :         pimpl_->seg_data.Put((*iter).GetZ(), offset + 34, 12, "%12.4f");
     239             : 
     240             :         GCP::EElevationUnit unit;
     241             :         GCP::EElevationDatum datum;
     242           0 :         (*iter).GetElevationInfo(datum, unit);
     243             : 
     244             :         char unit_c[2];
     245             : 
     246           0 :         switch (unit)
     247             :         {
     248           0 :         case GCP::EMetres:
     249             :         case GCP::EUnknown:
     250           0 :             unit_c[0] = 'M';
     251           0 :             break;
     252           0 :         case GCP::EAmericanFeet:
     253           0 :             unit_c[0] = 'A';
     254           0 :             break;
     255           0 :         case GCP::EInternationalFeet:
     256           0 :             unit_c[0] = 'F';
     257           0 :             break;
     258             :         }
     259             : 
     260             :         char datum_c[2];
     261             : 
     262           0 :         switch(datum)
     263             :         {
     264           0 :         case GCP::EEllipsoidal:
     265           0 :             datum_c[0] = 'E';
     266           0 :             break;
     267           0 :         case GCP::EMeanSeaLevel:
     268           0 :             datum_c[0] = 'M';
     269           0 :             break;
     270             :         }
     271             : 
     272           0 :         unit_c[1] = '\0';
     273           0 :         datum_c[1] = '\0';
     274             : 
     275             :         // Write out elevation information
     276           0 :         pimpl_->seg_data.Put(unit_c, offset + 46, 1);
     277           0 :         pimpl_->seg_data.Put(datum_c, offset + 47, 1);
     278             : 
     279           0 :         pimpl_->seg_data.Put((*iter).GetX(), offset + 48, 22, "%22.14e");
     280           0 :         pimpl_->seg_data.Put((*iter).GetY(), offset + 70, 22, "%22.14e");
     281           0 :         pimpl_->seg_data.Put((*iter).GetPixelErr(), offset + 92, 10, "%10.4f");
     282           0 :         pimpl_->seg_data.Put((*iter).GetLineErr(), offset + 102, 10, "%10.4f");
     283           0 :         pimpl_->seg_data.Put((*iter).GetZErr(), offset + 112, 10, "%10.4f");
     284           0 :         pimpl_->seg_data.Put((*iter).GetXErr(), offset + 122, 14, "%14.4e");
     285           0 :         pimpl_->seg_data.Put((*iter).GetYErr(), offset + 136, 14, "%14.4e");
     286           0 :         pimpl_->seg_data.Put((*iter).GetIDString(), offset + 192, 64, true );
     287             : 
     288           0 :         ++id;
     289           0 :         ++iter;
     290             :     }
     291             : 
     292           0 :     WriteToFile(pimpl_->seg_data.buffer, 0, pimpl_->seg_data.buffer_size);
     293             : 
     294           0 :     pimpl_->changed = false;
     295             : }
     296             : 
     297             : // Clear a GCP Segment
     298           0 : void  CPCIDSKGCP2Segment::ClearGCPs(void)
     299             : {
     300           0 :     pimpl_->num_gcps = 0;
     301           0 :     pimpl_->gcps.clear();
     302           0 :     pimpl_->changed = true;
     303             : 
     304           0 :     RebuildSegmentData();
     305           0 : }

Generated by: LCOV version 1.14