How does GEOS cut a hole in a filled outline?
Subtract a reserved rectangle from a nameplate fill and follow how the Boolean difference rebuilds its boundary.
Example result GEOS 3.15.1 returns a valid polygon with one interior ring and an area of 96 square drawing units.
How do you leave room for text inside a fill?
Suppose a nameplate design needs a blank rectangle for a serial number inside an engraved pattern. An editor can compute the polygon difference in drawing coordinates and pass the result to the fill preview. GEOS is an OSGeo geometry engine. This study pins 3.15.1 and subtracts a 6 × 4 rectangle from a 12 × 10 outline, then inspects its rings and area. GEOS project governance
GEOS releases and build documentation · GEOS 3.15.1 source · LGPL 2.1 license text
Rebuild the polygon from its edges
GEOSDifference_r calls Geometry::difference. Ordinary overlapping polygons reach OverlayNGRobust::Overlay. In OverlayNG::computeEdgeOverlay, EdgeNodingBuilder splits edges where they intersect. The engine adds the resulting edges to a graph and labels which sides are inside or outside each input area. The difference keeps the area of the first input that lies outside the second. Closed boundaries are assembled into shells and holes. Here the reserved rectangle is entirely inside the outline, giving one shell around one hole and a net area of 120 − 24 = 96 square drawing units. C API difference entry point · Edge noding, labeling, and result extraction
Even valid inputs can expose floating-point topology errors. For straight-edged inputs like these, the robust entry point starts with floating-point overlay and can retry with snapping and then grid-based snap rounding. These fallbacks can move coordinates or alter tiny features. A complex artwork importer should resolve SVG fill rules, normalize units, and choose a curve-flattening tolerance before checking input and output topology. A fill preview also needs to handle multiple polygons, empty results, and holes. Floating-point overlay and snapping fallbacks
Build and run locally
Use a fresh directory on macOS or Linux with curl, tar, CMake, and a C++17 toolchain. Build the pinned GEOS static libraries and call the GEOS C API from C++. The demo owns the context, WKT reader, and geometries with scoped C++ handles. It was run on Apple Silicon macOS.
Download the C++17 example · Build commands in plain text
View the example source
#include <geos_c.h>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string_view>
int main() try {
if (std::string_view(GEOSversion()).substr(0, 7) != "3.15.1-") {
throw std::runtime_error("This demo pins GEOS 3.15.1");
}
const auto finish = [](GEOSContextHandle_t handle) { GEOS_finish_r(handle); };
const std::unique_ptr<GEOSContextHandle_HS, decltype(finish)>
context(GEOS_init_r(), finish);
if (!context) throw std::runtime_error("Could not create GEOS context");
const auto handle = context.get();
const auto destroyReader = [handle](GEOSWKTReader* reader) {
GEOSWKTReader_destroy_r(handle, reader);
};
const std::unique_ptr<GEOSWKTReader, decltype(destroyReader)>
reader(GEOSWKTReader_create_r(handle), destroyReader);
if (!reader) throw std::runtime_error("Could not create WKT reader");
const auto destroyGeometry = [handle](GEOSGeometry* geometry) {
GEOSGeom_destroy_r(handle, geometry);
};
const auto read = [&](const char* wkt) {
return std::unique_ptr<GEOSGeometry, decltype(destroyGeometry)>(
GEOSWKTReader_read_r(handle, reader.get(), wkt), destroyGeometry);
};
const auto plate = read("POLYGON ((0 0, 12 0, 12 10, 0 10, 0 0))");
const auto keepOut = read("POLYGON ((3 3, 9 3, 9 7, 3 7, 3 3))");
if (!plate || !keepOut ||
GEOSisValid_r(handle, plate.get()) != 1 ||
GEOSisValid_r(handle, keepOut.get()) != 1) {
throw std::runtime_error("Invalid input");
}
const std::unique_ptr<GEOSGeometry, decltype(destroyGeometry)> result(
GEOSDifference_r(handle, plate.get(), keepOut.get()), destroyGeometry);
double area = 0;
if (!result || GEOSGeomTypeId_r(handle, result.get()) != GEOS_POLYGON ||
GEOSisValid_r(handle, result.get()) != 1 ||
GEOSArea_r(handle, result.get(), &area) != 1) {
throw std::runtime_error("Unexpected difference result");
}
const int holes = GEOSGetNumInteriorRings_r(handle, result.get());
if (holes < 0) throw std::runtime_error("Could not count interior rings");
std::cout << "GEOS " << GEOSversion() << '\n'
<< "type=Polygon valid=yes interior_rings=" << holes
<< " area=" << area << '\n';
return area == 96 && holes == 1 ? 0 : 1;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}mkdir geos-polygon-study && cd geos-polygon-study
curl -fsSL https://download.osgeo.org/geos/geos-3.15.1.tar.bz2 -o geos.tar.bz2
tar -xjf geos.tar.bz2
curl -fsSLO https://www.juyaoai.com/examples/geos-polygon-difference/demo.cpp
cmake -S geos-3.15.1 -B geos-build -DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF -DBUILD_TESTING=OFF
cmake --build geos-build --target geos_c --parallel 4
c++ -std=c++17 -I geos-build/capi -I geos-3.15.1/include demo.cpp geos-build/lib/libgeos_c.a \
geos-build/lib/libgeos.a -pthread -lm -o geos-demo
./geos-demoRun output
GEOS 3.15.1-CAPI-1.21.2
type=Polygon valid=yes interior_rings=1 area=96
Move the reserved rectangle across the right edge. For instance, use [9,15] for x and keep [3,7] for y. The expected result is area 108 and zero interior rings because the removed region opens onto the exterior. Update the final area and ring checks in the demo to test this case.