USGS

Isis 3.0 Object Programmers' Reference

Home

SubArea.cpp
Go to the documentation of this file.
1 
23 #include <float.h>
24 #include <iostream>
25 #include <string>
26 #include <sstream>
27 
28 #include "SubArea.h"
29 #include "AlphaCube.h"
30 #include "Projection.h"
31 #include "ProjectionFactory.h"
32 #include "TProjection.h"
33 #include "IException.h"
34 #include "IString.h"
35 
36 using namespace std;
37 namespace Isis {
38 
76  void SubArea::SetSubArea(const int orignl, const int origns,
77  const int sl, const int ss, const int el,
78  const int es, const double linc, const double sinc) {
79 
80  // Save size of original image file
81  p_nl = orignl;
82  p_ns = origns;
83 
84  // Save the subarea information
85  p_sl = sl;
86  p_ss = ss;
87  p_el = el;
88  p_es = es;
89 
90  if(p_sl > p_el) {
91  string msg = "Invalid start/end line range [sl,el] specified for subarea";
92  throw IException(IException::Programmer, msg, _FILEINFO_);
93  }
94 
95  if(p_ss > p_es) {
96  string msg = "Invalid start/end sample range [ss,es] specified for subarea";
97  throw IException(IException::Programmer, msg, _FILEINFO_);
98  }
99 
100  p_linc = linc;
101  if(p_linc <= 0.0) {
102  string msg = "Invalid line increment [linc] specified for subarea";
103  throw IException(IException::Programmer, msg, _FILEINFO_);
104  }
105 
106  p_sinc = sinc;
107  if(p_sinc <= 0.0) {
108  string msg = "Invalid sample increment [sinc] specified for subarea";
109  throw IException(IException::Programmer, msg, _FILEINFO_);
110  }
111  }
112 
142  void SubArea::UpdateLabel(Cube *icube, Cube *ocube, PvlGroup &results) {
143 
144  Pvl inlabel = *icube->label();
145 
146  // If the linc and sinc are not equal, then the Instrument and
147  // Mapping groups are no longer valid.
148  if(p_linc != p_sinc) {
149  if(inlabel.findObject("IsisCube").hasGroup("Mapping")) {
150  inlabel.findObject("IsisCube").deleteGroup("Mapping");
151  results += PvlKeyword("MappingGroupDeleted", "True");
152 
153  // We don't want to think our projected cube is unprojected, so if we
154  // delete a mapping group and we have a camera there is a problem.
155  // Remove the camera.
156  if(inlabel.findObject("IsisCube").hasGroup("Instrument")) {
157  inlabel.findObject("IsisCube").deleteGroup("Instrument");
158  results += PvlKeyword("InstrumentGroupDeleted", "True");
159  }
160  }
161  }
162 
163  if(inlabel.findObject("IsisCube").hasGroup("Mapping")) {
164  // Update the upper left corner X,Y values if the starting line or
165  // starting sample are changed.
166  TProjection *proj = (TProjection *) icube->projection();
167  if(p_sl != 1 || p_ss != 1) {
168  proj->SetWorld(p_ss - 0.5, p_sl - 0.5);
169  PvlGroup &mapgroup = inlabel.findObject("IsisCube").findGroup("Mapping", Pvl::Traverse);
170  mapgroup.addKeyword(PvlKeyword("UpperLeftCornerX", toString(proj->XCoord())),
171  Pvl::Replace);
172  mapgroup.addKeyword(PvlKeyword("UpperLeftCornerY", toString(proj->YCoord())),
173  Pvl::Replace);
174  }
175 
176  // If the linc and sinc are not equal to 1, then update the
177  // mapping scale and resolution.
178  if(p_linc == p_sinc && p_linc != 1.0) {
179  PvlGroup &mapgroup = inlabel.findObject("IsisCube").findGroup("Mapping", Pvl::Traverse);
180  QString pixresUnit = mapgroup["PixelResolution"].unit();
181  double pixres = toDouble(mapgroup["PixelResolution"][0]);
182  mapgroup["PixelResolution"] = toString(pixres * p_linc);
183  mapgroup["PixelResolution"].setUnits(pixresUnit);
184  QString scaleUnit = mapgroup["Scale"].unit();
185  double scale = mapgroup["Scale"];
186  mapgroup["Scale"] = toString(scale / p_linc);
187  mapgroup["Scale"].setUnits(scaleUnit);
188  }
189 
190  // If the outer bounds of the image are changed, then the
191  // latitude,longitude range is no longer valid.
192  if(p_sl != 1 || p_ss != 1 || p_el != p_nl || p_es != p_ns) {
193  PvlGroup &mapgroup = inlabel.findObject("IsisCube").findGroup("Mapping", Pvl::Traverse);
194  if (proj->IsEquatorialCylindrical()) {
195  double minlat, maxlat;
196  double minlon, maxlon;
197  proj->SetWorld(p_ss-.5, p_sl-.5);
198  if (proj->IsGood()) {
199  maxlat = proj->UniversalLatitude();
200  if (proj->IsPlanetographic()) {
201  maxlat = proj->ToPlanetographic(maxlat);
202  }
203  if (proj->IsPositiveEast()) {
204  minlon = proj->UniversalLongitude();
205  if (proj->Has180Domain()) {
206  minlon = proj->To180Domain(minlon);
207  }
208  }
209  else {
210  minlon = proj->ToPositiveWest(proj->UniversalLongitude(), 360);
211  if (proj->Has180Domain()) {
212  minlon = proj->To180Domain(proj->ToPositiveWest(proj->UniversalLongitude(), 360));
213  }
214  }
215  proj->SetWorld(p_es+.5, p_el+.5);
216  if (proj->IsGood()) {
217  minlat = proj->UniversalLatitude();
218  if (proj->IsPlanetographic()) {
219  minlat = proj->ToPlanetographic(minlat);
220  }
221  if (proj->IsPositiveEast()) {
222  maxlon = proj->UniversalLongitude();
223  if (proj->Has180Domain()) {
224  maxlon = proj->To180Domain(maxlon);
225  }
226  }
227  else {
228  maxlon = proj->ToPositiveWest(proj->UniversalLongitude(), 360);
229  if (proj->Has180Domain()) {
230  maxlon = proj->To180Domain(proj->ToPositiveWest(proj->UniversalLongitude(), 360));
231  }
232  }
233  mapgroup.addKeyword(PvlKeyword("MinimumLatitude",toString(minlat)),Pvl::Replace);
234  mapgroup.addKeyword(PvlKeyword("MaximumLatitude",toString(maxlat)),Pvl::Replace);
235  mapgroup.addKeyword(PvlKeyword("MinimumLongitude",toString(minlon)),Pvl::Replace);
236  mapgroup.addKeyword(PvlKeyword("MaximumLongitude",toString(maxlon)),Pvl::Replace);
237  }
238  }
239  }
240  else {
241  if(mapgroup.hasKeyword("MinimumLatitude")) {
242  mapgroup.deleteKeyword("MinimumLatitude");
243  }
244  if(mapgroup.hasKeyword("MaximumLatitude")) {
245  mapgroup.deleteKeyword("MaximumLatitude");
246  }
247  if(mapgroup.hasKeyword("MinimumLongitude")) {
248  mapgroup.deleteKeyword("MinimumLongitude");
249  }
250  if(mapgroup.hasKeyword("MaximumLongitude")) {
251  mapgroup.deleteKeyword("MaximumLongitude");
252  }
253  }
254  }
255  }
256 
257  // Make changes to the output cube label
258  if(ocube->hasGroup("Instrument")) {
259  ocube->deleteGroup("Instrument");
260  }
261  if(inlabel.findObject("IsisCube").hasGroup("Instrument")) {
262  PvlGroup inst;
263  inst = inlabel.findObject("IsisCube").findGroup("Instrument");
264  ocube->putGroup(inst);
265  }
266 
267  if(ocube->hasGroup("Mapping")) {
268  ocube->deleteGroup("Mapping");
269  }
270  if(inlabel.findObject("IsisCube").hasGroup("Mapping")) {
271  PvlGroup mapgrp;
272  mapgrp = inlabel.findObject("IsisCube").findGroup("Mapping");
273  ocube->putGroup(mapgrp);
274  }
275 
276  // Update the AlphaCube group - this group will only be updated if
277  // a Mapping group does not exist in the labels.
278  AlphaCube aCube(p_ns, p_nl, ocube->sampleCount(), ocube->lineCount(),
279  p_ss - 0.5, p_sl - 0.5, p_es + 0.5, p_el + 0.5);
280  aCube.UpdateGroup(*ocube);
281  }
282 }