Skip to content

Commit c70c4c4

Browse files
committed
GridIntersection::subdividePolygon: crop grid to geometry extent
1 parent cf28cd6 commit c70c4c4

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

include/geos/operation/grid/Grid.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ class Grid
154154

155155
double getRowY(size_t row) const { return m_extent.getMaxY() - (static_cast<double>(row - extent_tag::padding) + 0.5) * m_dy; }
156156

157+
Grid crop(const geom::Envelope& e) const
158+
{
159+
if (m_extent.intersects(e)) {
160+
return shrinkToFit(e.intersection(m_extent));
161+
} else {
162+
return makeEmpty();
163+
}
164+
}
165+
157166
Grid<extent_tag> shrinkToFit(const geom::Envelope& b) const
158167
{
159168
if (b.getArea() == 0) {

src/operation/grid/GridIntersection.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,14 @@ GridIntersection::subdividePolygon(const Grid<bounded_extent>& p_grid, const Geo
512512

513513
const geom::GeometryFactory& gfact = *g.getFactory();
514514

515-
Grid<infinite_extent> grid = make_infinite(p_grid, *g.getEnvelopeInternal());
516-
Matrix<std::unique_ptr<Cell>> cells(grid.getNumRows(), grid.getNumCols());
515+
const auto cropped_grid = p_grid.crop(*g.getEnvelopeInternal());
517516

518-
traverse_polygons(cells, grid, g);
517+
const Grid<infinite_extent> cell_grid = make_infinite(cropped_grid, *g.getEnvelopeInternal());
518+
Matrix<std::unique_ptr<Cell>> cells(cell_grid.getNumRows(), cell_grid.getNumCols());
519519

520-
const auto areas = collectAreas(cells, p_grid, g);
520+
traverse_polygons(cells, cell_grid, g);
521+
522+
const auto areas = collectAreas(cells, cropped_grid, g);
521523

522524
std::vector<std::unique_ptr<Geometry>> geoms;
523525
std::vector<std::unique_ptr<Geometry>> edge_geoms;
@@ -545,7 +547,7 @@ GridIntersection::subdividePolygon(const Grid<bounded_extent>& p_grid, const Geo
545547
}
546548
} else if (!edge && areas(i - 1, j - 1) == fill_values<float>::INTERIOR) {
547549
// Cell is entirely covered by polygon
548-
Envelope env = grid.getCellEnvelope(i, j);
550+
Envelope env = cell_grid.getCellEnvelope(i, j);
549551
geoms.push_back(gfact.toGeometry(&env));
550552
}
551553
}

0 commit comments

Comments
 (0)