diff --git a/autotest/cpp/test_ogr.cpp b/autotest/cpp/test_ogr.cpp index e6605d9f03b8..bb0613b33f67 100644 --- a/autotest/cpp/test_ogr.cpp +++ b/autotest/cpp/test_ogr.cpp @@ -4598,4 +4598,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 b64c98f6ff88..604876e1dd09 100644 --- a/ogr/ogr_geometry.h +++ b/ogr/ogr_geometry.h @@ -2715,6 +2715,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& ) */ /************************************************************************/