Skip to content

Commit

Permalink
Merge pull request OSGeo#11165 from dbaston/ogrgf-fromwkt-return-unique
Browse files Browse the repository at this point in the history
OGRGeometryFactory: Add createFromWkt overload returning unique_ptr
  • Loading branch information
rouault authored Nov 4, 2024
2 parents 38151bc + 886fe26 commit 290ee83
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
31 changes: 9 additions & 22 deletions autotest/cpp/test_ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4400,21 +4400,16 @@ TEST_F(test_ogr, OGRFeature_SetGeometry)
poFeatureDefn->Reference();

OGRFeature oFeat(poFeatureDefn);
std::unique_ptr<OGRGeometry> 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);
Expand All @@ -4434,23 +4429,15 @@ TEST_F(test_ogr, OGRFeature_SetGeomField)

// failure
{
std::unique_ptr<OGRGeometry> 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<OGRGeometry> 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);
}

Expand Down
3 changes: 3 additions & 0 deletions ogr/ogr_geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <climits>
#include <cmath>
#include <memory>
#include <utility>

/**
* \file ogr_geometry.h
Expand Down Expand Up @@ -4234,6 +4235,8 @@ class CPL_DLL OGRGeometryFactory
OGRGeometry **);
static OGRErr createFromWkt(const char **, const OGRSpatialReference *,
OGRGeometry **);
static std::pair<std::unique_ptr<OGRGeometry>, OGRErr>
createFromWkt(const char *, const OGRSpatialReference * = nullptr);

/** Deprecated.
* @deprecated in GDAL 2.3
Expand Down
31 changes: 31 additions & 0 deletions ogr/ogrgeometryfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_ptr<OGRGeometry>, OGRErr>
OGRGeometryFactory::createFromWkt(const char *pszData,
const OGRSpatialReference *poSR)

{
std::unique_ptr<OGRGeometry> poGeom;
OGRGeometry *poTmpGeom;
auto err = createFromWkt(&pszData, poSR, &poTmpGeom);
poGeom.reset(poTmpGeom);

return {std::move(poGeom), err};
}

/************************************************************************/
/* OGR_G_CreateFromWkt() */
/************************************************************************/
Expand Down

0 comments on commit 290ee83

Please sign in to comment.