Isis 3 Programmer Reference
ProcessGroundPolygons.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "ProcessGroundPolygons.h"
8
9#include <vector>
10
11#include "geos/geom/CoordinateSequence.h"
12#include "geos/geom/LineString.h"
13
14#include "Application.h"
15#include "BoxcarCachingAlgorithm.h"
16#include "IException.h"
17#include "PolygonTools.h"
18#include "Projection.h"
19
20using namespace std;
21namespace Isis {
22
23 ProcessGroundPolygons::ProcessGroundPolygons() {
24 p_groundMap = NULL;
25 }
26
27
38 void ProcessGroundPolygons::Rasterize(std::vector<double> &lat,
39 std::vector<double> &lon,
40 std::vector<double> &values) {
41
42 // Decide if we need to split the poly on the 360 boundry
43 // Yes, we can do this better. The lat and lon vectors should be passed in as polygons, but
44 // for now this is reusing older code.
45 bool crosses = false;
46 for (unsigned int i = 0; i < lon.size() - 1; i++) {
47 if (fabs(lon[i] - lon[i+1]) > 180.0) {
48 crosses = true;
49 break;
50 }
51 }
52
53 if (crosses) {
54 // Make a polygon from the lat/lon vectors and split it on 360
55 geos::geom::CoordinateSequence pts;
56 for (unsigned int i = 0; i < lat.size(); i++) {
57 pts.add(geos::geom::Coordinate(lon[i], lat[i]));
58 }
59 pts.add(geos::geom::Coordinate(lon[0], lat[0]));
60
61 geos::geom::Polygon *crossingPoly = Isis::globalFactory->createPolygon(
62 globalFactory->createLinearRing(pts)).release();
63
64 geos::geom::MultiPolygon *splitPoly = NULL;
65 try {
66 splitPoly = PolygonTools::SplitPolygonOn360(crossingPoly);
67 }
68 // Ignore any pixel footprints that could not be split. This should only be pixels
69 // that contain the pole.
70 catch (IException &) {
71 // See leading comment
72 }
73
74 delete crossingPoly;
75
76 if (splitPoly != NULL) {
77 // Process the polygons in the split multipolygon as if we were still using the lat/lon vectors
78 for (unsigned int g = 0; g < splitPoly->getNumGeometries(); ++g) {
79 const geos::geom::Polygon *poly =
80 dynamic_cast<const geos::geom::Polygon *>(splitPoly->getGeometryN(g));
81
82 geos::geom::CoordinateSequence *llcoords =
83 poly->getExteriorRing()->getCoordinates().release();
84
85 // Move each coordinate in the exterior ring of this lat/lon polygon to vectors
86 // Ignore any holes in the polygon
87 std::vector<double> tlat;
88 std::vector<double> tlon;
89 for (unsigned int cord = 0; cord < llcoords->getSize() - 1; ++cord) {
90 tlon.push_back(llcoords->getAt(cord).x);
91 tlat.push_back(llcoords->getAt(cord).y);
92 }
93
94 Convert(tlat, tlon);
95 ProcessPolygons::Rasterize(p_samples, p_lines, values);
96 }
97 delete splitPoly;
98 }
99 }
100 else {
101 Convert(lat, lon);
102 ProcessPolygons::Rasterize(p_samples, p_lines, values);
103 }
104 }
105
106
117 void ProcessGroundPolygons::Rasterize(std::vector<double> &lat,
118 std::vector<double> &lon,
119 int &band, double &value) {
120
121 Convert(lat, lon);
122 ProcessPolygons::Rasterize(p_samples, p_lines, band, value);
123 }
124
125
133 void ProcessGroundPolygons::Convert(std::vector<double> &lat,
134 std::vector<double> &lon) {
135
136 p_samples.clear();
137 p_lines.clear();
138
139 for (unsigned int i = 0; i < lat.size(); i++) {
140 if (p_groundMap->SetUniversalGround(lat[i], lon[i])) {
141 p_samples.push_back(p_groundMap->Sample());
142 p_lines.push_back(p_groundMap->Line());
143 }
144 }
145 }
146
147
156
157 if(p_groundMap != NULL) {
158 delete p_groundMap;
159 }
160
162 }
163
164
172
173 if(p_groundMap != NULL) {
174 delete p_groundMap;
175 }
176
178 }
179
180
189 const QString &avgFileName,
190 const QString &countFileName) {
191 /*We need a ground map for converting lat/long to line/sample see Convert()*/
192 Cube cube(cubeStr, "r");
193 p_groundMap = new UniversalGroundMap(cube);
194 ProcessPolygons::AppendOutputCube(avgFileName, countFileName);
195
196 }
197
198
208 void ProcessGroundPolygons::SetStatCubes(const QString &avgFileName,
209 const QString &countFileName,
211 QString &cubeStr) {
212 /*We need a ground map for converting lat/long to line/sample see Convert()*/
213 Cube cube(cubeStr);
214 p_groundMap = new UniversalGroundMap(cube);
215
216 /*setup input cube to transfer projection or camera labels*/
217 CubeAttributeInput inAtts;
218 Isis::Process::SetInputCube(cubeStr, inAtts, 0);
219 int nBands = this->InputCubes[0]->bandCount();
220 int nLines = this->InputCubes[0]->lineCount();
221 int nSamples = this->InputCubes[0]->sampleCount();
222
223 this->Process::SetOutputCube(avgFileName, outAtts, nSamples, nLines, nBands);
224 this->Process::SetOutputCube(countFileName, outAtts, nSamples, nLines, nBands);
225
226 OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
227 OutputCubes[1]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
228
230
231 }
232
233
243 void ProcessGroundPolygons::SetStatCubes(const QString &parameter,
244 QString &cube) {
245
246 QString avgString =
247 Application::GetUserInterface().GetCubeName(parameter);
249 Application::GetUserInterface().GetOutputAttribute(parameter);
250
251 FileName file(avgString);
252 QString path = file.path();
253 QString filename = file.baseName();
254 QString countString = path + "/" + filename + "-count-";
255
256 SetStatCubes(avgString, countString, atts, cube);
257
258 }
259
260
268 void ProcessGroundPolygons::SetStatCubes(const QString &parameter,
269 Isis::Pvl &map, int bands) {
270
271 QString avgString =
272 Application::GetUserInterface().GetCubeName(parameter);
274 Application::GetUserInterface().GetOutputAttribute(parameter);
275
276 FileName file(avgString);
277 QString path = file.path();
278 QString filename = file.baseName();
279 QString countString = path + "/" + filename + "-count-";
280
281 SetStatCubes(avgString, countString, atts, map, bands);
282
283 }
284
285
300 void ProcessGroundPolygons::SetStatCubes(const QString &avgFileName,
301 const QString &countFileName,
303 Isis::Pvl &map, int bands) {
304 int samples, lines;
305
306 Projection *proj = ProjectionFactory::CreateForCube(map, samples, lines,
307 false);
308
309 this->ProcessPolygons::SetStatCubes(avgFileName, countFileName, atts,
310 samples, lines, bands);
311
312 OutputCubes[0]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
313 OutputCubes[1]->addCachingAlgorithm(new BoxcarCachingAlgorithm());
314
315 /*Write the pvl group to the cube files.*/
316
317 PvlGroup group = map.findGroup("Mapping", Pvl::Traverse);
318
319 OutputCubes[0]->putGroup(group);
320 OutputCubes[1]->putGroup(group);
321
322 // If there is an alpha cube in the label passed, attach to output cubes
323 if (map.hasGroup("AlphaCube")) {
324 PvlGroup alpha = map.findGroup("AlphaCube", Pvl::Traverse);
325 OutputCubes[0]->putGroup(alpha);
326 OutputCubes[1]->putGroup(alpha);
327 }
328
329 /*We need a ground map for converting lat/long to line/sample see Convert()*/
330 p_groundMap = new UniversalGroundMap(*OutputCubes[0]);
331
332 delete proj;
333 }
334
335} /* end namespace isis*/
336
static UserInterface & GetUserInterface()
Returns the UserInterface object.
This algorithm is designed for applications that jump around between a couple of spots in the cube wi...
Manipulate and parse attributes of input cube filenames.
Manipulate and parse attributes of output cube filenames.
IO Handler for Isis Cubes.
Definition Cube.h:168
File name manipulation and expansion.
Definition FileName.h:100
Isis exception class.
Definition IException.h:91
static geos::geom::MultiPolygon * SplitPolygonOn360(const geos::geom::Polygon *inPoly)
If the cube crosses the 0/360 boundary and does not include a pole, this will divide the polygon into...
void Convert(std::vector< double > &lat, std::vector< double > &lon)
Converts lat/long to line/sample using the universal ground map object.
void AppendOutputCube(QString &cube, const QString &avgFileName, const QString &countFileName="")
This gives the option to append to the cube.
void Finalize()
This method cleans up any open outputcube files and deletes the pointer to the universal ground map i...
void EndProcess()
This method cleans up any open outputcube files and deletes the pointer to the universal ground map i...
void SetStatCubes(const QString &parameter, QString &cube)
This is a method that is called directly from the application.
void Rasterize(std::vector< double > &lat, std::vector< double > &lon, std::vector< double > &values)
This method gets called from the application with the lat/lon vertices of a polygon along with a vect...
std::vector< Isis::Cube * > InputCubes
A vector of pointers to opened Cube objects.
Definition Process.h:185
virtual Isis::Cube * SetOutputCube(const QString &parameter)
Allocates a user-specified output cube whose size matches the first input cube.
Definition Process.cpp:163
std::vector< Isis::Cube * > OutputCubes
A vector of pointers to allocated Cube objects.
Definition Process.h:191
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:139
void ClearInputCubes()
Close owned input cubes from the list and clear the list.
Definition Process.cpp:614
Isis::Cube * AppendOutputCube(const QString &avgFileName, const QString &countFileName="")
This gives the option to append to the cube.
void Rasterize(std::vector< double > &samples, std::vector< double > &lines, std::vector< double > &values)
void SetStatCubes(const QString &parameter, const int nsamps, const int nlines, int nbands=1)
void Finalize()
Cleans up by closing cubes and freeing memory for owned cubes.
static Isis::Projection * CreateForCube(Isis::Pvl &label, int &ns, int &nl, bool sizeMatch=true)
This method creates a map projection for a cube given a label.
Base class for Map Projections.
Definition Projection.h:155
Contains multiple PvlContainers.
Definition PvlGroup.h:41
Container for cube-like labels.
Definition Pvl.h:119
@ Traverse
Search child objects.
Definition PvlObject.h:158
Universal Ground Map.
double Sample() const
Returns the current line value of the camera model or projection.
double Line() const
Returns the current line value of the camera model or projection.
bool SetUniversalGround(double lat, double lon)
Returns whether the lat/lon position was set successfully in the camera model or projection.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.