Commit 48752486 authored by Jesse Mapel's avatar Jesse Mapel Committed by Makayla Shepherd
Browse files

Removed old ControlNetFile code

parent 5a71eed3
Loading
Loading
Loading
Loading
+0 −86
Original line number Diff line number Diff line
#ifndef ControlNetFile_h
#define ControlNetFile_h

namespace Isis {
  class FileName;
  class Pvl;

  /**
   * @brief Generic Binary Control Net File Representation
   *
   * This class is the parent for all binary forms of the control network
   *   files. Each one must be readable, writable, and convertable to Pvl.
   *   Other than that, they can be (and probably will be) literally anything as
   *   long as it has a Pvl header.
   *
   * @author 2011-04-07 Steven Lambright
   *
   * @internal
   */
  class ControlNetFile {
    public:
      /**
       * Constructor. No data so this does nothing.
       */
      ControlNetFile() {};
      /**
       * Destructor. No data so this does nothing.
       */
      virtual ~ControlNetFile() {};

      /**
       * This reads the binary file into memory. The header is the Pvl that must
       *   be at the top of the file (it's how we could tell it was binary in
       *   the first place).
       *
       * @param header The pvl at the top of the file down to the "End" keyword
       * @param file The filename of the binary file to be read
       */
      virtual void Read(const Pvl &header, const FileName &file) = 0;

      /**
       * This writes the binary file that is in memory to disk. The behavior of
       *   this method is undefined if the required data is not set
       *   (ControlNetVersioner::LatestPvlToBinary guarantees they are, and this
       *   should never be called for old versions).
       *
       * @param file The filename of the binary file to be written
       */
      virtual void Write(const FileName &file) const = 0;

      /**
       * Convert the binary representation to Pvl (any pvl version).
       */
      virtual Pvl toPvl() const = 0;

    private:
      /**
       * Disallow copy construction. This should never happen.
       *
       * @param other File to copy from
       */
      ControlNetFile(const ControlNetFile &other);

      /**
       * Disallow assignment. This should never happen.
       *
       * @param other File to copy from
       */
      ControlNetFile &operator=(const ControlNetFile &other);
  };

}

// Always include the latest version only
#include "ControlNetFileV0002.h"

namespace Isis {
  class ControlNetFileV0002;

  /**
   * To minimize changes in other places, allow others to use "Latest"
   */
  typedef ControlNetFileV0002 LatestControlNetFile;
}

#endif
+0 −322
Original line number Diff line number Diff line
#include "ControlNetFileV0001.h"

#include <fstream>

#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/io/coded_stream.h>

#include "ControlNetFileV0001.pb.h"
#include "ControlMeasureLogData.h"
#include "FileName.h"
#include "IException.h"
#include "Progress.h"
#include "Pvl.h"
#include "PvlGroup.h"
#include "PvlObject.h"
#include "PvlKeyword.h"

using namespace google::protobuf;
using namespace google::protobuf::io;
using namespace std;

namespace Isis {
  ControlNetFileV0001::ControlNetFileV0001() {
    p_network = new ControlNetFileProtoV0001;
    p_logData = new ControlNetLogDataProtoV0001;
  }


  ControlNetFileV0001::~ControlNetFileV0001() {
    delete p_network;
    delete p_logData;
  }


  void ControlNetFileV0001::Read(const Pvl &head, const FileName &file) {
    const PvlObject &protoBufferInfo = head.findObject("ProtoBuffer");
    const PvlObject &protoBufferCore = protoBufferInfo.findObject("Core");

    BigInt coreStartPos = protoBufferCore["StartByte"];
    BigInt coreLength = protoBufferCore["Bytes"];

    fstream input(file.expanded().toLatin1().data(), ios::in | ios::binary);
    if (!input.is_open()) {
      IString msg = "Failed to open PB file" + file.name();
      throw IException(IException::Programmer, msg, _FILEINFO_);
    }

    input.seekg(coreStartPos, ios::beg);
    IstreamInputStream inStream(&input);
    CodedInputStream codedInStream(&inStream);
    codedInStream.PushLimit(coreLength);
    // max 512MB, warn at 400MB
    codedInStream.SetTotalBytesLimit(1024 * 1024 * 512, 1024 * 1024 * 400);

    // Now stream the rest of the input into the google protocol buffer.
    try {
      if (!p_network->ParseFromCodedStream(&codedInStream)) {
        IString msg = "Failed to read input PB file " + file.name();
        throw IException(IException::Programmer, msg, _FILEINFO_);
      }
    }
    catch (IException &e) {
      string msg = "Cannot parse binary PB file";
      throw IException(e, IException::User, msg, _FILEINFO_);
    }
    catch (...) {
      string msg = "Cannot parse binary PB file";
      throw IException(IException::User, msg, _FILEINFO_);
    }

    const PvlObject &logDataInfo = protoBufferInfo.findObject("LogData");
    BigInt logStartPos = logDataInfo["StartByte"];
    BigInt logLength = logDataInfo["Bytes"];

    input.clear();
    input.seekg(logStartPos, ios::beg);
    IstreamInputStream logInStream(&input);
    CodedInputStream codedLogInStream(&logInStream);
    codedLogInStream.PushLimit(logLength);
    // max 512MB, warn at 400MB
    codedLogInStream.SetTotalBytesLimit(1024 * 1024 * 512, 1024 * 1024 * 400);

    // Now stream the rest of the input into the google protocol buffer.
    try {
      if (!p_logData->ParseFromCodedStream(&codedLogInStream)) {
        IString msg = "Failed to read log data in PB file [" + file.name() + "]";
        throw IException(IException::Programmer, msg, _FILEINFO_);
      }
    }
    catch (...) {
      string msg = "Cannot parse binary PB file's log data";
      throw IException(IException::User, msg, _FILEINFO_);
    }
  }


  Pvl ControlNetFileV0001::toPvl() const {
    Pvl pvl;
    pvl.addObject(PvlObject("ControlNetwork"));
    PvlObject &network = pvl.findObject("ControlNetwork");

    network += PvlKeyword("NetworkId", p_network->networkid().c_str());
    network += PvlKeyword("TargetName", p_network->targetname().c_str());
    network += PvlKeyword("UserName", p_network->username().c_str());
    network += PvlKeyword("Created", p_network->created().c_str());
    network += PvlKeyword("LastModified", p_network->lastmodified().c_str());
    network += PvlKeyword("Description", p_network->description().c_str());

    // This is the Pvl version we're converting to
    network += PvlKeyword("Version", "1");

    for (int i = 0; i < p_network->points_size(); i++) {
      const ControlNetFileProtoV0001_PBControlPoint &binaryPoint =
          p_network->points(i);
      PvlObject pvlPoint("ControlPoint");

      if(binaryPoint.type() == ControlNetFileProtoV0001_PBControlPoint::Ground)
        pvlPoint += PvlKeyword("PointType", "Ground");
      else
        pvlPoint += PvlKeyword("PointType", "Tie");

      pvlPoint += PvlKeyword("PointId", binaryPoint.id().c_str());
      pvlPoint += PvlKeyword("ChooserName", binaryPoint.choosername().c_str());
      pvlPoint += PvlKeyword("DateTime", binaryPoint.datetime().c_str());

      if (binaryPoint.editlock()) {
        pvlPoint += PvlKeyword("EditLock", "True");
      }

      if (binaryPoint.ignore()) {
        pvlPoint += PvlKeyword("Ignore", "True");
      }

      switch (binaryPoint.apriorisurfpointsource()) {
        case ControlNetFileProtoV0001_PBControlPoint::None:
          break;
        case ControlNetFileProtoV0001_PBControlPoint::User:
          pvlPoint += PvlKeyword("AprioriXYZSource", "User");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::AverageOfMeasures:
          pvlPoint += PvlKeyword("AprioriXYZSource", "AverageOfMeasures");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::Reference:
          pvlPoint += PvlKeyword("AprioriXYZSource", "Reference");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::Basemap:
          pvlPoint += PvlKeyword("AprioriXYZSource", "Basemap");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::BundleSolution:
          pvlPoint += PvlKeyword("AprioriXYZSource", "BundleSolution");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::Ellipsoid:
        case ControlNetFileProtoV0001_PBControlPoint::DEM:
          break;
      }

      if (binaryPoint.has_apriorisurfpointsourcefile())
        pvlPoint += PvlKeyword("AprioriXYZSourceFile",
                        binaryPoint.apriorisurfpointsourcefile().c_str());

      switch (binaryPoint.aprioriradiussource()) {
        case ControlNetFileProtoV0001_PBControlPoint::None:
          break;
        case ControlNetFileProtoV0001_PBControlPoint::User:
          pvlPoint += PvlKeyword("AprioriRadiusSource", "User");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::AverageOfMeasures:
          pvlPoint += PvlKeyword("AprioriRadiusSource", "AverageOfMeasures");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::Reference:
          pvlPoint += PvlKeyword("AprioriRadiusSource", "Reference");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::Basemap:
          pvlPoint += PvlKeyword("AprioriRadiusSource", "Basemap");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::BundleSolution:
          pvlPoint += PvlKeyword("AprioriRadiusSource", "BundleSolution");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::Ellipsoid:
          pvlPoint += PvlKeyword("AprioriRadiusSource", "Ellipsoid");
          break;
        case ControlNetFileProtoV0001_PBControlPoint::DEM:
          pvlPoint += PvlKeyword("AprioriRadiusSource", "DEM");
          break;
      }

      if (binaryPoint.has_aprioriradiussourcefile())
        pvlPoint += PvlKeyword("AprioriRadiusSourceFile",
                        binaryPoint.aprioriradiussourcefile().c_str());

      if(binaryPoint.has_apriorix()) {
        pvlPoint += PvlKeyword("AprioriX", toString(binaryPoint.apriorix()), "meters");
        pvlPoint += PvlKeyword("AprioriY", toString(binaryPoint.aprioriy()), "meters");
        pvlPoint += PvlKeyword("AprioriZ", toString(binaryPoint.aprioriz()), "meters");

        if(binaryPoint.aprioricovar_size()) {
          PvlKeyword matrix("AprioriCovarianceMatrix");
          matrix += toString(binaryPoint.aprioricovar(0));
          matrix += toString(binaryPoint.aprioricovar(1));
          matrix += toString(binaryPoint.aprioricovar(2));
          matrix += toString(binaryPoint.aprioricovar(3));
          matrix += toString(binaryPoint.aprioricovar(4));
          matrix += toString(binaryPoint.aprioricovar(5));
          pvlPoint += matrix;
        }
      }

      if(binaryPoint.latitudeconstrained() &&
         (binaryPoint.aprioricovar_size() || binaryPoint.adjustedcovar_size()))
        pvlPoint += PvlKeyword("LatitudeConstrained", "True");

      if(binaryPoint.longitudeconstrained() &&
         (binaryPoint.aprioricovar_size() || binaryPoint.adjustedcovar_size()))
        pvlPoint += PvlKeyword("LongitudeConstrained", "True");

      if(binaryPoint.radiusconstrained() &&
         (binaryPoint.aprioricovar_size() || binaryPoint.adjustedcovar_size()))
        pvlPoint += PvlKeyword("RadiusConstrained", "True");

      if(binaryPoint.has_adjustedx()) {
        pvlPoint += PvlKeyword("AdjustedX", toString(binaryPoint.adjustedx()), "meters");
        pvlPoint += PvlKeyword("AdjustedY", toString(binaryPoint.adjustedy()), "meters");
        pvlPoint += PvlKeyword("AdjustedZ", toString(binaryPoint.adjustedz()), "meters");

        if(binaryPoint.adjustedcovar_size()) {
          PvlKeyword matrix("AdjustedCovarianceMatrix");
          matrix += toString(binaryPoint.adjustedcovar(0));
          matrix += toString(binaryPoint.adjustedcovar(1));
          matrix += toString(binaryPoint.adjustedcovar(2));
          matrix += toString(binaryPoint.adjustedcovar(3));
          matrix += toString(binaryPoint.adjustedcovar(4));
          matrix += toString(binaryPoint.adjustedcovar(5));
          pvlPoint += matrix;
        }
      }

      for (int j = 0; j < binaryPoint.measures_size(); j++) {
        PvlGroup pvlMeasure("ControlMeasure");
        const ControlNetFileProtoV0001_PBControlPoint_PBControlMeasure &
            binaryMeasure = binaryPoint.measures(j);
        pvlMeasure += PvlKeyword("SerialNumber", binaryMeasure.serialnumber().c_str());

        switch(binaryMeasure.type()) {
          case ControlNetFileProtoV0001_PBControlPoint_PBControlMeasure_MeasureType_Candidate:
            pvlMeasure += PvlKeyword("MeasureType", "Candidate");
            break;
          case ControlNetFileProtoV0001_PBControlPoint_PBControlMeasure_MeasureType_Manual:
            pvlMeasure += PvlKeyword("MeasureType", "Manual");
            break;
          case ControlNetFileProtoV0001_PBControlPoint_PBControlMeasure_MeasureType_RegisteredPixel:
            pvlMeasure += PvlKeyword("MeasureType", "RegisteredPixel");
            break;
          case ControlNetFileProtoV0001_PBControlPoint_PBControlMeasure_MeasureType_RegisteredSubPixel:
            pvlMeasure += PvlKeyword("MeasureType", "RegisteredSubPixel");
            break;
        }

        pvlMeasure += PvlKeyword("ChooserName", binaryMeasure.choosername().c_str());
        pvlMeasure += PvlKeyword("DateTime", binaryMeasure.datetime().c_str());

        if(binaryMeasure.editlock())
          pvlMeasure += PvlKeyword("EditLock", "True");

        if(binaryMeasure.ignore())
          pvlMeasure += PvlKeyword("Ignore", "True");

        if(binaryMeasure.has_measurement()) {
          pvlMeasure += PvlKeyword("Sample", toString(binaryMeasure.measurement().sample()));
          pvlMeasure += PvlKeyword("Line", toString(binaryMeasure.measurement().line()));

          if (binaryMeasure.measurement().has_sampleresidual())
            pvlMeasure += PvlKeyword("SampleResidual",
                toString(binaryMeasure.measurement().sampleresidual()), "pixels");

          if (binaryMeasure.measurement().has_lineresidual())
            pvlMeasure += PvlKeyword("LineResidual",
                toString(binaryMeasure.measurement().lineresidual()), "pixels");
        }

        if (binaryMeasure.has_diameter())
          pvlMeasure += PvlKeyword("Diameter", toString(binaryMeasure.diameter()));

        if (binaryMeasure.has_apriorisample())
          pvlMeasure += PvlKeyword("AprioriSample", toString(binaryMeasure.apriorisample()));

        if (binaryMeasure.has_aprioriline())
          pvlMeasure += PvlKeyword("AprioriLine", toString(binaryMeasure.aprioriline()));

        if (binaryMeasure.has_samplesigma())
          pvlMeasure += PvlKeyword("SampleSigma", toString(binaryMeasure.samplesigma()));

        if (binaryMeasure.has_samplesigma())
          pvlMeasure += PvlKeyword("LineSigma", toString(binaryMeasure.linesigma()));

         for (int logEntry = 0;
             logEntry <
               p_logData->points(i).measures(j).loggedmeasuredata_size();
             logEntry ++) {
           const ControlNetLogDataProtoV0001_Point_Measure_DataEntry &log =
               p_logData->points(i).measures(j).loggedmeasuredata(logEntry);

           try {
             ControlMeasureLogData interpreter(log);
             pvlMeasure += interpreter.ToKeyword();
           }
           catch(IException &) {
           }
         }

        if(binaryPoint.has_referenceindex() &&
           binaryPoint.referenceindex() == j)
          pvlMeasure += PvlKeyword("Reference", "True");

        pvlPoint.addGroup(pvlMeasure);
      }

      network.addObject(pvlPoint);
    }

    return pvl;
  }
}
+0 −95
Original line number Diff line number Diff line
#ifndef ControlNetFileV0001_h
#define ControlNetFileV0001_h
/**
 * @file
 * $Revision: 1.9 $
 * $Date: 2009/07/15 17:33:52 $
 *
 *   Unless noted otherwise, the portions of Isis written by the USGS are
 *   public domain. See individual third-party library and package descriptions
 *   for intellectual property information, user agreements, and related
 *   information.
 *
 *   Although Isis has been used by the USGS, no warranty, expressed or
 *   implied, is made by the USGS as to the accuracy and functioning of such
 *   software and related material nor shall the fact of distribution
 *   constitute any such warranty, and no responsibility is assumed by the
 *   USGS in connection therewith.
 *
 *   For additional information, launch
 *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html
 *   in a browser or see the Privacy &amp; Disclaimers page on the Isis website,
 *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *   http://www.usgs.gov/privacy.html.
 */

#include "ControlNetFile.h"

template <typename A> class QList;

namespace Isis {
  class ControlNetFileProtoV0001;
  class ControlNetLogDataProtoV0001;

  /**
   * @brief Handle Binary Control Network Files version 1
   *
   * This functionality and history was extracted from Isis::ControlNet
   *
   * @author 2011-04-08 Steven Lambright
   *
   * @internal
   *   @history 2010-01-12 Tracie Sucharski Added support for binary networks,
   *                added new parameters, renamed ComputeErrors to
   *                ComputeResiduals, renamed MaximumError to MaximumResidual,
   *                renamed AverageError to AverageResidual.
   *   @history 2010-08-05 Steven Lambright New label format much closer to a
   *                cube so that we can expand upon it easily later. Also added
   *                support for more than just the protocol buffer in the file,
   *                at the current cost of reading the protocol buffer's binary
   *                data into memory. This might need to be changed later.
   *   @history 2011-04-04 Steven Lambright - Reading is more likely to work...
   *                not sure why my changes fixed it for very large networks.
   *                Binary reads now do the same progress as Pvl for console
   *                output consistency (and because it can take time).
   *   @history 2011-04-08 Steven Lambright - Extracted functionality to
   *                ControlNetFileV0001 class
   */
  class ControlNetFileV0001 : public ControlNetFile {
    public:
      ControlNetFileV0001();
      virtual ~ControlNetFileV0001();

      virtual void Read(const Pvl &header, const FileName &file);

      // We don't need old Write methods... since this wasn't already
      //   implemented I'm not going to bother to implement it now.
      virtual void Write(const FileName &file) const {};
      virtual Pvl toPvl() const;

      /**
       * Retrieve the protocol buffer that encapsulates the entire control
       *   network.
       */
      ControlNetFileProtoV0001 &GetNetwork() {
        return *p_network;
      }


      /**
       * Retrieve the protocol buffer that encapsulates all of the log data.
       */
      ControlNetLogDataProtoV0001 &GetLogData() {
        return *p_logData;
      }

    private:
      //! This contains the entire cnet
      ControlNetFileProtoV0001 *p_network;

      //! This contains all of the log data in the cnet
      ControlNetLogDataProtoV0001 *p_logData;
  };
}

#endif
+0 −570

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −100
Original line number Diff line number Diff line
#ifndef ControlNetFileV0002_h
#define ControlNetFileV0002_h
/**
 * @file
 * $Revision: 1.9 $
 * $Date: 2009/07/15 17:33:52 $
 *
 *   Unless noted otherwise, the portions of Isis written by the USGS are
 *   public domain. See individual third-party library and package descriptions
 *   for intellectual property information, user agreements, and related
 *   information.
 *
 *   Although Isis has been used by the USGS, no warranty, expressed or
 *   implied, is made by the USGS as to the accuracy and functioning of such
 *   software and related material nor shall the fact of distribution
 *   constitute any such warranty, and no responsibility is assumed by the
 *   USGS in connection therewith.
 *
 *   For additional information, launch
 *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html
 *   in a browser or see the Privacy &amp; Disclaimers page on the Isis website,
 *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *   http://www.usgs.gov/privacy.html.
 */

#include "ControlNetFile.h"

template <typename A> class QList;

namespace Isis {
  class ControlNetFileHeaderV0002;
  class ControlPointFileEntryV0002;
  class FileName;

  /**
   * @brief Handle Binary Control Network Files version 2
   *
   * We went to binary v2 in order to split up the protocol buffer messages
   *   by ControlPoint in order to completely avoid the maximum file size
   *   limitation (512MB before protocol buffers might fail).
   *
   * This version takes the separate 'log' and 'network' sections and combines
   *   them while simuntaneously splitting up the control points and network
   *   header. Please keep in mind you can play with optional keywords all
   *   day long without requiring a new binary control network version - this
   *   should be done only when necessary. Upgrading the Pvl version does NOT
   *   require having a new "ControlNetFile" child - simply handle that directly
   *   in the ControlNetVersioner.
   *
   * @author 2011-04-07 Steven Lambright
   *
   * @internal
   *   @history 2011-06-21 Steven Lambright - Files can have a larger size now
   *   @history 2012-11-30 Debbie A. Cook - Changed to use TProjection and RingPlaneProjection
   *                           instead of Projection.  References #775.
   *   @history 2013-05-22 Kimberly Oyama and Tracie Sucharski - Added the JIGSAWREJECTED
   *                           keyword to the toPvl() method. Fixes #661.
   *   @history 2016-04-22 Jeannie Backer - Removed thrown exception in
   *                           toPvl() when unable to find TargetRadii values. Instead, we
   *                           will just leave these values blank. References #3892
   *   @history 2017-12-07 Jesse Mapel - Changed how often read streams are recreated to avoid
   *                           protobuf errors. Fixes #5260.
   */
  class ControlNetFileV0002 : public ControlNetFile {
    public:
      ControlNetFileV0002();
      virtual ~ControlNetFileV0002();

      virtual void Read(const Pvl &header, const FileName &file);
      virtual void Write(const FileName &file) const;
      virtual Pvl toPvl() const;

      /**
       * Get the control network level information - things like NetworkID,
       *   TargetName, etc...
       *
       * "ControlNetFileHeaderV0002::pointmessagesizes" is only used for IO
       *   and you cannot assume it is populated.
       */
      ControlNetFileHeaderV0002 &GetNetworkHeader() {
        return *p_networkHeader;
      }

      /**
       * Get the control point data along with the log data.
       */
      QList<ControlPointFileEntryV0002> &GetNetworkPoints() {
        return *p_controlPoints;
      }

    private:
      //! This contains global cnet information...
      ControlNetFileHeaderV0002 *p_networkHeader;

      //! All of the control points
      QList<ControlPointFileEntryV0002> *p_controlPoints;
  };
}

#endif