Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add grid subdivision / rasterization algorithms #1232

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- cxx_compiler: g++
c_compiler: gcc
build_type: Coverage
cxxstd: 14
cxxstd: 17
arch: 64
packages: g++
cmake: 3.15.*
Expand All @@ -51,7 +51,7 @@ jobs:
- cxx_compiler: g++-7
c_compiler: gcc-7
build_type: Release
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'g++-7-multilib gcc-7-multilib'
cmake: 3.17.*
Expand All @@ -60,7 +60,7 @@ jobs:
- cxx_compiler: g++-7
c_compiler: gcc-7
build_type: Release
cxxstd: 14
cxxstd: 17
arch: 32
packages: 'g++-7-multilib gcc-7-multilib g++-multilib gcc-multilib'
cmake: 3.18.*
Expand All @@ -69,7 +69,7 @@ jobs:
- cxx_compiler: g++-8
c_compiler: gcc-8
build_type: Release
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'g++-8-multilib gcc-8-multilib'
cmake: 3.21.*
Expand All @@ -78,7 +78,7 @@ jobs:
- cxx_compiler: g++-9
c_compiler: gcc-9
build_type: Release
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'g++-9-multilib gcc-9-multilib'
cmake: 3.23.*
Expand All @@ -87,7 +87,7 @@ jobs:
- cxx_compiler: g++-10
c_compiler: gcc-10
build_type: Release
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'g++-10-multilib gcc-10-multilib'
cmake: 3.25.*
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
- cxx_compiler: clang++-7
c_compiler: clang-7
build_type: Release
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'clang-7'
cmake: 3.15.*
Expand All @@ -138,7 +138,7 @@ jobs:
- cxx_compiler: clang++-8
c_compiler: clang-8
build_type: Release
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'clang-8'
cmake: 3.17.*
Expand All @@ -147,7 +147,7 @@ jobs:
- cxx_compiler: clang++-9
c_compiler: clang-9
build_type: Release
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'clang-9'
cmake: 3.20.*
Expand All @@ -156,7 +156,7 @@ jobs:
- cxx_compiler: clang++-10
c_compiler: clang-10
build_type: Release
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'clang-10'
cmake: 3.21.*
Expand All @@ -168,7 +168,7 @@ jobs:
- cxx_compiler: clang++-11
c_compiler: clang-11
build_type: Debug
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'clang-11'
cmake: 3.23.*
Expand All @@ -177,7 +177,7 @@ jobs:
- cxx_compiler: clang++-12
c_compiler: clang-12
build_type: ASAN
cxxstd: 14
cxxstd: 17
arch: 64
packages: 'clang-12'
cmake: 3.25.*
Expand Down Expand Up @@ -392,7 +392,7 @@ jobs:
matrix:
ci:
- os: windows-2019
cxxstd: 14
cxxstd: 17
arch: x86

- os: windows-2022
Expand Down
8 changes: 8 additions & 0 deletions benchmarks/operation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ if (benchmark_FOUND)
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/benchmarks>)
target_link_libraries(perf_coverage_union PRIVATE
benchmark::benchmark geos geos_cxx_flags)

add_executable(perf_grid_intersection GridIntersectionPerfTest.cpp)
target_include_directories(perf_grid_intersection PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/benchmarks>)
target_link_libraries(perf_grid_intersection
benchmark::benchmark geos geos_cxx_flags)
endif()
121 changes: 121 additions & 0 deletions benchmarks/operation/GridIntersectionPerfTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2025 ISciences LLC
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/

#include <benchmark/benchmark.h>
#include <BenchmarkUtils.h>

#include <geos/geom/Envelope.h>
#include <geos/geom/prep/PreparedGeometryFactory.h>
#include <geos/operation/grid/Grid.h>

#include <geos/operation/grid/GridIntersection.h>
#include <geos/operation/intersection/Rectangle.h>
#include <geos/operation/intersection/RectangleIntersection.h>

using geos::geom::CoordinateXY;
using geos::geom::Envelope;
using geos::geom::Geometry;
using Grid = geos::operation::grid::Grid<geos::operation::grid::bounded_extent>;

template<bool AreaOnly>
struct GridIntersection {
static double Intersection(const Envelope& env, int nx, int ny, const Geometry& g) {
Grid grid(env, env.getWidth() / nx, env.getHeight() / ny);
if constexpr (AreaOnly) {
auto result = geos::operation::grid::GridIntersection::grid_intersection(grid, g);
float area = 0;
for (std::size_t i = 0; i < result->rows(); i++) {
for (std::size_t j = 0; j < result->cols(); j++) {
area += (*result)(i, j);
}
}
return static_cast<double>(area);
} else {
auto subdivided = geos::operation::grid::GridIntersection::subdivide_polygon(grid, g, true);
return subdivided->getArea();
}
}
};

using GridIntersectionAreaOnly = GridIntersection<true>;
using GridIntersectionFull = GridIntersection<false>;

template<bool UseRectangleIntersection>
struct SingleIntersection {

static double Intersection(const Envelope& env, int nx, int ny, const Geometry& g) {
double dx = env.getWidth() / nx;
double dy = env.getHeight() / ny;

double x0 = env.getMinX();
double y0 = env.getMinY();

const auto& gfact = *g.getFactory();
auto prepGeom = geos::geom::prep::PreparedGeometryFactory::prepare(&g);

double area = 0;

for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
Envelope subEnv(x0 + i*dx, x0 + (i+1)*dx, y0 + j*dy, y0 + (j+1)*dy);
auto cellGeom = gfact.toGeometry(&subEnv);
if (!prepGeom->intersects(cellGeom.get())) {
continue;
}

std::unique_ptr<Geometry> isect;
if constexpr (UseRectangleIntersection) {
geos::operation::intersection::Rectangle rect(x0 + i*dx, y0 + j*dy, x0 + (i+1)*dx, y0 + (j+1)*dy);
isect = geos::operation::intersection::RectangleIntersection::clip(g, rect);
} else {
isect = g.intersection(cellGeom.get());
}

area += isect->getArea();
}
}

return area;
}
};

using PolygonIntersection = SingleIntersection<false>;
using RectangleIntersection = SingleIntersection<true>;

template<typename Impl>
static void BM_GridIntersection(benchmark::State& state)
{
auto nCells = state.range(0);

auto nx = static_cast<int>(std::ceil(std::sqrt(nCells)));
auto ny = static_cast<int>(std::ceil(std::sqrt(nCells)));

CoordinateXY center;
Envelope env(0, nx, 0, ny);
env.centre(center);

auto geom = geos::benchmark::createSineStar(center, env.getWidth() / 2, 500);

for (auto _ : state) {
Impl::Intersection(env, nx, ny, *geom);
}

}

BENCHMARK_TEMPLATE(BM_GridIntersection, GridIntersectionAreaOnly)->Range(1000, 1000000);
BENCHMARK_TEMPLATE(BM_GridIntersection, GridIntersectionFull)->Range(1000, 1000000);
BENCHMARK_TEMPLATE(BM_GridIntersection, RectangleIntersection)->Range(1000, 1000000);
BENCHMARK_TEMPLATE(BM_GridIntersection, PolygonIntersection)->Range(1000, 1000000);

BENCHMARK_MAIN();
13 changes: 13 additions & 0 deletions capi/geos_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,19 @@ extern "C" {
return GEOSClipByRect_r(handle, g, xmin, ymin, xmax, ymax);
}

Geometry*
GEOSSubdivideByGrid(const Geometry* g, double xmin, double ymin, double xmax, double ymax,
unsigned nx, unsigned ny, int include_exterior)
{
return GEOSSubdivideByGrid_r(handle, g, xmin, ymin, xmax, ymax, nx, ny, include_exterior);
}

int
GEOSGridIntersectionFractions(const Geometry* g, double xmin, double ymin, double xmax, double ymax,
unsigned nx, unsigned ny, float* buf)
{
return GEOSGridIntersectionFractions_r(handle, g, xmin, ymin, xmax, ymax, nx, ny, buf);
}

Geometry*
GEOSGeom_transformXY(const GEOSGeometry* g, GEOSTransformXYCallback callback, void* userdata) {
Expand Down
68 changes: 68 additions & 0 deletions capi/geos_c.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,24 @@ extern GEOSGeometry GEOS_DLL *GEOSClipByRect_r(
double xmin, double ymin,
double xmax, double ymax);

/** \see GEOSSubdivideByGrid */
extern GEOSGeometry GEOS_DLL *GEOSSubdivideByGrid_r(
GEOSContextHandle_t handle,
const GEOSGeometry* g,
double xmin, double ymin,
double xmax, double ymax,
unsigned nx, unsigned ny,
int include_exterior);

/** \see GEOSGridIntersectionFractions */
extern int GEOS_DLL GEOSGridIntersectionFractions_r(
GEOSContextHandle_t handle,
const GEOSGeometry* g,
double xmin, double ymin,
double xmax, double ymax,
unsigned nx, unsigned ny,
float* buf);

/** \see GEOSPolygonize */
extern GEOSGeometry GEOS_DLL *GEOSPolygonize_r(
GEOSContextHandle_t handle,
Expand Down Expand Up @@ -3788,6 +3806,56 @@ extern GEOSGeometry GEOS_DLL *GEOSClipByRect(
double xmin, double ymin,
double xmax, double ymax);

/**
* Compute the intersection of a geometry with each polygon in
* a rectangular grid.
* \param g The input geometry to be clipped
* \param xmin Left bound of grd
* \param ymin Lower bound of grid
* \param xmax Right bound of grid
* \param ymax Upper bound of grid
* \param nx number of columns in grid
* \param ny number of rows in grid
* \param include_exterior whether to include portions of the
input geometry that do not intersect the grid in
the returned result.
* \return A geometry collection whose components represent the
* intersection with each cell in the grid.
* Caller is responsible for freeing with GEOSGeom_destroy().
* \see GEOSGetGridIntersectionFractions
*
* \since 3.14.0
*/
extern GEOSGeometry GEOS_DLL *GEOSSubdivideByGrid(
const GEOSGeometry* g,
double xmin, double ymin,
double xmax, double ymax,
unsigned nx, unsigned ny,
int include_exterior);

/**
* Determine the fraction of each cell in a rectangular grid
* that is covered by a polygon.
* \param g The input geometry
* \param xmin Left bound of grd
* \param ymin Lower bound of grid
* \param xmax Right bound of grid
* \param ymax Upper bound of grid
* \param nx number of columns in grid
* \param ny number of rows in grid
* \param buf a buffer of size nx*ny to which the grid cell
* coverage fractions will be written in row-major order
* \return 1 if the operation was successful, 0 on exception
*
* \since 3.14.0
*/
extern int GEOS_DLL GEOSGridIntersectionFractions(
const GEOSGeometry* g,
double xmin, double ymin,
double xmax, double ymax,
unsigned nx, unsigned ny,
float* buf);

/**
* Find paths shared between the two given lineal geometries.
*
Expand Down
Loading