USGS

Isis 3.0 Object Programmers' Reference

Home

ControlNetFileV0001.cpp
1 #include "ControlNetFileV0001.h"
2 
3 #include <fstream>
4 
5 #include <google/protobuf/io/zero_copy_stream_impl.h>
6 #include <google/protobuf/io/coded_stream.h>
7 
8 #include "ControlNetFileV0001.pb.h"
10 #include "FileName.h"
11 #include "IException.h"
12 #include "Progress.h"
13 #include "Pvl.h"
14 #include "PvlGroup.h"
15 #include "PvlObject.h"
16 #include "PvlKeyword.h"
17 
18 using namespace google::protobuf;
19 using namespace google::protobuf::io;
20 using namespace std;
21 
22 namespace Isis {
23  ControlNetFileV0001::ControlNetFileV0001() {
24  p_network = new ControlNetFileProtoV0001;
25  p_logData = new ControlNetLogDataProtoV0001;
26  }
27 
28 
29  ControlNetFileV0001::~ControlNetFileV0001() {
30  delete p_network;
31  delete p_logData;
32  }
33 
34 
35  void ControlNetFileV0001::Read(const Pvl &head, const FileName &file) {
36  const PvlObject &protoBufferInfo = head.findObject("ProtoBuffer");
37  const PvlObject &protoBufferCore = protoBufferInfo.findObject("Core");
38 
39  BigInt coreStartPos = protoBufferCore["StartByte"];
40  BigInt coreLength = protoBufferCore["Bytes"];
41 
42  fstream input(file.expanded().toAscii().data(), ios::in | ios::binary);
43  if (!input.is_open()) {
44  IString msg = "Failed to open PB file" + file.name();
45  throw IException(IException::Programmer, msg, _FILEINFO_);
46  }
47 
48  input.seekg(coreStartPos, ios::beg);
49  IstreamInputStream inStream(&input);
50  CodedInputStream codedInStream(&inStream);
51  codedInStream.PushLimit(coreLength);
52  // max 512MB, warn at 400MB
53  codedInStream.SetTotalBytesLimit(1024 * 1024 * 512, 1024 * 1024 * 400);
54 
55  // Now stream the rest of the input into the google protocol buffer.
56  try {
57  if (!p_network->ParseFromCodedStream(&codedInStream)) {
58  IString msg = "Failed to read input PB file " + file.name();
59  throw IException(IException::Programmer, msg, _FILEINFO_);
60  }
61  }
62  catch (IException &e) {
63  string msg = "Cannot parse binary PB file";
64  throw IException(e, IException::User, msg, _FILEINFO_);
65  }
66  catch (...) {
67  string msg = "Cannot parse binary PB file";
68  throw IException(IException::User, msg, _FILEINFO_);
69  }
70 
71  const PvlObject &logDataInfo = protoBufferInfo.findObject("LogData");
72  BigInt logStartPos = logDataInfo["StartByte"];
73  BigInt logLength = logDataInfo["Bytes"];
74 
75  input.clear();
76  input.seekg(logStartPos, ios::beg);
77  IstreamInputStream logInStream(&input);
78  CodedInputStream codedLogInStream(&logInStream);
79  codedLogInStream.PushLimit(logLength);
80  // max 512MB, warn at 400MB
81  codedLogInStream.SetTotalBytesLimit(1024 * 1024 * 512, 1024 * 1024 * 400);
82 
83  // Now stream the rest of the input into the google protocol buffer.
84  try {
85  if (!p_logData->ParseFromCodedStream(&codedLogInStream)) {
86  IString msg = "Failed to read log data in PB file [" + file.name() + "]";
87  throw IException(IException::Programmer, msg, _FILEINFO_);
88  }
89  }
90  catch (...) {
91  string msg = "Cannot parse binary PB file's log data";
92  throw IException(IException::User, msg, _FILEINFO_);
93  }
94  }
95 
96 
97  Pvl ControlNetFileV0001::toPvl() const {
98  Pvl pvl;
99  pvl.addObject(PvlObject("ControlNetwork"));
100  PvlObject &network = pvl.findObject("ControlNetwork");
101 
102  network += PvlKeyword("NetworkId", p_network->networkid().c_str());
103  network += PvlKeyword("TargetName", p_network->targetname().c_str());
104  network += PvlKeyword("UserName", p_network->username().c_str());
105  network += PvlKeyword("Created", p_network->created().c_str());
106  network += PvlKeyword("LastModified", p_network->lastmodified().c_str());
107  network += PvlKeyword("Description", p_network->description().c_str());
108 
109  // This is the Pvl version we're converting to
110  network += PvlKeyword("Version", "1");
111 
112  for (int i = 0; i < p_network->points_size(); i++) {
113  const ControlNetFileProtoV0001_PBControlPoint &binaryPoint =
114  p_network->points(i);
115  PvlObject pvlPoint("ControlPoint");
116 
117  if(binaryPoint.type() == ControlNetFileProtoV0001_PBControlPoint::Ground)
118  pvlPoint += PvlKeyword("PointType", "Ground");
119  else
120  pvlPoint += PvlKeyword("PointType", "Tie");
121 
122  pvlPoint += PvlKeyword("PointId", binaryPoint.id().c_str());
123  pvlPoint += PvlKeyword("ChooserName", binaryPoint.choosername().c_str());
124  pvlPoint += PvlKeyword("DateTime", binaryPoint.datetime().c_str());
125 
126  if (binaryPoint.editlock()) {
127  pvlPoint += PvlKeyword("EditLock", "True");
128  }
129 
130  if (binaryPoint.ignore()) {
131  pvlPoint += PvlKeyword("Ignore", "True");
132  }
133 
134  switch (binaryPoint.apriorisurfpointsource()) {
135  case ControlNetFileProtoV0001_PBControlPoint::None:
136  break;
137  case ControlNetFileProtoV0001_PBControlPoint::User:
138  pvlPoint += PvlKeyword("AprioriXYZSource", "User");
139  break;
140  case ControlNetFileProtoV0001_PBControlPoint::AverageOfMeasures:
141  pvlPoint += PvlKeyword("AprioriXYZSource", "AverageOfMeasures");
142  break;
143  case ControlNetFileProtoV0001_PBControlPoint::Reference:
144  pvlPoint += PvlKeyword("AprioriXYZSource", "Reference");
145  break;
146  case ControlNetFileProtoV0001_PBControlPoint::Basemap:
147  pvlPoint += PvlKeyword("AprioriXYZSource", "Basemap");
148  break;
149  case ControlNetFileProtoV0001_PBControlPoint::BundleSolution:
150  pvlPoint += PvlKeyword("AprioriXYZSource", "BundleSolution");
151  break;
152  case ControlNetFileProtoV0001_PBControlPoint::Ellipsoid:
153  case ControlNetFileProtoV0001_PBControlPoint::DEM:
154  break;
155  }
156 
157  if (binaryPoint.has_apriorisurfpointsourcefile())
158  pvlPoint += PvlKeyword("AprioriXYZSourceFile",
159  binaryPoint.apriorisurfpointsourcefile().c_str());
160 
161  switch (binaryPoint.aprioriradiussource()) {
162  case ControlNetFileProtoV0001_PBControlPoint::None:
163  break;
164  case ControlNetFileProtoV0001_PBControlPoint::User:
165  pvlPoint += PvlKeyword("AprioriRadiusSource", "User");
166  break;
167  case ControlNetFileProtoV0001_PBControlPoint::AverageOfMeasures:
168  pvlPoint += PvlKeyword("AprioriRadiusSource", "AverageOfMeasures");
169  break;
170  case ControlNetFileProtoV0001_PBControlPoint::Reference:
171  pvlPoint += PvlKeyword("AprioriRadiusSource", "Reference");
172  break;
173  case ControlNetFileProtoV0001_PBControlPoint::Basemap:
174  pvlPoint += PvlKeyword("AprioriRadiusSource", "Basemap");
175  break;
176  case ControlNetFileProtoV0001_PBControlPoint::BundleSolution:
177  pvlPoint += PvlKeyword("AprioriRadiusSource", "BundleSolution");
178  break;
179  case ControlNetFileProtoV0001_PBControlPoint::Ellipsoid:
180  pvlPoint += PvlKeyword("AprioriRadiusSource", "Ellipsoid");
181  break;
182  case ControlNetFileProtoV0001_PBControlPoint::DEM:
183  pvlPoint += PvlKeyword("AprioriRadiusSource", "DEM");
184  break;
185  }
186 
187  if (binaryPoint.has_aprioriradiussourcefile())
188  pvlPoint += PvlKeyword("AprioriRadiusSourceFile",
189  binaryPoint.aprioriradiussourcefile().c_str());
190 
191  if(binaryPoint.has_apriorix()) {
192  pvlPoint += PvlKeyword("AprioriX", toString(binaryPoint.apriorix()), "meters");
193  pvlPoint += PvlKeyword("AprioriY", toString(binaryPoint.aprioriy()), "meters");
194  pvlPoint += PvlKeyword("AprioriZ", toString(binaryPoint.aprioriz()), "meters");
195 
196  if(binaryPoint.aprioricovar_size()) {
197  PvlKeyword matrix("AprioriCovarianceMatrix");
198  matrix += toString(binaryPoint.aprioricovar(0));
199  matrix += toString(binaryPoint.aprioricovar(1));
200  matrix += toString(binaryPoint.aprioricovar(2));
201  matrix += toString(binaryPoint.aprioricovar(3));
202  matrix += toString(binaryPoint.aprioricovar(4));
203  matrix += toString(binaryPoint.aprioricovar(5));
204  pvlPoint += matrix;
205  }
206  }
207 
208  if(binaryPoint.latitudeconstrained() &&
209  (binaryPoint.aprioricovar_size() || binaryPoint.adjustedcovar_size()))
210  pvlPoint += PvlKeyword("LatitudeConstrained", "True");
211 
212  if(binaryPoint.longitudeconstrained() &&
213  (binaryPoint.aprioricovar_size() || binaryPoint.adjustedcovar_size()))
214  pvlPoint += PvlKeyword("LongitudeConstrained", "True");
215 
216  if(binaryPoint.radiusconstrained() &&
217  (binaryPoint.aprioricovar_size() || binaryPoint.adjustedcovar_size()))
218  pvlPoint += PvlKeyword("RadiusConstrained", "True");
219 
220  if(binaryPoint.has_adjustedx()) {
221  pvlPoint += PvlKeyword("AdjustedX", toString(binaryPoint.adjustedx()), "meters");
222  pvlPoint += PvlKeyword("AdjustedY", toString(binaryPoint.adjustedy()), "meters");
223  pvlPoint += PvlKeyword("AdjustedZ", toString(binaryPoint.adjustedz()), "meters");
224 
225  if(binaryPoint.adjustedcovar_size()) {
226  PvlKeyword matrix("AdjustedCovarianceMatrix");
227  matrix += toString(binaryPoint.adjustedcovar(0));
228  matrix += toString(binaryPoint.adjustedcovar(1));
229  matrix += toString(binaryPoint.adjustedcovar(2));
230  matrix += toString(binaryPoint.adjustedcovar(3));
231  matrix += toString(binaryPoint.adjustedcovar(4));
232  matrix += toString(binaryPoint.adjustedcovar(5));
233  pvlPoint += matrix;
234  }
235  }
236 
237  for (int j = 0; j < binaryPoint.measures_size(); j++) {
238  PvlGroup pvlMeasure("ControlMeasure");
240  binaryMeasure = binaryPoint.measures(j);
241  pvlMeasure += PvlKeyword("SerialNumber", binaryMeasure.serialnumber().c_str());
242 
243  switch(binaryMeasure.type()) {
244  case ControlNetFileProtoV0001_PBControlPoint_PBControlMeasure_MeasureType_Candidate:
245  pvlMeasure += PvlKeyword("MeasureType", "Candidate");
246  break;
247  case ControlNetFileProtoV0001_PBControlPoint_PBControlMeasure_MeasureType_Manual:
248  pvlMeasure += PvlKeyword("MeasureType", "Manual");
249  break;
250  case ControlNetFileProtoV0001_PBControlPoint_PBControlMeasure_MeasureType_RegisteredPixel:
251  pvlMeasure += PvlKeyword("MeasureType", "RegisteredPixel");
252  break;
253  case ControlNetFileProtoV0001_PBControlPoint_PBControlMeasure_MeasureType_RegisteredSubPixel:
254  pvlMeasure += PvlKeyword("MeasureType", "RegisteredSubPixel");
255  break;
256  }
257 
258  pvlMeasure += PvlKeyword("ChooserName", binaryMeasure.choosername().c_str());
259  pvlMeasure += PvlKeyword("DateTime", binaryMeasure.datetime().c_str());
260 
261  if(binaryMeasure.editlock())
262  pvlMeasure += PvlKeyword("EditLock", "True");
263 
264  if(binaryMeasure.ignore())
265  pvlMeasure += PvlKeyword("Ignore", "True");
266 
267  if(binaryMeasure.has_measurement()) {
268  pvlMeasure += PvlKeyword("Sample", toString(binaryMeasure.measurement().sample()));
269  pvlMeasure += PvlKeyword("Line", toString(binaryMeasure.measurement().line()));
270 
271  if (binaryMeasure.measurement().has_sampleresidual())
272  pvlMeasure += PvlKeyword("SampleResidual",
273  toString(binaryMeasure.measurement().sampleresidual()), "pixels");
274 
275  if (binaryMeasure.measurement().has_lineresidual())
276  pvlMeasure += PvlKeyword("LineResidual",
277  toString(binaryMeasure.measurement().lineresidual()), "pixels");
278  }
279 
280  if (binaryMeasure.has_diameter())
281  pvlMeasure += PvlKeyword("Diameter", toString(binaryMeasure.diameter()));
282 
283  if (binaryMeasure.has_apriorisample())
284  pvlMeasure += PvlKeyword("AprioriSample", toString(binaryMeasure.apriorisample()));
285 
286  if (binaryMeasure.has_aprioriline())
287  pvlMeasure += PvlKeyword("AprioriLine", toString(binaryMeasure.aprioriline()));
288 
289  if (binaryMeasure.has_samplesigma())
290  pvlMeasure += PvlKeyword("SampleSigma", toString(binaryMeasure.samplesigma()));
291 
292  if (binaryMeasure.has_samplesigma())
293  pvlMeasure += PvlKeyword("LineSigma", toString(binaryMeasure.linesigma()));
294 
295  for (int logEntry = 0;
296  logEntry <
297  p_logData->points(i).measures(j).loggedmeasuredata_size();
298  logEntry ++) {
300  p_logData->points(i).measures(j).loggedmeasuredata(logEntry);
301 
302  try {
303  ControlMeasureLogData interpreter(log);
304  pvlMeasure += interpreter.ToKeyword();
305  }
306  catch(IException &) {
307  }
308  }
309 
310  if(binaryPoint.has_referenceindex() &&
311  binaryPoint.referenceindex() == j)
312  pvlMeasure += PvlKeyword("Reference", "True");
313 
314  pvlPoint.addGroup(pvlMeasure);
315  }
316 
317  network.addObject(pvlPoint);
318  }
319 
320  return pvl;
321  }
322 }