5 #include <google/protobuf/io/zero_copy_stream_impl.h>
6 #include <google/protobuf/io/coded_stream.h>
7 #include <boost/numeric/ublas/symmetric.hpp>
8 #include <boost/numeric/ublas/io.hpp>
14 #include "ControlNetFileV0002.pb.h"
24 using namespace google::protobuf;
25 using namespace google::protobuf::io;
26 using boost::numeric::ublas::symmetric_matrix;
27 using boost::numeric::ublas::upper;
31 ControlNetFileV0002::ControlNetFileV0002() {
32 p_networkHeader =
new ControlNetFileHeaderV0002;
37 ControlNetFileV0002::~ControlNetFileV0002() {
38 delete p_networkHeader;
39 delete p_controlPoints;
52 void ControlNetFileV0002::Read(
const Pvl &header,
const FileName &file) {
56 BigInt headerStartPos = protoBufferCore[
"HeaderStartByte"];
57 BigInt headerLength = protoBufferCore[
"HeaderBytes"];
59 fstream input(file.expanded().toLatin1().data(), ios::in | ios::binary);
60 if (!input.is_open()) {
61 IString msg =
"Failed to open control network file" + file.name();
65 input.seekg(headerStartPos, ios::beg);
66 streampos filePos = input.tellg();
67 IstreamInputStream headerInStream(&input);
68 CodedInputStream headerCodedInStream(&headerInStream);
70 headerCodedInStream.SetTotalBytesLimit(1024 * 1024 * 512,
75 filePos += headerLength;
76 int oldLimit = headerCodedInStream.PushLimit(headerLength);
77 if (!p_networkHeader->ParseFromCodedStream(&headerCodedInStream)) {
78 IString msg =
"Failed to read input control net file [" +
82 headerCodedInStream.PopLimit(oldLimit);
89 IstreamInputStream *pointInStream = NULL;
90 CodedInputStream *pointCodedInStream = NULL;
92 for (
int cp = 0; cp < p_networkHeader->pointmessagesizes_size(); cp ++) {
93 if (cp % 50000 == 0 && pointCodedInStream && pointInStream) {
94 delete pointCodedInStream;
95 pointCodedInStream = NULL;
101 if (pointInStream == NULL) {
103 input.open(file.expanded().toLatin1().data(), ios::in | ios::binary);
104 input.seekg(filePos, ios::beg);
106 pointInStream =
new IstreamInputStream(&input);
107 pointCodedInStream =
new CodedInputStream(pointInStream);
109 pointCodedInStream->SetTotalBytesLimit(1024 * 1024 * 512,
113 int size = p_networkHeader->pointmessagesizes(cp);
114 oldLimit = pointCodedInStream->PushLimit(size);
118 newPoint.ParseFromCodedStream(pointCodedInStream);
120 if (newPoint.type() == ControlPointFileEntryV0002::obsolete_Tie ||
121 newPoint.type() == ControlPointFileEntryV0002::obsolete_Ground) {
122 if (newPoint.aprioricovar_size())
123 newPoint.set_type(ControlPointFileEntryV0002::Constrained);
126 p_controlPoints->append(newPoint);
127 pointCodedInStream->PopLimit(oldLimit);
130 if (pointCodedInStream) {
131 delete pointCodedInStream;
132 pointCodedInStream = NULL;
136 delete pointInStream;
137 pointInStream = NULL;
141 string msg =
"Cannot understand binary PB file";
146 void ControlNetFileV0002::Write(
const FileName &file)
const {
148 p_networkHeader->clear_pointmessagesizes();
149 BigInt pointsSize = 0;
150 BigInt numMeasures = 0;
151 for (
int cpIndex = 0; cpIndex < p_controlPoints->size(); cpIndex ++) {
152 numMeasures += p_controlPoints->at(cpIndex).measures_size();
153 int size = p_controlPoints->at(cpIndex).ByteSize();
155 p_networkHeader->add_pointmessagesizes(size);
158 streampos coreHeaderSize = p_networkHeader->ByteSize();
160 const int labelBytes = 65536;
161 fstream output(file.expanded().toLatin1().data(),
162 ios::out | ios::trunc | ios::binary);
164 char *blankLabel =
new char[labelBytes];
165 memset(blankLabel, 0, labelBytes);
166 output.write(blankLabel, labelBytes);
167 delete [] blankLabel;
169 streampos startCoreHeaderPos = output.tellp();
171 if (!p_networkHeader->SerializeToOstream(&output)) {
172 IString msg =
"Failed to write output control network file [" +
177 streampos curPosition = startCoreHeaderPos + coreHeaderSize;
178 for (
int cpIndex = 0; cpIndex < p_controlPoints->size(); cpIndex ++) {
179 if (!p_controlPoints->at(cpIndex).IsInitialized()) {
180 IString msg =
"Failed to write output control network file [" +
181 file.name() +
"] because control points are missing required "
186 if (!p_controlPoints->at(cpIndex).SerializeToOstream(&output)) {
187 IString msg =
"Failed to write output control network file [" +
188 file.name() +
"] while attempting to write control points";
192 curPosition += p_controlPoints->at(cpIndex).ByteSize();
200 toString((BigInt) startCoreHeaderPos)));
203 toString((BigInt) ( startCoreHeaderPos + coreHeaderSize))));
208 PvlGroup netInfo(
"ControlNetworkInfo");
209 netInfo.addComment(
"This group is for informational purposes only");
210 netInfo +=
PvlKeyword(
"NetworkId", p_networkHeader->networkid().c_str());
211 netInfo +=
PvlKeyword(
"TargetName", p_networkHeader->targetname().c_str());
212 netInfo +=
PvlKeyword(
"UserName", p_networkHeader->username().c_str());
213 netInfo +=
PvlKeyword(
"Created", p_networkHeader->created().c_str());
214 netInfo +=
PvlKeyword(
"LastModified", p_networkHeader->lastmodified().c_str());
215 netInfo +=
PvlKeyword(
"Description", p_networkHeader->description().c_str());
223 output.seekp(0, ios::beg);
246 Pvl ControlNetFileV0002::toPvl()
const {
251 network +=
PvlKeyword(
"NetworkId", p_networkHeader->networkid().c_str());
252 network +=
PvlKeyword(
"TargetName", p_networkHeader->targetname().c_str());
253 network +=
PvlKeyword(
"UserName", p_networkHeader->username().c_str());
254 network +=
PvlKeyword(
"Created", p_networkHeader->created().c_str());
255 network +=
PvlKeyword(
"LastModified", p_networkHeader->lastmodified().c_str());
256 network +=
PvlKeyword(
"Description", p_networkHeader->description().c_str());
263 QString target = (QString)network.findKeyword(
"TargetName",Pvl::Traverse);
266 NaifStatus::CheckErrors();
267 pvlRadii = Target::radiiGroup(target);
275 foreach(binaryPoint, *p_controlPoints) {
278 if (binaryPoint.type() == ControlPointFileEntryV0002::Fixed) {
281 else if (binaryPoint.type() == ControlPointFileEntryV0002::Constrained) {
282 pvlPoint +=
PvlKeyword(
"PointType",
"Constrained");
288 pvlPoint +=
PvlKeyword(
"PointId", binaryPoint.id().c_str());
289 pvlPoint +=
PvlKeyword(
"ChooserName", binaryPoint.choosername().c_str());
290 pvlPoint +=
PvlKeyword(
"DateTime", binaryPoint.datetime().c_str());
292 if (binaryPoint.editlock()) {
296 if (binaryPoint.ignore()) {
300 switch (binaryPoint.apriorisurfpointsource()) {
301 case ControlPointFileEntryV0002::None:
303 case ControlPointFileEntryV0002::User:
304 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"User");
306 case ControlPointFileEntryV0002::AverageOfMeasures:
307 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"AverageOfMeasures");
309 case ControlPointFileEntryV0002::Reference:
310 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"Reference");
312 case ControlPointFileEntryV0002::Basemap:
313 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"Basemap");
315 case ControlPointFileEntryV0002::BundleSolution:
316 pvlPoint +=
PvlKeyword(
"AprioriXYZSource",
"BundleSolution");
318 case ControlPointFileEntryV0002::Ellipsoid:
319 case ControlPointFileEntryV0002::DEM:
323 if (binaryPoint.has_apriorisurfpointsourcefile())
324 pvlPoint +=
PvlKeyword(
"AprioriXYZSourceFile",
325 binaryPoint.apriorisurfpointsourcefile().c_str());
327 switch (binaryPoint.aprioriradiussource()) {
328 case ControlPointFileEntryV0002::None:
330 case ControlPointFileEntryV0002::User:
331 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"User");
333 case ControlPointFileEntryV0002::AverageOfMeasures:
334 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"AverageOfMeasures");
336 case ControlPointFileEntryV0002::Reference:
337 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"Reference");
339 case ControlPointFileEntryV0002::Basemap:
340 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"Basemap");
342 case ControlPointFileEntryV0002::BundleSolution:
343 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"BundleSolution");
345 case ControlPointFileEntryV0002::Ellipsoid:
346 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"Ellipsoid");
348 case ControlPointFileEntryV0002::DEM:
349 pvlPoint +=
PvlKeyword(
"AprioriRadiusSource",
"DEM");
353 if (binaryPoint.has_aprioriradiussourcefile())
354 pvlPoint +=
PvlKeyword(
"AprioriRadiusSourceFile",
355 binaryPoint.aprioriradiussourcefile().c_str());
357 if (binaryPoint.has_apriorix()) {
365 Displacement(binaryPoint.apriorix(),Displacement::Meters),
366 Displacement(binaryPoint.aprioriy(),Displacement::Meters),
367 Displacement(binaryPoint.aprioriz(),Displacement::Meters));
368 pvlPoint.findKeyword(
"AprioriX").
addComment(
"AprioriLatitude = " +
371 pvlPoint.findKeyword(
"AprioriY").
addComment(
"AprioriLongitude = " +
374 pvlPoint.findKeyword(
"AprioriZ").
addComment(
"AprioriRadius = " +
378 if (binaryPoint.aprioricovar_size()) {
380 matrix +=
toString(binaryPoint.aprioricovar(0));
381 matrix +=
toString(binaryPoint.aprioricovar(1));
382 matrix +=
toString(binaryPoint.aprioricovar(2));
383 matrix +=
toString(binaryPoint.aprioricovar(3));
384 matrix +=
toString(binaryPoint.aprioricovar(4));
385 matrix +=
toString(binaryPoint.aprioricovar(5));
388 if (pvlRadii.
hasKeyword(
"EquatorialRadius")) {
390 Distance(pvlRadii[
"EquatorialRadius"],Distance::Meters),
391 Distance(pvlRadii[
"EquatorialRadius"],Distance::Meters),
392 Distance(pvlRadii[
"PolarRadius"],Distance::Meters));
393 symmetric_matrix<double, upper> covar;
396 covar(0, 0) = binaryPoint.aprioricovar(0);
397 covar(0, 1) = binaryPoint.aprioricovar(1);
398 covar(0, 2) = binaryPoint.aprioricovar(2);
399 covar(1, 1) = binaryPoint.aprioricovar(3);
400 covar(1, 2) = binaryPoint.aprioricovar(4);
401 covar(2, 2) = binaryPoint.aprioricovar(5);
403 QString sigmas =
"AprioriLatitudeSigma = " +
405 " <meters> AprioriLongitudeSigma = " +
407 " <meters> AprioriRadiusSigma = " +
410 pvlPoint.findKeyword(
"AprioriCovarianceMatrix").addComment(sigmas);
415 if (binaryPoint.latitudeconstrained())
416 pvlPoint +=
PvlKeyword(
"LatitudeConstrained",
"True");
418 if (binaryPoint.longitudeconstrained())
419 pvlPoint +=
PvlKeyword(
"LongitudeConstrained",
"True");
421 if (binaryPoint.radiusconstrained())
422 pvlPoint +=
PvlKeyword(
"RadiusConstrained",
"True");
424 if (binaryPoint.has_adjustedx()) {
432 Displacement(binaryPoint.adjustedx(),Displacement::Meters),
433 Displacement(binaryPoint.adjustedy(),Displacement::Meters),
434 Displacement(binaryPoint.adjustedz(),Displacement::Meters));
435 pvlPoint.findKeyword(
"AdjustedX").
addComment(
"AdjustedLatitude = " +
438 pvlPoint.findKeyword(
"AdjustedY").
addComment(
"AdjustedLongitude = " +
441 pvlPoint.findKeyword(
"AdjustedZ").
addComment(
"AdjustedRadius = " +
445 if (binaryPoint.adjustedcovar_size()) {
446 PvlKeyword matrix(
"AdjustedCovarianceMatrix");
447 matrix +=
toString(binaryPoint.adjustedcovar(0));
448 matrix +=
toString(binaryPoint.adjustedcovar(1));
449 matrix +=
toString(binaryPoint.adjustedcovar(2));
450 matrix +=
toString(binaryPoint.adjustedcovar(3));
451 matrix +=
toString(binaryPoint.adjustedcovar(4));
452 matrix +=
toString(binaryPoint.adjustedcovar(5));
455 if (pvlRadii.
hasKeyword(
"EquatorialRadius")) {
457 Distance(pvlRadii[
"EquatorialRadius"],Distance::Meters),
458 Distance(pvlRadii[
"EquatorialRadius"],Distance::Meters),
459 Distance(pvlRadii[
"PolarRadius"],Distance::Meters));
460 symmetric_matrix<double, upper> covar;
463 covar(0, 0) = binaryPoint.adjustedcovar(0);
464 covar(0, 1) = binaryPoint.adjustedcovar(1);
465 covar(0, 2) = binaryPoint.adjustedcovar(2);
466 covar(1, 1) = binaryPoint.adjustedcovar(3);
467 covar(1, 2) = binaryPoint.adjustedcovar(4);
468 covar(2, 2) = binaryPoint.adjustedcovar(5);
470 QString sigmas =
"AdjustedLatitudeSigma = " +
472 " <meters> AdjustedLongitudeSigma = " +
474 " <meters> AdjustedRadiusSigma = " +
477 pvlPoint.findKeyword(
"AdjustedCovarianceMatrix").addComment(sigmas);
482 for (
int j = 0; j < binaryPoint.measures_size(); j++) {
483 PvlGroup pvlMeasure(
"ControlMeasure");
485 binaryMeasure = binaryPoint.measures(j);
486 pvlMeasure +=
PvlKeyword(
"SerialNumber", binaryMeasure.serialnumber().c_str());
488 switch(binaryMeasure.type()) {
489 case ControlPointFileEntryV0002_Measure_MeasureType_Candidate:
490 pvlMeasure +=
PvlKeyword(
"MeasureType",
"Candidate");
492 case ControlPointFileEntryV0002_Measure_MeasureType_Manual:
493 pvlMeasure +=
PvlKeyword(
"MeasureType",
"Manual");
495 case ControlPointFileEntryV0002_Measure_MeasureType_RegisteredPixel:
496 pvlMeasure +=
PvlKeyword(
"MeasureType",
"RegisteredPixel");
498 case ControlPointFileEntryV0002_Measure_MeasureType_RegisteredSubPixel:
499 pvlMeasure +=
PvlKeyword(
"MeasureType",
"RegisteredSubPixel");
503 if (binaryMeasure.has_choosername())
504 pvlMeasure +=
PvlKeyword(
"ChooserName", binaryMeasure.choosername().c_str());
506 if (binaryMeasure.has_datetime())
507 pvlMeasure +=
PvlKeyword(
"DateTime", binaryMeasure.datetime().c_str());
509 if (binaryMeasure.editlock())
512 if (binaryMeasure.ignore())
515 if (binaryMeasure.has_sample())
518 if (binaryMeasure.has_line())
521 if (binaryMeasure.has_diameter())
524 if (binaryMeasure.has_apriorisample())
527 if (binaryMeasure.has_aprioriline())
530 if (binaryMeasure.has_samplesigma())
534 if (binaryMeasure.has_samplesigma())
538 if (binaryMeasure.has_sampleresidual())
542 if (binaryMeasure.has_lineresidual())
546 if (binaryMeasure.has_jigsawrejected()) {
550 for (
int logEntry = 0;
551 logEntry < binaryMeasure.log_size();
554 binaryMeasure.log(logEntry);
560 if (binaryPoint.has_referenceindex() &&
561 binaryPoint.referenceindex() == j)
562 pvlMeasure +=
PvlKeyword(
"Reference",
"True");
564 pvlPoint.addGroup(pvlMeasure);
567 network.addObject(pvlPoint);
This class defines a body-fixed surface point.
double degrees() const
Get the angle in units of Degrees.
File name manipulation and expansion.
Distance GetLocalRadius() const
Return the radius of the surface point.
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Statistical and similar ControlMeasure associated information.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
void SetRectangularMatrix(const boost::numeric::ublas::symmetric_matrix< double, boost::numeric::ublas::upper > &covar)
Set rectangular covariance matrix.
Distance measurement, usually in meters.
Longitude GetLongitude() const
Return the body-fixed longitude for the surface point.
void SetRadii(const Distance &majorRadius, const Distance &minorRadius, const Distance &polarRadius)
Reset the radii of the surface body of the surface point.
Distance GetLonSigmaDistance() const
Return the longiitude sigma in meters.
void addObject(const PvlObject &object)
Add a PvlObject.
void addComment(QString comment)
Add a comment to the PvlKeyword.
double meters() const
Get the distance in meters.
Latitude GetLatitude() const
Return the body-fixed latitude for the surface point.
Contains multiple PvlContainers.
#define _FILEINFO_
Macro for the filename and line number.
A single keyword-value pair.
Container for cube-like labels.
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
Displacement is a signed length, usually in meters.
Distance GetLatSigmaDistance() const
Return the latitude sigma in meters.
Adds specific functionality to C++ strings.
Contains Pvl Groups and Pvl Objects.
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
PvlKeyword ToKeyword() const
This converts the log data to a PvlKeyword.
void SetRectangular(const Displacement &x, const Displacement &y, const Displacement &z, const Distance &xSigma=Distance(), const Distance &ySigma=Distance(), const Distance &zSigma=Distance())
Set surface point in rectangular body-fixed coordinates wtih optional sigmas.
Unless noted otherwise, the portions of Isis written by the USGS are public domain.