Skip to content

Commit eee29c9

Browse files
committed
GridIntersection: Add comments, remove unused variable
1 parent 53a5812 commit eee29c9

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

include/geos/operation/grid/Grid.h

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
#include <geos/geom/Envelope.h>
2222

23-
constexpr double DEFAULT_GRID_COMPAT_TOL = 1e-6;
24-
2523
namespace geos::operation::grid {
2624
struct infinite_extent
2725
{
@@ -33,11 +31,19 @@ struct bounded_extent
3331
static constexpr size_t padding = 0;
3432
};
3533

34+
/**
35+
* @brief The Grid class represents a grid of constant-size rectangular cells that covers a specified envelope.
36+
* The width of the cells may be different from the height. If the Grid has an "infinite" extent rather than
37+
* a "bounded" extent, then an extra row and column will be added on all side of the grid. The size of the cells
38+
* in these columns may be larger than those in the primary grid, such that the extended grid covers a "domain"
39+
* that is larger than the extent of the regular grid.
40+
*/
3641
template<typename extent_tag>
3742
class GEOS_DLL Grid
3843
{
3944

4045
public:
46+
/// Construct a bounded grid covering a specified extent.
4147
Grid(const geom::Envelope& extent, double dx, double dy)
4248
: m_extent{ extent }
4349
, m_domain{}
@@ -50,6 +56,8 @@ class GEOS_DLL Grid
5056
static_assert(std::is_same_v<extent_tag, bounded_extent>);
5157
}
5258

59+
/// Construct an infinite grid covering a specified extent with regularly-sized cells, and adding a row and column
60+
/// of variably-sized cells to each edge of the primary grid such that the specified domain is covered.
5361
Grid(const geom::Envelope& extent, double dx, double dy, const geom::Envelope& domain)
5462
: m_extent{ extent }
5563
, m_domain{ domain }
@@ -73,6 +81,7 @@ class GEOS_DLL Grid
7381
}
7482
}
7583

84+
/// Get the column in which the specified x coordinate would fall.
7685
size_t getColumn(double x) const
7786
{
7887
if (extent_tag::padding) {
@@ -98,6 +107,7 @@ class GEOS_DLL Grid
98107
getColumn(m_extent.getMaxX()));
99108
}
100109

110+
/// Get the row in which the specified y coordinate would fall.
101111
size_t getRow(double y) const
102112
{
103113
if (extent_tag::padding) {
@@ -123,6 +133,8 @@ class GEOS_DLL Grid
123133
getRow(m_extent.getMinY()));
124134
}
125135

136+
/// Get the cell index in which the specified x and y values would fall. Cells are indexed from left-to-right,
137+
/// then top-to-bottom.
126138
std::size_t getCell(double x, double y) const
127139
{
128140
return getRow(y) * getNumCols() + getColumn(x);
@@ -150,12 +162,20 @@ class GEOS_DLL Grid
150162

151163
const geom::Envelope& getExtent() const { return m_extent; }
152164

165+
/// Return the number of rows by which another grid is offset from this Grid. It is assumed that the two
166+
/// grids have the same resolution, and that the maximum y value of the other grid is less than or equal
167+
/// to the maximum y value of this grid.
153168
size_t getRowOffset(const Grid& other) const { return static_cast<size_t>(std::round(std::abs(other.m_extent.getMaxY() - m_extent.getMaxY()) / m_dy)); }
154169

170+
/// Return the number of columns by which another grid is offset from this Grid. It is assumed that the two
171+
/// grids have the same resolution, and that the minimum x value of the other grid is greater than or equal
172+
/// to the minimum x value of this grid.
155173
size_t getColOffset(const Grid& other) const { return static_cast<size_t>(std::round(std::abs(m_extent.getMinX() - other.m_extent.getMinX()) / m_dx)); }
156174

175+
/// Get the x coordinate at the center of the specified column.
157176
double getColX(size_t col) const { return m_extent.getMinX() + (static_cast<double>(col - extent_tag::padding) + 0.5) * m_dx; }
158177

178+
/// Get the y coordinate at the center of the specified row.
159179
double getRowY(size_t row) const { return m_extent.getMaxY() - (static_cast<double>(row - extent_tag::padding) + 0.5) * m_dy; }
160180

161181
Grid crop(const geom::Envelope& e) const
@@ -167,6 +187,10 @@ class GEOS_DLL Grid
167187
}
168188
}
169189

190+
/// Reduce the size of the grid to contain only the provided Envelope
191+
/// If calcExtentFromNewGrid is true, then the xmax and ymin of the new
192+
/// grid will be calculated relative to the origin point of the original grid
193+
/// rather than the newly cropped grid.
170194
Grid<extent_tag> shrinkToFit(const geom::Envelope& b, bool calcExtentFromNewGrid=true) const
171195
{
172196
if (b.getArea() == 0) {
@@ -295,11 +319,12 @@ class GEOS_DLL Grid
295319
double m_dx;
296320
double m_dy;
297321

298-
double m_xOrig;
322+
double m_xOrig; // origin point that is distinct from xmin of m_extent. Used to allow a subgrid to calculate
323+
// sub-envelopes that exactly match those of the parent grid
299324
double m_yOrig;
300325

301-
size_t m_skipRows{0};
302-
size_t m_skipCols{0};
326+
size_t m_skipRows{0}; // number of rows to skip when computing a cell envelope using m_yOrig
327+
size_t m_skipCols{0}; // number of cols to skip when computing a cell envelope using m_xOrig
303328

304329
size_t m_num_rows;
305330
size_t m_num_cols;

include/geos/operation/grid/GridIntersection.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,25 @@ class GEOS_DLL GridIntersection
6060

6161
/**
6262
* @brief Partition a polygonal geometry by a grid
63+
* @param grid the Grid by which the polygon should be partitioned. The Grid does not need to cover the polygon.
64+
* @param g a polygonal geometry
65+
* @param includeExterior whether the result should contain any portions of the input that fall outside the grid.
66+
* @return a GeometryCollection containing the partitioned polygon.
6367
*/
64-
static std::unique_ptr<geom::Geometry> subdividePolygon(const Grid<bounded_extent>& p_grid, const geom::Geometry& g, bool includeExterior);
68+
static std::unique_ptr<geom::Geometry> subdividePolygon(const Grid<bounded_extent>& grid, const geom::Geometry& g, bool includeExterior);
6569

70+
/**
71+
* @brief Calculate the fraction of each cell in a Grid that is covered by a polygon.
72+
* @param grid the Grid for which coverage fractions should be returned
73+
* @param g a polygonal or linear geometry
74+
* @return a matrix having the same number of rows and columns as the grid, whose values contain either the
75+
* fraction of the cell area (for polygonal inputs) or the length of the intersection (for linear inputs)
76+
*/
6677
static std::shared_ptr<Matrix<float>>
67-
getIntersectionFractions(const Grid<bounded_extent>& raster_grid, const geom::Geometry& g);
78+
getIntersectionFractions(const Grid<bounded_extent>& grid, const geom::Geometry& g);
6879

6980
static std::shared_ptr<Matrix<float>>
70-
getIntersectionFractions(const Grid<bounded_extent>& raster_grid, const geom::Envelope& box);
81+
getIntersectionFractions(const Grid<bounded_extent>& grid, const geom::Envelope& box);
7182

7283
/**
7384
* @brief Determines the bounding box of the raster-vector intersection. Considers the bounding boxes

0 commit comments

Comments
 (0)