7 #include "ProcessPolygons.h"
8 #include "PolygonTools.h"
9 #include "Application.h"
10 #include "BoxcarCachingAlgorithm.h"
11 #include "SpecialPixel.h"
13 #include "geos/geom/CoordinateSequence.h"
14 #include "geos/geom/CoordinateArraySequence.h"
15 #include "geos/geom/Coordinate.h"
16 #include "geos/geom/Envelope.h"
17 #include "geos/geom/LineString.h"
18 #include "geos/geom/Geometry.h"
19 #include "geos/geom/Point.h"
20 #include "geos/geom/prep/PreparedGeometryFactory.h"
21 #include "geos/geom/prep/PreparedPolygon.h"
22 #include "geos/util/IllegalArgumentException.h"
23 #include "geos/operation/overlay/snap/GeometrySnapper.h"
28 ProcessPolygons::ProcessPolygons() {
44 void ProcessPolygons::Rasterize(std::vector<double> &samples,
45 std::vector<double> &lines,
46 std::vector<double> &values) {
48 m_sampleVertices = samples;
49 m_lineVertices = lines;
65 void ProcessPolygons::Rasterize(std::vector<double> &samples,
66 std::vector<double> &lines,
67 int &band,
double &value) {
69 m_sampleVertices = samples;
70 m_lineVertices = lines;
73 m_dns.push_back(value);
89 void ProcessPolygons::FillPolygon(
int Flag) {
92 geos::geom::CoordinateSequence *pts =
new geos::geom::CoordinateArraySequence();
93 for (
unsigned int i = 0; i < m_sampleVertices.size(); i++) {
94 pts->add(geos::geom::Coordinate(m_sampleVertices[i], m_lineVertices[i]));
96 pts->add(geos::geom::Coordinate(m_sampleVertices[0], m_lineVertices[0]));
102 geos::geom::Polygon *spikedPixelPoly = Isis::globalFactory->createPolygon(
103 globalFactory->createLinearRing(pts), NULL);
105 const geos::geom::Polygon *projectedInputPixelPoly;
107 if (spikedPixelPoly->isValid()) {
108 projectedInputPixelPoly = spikedPixelPoly;
111 geos::geom::MultiPolygon *despikedPixelPoly;
113 despikedPixelPoly = PolygonTools::Despike(spikedPixelPoly);
116 delete spikedPixelPoly;
121 despikedPixelPoly = PolygonTools::Despike(spikedPixelPoly);
124 delete spikedPixelPoly;
128 if (despikedPixelPoly->getNumGeometries() > 1)
return;
129 projectedInputPixelPoly =
130 dynamic_cast<const geos::geom::Polygon *
>(despikedPixelPoly->getGeometryN(0));
134 if (!projectedInputPixelPoly->intersects(m_imagePoly))
return;
136 geos::geom::MultiPolygon *intersectPoly = PolygonTools::MakeMultiPolygon(
137 m_imagePoly->intersection(projectedInputPixelPoly));
138 geos::geom::prep::PreparedPolygon *preparedPoly =
139 new geos::geom::prep::PreparedPolygon(intersectPoly);
140 const geos::geom::Envelope *envelope = intersectPoly->getEnvelopeInternal();
143 for (
double x = floor(envelope->getMinX()); x <= ceil(envelope->getMaxX()); x++) {
144 if (x == 0)
continue;
146 for (
double y = floor(envelope->getMinY()); y <= ceil(envelope->getMaxY()); y++) {
147 if (y == 0)
continue;
152 geos::geom::Coordinate c(x, y);
153 geos::geom::Point *p = Isis::globalFactory->createPoint(c);
154 contains = preparedPoly->contains(p);
158 geos::geom::CoordinateSequence *tpts =
new geos::geom::CoordinateArraySequence();
159 tpts->add(geos::geom::Coordinate(x - 0.5, y - 0.5));
160 tpts->add(geos::geom::Coordinate(x + 0.5, y - 0.5));
161 tpts->add(geos::geom::Coordinate(x + 0.5, y + 0.5));
162 tpts->add(geos::geom::Coordinate(x - 0.5, y + 0.5));
163 tpts->add(geos::geom::Coordinate(x - 0.5, y - 0.5));
165 geos::geom::Polygon *outPixelFootPrint = Isis::globalFactory->createPolygon(
166 globalFactory->createLinearRing(tpts), NULL);
167 contains = preparedPoly->intersects(outPixelFootPrint);
168 delete outPixelFootPrint;
174 m_average->SetBasePosition((
int)(x + 0.5), (
int)(y + 0.5), 1);
175 this->OutputCubes[0]->read(*m_average);
176 m_count->SetBasePosition((
int)(x + 0.5), (
int)(y + 0.5), 1);
177 this->OutputCubes[1]->read(*m_count);
180 for (
unsigned int i = 0; i < m_dns.size(); i++) {
190 double inputDn = m_dns[i];
195 double currentCount = (*m_count)[band];
196 double newCount = ++((*m_count)[band]);
197 double currentAverage = (*m_average)[band];
198 (*m_average)[band] = ((currentAverage * currentCount) + inputDn) / newCount;
201 (*m_average)[band] = inputDn;
202 (*m_count)[band] = 1;
209 (*m_average)[band] = inputDn;
216 this->OutputCubes[0]->write(*m_average);
217 this->OutputCubes[1]->write(*m_count);
225 delete projectedInputPixelPoly;
226 delete intersectPoly;
231 catch(geos::util::IllegalArgumentException *ill) {
232 QString msg =
"ERROR! geos exception 1 [";
233 msg += (QString)ill->what() +
"]";
235 throw IException(IException::Programmer, msg, _FILEINFO_);
245 void ProcessPolygons::EndProcess() {
250 Process::EndProcess();
258 void ProcessPolygons::Finalize() {
275 Isis::Cube *ProcessPolygons::AppendOutputCube(
const QString &avgFileName,
276 const QString &countFileName) {
279 QString path = file->
path();
280 QString filename = file->
baseName();
285 averageCube->
open(avgFileName,
"rw");
286 AddOutputCube(averageCube);
291 if (countFileName ==
"") {
294 QString openFile = path +
"/" + filename +
"-count-." + extension;
295 countCube->
open(openFile,
"rw");
299 countCube->
open(countFileName,
"rw");
302 AddOutputCube(countCube);
317 void ProcessPolygons::SetStatCubes(
const QString &avgFileName,
const
318 QString &countFileName,
320 const int nsamps,
const int nlines,
323 this->Process::SetOutputCube(avgFileName, atts, nsamps, nlines, nbands);
324 this->Process::SetOutputCube(countFileName, atts, nsamps, nlines, nbands);
329 geos::geom::CoordinateArraySequence imagePts;
331 imagePts.add(geos::geom::Coordinate(0.0, 0.0));
332 imagePts.add(geos::geom::Coordinate(0.0, this->OutputCubes[0]->lineCount()));
333 imagePts.add(geos::geom::Coordinate(this->OutputCubes[0]->sampleCount(),
334 this->OutputCubes[0]->lineCount()));
335 imagePts.add(geos::geom::Coordinate(this->OutputCubes[0]->sampleCount(), 0.0));
336 imagePts.add(geos::geom::Coordinate(0.0, 0.0));
338 m_imagePoly = Isis::globalFactory->createPolygon(
339 globalFactory->createLinearRing(imagePts), NULL);
341 m_average =
new Brick(*this->OutputCubes[0], 1, 1, nbands);
342 m_count =
new Brick(*this->OutputCubes[1], 1, 1, nbands);
355 void ProcessPolygons::SetStatCubes(
const QString ¶meter,
356 const int nsamps,
const int nlines,
360 Application::GetUserInterface().GetFileName(parameter);
363 Application::GetUserInterface().GetOutputAttribute(parameter);
366 QString path = file->
path();
367 QString filename = file->
baseName();
368 QString countString = path +
"/" + filename +
"-count";
369 SetStatCubes(avgString, countString, atts, nsamps, nlines, nbands);
380 void ProcessPolygons::SetIntersectAlgorithm(
const bool useCenter) {
381 m_useCenter = useCenter;