Skip to content

Commit

Permalink
Issue 1824 (#1830)
Browse files Browse the repository at this point in the history
* Force initial cell I/J to be in bounds where not otherwise checked.

* Remove unnecessary assignments.

* Make sure width/height are in range.

* Remove misplaced '='.

* Add grid OOB test.

* Add test file.
  • Loading branch information
abellgithub committed Mar 5, 2018
1 parent fb00f85 commit dac7cfe
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 36 deletions.
47 changes: 29 additions & 18 deletions io/GDALGrid.cpp
Expand Up @@ -48,6 +48,15 @@ GDALGrid::GDALGrid(size_t width, size_t height, double edgeLength,
m_width(width), m_height(height), m_windowSize(windowSize),
m_edgeLength(edgeLength), m_radius(radius), m_outputTypes(outputTypes)
{
if (width > std::numeric_limits<int>::max() ||
height > std::numeric_limits<int>::max())
{
std::ostringstream oss;
oss << "Grid width or height is too large. Width and height are "
"limited to " << std::numeric_limits<int>::max() << " cells."
"Try setting bounds or increasing resolution.";
throw error(oss.str());
}
size_t size(width * height);

m_count.reset(new DataVec(size));
Expand Down Expand Up @@ -199,9 +208,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 = 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 +223,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 = 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 +243,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 = std::max(jOrigin, 0);
while (i >= 0 && j < (int)m_height)
{
double d = distance(i, j, x, y);
Expand All @@ -252,15 +263,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 = 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 +282,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 @@ -283,11 +294,11 @@ void GDALGrid::addPoint(double x, double y, double z)
double d = distance(iOrigin, jOrigin, x, y);
if (d < m_radius &&
iOrigin >= 0 && jOrigin >= 0 &&
iOrigin < (int)m_width && jOrigin <= (int)m_height)
iOrigin < (int)m_width && jOrigin < (int)m_height)
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
40 changes: 40 additions & 0 deletions test/data/gdal/grid_bounds.txt
@@ -0,0 +1,40 @@
X,Y,Z

0, 0, 0
.5, .5, 1
1.5, .5, 2
2.5, .5, 3
3.5, .5, 4
4.5, .5, 5
3.5, 1, 4.4
4.5, 1, 5.4
.5, 1.5, 2
1.5, 1.5, 3
2.5, 1.5, 4
3, 1.5, 4.4
3.5, 1.5, 5
4, 1.5, 5.4
4.5, 1.5, 6
3.5, 2, 5.4
4.5, 2, 6.4
.5, 2.5, 3
1.5, 2.5, 4
2.5, 2.5, 5
3.5, 2.5, 6
4.5, 2.5, 7
.5, 3.5, 4
2.5, 3.5, 6
3.5, 3.5, 7
4.5, 3.5, 8
.5, 4.5, 5
2.5, 4.5, 7
3.5, 4.5, 8
4.5, 4.6, 9.1
4.7, 4.5, 8.9
4.3, 4.5, 8.9
-1000, 2, -45
-1000, -10000, 4
3.2, -1000, 7
10000, 3, 4
2, 10000, 45
999, 9995, 45

0 comments on commit dac7cfe

Please sign in to comment.