From 886fe26d3809521bbdd4c59dfabde4eda608ec10 Mon Sep 17 00:00:00 2001 From: Daniel Baston Date: Tue, 29 Oct 2024 12:02:20 -0400 Subject: [PATCH] OGRGeometryFactory: Add createFromWkt overload returning unique_ptr --- autotest/cpp/test_ogr.cpp | 31 +++++++++---------------------- ogr/ogr_geometry.h | 3 +++ ogr/ogrgeometryfactory.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/autotest/cpp/test_ogr.cpp b/autotest/cpp/test_ogr.cpp index 9f2de42a3150..b43f40d1cd65 100644 --- a/autotest/cpp/test_ogr.cpp +++ b/autotest/cpp/test_ogr.cpp @@ -4400,21 +4400,16 @@ TEST_F(test_ogr, OGRFeature_SetGeometry) poFeatureDefn->Reference(); OGRFeature oFeat(poFeatureDefn); - std::unique_ptr poGeom; - OGRGeometry *poTmpGeom; - ASSERT_EQ( - OGRGeometryFactory::createFromWkt("POINT (3 7)", nullptr, &poTmpGeom), - OGRERR_NONE); - poGeom.reset(poTmpGeom); + auto [poGeom, err] = OGRGeometryFactory::createFromWkt("POINT (3 7)"); + ASSERT_EQ(err, OGRERR_NONE); + ASSERT_EQ(oFeat.SetGeometry(std::move(poGeom)), OGRERR_NONE); EXPECT_EQ(oFeat.GetGeometryRef()->toPoint()->getX(), 3); EXPECT_EQ(oFeat.GetGeometryRef()->toPoint()->getY(), 7); // set it again to make sure previous feature geometry is freed - ASSERT_EQ( - OGRGeometryFactory::createFromWkt("POINT (2 8)", nullptr, &poTmpGeom), - OGRERR_NONE); - poGeom.reset(poTmpGeom); + std::tie(poGeom, err) = OGRGeometryFactory::createFromWkt("POINT (2 8)"); + ASSERT_EQ(err, OGRERR_NONE); ASSERT_EQ(oFeat.SetGeometry(std::move(poGeom)), OGRERR_NONE); EXPECT_EQ(oFeat.GetGeometryRef()->toPoint()->getX(), 2); EXPECT_EQ(oFeat.GetGeometryRef()->toPoint()->getY(), 8); @@ -4434,23 +4429,15 @@ TEST_F(test_ogr, OGRFeature_SetGeomField) // failure { - std::unique_ptr poGeom; - OGRGeometry *poTmpGeom; - ASSERT_EQ(OGRGeometryFactory::createFromWkt("POINT (3 7)", nullptr, - &poTmpGeom), - OGRERR_NONE); - poGeom.reset(poTmpGeom); + auto [poGeom, err] = OGRGeometryFactory::createFromWkt("POINT (3 7)"); + ASSERT_EQ(err, OGRERR_NONE); EXPECT_EQ(oFeat.SetGeomField(13, std::move(poGeom)), OGRERR_FAILURE); } // success { - std::unique_ptr poGeom; - OGRGeometry *poTmpGeom; - ASSERT_EQ(OGRGeometryFactory::createFromWkt("POINT (3 7)", nullptr, - &poTmpGeom), - OGRERR_NONE); - poGeom.reset(poTmpGeom); + auto [poGeom, err] = OGRGeometryFactory::createFromWkt("POINT (3 7)"); + ASSERT_EQ(err, OGRERR_NONE); EXPECT_EQ(oFeat.SetGeomField(1, std::move(poGeom)), OGRERR_NONE); } diff --git a/ogr/ogr_geometry.h b/ogr/ogr_geometry.h index abd66ace0e8f..7589a90864d4 100644 --- a/ogr/ogr_geometry.h +++ b/ogr/ogr_geometry.h @@ -25,6 +25,7 @@ #include #include #include +#include /** * \file ogr_geometry.h @@ -4234,6 +4235,8 @@ class CPL_DLL OGRGeometryFactory OGRGeometry **); static OGRErr createFromWkt(const char **, const OGRSpatialReference *, OGRGeometry **); + static std::pair, OGRErr> + createFromWkt(const char *, const OGRSpatialReference * = nullptr); /** Deprecated. * @deprecated in GDAL 2.3 diff --git a/ogr/ogrgeometryfactory.cpp b/ogr/ogrgeometryfactory.cpp index 963e36276981..e68303667da9 100644 --- a/ogr/ogrgeometryfactory.cpp +++ b/ogr/ogrgeometryfactory.cpp @@ -481,6 +481,37 @@ OGRErr OGRGeometryFactory::createFromWkt(const char *pszData, return createFromWkt(&pszData, poSR, ppoReturn); } +/** + * \brief Create a geometry object of the appropriate type from its + * well known text representation. + * + * The C function OGR_G_CreateFromWkt() is the same as this method. + * + * @param pszData input zero terminated string containing well known text + * representation of the geometry to be created. + * @param poSR pointer to the spatial reference to be assigned to the + * created geometry object. This may be NULL. + + * @return a pair of the newly created geometry an error code of OGRERR_NONE + * if all goes well, otherwise any of OGRERR_NOT_ENOUGH_DATA, + * OGRERR_UNSUPPORTED_GEOMETRY_TYPE, or OGRERR_CORRUPT_DATA. + * + * @since GDAL 3.11 + */ + +std::pair, OGRErr> +OGRGeometryFactory::createFromWkt(const char *pszData, + const OGRSpatialReference *poSR) + +{ + std::unique_ptr poGeom; + OGRGeometry *poTmpGeom; + auto err = createFromWkt(&pszData, poSR, &poTmpGeom); + poGeom.reset(poTmpGeom); + + return {std::move(poGeom), err}; +} + /************************************************************************/ /* OGR_G_CreateFromWkt() */ /************************************************************************/