1 #include "ProcessPolygons.h"
7 #include "geos/geom/CoordinateSequence.h"
8 #include "geos/geom/CoordinateArraySequence.h"
9 #include "geos/geom/Coordinate.h"
10 #include "geos/geom/Envelope.h"
11 #include "geos/geom/LineString.h"
12 #include "geos/geom/Geometry.h"
13 #include "geos/geom/Point.h"
14 #include "geos/geom/prep/PreparedGeometryFactory.h"
15 #include "geos/geom/prep/PreparedPolygon.h"
16 #include "geos/util/IllegalArgumentException.h"
17 #include "geos/operation/overlay/snap/GeometrySnapper.h"
22 ProcessPolygons::ProcessPolygons() {
38 void ProcessPolygons::Rasterize(std::vector<double> &samples,
39 std::vector<double> &lines,
40 std::vector<double> &values) {
42 m_sampleVertices = samples;
43 m_lineVertices = lines;
59 void ProcessPolygons::Rasterize(std::vector<double> &samples,
60 std::vector<double> &lines,
61 int &band,
double &value) {
63 m_sampleVertices = samples;
64 m_lineVertices = lines;
67 m_dns.push_back(value);
83 void ProcessPolygons::FillPolygon(
int Flag) {
86 geos::geom::CoordinateSequence *pts =
new geos::geom::CoordinateArraySequence();
87 for (
unsigned int i = 0; i < m_sampleVertices.size(); i++) {
88 pts->add(geos::geom::Coordinate(m_sampleVertices[i], m_lineVertices[i]));
90 pts->add(geos::geom::Coordinate(m_sampleVertices[0], m_lineVertices[0]));
96 geos::geom::Polygon *spikedPixelPoly = Isis::globalFactory.createPolygon(
97 globalFactory.createLinearRing(pts), NULL);
99 const geos::geom::Polygon *projectedInputPixelPoly;
101 if (spikedPixelPoly->isValid()) {
102 projectedInputPixelPoly = spikedPixelPoly;
105 geos::geom::MultiPolygon *despikedPixelPoly;
107 despikedPixelPoly = PolygonTools::Despike(spikedPixelPoly);
110 delete spikedPixelPoly;
115 despikedPixelPoly = PolygonTools::Despike(spikedPixelPoly);
118 delete spikedPixelPoly;
122 if (despikedPixelPoly->getNumGeometries() > 1)
return;
123 projectedInputPixelPoly =
124 dynamic_cast<const geos::geom::Polygon *
>(despikedPixelPoly->getGeometryN(0));
128 if (!projectedInputPixelPoly->intersects(m_imagePoly))
return;
130 geos::geom::MultiPolygon *intersectPoly = PolygonTools::MakeMultiPolygon(
131 m_imagePoly->intersection(projectedInputPixelPoly));
132 geos::geom::prep::PreparedPolygon *preparedPoly =
133 new geos::geom::prep::PreparedPolygon(intersectPoly);
134 const geos::geom::Envelope *envelope = intersectPoly->getEnvelopeInternal();
137 for (
double x = floor(envelope->getMinX()); x <= ceil(envelope->getMaxX()); x++) {
138 if (x == 0)
continue;
140 for (
double y = floor(envelope->getMinY()); y <= ceil(envelope->getMaxY()); y++) {
141 if (y == 0)
continue;
146 geos::geom::Coordinate c(x, y);
147 geos::geom::Point *p = Isis::globalFactory.createPoint(c);
148 contains = preparedPoly->contains(p);
152 geos::geom::CoordinateSequence *tpts =
new geos::geom::CoordinateArraySequence();
153 tpts->add(geos::geom::Coordinate(x - 0.5, y - 0.5));
154 tpts->add(geos::geom::Coordinate(x + 0.5, y - 0.5));
155 tpts->add(geos::geom::Coordinate(x + 0.5, y + 0.5));
156 tpts->add(geos::geom::Coordinate(x - 0.5, y + 0.5));
157 tpts->add(geos::geom::Coordinate(x - 0.5, y - 0.5));
159 geos::geom::Polygon *outPixelFootPrint = Isis::globalFactory.createPolygon(
160 globalFactory.createLinearRing(tpts), NULL);
161 contains = preparedPoly->intersects(outPixelFootPrint);
162 delete outPixelFootPrint;
168 m_average->SetBasePosition((
int)(x + 0.5), (
int)(y + 0.5), 1);
169 this->OutputCubes[0]->read(*m_average);
170 m_count->SetBasePosition((
int)(x + 0.5), (
int)(y + 0.5), 1);
171 this->OutputCubes[1]->read(*m_count);
174 for (
unsigned int i = 0; i < m_dns.size(); i++) {
184 double inputDn = m_dns[i];
189 double currentCount = (*m_count)[band];
190 double newCount = ++((*m_count)[band]);
191 double currentAverage = (*m_average)[band];
192 (*m_average)[band] = ((currentAverage * currentCount) + inputDn) / newCount;
195 (*m_average)[band] = inputDn;
196 (*m_count)[band] = 1;
203 (*m_average)[band] = inputDn;
210 this->OutputCubes[0]->write(*m_average);
211 this->OutputCubes[1]->write(*m_count);
219 delete projectedInputPixelPoly;
220 delete intersectPoly;
225 catch(geos::util::IllegalArgumentException *ill) {
226 QString msg =
"ERROR! geos exception 1 [";
227 msg += (QString)ill->what() +
"]";
239 void ProcessPolygons::EndProcess() {
244 Process::EndProcess();
252 void ProcessPolygons::Finalize() {
269 Isis::Cube *ProcessPolygons::AppendOutputCube(
const QString &avgFileName,
270 const QString &countFileName) {
273 QString path = file->
path();
274 QString filename = file->baseName();
275 QString extension = file->extension();
279 averageCube->
open(avgFileName,
"rw");
280 AddOutputCube(averageCube);
285 if (countFileName ==
"") {
288 QString openFile = path +
"/" + filename +
"-count-." + extension;
289 countCube->
open(openFile,
"rw");
293 countCube->
open(countFileName,
"rw");
296 AddOutputCube(countCube);
311 void ProcessPolygons::SetStatCubes(
const QString &avgFileName,
const
312 QString &countFileName,
314 const int nsamps,
const int nlines,
317 this->Process::SetOutputCube(avgFileName, atts, nsamps, nlines, nbands);
318 this->Process::SetOutputCube(countFileName, atts, nsamps, nlines, nbands);
323 geos::geom::CoordinateArraySequence imagePts;
325 imagePts.add(geos::geom::Coordinate(0.0, 0.0));
326 imagePts.add(geos::geom::Coordinate(0.0, this->OutputCubes[0]->lineCount()));
327 imagePts.add(geos::geom::Coordinate(this->OutputCubes[0]->sampleCount(),
328 this->OutputCubes[0]->lineCount()));
329 imagePts.add(geos::geom::Coordinate(this->OutputCubes[0]->sampleCount(), 0.0));
330 imagePts.add(geos::geom::Coordinate(0.0, 0.0));
332 m_imagePoly = Isis::globalFactory.createPolygon(
333 globalFactory.createLinearRing(imagePts), NULL);
335 m_average =
new Brick(*this->OutputCubes[0], 1, 1, nbands);
336 m_count =
new Brick(*this->OutputCubes[1], 1, 1, nbands);
349 void ProcessPolygons::SetStatCubes(
const QString ¶meter,
350 const int nsamps,
const int nlines,
354 Application::GetUserInterface().GetFileName(parameter);
357 Application::GetUserInterface().GetOutputAttribute(parameter);
360 QString path = file->
path();
361 QString filename = file->baseName();
362 QString countString = path +
"/" + filename +
"-count";
363 SetStatCubes(avgString, countString, atts, nsamps, nlines, nbands);
374 void ProcessPolygons::SetIntersectAlgorithm(
const bool useCenter) {
375 m_useCenter = useCenter;