Skip to content

Commit

Permalink
OGRGeometryFactory: Add createFromWkt overload returning unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Oct 29, 2024
1 parent 1c459f5 commit 5571e5b
Show file tree
Hide file tree
Showing 3 changed files with 42 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
2 changes: 2 additions & 0 deletions ogr/ogr_geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -4234,6 +4234,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 5571e5b

Please sign in to comment.