GEOS 3.16.0dev
Grid.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2018-2025 ISciences, LLC
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
12 *
13 **********************************************************************/
14
15#pragma once
16
17#include <numeric>
18#include <stdexcept>
19#include <vector>
20
21#include <geos/geom/Envelope.h>
22#include <geos/util/IllegalArgumentException.h>
23
24namespace geos::operation::grid {
25struct infinite_extent
26{
27 static constexpr size_t padding = 1;
28};
29
30struct bounded_extent
31{
32 static constexpr size_t padding = 0;
33};
34
42template<typename extent_tag>
43class GEOS_DLL Grid
44{
45
46 public:
48 Grid(const geom::Envelope& extent, double dx, double dy)
49 : m_extent{ extent }
50 , m_domain{}
51 , m_dx{ dx }
52 , m_dy{ dy }
53 , m_xOrig { extent.getMinX() }
54 , m_yOrig { extent.getMaxY() }
55 , m_num_rows{ 2 * extent_tag::padding + (extent.getMaxY() > extent.getMinY() ? static_cast<size_t>(std::round(extent.getHeight() / dy)) : 0) }
56 , m_num_cols{ 2 * extent_tag::padding + (extent.getMaxX() > extent.getMinX() ? static_cast<size_t>(std::round(extent.getWidth() / dx)) : 0) } {
57 static_assert(std::is_same_v<extent_tag, bounded_extent>);
58 }
59
62 Grid(const geom::Envelope& extent, double dx, double dy, const geom::Envelope& domain)
63 : m_extent{ extent }
64 , m_domain{ domain }
65 , m_dx{ dx }
66 , m_dy{ dy }
67 , m_xOrig { extent.getMinX() }
68 , m_yOrig { extent.getMaxY() }
69 , m_num_rows{ 2 * extent_tag::padding + (extent.getMaxY() > extent.getMinY() ? static_cast<size_t>(std::round(extent.getHeight() / dy)) : 0) }
70 , m_num_cols{ 2 * extent_tag::padding + (extent.getMaxX() > extent.getMinX() ? static_cast<size_t>(std::round(extent.getWidth() / dx)) : 0) }
71 {
72 static_assert(std::is_same_v<extent_tag, infinite_extent>);
73 if (!isEmpty()) {
74 if (m_domain.getMinX() >= m_extent.getMinX() || m_domain.getMaxX() <= m_extent.getMaxX() ||
75 m_domain.getMinY() >= m_extent.getMinY() || m_domain.getMaxY() <= m_extent.getMaxY()) {
76 throw geos::util::IllegalArgumentException("Domain of infinite grid must be larger than extent");
77 }
78 }
79 }
80
81
82 static Grid makeEmpty()
83 {
84 if constexpr (std::is_same_v<extent_tag, bounded_extent>) {
85 return Grid({ 0, 0, 0, 0 }, 0, 0);
86 } else {
87 return Grid({ 0, 0, 0, 0 }, 0, 0, geom::Envelope());
88 }
89 }
90
92 size_t getColumn(double x) const
93 {
94 if (extent_tag::padding) {
95 if (x < m_extent.getMinX())
96 return 0;
97 if (x > m_extent.getMaxX())
98 return m_num_cols - 1;
99 if (x == m_extent.getMaxX()) // special case, returning the cell for which xmax is the right
100 return m_num_cols - 2;
101 } else {
102 if (x < m_extent.getMinX() || x > m_extent.getMaxX())
103 throw std::out_of_range("x");
104
105 if (x == m_extent.getMaxX())
106 return m_num_cols - 1;
107 }
108
109 // Since we've already range-checked our x value, make sure that
110 // the computed column index is no greater than the column index
111 // associated with xmax.
112 return std::min(
113 extent_tag::padding + static_cast<size_t>(std::floor((x - m_extent.getMinX()) / m_dx)),
114 getColumn(m_extent.getMaxX()));
115 }
116
118 size_t getRow(double y) const
119 {
120 if (extent_tag::padding) {
121 if (y > m_extent.getMaxY())
122 return 0;
123 if (y < m_extent.getMinY())
124 return m_num_rows - 1;
125 if (y == m_extent.getMinY()) // special case, returning the cell for which ymin is the bottom
126 return m_num_rows - 2;
127 } else {
128 if (y < m_extent.getMinY() || y > m_extent.getMaxY())
129 throw std::out_of_range("y");
130
131 if (y == m_extent.getMinY())
132 return m_num_rows - 1;
133 }
134
135 // Since we've already range-checked our y value, make sure that
136 // the computed row index is no greater than the column index
137 // associated with ymin.
138 return std::min(
139 extent_tag::padding + static_cast<size_t>(std::floor((m_extent.getMaxY() - y) / m_dy)),
140 getRow(m_extent.getMinY()));
141 }
142
145 std::size_t getCell(double x, double y) const
146 {
147 return getRow(y) * getNumCols() + getColumn(x);
148 }
149
150 bool isEmpty() const { return m_num_rows <= 2 * extent_tag::padding && m_num_cols <= 2 * extent_tag::padding; }
151
152 size_t getNumRows() const { return m_num_rows; }
153
154 size_t getNumCols() const { return m_num_cols; }
155
156 size_t getSize() const { return getNumRows() * getNumCols(); }
157
158 double xmin() const { return m_extent.getMinX(); }
159
160 double xmax() const { return m_extent.getMaxX(); }
161
162 double ymin() const { return m_extent.getMinY(); }
163
164 double ymax() const { return m_extent.getMaxY(); }
165
166 double dx() const { return m_dx; }
167
168 double dy() const { return m_dy; }
169
170 const geom::Envelope& getExtent() const { return m_extent; }
171
175 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)); }
176
180 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)); }
181
183 double getColX(size_t col) const { return m_extent.getMinX() + (static_cast<double>(col - extent_tag::padding) + 0.5) * m_dx; }
184
186 double getRowY(size_t row) const { return m_extent.getMaxY() - (static_cast<double>(row - extent_tag::padding) + 0.5) * m_dy; }
187
188 Grid crop(const geom::Envelope& e) const
189 {
190 if (m_extent.intersects(e)) {
191 return shrinkToFit(e.intersection(m_extent));
192 } else {
193 return makeEmpty();
194 }
195 }
196
201 Grid<extent_tag> shrinkToFit(const geom::Envelope& b, bool calcExtentFromNewGrid=true) const
202 {
203 if (b.getArea() == 0) {
204 return makeEmpty();
205 }
206
207 if (b.getMinX() < m_extent.getMinX() || b.getMinY() < m_extent.getMinY() || b.getMaxX() > m_extent.getMaxX() || b.getMaxY() > m_extent.getMaxY()) {
208 throw std::range_error("Cannot shrink extent to bounds larger than original.");
209 }
210
211 size_t col0 = getColumn(b.getMinX());
212 size_t row1 = getRow(b.getMaxY());
213
214 // Shrink xmin and ymax to fit the upper-left corner of the supplied extent
215 double snapped_xmin = m_extent.getMinX() + static_cast<double>(col0 - extent_tag::padding) * m_dx;
216 double snapped_ymax = m_extent.getMaxY() - static_cast<double>(row1 - extent_tag::padding) * m_dy;
217
218 // Make sure snapped_xmin and snapped_ymax are within the reduced extent.
219 // Because of floating point round-off errors, this is not always the case.
220 if (b.getMinX() < snapped_xmin) {
221 snapped_xmin -= m_dx;
222 col0--;
223 }
224
225 if (b.getMaxY() > snapped_ymax) {
226 snapped_ymax += m_dy;
227 row1--;
228 }
229
230 size_t col1 = getColumn(b.getMaxX());
231 size_t row0 = getRow(b.getMinY());
232
233 size_t num_rows = 1 + (row0 - row1);
234 size_t num_cols = 1 + (col1 - col0);
235
236 // If xmax or ymin falls cleanly on a cell boundary, we don't
237 // need as many rows or columns as we otherwise would, because
238 // we assume that the rightmost cell of the grid is a closed
239 // interval in x, and the lowermost cell of the grid is a
240 // closed interval in y.
241 if (num_rows > 2 && (snapped_ymax - static_cast<double>(num_rows - 1) * m_dy <= b.getMinY())) {
242 num_rows--;
243 }
244 if (num_cols > 2 && (snapped_xmin + static_cast<double>(num_cols - 1) * m_dx >= b.getMaxX())) {
245 num_cols--;
246 }
247
248 const double snapped_xmax = m_extent.getMinX() + static_cast<double>(col0 + num_cols - extent_tag::padding) * m_dx;
249 const double snapped_ymin = m_extent.getMaxY() - static_cast<double>(row1 + num_rows - extent_tag::padding) * m_dy;
250
251 // Perform offsets relative to the new xmin, ymax origin
252 // points. If this is not done, then floating point roundoff
253 // error can cause progressive shrink() calls with the same
254 // inputs to produce different results.
255 geom::Envelope reduced_box = {
256 snapped_xmin,
257 calcExtentFromNewGrid ? std::max(snapped_xmin + static_cast<double>(num_cols) * m_dx, b.getMaxX()) : snapped_xmax,
258 calcExtentFromNewGrid ? std::min(snapped_ymax - static_cast<double>(num_rows) * m_dy, b.getMinY()) : snapped_ymin,
259 snapped_ymax
260 };
261
262 // Fudge computed xmax and ymin, if needed, to prevent extent
263 // from growing during a shrink operation.
264 if (reduced_box.getMaxX() > m_extent.getMaxX()) {
265 if (std::round(reduced_box.getWidth() / m_dx) ==
266 std::round((m_extent.getMaxX() - reduced_box.getMinX()) / m_dx)) {
267 reduced_box = geom::Envelope(reduced_box.getMinX(), m_extent.getMaxX(), reduced_box.getMinY(), reduced_box.getMaxY());
268 } else {
269 throw std::runtime_error("Shrink operation failed.");
270 }
271 }
272 if (reduced_box.getMinY() < m_extent.getMinY()) {
273 if (std::round(reduced_box.getHeight() / m_dy) ==
274 std::round((reduced_box.getMaxY() - m_extent.getMinY()) / m_dy)) {
275 reduced_box = geom::Envelope(reduced_box.getMinX(), reduced_box.getMaxX(), m_extent.getMinY(), reduced_box.getMaxY());
276 } else {
277 throw std::runtime_error("Shrink operation failed.");
278 }
279 }
280
281 if constexpr (std::is_same_v<extent_tag, bounded_extent>) {
282 Grid<extent_tag> reduced{reduced_box, m_dx, m_dy};
283
284 if (!calcExtentFromNewGrid) {
285 reduced.m_xOrig = m_xOrig;
286 reduced.m_yOrig = m_yOrig;
287 reduced.m_skipRows = reduced.getRowOffset(*this);
288 reduced.m_skipCols = reduced.getColOffset(*this);
289 } else if (!reduced.getExtent().contains(b)) {
290 throw std::runtime_error("Shrink operation failed.");
291 }
292 return reduced;
293 } else {
294 Grid<extent_tag> reduced{reduced_box, m_dx, m_dy, m_domain};
295
296 if (!calcExtentFromNewGrid) {
297 reduced.m_xOrig = m_xOrig;
298 reduced.m_yOrig = m_yOrig;
299 reduced.m_skipRows = reduced.getRowOffset(*this);
300 reduced.m_skipCols = reduced.getColOffset(*this);
301 }else if (!reduced.getExtent().contains(b)) {
302 throw std::runtime_error("Shrink operation failed.");
303 }
304 return reduced;
305 }
306 }
307
308 bool operator==(const Grid<extent_tag>& b) const
309 {
310 return m_extent == b.m_extent &&
311 m_dx == b.m_dx &&
312 m_dy == b.m_dy;
313 }
314
315 bool operator!=(const Grid<extent_tag>& b) const
316 {
317 return !(*this == b);
318 }
319
320 geom::Envelope getCellEnvelope(size_t row, size_t col) const;
321
322 private:
323 geom::Envelope m_extent;
324 geom::Envelope m_domain;
325
326 double m_dx;
327 double m_dy;
328
329 double m_xOrig; // origin point that is distinct from xmin of m_extent. Used to allow a subgrid to calculate
330 // sub-envelopes that exactly match those of the parent grid
331 double m_yOrig;
332
333 size_t m_skipRows{0}; // number of rows to skip when computing a cell envelope using m_yOrig
334 size_t m_skipCols{0}; // number of cols to skip when computing a cell envelope using m_xOrig
335
336 size_t m_num_rows;
337 size_t m_num_cols;
338
339 friend Grid<infinite_extent> make_infinite(const Grid<bounded_extent>&, const geom::Envelope&);
340 friend Grid<bounded_extent> make_finite(const Grid<infinite_extent>&);
341};
342
344make_infinite(const Grid<bounded_extent>& grid, const geom::Envelope& domain);
346make_finite(const Grid<infinite_extent>& grid);
347
348}
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition Envelope.h:59
double getMinX() const
Returns the Envelope minimum x-value. Null envelopes do not have maximum values.
Definition Envelope.h:350
bool intersection(const Envelope &env, Envelope &result) const
Computes the intersection of two Envelopes.
bool contains(const Envelope &other) const
Tests if the Envelope other lies wholly inside this Envelope (inclusive of the boundary).
Definition Envelope.h:525
double getArea() const
Gets the area of this envelope.
Definition Envelope.h:290
double getHeight() const
Returns the difference between the maximum and minimum y values.
Definition Envelope.h:275
double getWidth() const
Returns the difference between the maximum and minimum x values.
Definition Envelope.h:262
double getMaxX() const
Returns the Envelope maximum x-value. Null envelopes do not have maximum values.
Definition Envelope.h:330
double getMaxY() const
Returns the Envelope maximum y-value. Null envelopes do not have maximum values.
Definition Envelope.h:320
double getMinY() const
Returns the Envelope minimum y-value. Null envelopes do not have maximum values.
Definition Envelope.h:340
The Grid class represents a grid of constant-size rectangular cells that covers a specified envelope....
Definition Grid.h:44
Grid< extent_tag > shrinkToFit(const geom::Envelope &b, bool calcExtentFromNewGrid=true) const
Definition Grid.h:201
double getColX(size_t col) const
Get the x coordinate at the center of the specified column.
Definition Grid.h:183
Grid(const geom::Envelope &extent, double dx, double dy, const geom::Envelope &domain)
Definition Grid.h:62
Grid(const geom::Envelope &extent, double dx, double dy)
Construct a bounded grid covering a specified extent.
Definition Grid.h:48
size_t getColumn(double x) const
Get the column in which the specified x coordinate would fall.
Definition Grid.h:92
size_t getColOffset(const Grid &other) const
Definition Grid.h:180
size_t getRow(double y) const
Get the row in which the specified y coordinate would fall.
Definition Grid.h:118
double getRowY(size_t row) const
Get the y coordinate at the center of the specified row.
Definition Grid.h:186
size_t getRowOffset(const Grid &other) const
Definition Grid.h:175
std::size_t getCell(double x, double y) const
Definition Grid.h:145
Indicates one or more illegal arguments.
Definition IllegalArgumentException.h:33