Skip to content

Commit 0007c3d

Browse files
committed
Use new OGRGeometryFactory::forceTo() variant that takes and returns std::unique_ptr
1 parent b10432b commit 0007c3d

19 files changed

+139
-119
lines changed

apps/gdalalg_vector_buffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ std::unique_ptr<OGRFeature> GDALVectorBufferAlgorithmLayer::TranslateFeature(
139139
{
140140
const auto poGeomFieldDefn =
141141
m_poFeatureDefn->GetGeomFieldDefn(i);
142-
poGeom.reset(OGRGeometryFactory::forceTo(
143-
poGeom.release(), poGeomFieldDefn->GetType()));
142+
poGeom = OGRGeometryFactory::forceTo(
143+
std::move(poGeom), poGeomFieldDefn->GetType());
144144
if (poGeom)
145145
{
146146
poGeom->assignSpatialReference(

apps/gdalalg_vector_clip.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,24 @@ class GDALVectorClipAlgorithmLayer final : public GDALVectorPipelineOutputLayer
139139
else if (OGR_GT_GetCollection(eFeatGeomType) ==
140140
m_eFlattenSrcLayerGeomType)
141141
{
142-
poIntersection.reset(OGRGeometryFactory::forceTo(
143-
poIntersection.release(), m_eSrcLayerGeomType));
144-
poSrcFeature->SetGeometryDirectly(poIntersection.release());
142+
poIntersection = OGRGeometryFactory::forceTo(
143+
std::move(poIntersection), m_eSrcLayerGeomType);
144+
poSrcFeature->SetGeometry(std::move(poIntersection));
145145
apoOutFeatures.push_back(std::move(poSrcFeature));
146146
}
147147
else if (m_eFlattenSrcLayerGeomType == wkbGeometryCollection)
148148
{
149149
auto poGeomColl = std::make_unique<OGRGeometryCollection>();
150150
poGeomColl->addGeometry(std::move(poIntersection));
151-
poSrcFeature->SetGeometryDirectly(poGeomColl.release());
151+
poSrcFeature->SetGeometry(std::move(poGeomColl));
152152
apoOutFeatures.push_back(std::move(poSrcFeature));
153153
}
154154
// else discard geometries of incompatible type with the
155155
// layer geometry type
156156
}
157157
else
158158
{
159-
poSrcFeature->SetGeometryDirectly(poIntersection.release());
159+
poSrcFeature->SetGeometry(std::move(poIntersection));
160160
apoOutFeatures.push_back(std::move(poSrcFeature));
161161
}
162162
}

apps/gdalalg_vector_explode_collections.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ void GDALVectorExplodeCollectionsAlgorithmLayer::TranslateFeature(
206206
auto poNewGeom =
207207
std::unique_ptr<OGRGeometry>(poSubGeomRef->clone());
208208
if (poNewGeom->getGeometryType() != eTargetType)
209-
poNewGeom.reset(OGRGeometryFactory::forceTo(
210-
poNewGeom.release(), eTargetType));
209+
poNewGeom = OGRGeometryFactory::forceTo(
210+
std::move(poNewGeom), eTargetType);
211211
if (m_opts.m_skip && !m_opts.m_type.empty() &&
212212
(!poNewGeom ||
213213
(wkbFlatten(eTargetType) != wkbUnknown &&

apps/gdalalg_vector_geom.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ GDALGeosNonStreamingAlgorithmLayer::GetNextProcessedFeature()
317317
wkbFlatten(poResultGeom->getGeometryType()) !=
318318
wkbFlatten(eLayerGeomType))
319319
{
320-
poResultGeom.reset(OGRGeometryFactory::forceTo(poResultGeom.release(),
321-
eLayerGeomType));
320+
poResultGeom = OGRGeometryFactory::forceTo(std::move(poResultGeom),
321+
eLayerGeomType);
322322
}
323323

324324
if (poResultGeom == nullptr)

apps/gdalalg_vector_set_geom_type.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,12 @@ GDALVectorSetGeomTypeAlgorithmLayer::TranslateFeature(
224224
const auto poGeomFieldDefn = m_poFeatureDefn->GetGeomFieldDefn(i);
225225
if (!m_opts.m_layerOnly && IsSelectedGeomField(i))
226226
{
227-
poGeom = poSrcFeature->StealGeometry(i);
228-
const auto eTargetType = ConvertType(poGeom->getGeometryType());
229227
auto poNewGeom = std::unique_ptr<OGRGeometry>(
230-
OGRGeometryFactory::forceTo(poGeom, eTargetType));
228+
poSrcFeature->StealGeometry(i));
229+
const auto eTargetType =
230+
ConvertType(poNewGeom->getGeometryType());
231+
poNewGeom = OGRGeometryFactory::forceTo(std::move(poNewGeom),
232+
eTargetType);
231233
if (m_opts.m_skip &&
232234
(!poNewGeom ||
233235
(wkbFlatten(eTargetType) != wkbUnknown &&
@@ -237,7 +239,7 @@ GDALVectorSetGeomTypeAlgorithmLayer::TranslateFeature(
237239
}
238240
poNewGeom->assignSpatialReference(
239241
poGeomFieldDefn->GetSpatialRef());
240-
poSrcFeature->SetGeomFieldDirectly(i, poNewGeom.release());
242+
poSrcFeature->SetGeomField(i, std::move(poNewGeom));
241243
}
242244
else
243245
{

apps/ogr2ogr_lib.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7025,8 +7025,8 @@ bool LayerTranslator::Translate(
70257025
{
70267026
OGRwkbGeometryType eTargetType = OGR_GT_GetLinear(
70277027
poDstGeometry->getGeometryType());
7028-
poDstGeometry.reset(OGRGeometryFactory::forceTo(
7029-
poDstGeometry.release(), eTargetType));
7028+
poDstGeometry = OGRGeometryFactory::forceTo(
7029+
std::move(poDstGeometry), eTargetType);
70307030
}
70317031
}
70327032
else if (bReprojCanInvalidateValidity &&
@@ -7035,9 +7035,9 @@ bool LayerTranslator::Translate(
70357035
static_cast<OGRwkbGeometryType>(eGType)) &&
70367036
poDstGeometry->hasCurveGeometry(TRUE))
70377037
{
7038-
poDstGeometry.reset(OGRGeometryFactory::forceTo(
7039-
poDstGeometry.release(),
7040-
static_cast<OGRwkbGeometryType>(eGType)));
7038+
poDstGeometry = OGRGeometryFactory::forceTo(
7039+
std::move(poDstGeometry),
7040+
static_cast<OGRwkbGeometryType>(eGType));
70417041
}
70427042

70437043
// Collect left-most, right-most, top-most, bottom-most coordinates.
@@ -7124,21 +7124,20 @@ bool LayerTranslator::Translate(
71247124
{
71257125
OGRwkbGeometryType eTargetType = OGR_GT_GetLinear(
71267126
poDstGeometry->getGeometryType());
7127-
auto poDstGeometryTmp =
7127+
auto poDstGeometryTmp = OGRGeometryFactory::forceTo(
71287128
std::unique_ptr<OGRGeometry>(
7129-
OGRGeometryFactory::forceTo(
7130-
poReprojectedGeom->clone(),
7131-
eTargetType));
7129+
poReprojectedGeom->clone()),
7130+
eTargetType);
71327131
if (!IsValid(poDstGeometryTmp.get()))
71337132
{
71347133
CPLDebug("OGR2OGR",
71357134
"Curve geometry no longer valid after "
71367135
"reprojection: transforming it into "
71377136
"linear one before reprojecting");
7138-
poDstGeometry.reset(OGRGeometryFactory::forceTo(
7139-
poDstGeometry.release(), eTargetType));
7140-
poDstGeometry.reset(OGRGeometryFactory::forceTo(
7141-
poDstGeometry.release(), eType));
7137+
poDstGeometry = OGRGeometryFactory::forceTo(
7138+
std::move(poDstGeometry), eTargetType);
7139+
poDstGeometry = OGRGeometryFactory::forceTo(
7140+
std::move(poDstGeometry), eType);
71427141
}
71437142
else
71447143
{
@@ -7273,19 +7272,18 @@ bool LayerTranslator::Translate(
72737272
poDstGeometry->getGeometryType();
72747273
eTargetType =
72757274
ConvertType(m_eGeomTypeConversion, eTargetType);
7276-
poDstGeometry.reset(OGRGeometryFactory::forceTo(
7277-
poDstGeometry.release(), eTargetType));
7275+
poDstGeometry = OGRGeometryFactory::forceTo(
7276+
std::move(poDstGeometry), eTargetType);
72787277
}
72797278
else if (eGType != GEOMTYPE_UNCHANGED)
72807279
{
7281-
poDstGeometry.reset(OGRGeometryFactory::forceTo(
7282-
poDstGeometry.release(),
7283-
static_cast<OGRwkbGeometryType>(eGType)));
7280+
poDstGeometry = OGRGeometryFactory::forceTo(
7281+
std::move(poDstGeometry),
7282+
static_cast<OGRwkbGeometryType>(eGType));
72847283
}
72857284
}
72867285

7287-
poDstFeature->SetGeomFieldDirectly(iGeom,
7288-
poDstGeometry.release());
7286+
poDstFeature->SetGeomField(iGeom, std::move(poDstGeometry));
72897287
}
72907288

72917289
CPLErrorReset();

frmts/ogcapi/gdalogcapidataset.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ class OGCAPITiledLayer final
253253
OGRFeature *GetNextRawFeature();
254254
GDALDataset *OpenTile(int nX, int nY, bool &bEmptyContent);
255255
void FinalizeFeatureDefnWithLayer(OGRLayer *poUnderlyingLayer);
256-
OGRFeature *BuildFeature(OGRFeature *poSrcFeature, int nX, int nY);
256+
OGRFeature *BuildFeature(std::unique_ptr<OGRFeature> poSrcFeature, int nX,
257+
int nY);
257258

258259
CPL_DISALLOW_COPY_ASSIGN(OGCAPITiledLayer)
259260

@@ -2532,8 +2533,9 @@ void OGCAPITiledLayer::FinalizeFeatureDefnWithLayer(OGRLayer *poUnderlyingLayer)
25322533
/* BuildFeature() */
25332534
/************************************************************************/
25342535

2535-
OGRFeature *OGCAPITiledLayer::BuildFeature(OGRFeature *poSrcFeature, int nX,
2536-
int nY)
2536+
OGRFeature *
2537+
OGCAPITiledLayer::BuildFeature(std::unique_ptr<OGRFeature> poSrcFeature, int nX,
2538+
int nY)
25372539
{
25382540
int nCoalesce = GetCoalesceFactorForRow(nY);
25392541
if (nCoalesce <= 0)
@@ -2544,21 +2546,20 @@ OGRFeature *OGCAPITiledLayer::BuildFeature(OGRFeature *poSrcFeature, int nX,
25442546
const GIntBig nFID = nY * m_oTileMatrix.mMatrixWidth + nX +
25452547
poSrcFeature->GetFID() * m_oTileMatrix.mMatrixWidth *
25462548
m_oTileMatrix.mMatrixHeight;
2547-
auto poGeom = poSrcFeature->StealGeometry();
2549+
auto poGeom = std::unique_ptr<OGRGeometry>(poSrcFeature->StealGeometry());
25482550
if (poGeom && m_poFeatureDefn->GetGeomType() != wkbUnknown)
25492551
{
2550-
poGeom =
2551-
OGRGeometryFactory::forceTo(poGeom, m_poFeatureDefn->GetGeomType());
2552+
poGeom = OGRGeometryFactory::forceTo(std::move(poGeom),
2553+
m_poFeatureDefn->GetGeomType());
25522554
}
2553-
poFeature->SetFrom(poSrcFeature, true);
2555+
poFeature->SetFrom(poSrcFeature.get(), true);
25542556
poFeature->SetFID(nFID);
25552557
if (poGeom && m_poFeatureDefn->GetGeomFieldCount() > 0)
25562558
{
25572559
poGeom->assignSpatialReference(
25582560
m_poFeatureDefn->GetGeomFieldDefn(0)->GetSpatialRef());
25592561
}
2560-
poFeature->SetGeometryDirectly(poGeom);
2561-
delete poSrcFeature;
2562+
poFeature->SetGeometry(std::move(poGeom));
25622563
return poFeature;
25632564
}
25642565

@@ -2623,10 +2624,11 @@ OGRFeature *OGCAPITiledLayer::GetNextRawFeature()
26232624
FinalizeFeatureDefnWithLayer(m_poUnderlyingLayer);
26242625
}
26252626

2626-
auto poSrcFeature = m_poUnderlyingLayer->GetNextFeature();
2627+
auto poSrcFeature =
2628+
std::unique_ptr<OGRFeature>(m_poUnderlyingLayer->GetNextFeature());
26272629
if (poSrcFeature != nullptr)
26282630
{
2629-
return BuildFeature(poSrcFeature, m_nCurX, m_nCurY);
2631+
return BuildFeature(std::move(poSrcFeature), m_nCurX, m_nCurY);
26302632
}
26312633

26322634
m_poUnderlyingDS.reset();
@@ -2660,10 +2662,11 @@ OGRFeature *OGCAPITiledLayer::GetFeature(GIntBig nFID)
26602662
if (poUnderlyingLayer == nullptr)
26612663
return nullptr;
26622664
FinalizeFeatureDefnWithLayer(poUnderlyingLayer);
2663-
OGRFeature *poSrcFeature = poUnderlyingLayer->GetFeature(nFIDInTile);
2665+
auto poSrcFeature =
2666+
std::unique_ptr<OGRFeature>(poUnderlyingLayer->GetFeature(nFIDInTile));
26642667
if (poSrcFeature == nullptr)
26652668
return nullptr;
2666-
return BuildFeature(poSrcFeature, nX, nY);
2669+
return BuildFeature(std::move(poSrcFeature), nX, nY);
26672670
}
26682671

26692672
/************************************************************************/

ogr/ogr_geometry.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4263,7 +4263,12 @@ class CPL_DLL OGRGeometryFactory
42634263

42644264
static OGRGeometry *forceTo(OGRGeometry *poGeom,
42654265
OGRwkbGeometryType eTargetType,
4266-
const char *const *papszOptions = nullptr);
4266+
const char *const *papszOptions = nullptr)
4267+
#ifndef DOXYGEN_SKIP
4268+
CPL_WARN_DEPRECATED("Use variant that accepts and returns a "
4269+
"std::unique_ptr<OGRGeometry")
4270+
#endif
4271+
;
42674272

42684273
static std::unique_ptr<OGRGeometry>
42694274
forceTo(std::unique_ptr<OGRGeometry> poGeom, OGRwkbGeometryType eTargetType,

ogr/ogrfeature.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,10 @@ OGRGeometryH OGR_F_GetGeometryRef(OGRFeatureH hFeat)
793793
{
794794
const OGRwkbGeometryType eTargetType =
795795
OGR_GT_GetLinear(poGeom->getGeometryType());
796-
poGeom = OGRGeometryFactory::forceTo(poFeature->StealGeometry(),
797-
eTargetType);
798-
poFeature->SetGeomFieldDirectly(0, poGeom);
796+
auto poNewGeom = OGRGeometryFactory::forceTo(
797+
std::unique_ptr<OGRGeometry>(poFeature->StealGeometry()),
798+
eTargetType);
799+
poFeature->SetGeomField(0, std::move(poNewGeom));
799800
poGeom = poFeature->GetGeometryRef();
800801
}
801802

@@ -915,9 +916,10 @@ OGRGeometryH OGR_F_GetGeomFieldRef(OGRFeatureH hFeat, int iField)
915916
{
916917
const OGRwkbGeometryType eTargetType =
917918
OGR_GT_GetLinear(poGeom->getGeometryType());
918-
poGeom = OGRGeometryFactory::forceTo(poFeature->StealGeometry(iField),
919-
eTargetType);
920-
poFeature->SetGeomFieldDirectly(iField, poGeom);
919+
auto poNewGeom = OGRGeometryFactory::forceTo(
920+
std::unique_ptr<OGRGeometry>(poFeature->StealGeometry(iField)),
921+
eTargetType);
922+
poFeature->SetGeomField(iField, std::move(poNewGeom));
921923
poGeom = poFeature->GetGeomFieldRef(iField);
922924
}
923925

ogr/ogrgeojsonwriter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,10 +1142,10 @@ GetLinearCollection(const OGRGeometryCollection *poGeomColl)
11421142
else
11431143
{
11441144
auto poNewGeom = OGRGeometryFactory::forceTo(
1145-
poSubGeom->clone(),
1145+
std::unique_ptr<OGRGeometry>(poSubGeom->clone()),
11461146
OGR_GT_GetLinear(poSubGeom->getGeometryType()));
11471147
if (poNewGeom)
1148-
poFlatGeom->addGeometryDirectly(poNewGeom);
1148+
poFlatGeom->addGeometry(std::move(poNewGeom));
11491149
}
11501150
}
11511151
return poFlatGeom;
@@ -1176,8 +1176,8 @@ json_object *OGRGeoJSONWriteGeometry(const OGRGeometry *poGeometry,
11761176
}
11771177
else
11781178
{
1179-
poFlatGeom.reset(
1180-
OGRGeometryFactory::forceTo(poGeometry->clone(), eTargetType));
1179+
poFlatGeom = OGRGeometryFactory::forceTo(
1180+
std::unique_ptr<OGRGeometry>(poGeometry->clone()), eTargetType);
11811181
}
11821182
return OGRGeoJSONWriteGeometry(poFlatGeom.get(), oOptions);
11831183
}

0 commit comments

Comments
 (0)