Skip to content

Commit

Permalink
Force initial cell I/J to be in bounds where not otherwise checked.
Browse files Browse the repository at this point in the history
  • Loading branch information
abellgithub committed Mar 2, 2018
1 parent 63bcead commit 2468c9f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
36 changes: 19 additions & 17 deletions io/GDALGrid.cpp
Expand Up @@ -199,9 +199,11 @@ void GDALGrid::addPoint(double x, double y, double z)
// <- v


int i, j;
int iStart, jStart;
// First quadrant;
int i = iOrigin + 1;
int j = jOrigin;
i = iStart = std::max(0, iOrigin + 1);
j = jStart = std::min(jOrigin, int(m_height - 1));
while (i < (int)m_width && j >= 0)
{
double d = distance(i, j, x, y);
Expand All @@ -212,16 +214,16 @@ void GDALGrid::addPoint(double x, double y, double z)
}
else
{
if (i == iOrigin + 1)
if (i == iStart)
break;
i = iOrigin + 1;
i = iStart;
j--;
}
}

// Second quadrant;
i = iOrigin;
j = jOrigin - 1;
i = iStart = std::min(iOrigin, int(m_width - 1));
j = jStart = std::min(jOrigin - 1, int(m_height - 1));
while (i >= 0 && j >= 0)
{
double d = distance(i, j, x, y);
Expand All @@ -232,16 +234,16 @@ void GDALGrid::addPoint(double x, double y, double z)
}
else
{
if (j == jOrigin - 1)
if (j == jStart)
break;
j = jOrigin - 1;
j = jStart;
i--;
}
}

// Third quadrant;
i = iOrigin - 1;
j = jOrigin;
i = iStart = std::min(iOrigin - 1, int(m_width - 1));
j = jStart = std::max(jOrigin, 0);
while (i >= 0 && j < (int)m_height)
{
double d = distance(i, j, x, y);
Expand All @@ -252,15 +254,15 @@ void GDALGrid::addPoint(double x, double y, double z)
}
else
{
if (i == iOrigin - 1)
if (i == iStart)
break;
i = iOrigin - 1;
i = iStart;
j++;
}
}
// Fourth quadrant;
i = iOrigin;
j = jOrigin + 1;
i = iStart = std::max(iOrigin, 0);
j = jStart = std::max(jOrigin + 1, 0);
while (i < (int)m_width && j < (int)m_height)
{
double d = distance(i, j, x, y);
Expand All @@ -271,9 +273,9 @@ void GDALGrid::addPoint(double x, double y, double z)
}
else
{
if (j == jOrigin + 1)
if (j == jStart)
break;
j = jOrigin + 1;
j = jStart;
i++;
}
}
Expand All @@ -287,7 +289,7 @@ void GDALGrid::addPoint(double x, double y, double z)
update(iOrigin, jOrigin, z, d);
}

void GDALGrid::update(int i, int j, double val, double dist)
void GDALGrid::update(size_t i, size_t j, double val, double dist)
{
// Once we determine that a point is close enough to a cell to count it,
// this function does the actual math. We use the value of the
Expand Down
2 changes: 1 addition & 1 deletion io/GDALGrid.hpp
Expand Up @@ -139,7 +139,7 @@ class GDALGrid
}

// Update cell at i, j with value at a distance.
void update(int i, int j, double val, double dist);
void update(size_t i, size_t j, double val, double dist);

// Fill cell at index \c i with the nondata value.
void fillNodata(size_t i);
Expand Down
11 changes: 9 additions & 2 deletions io/GDALWriter.cpp
Expand Up @@ -143,8 +143,15 @@ void GDALWriter::createGrid(BOX2D bounds)
m_curBounds = bounds;
size_t width = ((m_curBounds.maxx - m_curBounds.minx) / m_edgeLength) + 1;
size_t height = ((m_curBounds.maxy - m_curBounds.miny) / m_edgeLength) + 1;
m_grid.reset(new GDALGrid(width, height, m_edgeLength, m_radius,
m_outputTypes, m_windowSize));
try
{
m_grid.reset(new GDALGrid(width, height, m_edgeLength, m_radius,
m_outputTypes, m_windowSize));
}
catch (GDALGrid::error& err)
{
throwError(err.what());
}
}


Expand Down

0 comments on commit 2468c9f

Please sign in to comment.