diff --git a/autotest/cpp/test_ogr.cpp b/autotest/cpp/test_ogr.cpp index 3faf8f0791b9..0ce901592594 100644 --- a/autotest/cpp/test_ogr.cpp +++ b/autotest/cpp/test_ogr.cpp @@ -4628,4 +4628,13 @@ TEST_F(test_ogr, GetDefaultArcStepSize) } } +TEST_F(test_ogr, OGRPolygon_two_vertex_constructor) +{ + OGRPolygon p(1, 2, 3, 4); + char *outWKT = nullptr; + p.exportToWkt(&outWKT, wkbVariantIso); + EXPECT_STREQ(outWKT, "POLYGON ((1 2,1 4,3 4,3 2,1 2))"); + CPLFree(outWKT); +} + } // namespace diff --git a/ogr/ogr_geometry.h b/ogr/ogr_geometry.h index e2227f3443f0..ddfdec51bd2a 100644 --- a/ogr/ogr_geometry.h +++ b/ogr/ogr_geometry.h @@ -2716,6 +2716,8 @@ class CPL_DLL OGRPolygon : public OGRCurvePolygon /** Create an empty polygon. */ OGRPolygon() = default; + OGRPolygon(double x1, double y1, double x2, double y2); + OGRPolygon(const OGRPolygon &other); /** Move constructor */ OGRPolygon(OGRPolygon &&other) = default; diff --git a/ogr/ogrpolygon.cpp b/ogr/ogrpolygon.cpp index 1daceec6e6c4..084ec0617859 100644 --- a/ogr/ogrpolygon.cpp +++ b/ogr/ogrpolygon.cpp @@ -27,6 +27,27 @@ #include "ogr_sfcgal.h" #include "ogr_p.h" +/************************************************************************/ +/* OGRPolygon(double x1, double y1, double x2, double y2) */ +/************************************************************************/ + +/** + * \brief Construct a rectangular polygon from opposite corner coordinates. + * + * @since GDAL 3.11 + */ + +OGRPolygon::OGRPolygon(double x1, double y1, double x2, double y2) +{ + auto poLR = std::make_unique(); + poLR->addPoint(x1, y1); + poLR->addPoint(x1, y2); + poLR->addPoint(x2, y2); + poLR->addPoint(x2, y1); + poLR->addPoint(x1, y1); + addRingDirectly(poLR.release()); +} + /************************************************************************/ /* OGRPolygon( const OGRPolygon& ) */ /************************************************************************/