Isis 3 Programmer Reference
ProcessGroundPolygons.cpp
1 #include "ProcessGroundPolygons.h"
2 
3 #include <vector>
4 
5 #include "geos/geom/CoordinateSequence.h"
6 #include "geos/geom/CoordinateArraySequence.h"
7 #include "geos/geom/LineString.h"
8 #include "geos/geosAlgorithm.h"
9 
10 #include "Application.h"
11 #include "BoxcarCachingAlgorithm.h"
12 #include "IException.h"
13 #include "PolygonTools.h"
14 #include "Projection.h"
15 
16 using namespace std;
17 namespace Isis {
18 
19  ProcessGroundPolygons::ProcessGroundPolygons() {
20  p_groundMap = NULL;
21  }
22 
23 
34  void ProcessGroundPolygons::Rasterize(std::vector<double> &lat,
35  std::vector<double> &lon,
36  std::vector<double> &values) {
37 
38  // Decide if we need to split the poly on the 360 boundry
39  // Yes, we can do this better. The lat and lon vectors should be passed in as polygons, but
40  // for now this is reusing older code.
41  bool crosses = false;
42  for (unsigned int i = 0; i < lon.size() - 1; i++) {
43  if (fabs(lon[i] - lon[i+1]) > 180.0) {
44  crosses = true;
45  break;
46  }
47  }
48 
49  if (crosses) {
50  // Make a polygon from the lat/lon vectors and split it on 360
51  geos::geom::CoordinateSequence *pts = new geos::geom::CoordinateArraySequence();
52  for (unsigned int i = 0; i < lat.size(); i++) {
53  pts->add(geos::geom::Coordinate(lon[i], lat[i]));
54  }
55  pts->add(geos::geom::Coordinate(lon[0], lat[0]));
56 
57  geos::geom::Polygon *crossingPoly = Isis::globalFactory.createPolygon(
58  globalFactory.createLinearRing(pts), NULL);
59 
60  geos::geom::MultiPolygon *splitPoly = NULL;
61  try {
62  splitPoly = PolygonTools::SplitPolygonOn360(crossingPoly);
63  }
64  // Ignore any pixel footprints that could not be split. This should only be pixels
65  // that contain the pole.
66  catch (IException &) {
67  // See leading comment
68  }
69 
70  delete crossingPoly;
71 
72  if (splitPoly != NULL) {
73  // Process the polygons in the split multipolygon as if we were still using the lat/lon vectors
74  for (unsigned int g = 0; g < splitPoly->getNumGeometries(); ++g) {
75  const geos::geom::Polygon *poly =
76  dynamic_cast<const geos::geom::Polygon *>(splitPoly->getGeometryN(g));
77 
78  geos::geom::CoordinateSequence *llcoords = poly->getExteriorRing()->getCoordinates();
79 
80  // Move each coordinate in the exterior ring of this lat/lon polygon to vectors
81  // Ignore any holes in the polygon
82  std::vector<double> tlat;
83  std::vector<double> tlon;
84  for (unsigned int cord = 0; cord < llcoords->getSize() - 1; ++cord) {
85  tlon.push_back(llcoords->getAt(cord).x);
86  tlat.push_back(llcoords->getAt(cord).y);
87  }
88 
89  Convert(tlat, tlon);
90  ProcessPolygons::Rasterize(p_samples, p_lines, values);
91  }
92  delete splitPoly;
93  }
94  }
95  else {
96  Convert(lat, lon);
97  ProcessPolygons::Rasterize(p_samples, p_lines, values);
98  }
99  }
100 
101 
112  void ProcessGroundPolygons::Rasterize(std::vector<double> &lat,
113  std::vector<double> &lon,
114  int &band, double &value) {
115 
116  Convert(lat, lon);
117  ProcessPolygons::Rasterize(p_samples, p_lines, band, value);
118  }
119 
120 
128  void ProcessGroundPolygons::Convert(std::vector<double> &lat,
129  std::vector<double> &lon) {
130 
131  p_samples.clear();
132  p_lines.clear();
133 
134  for (unsigned int i = 0; i < lat.size(); i++) {
135  if (p_groundMap->SetUniversalGround(lat[i], lon[i])) {
136  p_samples.push_back(p_groundMap->Sample());
137  p_lines.push_back(p_groundMap->Line());
138  }
139  }
140  }
141 
142 
150  void ProcessGroundPolygons::EndProcess() {
151 
152  if(p_groundMap != NULL) {
153  delete p_groundMap;
154  }
155 
156  ProcessPolygons::EndProcess();
157  }
158 
159 
166  void ProcessGroundPolygons::Finalize() {
167 
168  if(p_groundMap != NULL) {
169  delete p_groundMap;
170  }
171 
172  ProcessPolygons::Finalize();
173  }
174 
175 
183  void ProcessGroundPolygons::AppendOutputCube(QString &cubeStr,
184  const QString &avgFileName,
185  const QString &countFileName) {
186  /*We need a ground map for converting lat/long to line/sample see Convert()*/
187  Cube cube(cubeStr, "r");
188  p_groundMap = new UniversalGroundMap(cube);
189  ProcessPolygons::AppendOutputCube(avgFileName, countFileName);
190 
191  }
192 
193 
203  void ProcessGroundPolygons::SetStatCubes(const QString &avgFileName,
204  const QString &countFileName,
205  Isis::CubeAttributeOutput &outAtts,
206  QString &cubeStr) {
207  /*We need a ground map for converting lat/long to line/sample see Convert()*/
208  Cube cube(cubeStr);
209  p_groundMap = new UniversalGroundMap(cube);
210 
211  /*setup input cube to transfer projection or camera labels*/
212  CubeAttributeInput inAtts;
213  Isis::Process::SetInputCube(cubeStr, inAtts, 0);
214  int nBands = this->InputCubes[0]->bandCount();
215  int nLines = this->InputCubes[0]->lineCount();
216  int nSamples = this->InputCubes[0]->sampleCount();
217 
218  this->Process::SetOutputCube(avgFileName, outAtts, nSamples, nLines, nBands);
219  this->Process::SetOutputCube(countFileName, outAtts, nSamples, nLines, nBands);
220 
221  OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
222  OutputCubes[1]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
223 
224  ClearInputCubes();
225 
226  }
227 
228 
238  void ProcessGroundPolygons::SetStatCubes(const QString &parameter,
239  QString &cube) {
240 
241  QString avgString =
242  Application::GetUserInterface().GetFileName(parameter);
243  CubeAttributeOutput atts =
244  Application::GetUserInterface().GetOutputAttribute(parameter);
245 
246  FileName file(avgString);
247  QString path = file.path();
248  QString filename = file.baseName();
249  QString countString = path + "/" + filename + "-count-";
250 
251  SetStatCubes(avgString, countString, atts, cube);
252 
253  }
254 
255 
263  void ProcessGroundPolygons::SetStatCubes(const QString &parameter,
264  Isis::Pvl &map, int bands) {
265 
266  QString avgString =
267  Application::GetUserInterface().GetFileName(parameter);
268  CubeAttributeOutput atts =
269  Application::GetUserInterface().GetOutputAttribute(parameter);
270 
271  FileName file(avgString);
272  QString path = file.path();
273  QString filename = file.baseName();
274  QString countString = path + "/" + filename + "-count-";
275 
276  SetStatCubes(avgString, countString, atts, map, bands);
277 
278  }
279 
280 
295  void ProcessGroundPolygons::SetStatCubes(const QString &avgFileName,
296  const QString &countFileName,
298  Isis::Pvl &map, int bands) {
299  int samples, lines;
300 
301  Projection *proj = ProjectionFactory::CreateForCube(map, samples, lines,
302  false);
303 
304  this->ProcessPolygons::SetStatCubes(avgFileName, countFileName, atts,
305  samples, lines, bands);
306 
307  OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
308  OutputCubes[1]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
309 
310  /*Write the pvl group to the cube files.*/
311 
312  PvlGroup group = map.findGroup("Mapping", Pvl::Traverse);
313 
314  OutputCubes[0]->putGroup(group);
315  OutputCubes[1]->putGroup(group);
316 
317  // If there is an alpha cube in the label passed, attach to output cubes
318  if (map.hasGroup("AlphaCube")) {
319  PvlGroup alpha = map.findGroup("AlphaCube", Pvl::Traverse);
320  OutputCubes[0]->putGroup(alpha);
321  OutputCubes[1]->putGroup(alpha);
322  }
323 
324  /*We need a ground map for converting lat/long to line/sample see Convert()*/
325  p_groundMap = new UniversalGroundMap(*OutputCubes[0]);
326 
327  delete proj;
328  }
329 
330 } /* end namespace isis*/
331 
Manipulate and parse attributes of input cube filenames.
QString path() const
Returns the path of the file name.
Definition: FileName.cpp:119
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
File name manipulation and expansion.
Definition: FileName.h:116
Universal Ground Map.
Namespace for the standard library.
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition: PvlObject.h:222
This algorithm is designed for applications that jump around between a couple of spots in the cube wi...
Base class for Map Projections.
Definition: Projection.h:171
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
virtual Isis::Cube * SetInputCube(const QString &parameter, const int requirements=0)
Opens an input cube specified by the user and verifies requirements are met.
Definition: Process.cpp:243
Manipulate and parse attributes of output cube filenames.
Container for cube-like labels.
Definition: Pvl.h:135
QString baseName() const
Returns the name of the file without the path and without extensions.
Definition: FileName.cpp:161
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
IO Handler for Isis Cubes.
Definition: Cube.h:170