Skip to content

Commit

Permalink
Add OGRPolygon::OGRPolygon(double x1, double y1, double x2, double y2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jan 17, 2025
1 parent c1c885a commit ad91d52
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions autotest/cpp/test_ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions ogr/ogr_geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions ogr/ogrpolygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<OGRLinearRing>();
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& ) */
/************************************************************************/
Expand Down

0 comments on commit ad91d52

Please sign in to comment.