Skip to content

Commit 4738ce8

Browse files
authored
Merge pull request OSGeo#11958 from dbaston/geometry-create-from-wkt-usages
OGRGeometryFactory: Update createFromWkt usages to unique_ptr overload
2 parents d8e3962 + fc395f9 commit 4738ce8

12 files changed

+51
-70
lines changed

apps/gdal_grid_lib.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1342,10 +1342,9 @@ GDALGridOptionsNew(char **papszArgv,
13421342
STARTS_WITH_CI(osVal.c_str(), "MULTIPOLYGON")) &&
13431343
VSIStatL(osVal.c_str(), &sStat) != 0)
13441344
{
1345-
OGRGeometry *poGeom = nullptr;
1346-
OGRGeometryFactory::createFromWkt(osVal.c_str(), nullptr,
1347-
&poGeom);
1348-
psOptions->poClipSrc.reset(poGeom);
1345+
psOptions->poClipSrc =
1346+
OGRGeometryFactory::createFromWkt(osVal.c_str(), nullptr)
1347+
.first;
13491348
if (psOptions->poClipSrc == nullptr)
13501349
{
13511350
CPLError(CE_Failure, CPLE_IllegalArg,

apps/gdalwarp_lib.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -3358,10 +3358,10 @@ static CPLErr LoadCutline(const std::string &osCutlineDSNameOrWKT,
33583358
poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
33593359
poSRS->SetFromUserInput(osSRS.c_str());
33603360
}
3361-
OGRGeometry *poGeom = nullptr;
3362-
OGRGeometryFactory::createFromWkt(osCutlineDSNameOrWKT.c_str(),
3363-
poSRS.get(), &poGeom);
3364-
*phCutlineRet = OGRGeometry::ToHandle(poGeom);
3361+
3362+
auto [poGeom, _] = OGRGeometryFactory::createFromWkt(
3363+
osCutlineDSNameOrWKT.c_str(), poSRS.get());
3364+
*phCutlineRet = OGRGeometry::ToHandle(poGeom.release());
33653365
return *phCutlineRet ? CE_None : CE_Failure;
33663366
}
33673367

apps/ogr2ogr_lib.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -8378,10 +8378,8 @@ GDALVectorTranslateOptions *GDALVectorTranslateOptionsNew(
83788378
STARTS_WITH_CI(osVal.c_str(), "MULTIPOLYGON")) &&
83798379
VSIStatL(osVal.c_str(), &sStat) != 0)
83808380
{
8381-
OGRGeometry *poGeom = nullptr;
8382-
OGRGeometryFactory::createFromWkt(osVal.c_str(), nullptr,
8383-
&poGeom);
8384-
psOptions->poClipSrc.reset(poGeom);
8381+
psOptions->poClipSrc =
8382+
OGRGeometryFactory::createFromWkt(osVal.c_str()).first;
83858383
if (psOptions->poClipSrc == nullptr)
83868384
{
83878385
CPLError(
@@ -8433,10 +8431,8 @@ GDALVectorTranslateOptions *GDALVectorTranslateOptionsNew(
84338431
STARTS_WITH_CI(osVal.c_str(), "MULTIPOLYGON")) &&
84348432
VSIStatL(osVal.c_str(), &sStat) != 0)
84358433
{
8436-
OGRGeometry *poGeom = nullptr;
8437-
OGRGeometryFactory::createFromWkt(osVal.c_str(), nullptr,
8438-
&poGeom);
8439-
psOptions->poClipDst.reset(poGeom);
8434+
psOptions->poClipDst =
8435+
OGRGeometryFactory::createFromWkt(osVal.c_str()).first;
84408436
if (psOptions->poClipDst == nullptr)
84418437
{
84428438
CPLError(

apps/test_ogrsf.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,8 @@ static int TestCreateLayer(GDALDriver *poDriver, OGRwkbGeometryType eGeomType)
822822
const char *pszWKT = GetWKT(eGeomType);
823823
if (pszWKT != nullptr)
824824
{
825-
OGRGeometry *poGeom = nullptr;
826-
OGRGeometryFactory::createFromWkt(pszWKT, nullptr, &poGeom);
827-
poFeature->SetGeometryDirectly(poGeom);
825+
auto [poGeom, _] = OGRGeometryFactory::createFromWkt(pszWKT);
826+
poFeature->SetGeometry(std::move(poGeom));
828827
}
829828

830829
CPLErrorReset();
@@ -864,9 +863,8 @@ static int TestCreateLayer(GDALDriver *poDriver, OGRwkbGeometryType eGeomType)
864863
pszWKT = GetWKT(eOtherGeomType);
865864
if (pszWKT != nullptr)
866865
{
867-
OGRGeometry *poGeom = nullptr;
868-
OGRGeometryFactory::createFromWkt(pszWKT, nullptr, &poGeom);
869-
poFeature->SetGeometryDirectly(poGeom);
866+
auto [poGeom, _] = OGRGeometryFactory::createFromWkt(pszWKT);
867+
poFeature->SetGeometry(std::move(poGeom));
870868
}
871869

872870
CPLErrorReset();
@@ -915,9 +913,8 @@ static int TestCreateLayer(GDALDriver *poDriver, OGRwkbGeometryType eGeomType)
915913
pszWKT = GetWKT(eGeomType);
916914
if (pszWKT != nullptr)
917915
{
918-
OGRGeometry *poGeom = nullptr;
919-
OGRGeometryFactory::createFromWkt(pszWKT, nullptr, &poGeom);
920-
poFeature->SetGeometryDirectly(poGeom);
916+
auto [poGeom, _] = OGRGeometryFactory::createFromWkt(pszWKT);
917+
poFeature->SetGeometry(std::move(poGeom));
921918
}
922919
CPLErrorReset();
923920
CPLPushErrorHandler(CPLQuietErrorHandler);

frmts/netcdf/netcdflayer.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -1311,13 +1311,11 @@ bool netCDFLayer::FillFeatureFromVar(OGRFeature *poFeature, int nMainDimId,
13111311
}
13121312
if (pszWKT != nullptr)
13131313
{
1314-
OGRGeometry *poGeom = nullptr;
1315-
CPL_IGNORE_RET_VAL(
1316-
OGRGeometryFactory::createFromWkt(pszWKT, nullptr, &poGeom));
1314+
auto [poGeom, _] = OGRGeometryFactory::createFromWkt(pszWKT);
13171315
if (poGeom != nullptr)
13181316
{
13191317
poGeom->assignSpatialReference(GetSpatialRef());
1320-
poFeature->SetGeometryDirectly(poGeom);
1318+
poFeature->SetGeometry(std::move(poGeom));
13211319
}
13221320
CPLFree(pszWKT);
13231321
}

frmts/pdf/pdfcreatecopy.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,7 @@ GDALPDFObjectNum GDALPDFBaseWriter::WriteSRS_ISO32000(GDALDataset *poSrcDS,
690690
pszNEATLINE = poSrcDS->GetMetadataItem("NEATLINE");
691691
if (bHasGT && pszNEATLINE != nullptr && pszNEATLINE[0] != '\0')
692692
{
693-
OGRGeometry *poGeom = nullptr;
694-
OGRGeometryFactory::createFromWkt(pszNEATLINE, nullptr, &poGeom);
693+
auto [poGeom, _] = OGRGeometryFactory::createFromWkt(pszNEATLINE);
695694
if (poGeom != nullptr &&
696695
wkbFlatten(poGeom->getGeometryType()) == wkbPolygon)
697696
{
@@ -748,7 +747,6 @@ GDALPDFObjectNum GDALPDFBaseWriter::WriteSRS_ISO32000(GDALDataset *poSrcDS,
748747
}
749748
}
750749
}
751-
delete poGeom;
752750
}
753751

754752
if (pasGCPList)

frmts/pdf/pdfcreatefromcomposition.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,8 @@ bool GDALPDFComposerWriter::GenerateGeoreferencing(
683683
std::vector<xyPair> aBoundingPolygon;
684684
if (pszBoundingPolygon)
685685
{
686-
OGRGeometry *poGeom = nullptr;
687-
OGRGeometryFactory::createFromWkt(pszBoundingPolygon, nullptr, &poGeom);
686+
auto [poGeom, _] =
687+
OGRGeometryFactory::createFromWkt(pszBoundingPolygon);
688688
if (poGeom && poGeom->getGeometryType() == wkbPolygon)
689689
{
690690
auto poPoly = poGeom->toPolygon();
@@ -707,7 +707,6 @@ bool GDALPDFComposerWriter::GenerateGeoreferencing(
707707
}
708708
}
709709
}
710-
delete poGeom;
711710
}
712711

713712
const auto pszSRS = CPLGetXMLValue(psGeoreferencing, "SRS", nullptr);

ogr/ogr_wkb.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ size_t OGRWKTToWKBTranslator::TranslateWKT(void *pabyWKTStart, size_t nLength,
16181618
}
16191619

16201620
// General case going through a OGRGeometry
1621-
OGRGeometry *poGeometry = nullptr;
1621+
std::unique_ptr<OGRGeometry> poGeometry = nullptr;
16221622
if (bCanAlterByteAfter)
16231623
{
16241624
// Slight optimization for all geometries but the final one, to
@@ -1628,15 +1628,15 @@ size_t OGRWKTToWKBTranslator::TranslateWKT(void *pabyWKTStart, size_t nLength,
16281628
char *pszEnd = static_cast<char *>(pabyWKTStart) + nLength;
16291629
const char chBackup = *pszEnd;
16301630
*pszEnd = 0;
1631-
OGRGeometryFactory::createFromWkt(pszPtrStart, nullptr, &poGeometry);
1631+
poGeometry = OGRGeometryFactory::createFromWkt(pszPtrStart).first;
16321632
// cppcheck-suppress selfAssignment
16331633
*pszEnd = chBackup;
16341634
}
16351635
else
16361636
{
16371637
std::string osTmp;
16381638
osTmp.assign(pszPtrStart, nLength);
1639-
OGRGeometryFactory::createFromWkt(osTmp.c_str(), nullptr, &poGeometry);
1639+
poGeometry = OGRGeometryFactory::createFromWkt(osTmp.c_str()).first;
16401640
}
16411641
if (!poGeometry)
16421642
{
@@ -1651,6 +1651,5 @@ size_t OGRWKTToWKBTranslator::TranslateWKT(void *pabyWKTStart, size_t nLength,
16511651
return static_cast<size_t>(-1);
16521652
}
16531653
poGeometry->exportToWkb(wkbNDR, pabyWKB, wkbVariantIso);
1654-
delete poGeometry;
16551654
return nWKBSize;
16561655
}

ogr/ogrsf_frmts/csv/ogrcsvlayer.cpp

+12-15
Original file line numberDiff line numberDiff line change
@@ -1401,48 +1401,45 @@ OGRFeature *OGRCSVLayer::GetNextUnfilteredFeature()
14011401
const char *pszStr = papszTokens[iAttr];
14021402
while (*pszStr == ' ')
14031403
pszStr++;
1404-
OGRGeometry *poGeom = nullptr;
1404+
std::unique_ptr<OGRGeometry> poGeom = nullptr;
1405+
OGRErr eErr;
14051406

14061407
if (EQUAL(poGeomFieldDefn->GetNameRef(), ""))
14071408
{
1408-
if (OGRGeometryFactory::createFromWkt(
1409-
pszStr, nullptr, &poGeom) != OGRERR_NONE)
1409+
std::tie(poGeom, eErr) =
1410+
OGRGeometryFactory::createFromWkt(pszStr);
1411+
if (eErr != OGRERR_NONE)
14101412
{
14111413
CPLError(CE_Warning, CPLE_AppDefined,
14121414
"Ignoring invalid WKT: %s", pszStr);
1413-
delete poGeom;
1414-
poGeom = nullptr;
14151415
}
14161416
}
14171417
else
14181418
{
14191419
CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler);
14201420

1421-
if (OGRGeometryFactory::createFromWkt(
1422-
pszStr, nullptr, &poGeom) != OGRERR_NONE)
1423-
{
1424-
delete poGeom;
1425-
poGeom = nullptr;
1426-
}
1421+
std::tie(poGeom, eErr) =
1422+
OGRGeometryFactory::createFromWkt(pszStr);
14271423

14281424
if (!poGeom && *pszStr == '{')
14291425
{
1430-
poGeom = OGRGeometry::FromHandle(
1431-
OGR_G_CreateGeometryFromJson(pszStr));
1426+
poGeom.reset(OGRGeometry::FromHandle(
1427+
OGR_G_CreateGeometryFromJson(pszStr)));
14321428
}
14331429
else if (!poGeom && ((*pszStr >= '0' && *pszStr <= '9') ||
14341430
(*pszStr >= 'a' && *pszStr <= 'z') ||
14351431
(*pszStr >= 'A' && *pszStr <= 'Z')))
14361432
{
1437-
poGeom = OGRGeometryFromHexEWKB(pszStr, nullptr, FALSE);
1433+
poGeom.reset(
1434+
OGRGeometryFromHexEWKB(pszStr, nullptr, FALSE));
14381435
}
14391436
}
14401437

14411438
if (poGeom)
14421439
{
14431440
poGeom->assignSpatialReference(
14441441
poGeomFieldDefn->GetSpatialRef());
1445-
poFeature->SetGeomFieldDirectly(iGeom, poGeom);
1442+
poFeature->SetGeomField(iGeom, std::move(poGeom));
14461443
}
14471444
}
14481445

ogr/ogrsf_frmts/sqlite/ogrsqlitelayer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,10 @@ void OGRSQLiteLayer::BuildFeatureDefn(const char *pszLayerName, bool bIsSelect,
565565
{
566566
OGRSQLiteGeomFormat eGeomFormat = OSGF_None;
567567
CPLPushErrorHandler(CPLQuietErrorHandler);
568-
OGRGeometry *poGeometry = nullptr;
569-
if (OGRGeometryFactory::createFromWkt(
570-
pszText, nullptr, &poGeometry) == OGRERR_NONE)
568+
569+
auto [poGeometry, eErr] =
570+
OGRGeometryFactory::createFromWkt(pszText);
571+
if (eErr == OGRERR_NONE)
571572
{
572573
eGeomFormat = OSGF_WKT;
573574
auto poGeomFieldDefn =
@@ -579,7 +580,6 @@ void OGRSQLiteLayer::BuildFeatureDefn(const char *pszLayerName, bool bIsSelect,
579580
}
580581
CPLPopErrorHandler();
581582
CPLErrorReset();
582-
delete poGeometry;
583583
if (eGeomFormat != OSGF_None)
584584
continue;
585585
}

ogr/ogrsf_frmts/vrt/ogr_vrt.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class OGRVRTGeomFieldProps
4646
const OGRSpatialReference *poSRS;
4747

4848
bool bSrcClip;
49-
OGRGeometry *poSrcRegion;
49+
std::unique_ptr<OGRGeometry> poSrcRegion;
5050

5151
// Geometry interpretation related.
5252
OGRVRTGeometryStyle eGeometryStyle;

ogr/ogrsf_frmts/vrt/ogrvrtlayer.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ OGRVRTGeomFieldProps::~OGRVRTGeomFieldProps()
7575
{
7676
if (poSRS != nullptr)
7777
const_cast<OGRSpatialReference *>(poSRS)->Release();
78-
if (poSrcRegion != nullptr)
79-
delete poSrcRegion;
8078
}
8179

8280
/************************************************************************/
@@ -467,8 +465,9 @@ bool OGRVRTLayer::ParseGeometryField(CPLXMLNode *psNode,
467465
const char *pszSrcRegion = CPLGetXMLValue(psSrcRegionNode, "", nullptr);
468466
if (pszSrcRegion != nullptr)
469467
{
470-
OGRGeometryFactory::createFromWkt(pszSrcRegion, nullptr,
471-
&poProps->poSrcRegion);
468+
poProps->poSrcRegion =
469+
OGRGeometryFactory::createFromWkt(pszSrcRegion).first;
470+
472471
if (poProps->poSrcRegion == nullptr)
473472
{
474473
CPLError(CE_Warning, CPLE_AppDefined,
@@ -1325,7 +1324,7 @@ bool OGRVRTLayer::ResetSourceReading()
13251324
{
13261325
OGRGeometry *poNewSpatialGeom = nullptr;
13271326
OGRGeometry *poSrcRegion =
1328-
apoGeomFieldProps[m_iGeomFieldFilter]->poSrcRegion;
1327+
apoGeomFieldProps[m_iGeomFieldFilter]->poSrcRegion.get();
13291328
std::unique_ptr<OGRGeometry> poIntersection;
13301329

13311330
if (poSrcRegion == nullptr)
@@ -1442,7 +1441,8 @@ void OGRVRTLayer::ClipAndAssignSRS(OGRFeature *poFeature)
14421441
if (apoGeomFieldProps[i]->poSrcRegion != nullptr &&
14431442
apoGeomFieldProps[i]->bSrcClip && poGeom != nullptr)
14441443
{
1445-
poGeom = poGeom->Intersection(apoGeomFieldProps[i]->poSrcRegion);
1444+
poGeom =
1445+
poGeom->Intersection(apoGeomFieldProps[i]->poSrcRegion.get());
14461446
if (poGeom != nullptr)
14471447
poGeom->assignSpatialReference(
14481448
GetLayerDefn()->GetGeomFieldDefn(i)->GetSpatialRef());
@@ -1507,13 +1507,11 @@ OGRFeature *OGRVRTLayer::TranslateFeature(OGRFeature *&poSrcFeat,
15071507

15081508
if (pszWKT != nullptr)
15091509
{
1510-
OGRGeometry *poGeom = nullptr;
1511-
1512-
OGRGeometryFactory::createFromWkt(pszWKT, nullptr, &poGeom);
1510+
auto [poGeom, _] = OGRGeometryFactory::createFromWkt(pszWKT);
15131511
if (poGeom == nullptr)
15141512
CPLDebug("OGR_VRT", "Did not get geometry from %s", pszWKT);
15151513

1516-
poDstFeat->SetGeomFieldDirectly(i, poGeom);
1514+
poDstFeat->SetGeomField(i, std::move(poGeom));
15171515
}
15181516
}
15191517
else if (eGeometryStyle == VGS_WKB && iGeomField != -1)
@@ -1619,7 +1617,7 @@ OGRFeature *OGRVRTLayer::TranslateFeature(OGRFeature *&poSrcFeat,
16191617
{
16201618
OGRGeometry *poGeom = poDstFeat->GetGeomFieldRef(i);
16211619
if (poGeom != nullptr &&
1622-
!poGeom->Intersects(apoGeomFieldProps[i]->poSrcRegion))
1620+
!poGeom->Intersects(apoGeomFieldProps[i]->poSrcRegion.get()))
16231621
{
16241622
delete poSrcFeat;
16251623
delete poDstFeat;

0 commit comments

Comments
 (0)