Skip to content

Commit

Permalink
Merge pull request #11145 from dbaston/apps-add-unique-ptr
Browse files Browse the repository at this point in the history
apps: use unique_ptr overloads of addGeometry, addRing
  • Loading branch information
rouault authored Oct 29, 2024
2 parents 5479b45 + 62a2176 commit fb36ab7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
4 changes: 2 additions & 2 deletions apps/gdal_footprint_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ static bool GDALFootprintProcess(GDALDataset *poSrcDS, OGRLayer *poDstLayer,
CPLAssert(poGeom);
if (poGeom->getGeometryType() == wkbPolygon)
{
poMP->addGeometryDirectly(poGeom.release());
poMP->addGeometry(std::move(poGeom));
}
}
poMemLayer = std::make_unique<OGRMemLayer>("", nullptr, wkbUnknown);
Expand Down Expand Up @@ -1095,7 +1095,7 @@ static bool GDALFootprintProcess(GDALDataset *poSrcDS, OGRLayer *poDstLayer,
}
}
if (!poNewPoly->IsEmpty())
poMP->addGeometryDirectly(poNewPoly.release());
poMP->addGeometry(std::move(poNewPoly));
}
poGeom = std::move(poMP);
}
Expand Down
24 changes: 15 additions & 9 deletions apps/gdal_rasterize_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ static void InvertGeometries(GDALDatasetH hDstDS,
double adfGeoTransform[6] = {};
GDALGetGeoTransform(hDstDS, adfGeoTransform);

OGRLinearRing *poUniverseRing = new OGRLinearRing();
auto poUniverseRing = std::make_unique<OGRLinearRing>();

poUniverseRing->addPoint(
adfGeoTransform[0] + -2 * adfGeoTransform[1] + -2 * adfGeoTransform[2],
Expand All @@ -391,9 +391,9 @@ static void InvertGeometries(GDALDatasetH hDstDS,
adfGeoTransform[0] + -2 * adfGeoTransform[1] + -2 * adfGeoTransform[2],
adfGeoTransform[3] + -2 * adfGeoTransform[4] + -2 * adfGeoTransform[5]);

OGRPolygon *poUniversePoly = new OGRPolygon();
poUniversePoly->addRingDirectly(poUniverseRing);
poInvertMP->addGeometryDirectly(poUniversePoly);
auto poUniversePoly = std::make_unique<OGRPolygon>();
poUniversePoly->addRing(std::move(poUniverseRing));
poInvertMP->addGeometry(std::move(poUniversePoly));

bool bFoundNonPoly = false;
// If we have GEOS, use it to "subtract" each polygon from the universe
Expand Down Expand Up @@ -434,6 +434,9 @@ static void InvertGeometries(GDALDatasetH hDstDS,
return;
}

OGRPolygon &hUniversePoly =
*poInvertMP->getGeometryRef(poInvertMP->getNumGeometries() - 1);

/* -------------------------------------------------------------------- */
/* If we don't have GEOS, add outer rings of polygons as inner */
/* rings of poUniversePoly and inner rings as sub-polygons. Note */
Expand All @@ -460,15 +463,18 @@ static void InvertGeometries(GDALDatasetH hDstDS,
}

const auto ProcessPoly =
[poUniversePoly, poInvertMP](OGRPolygon *poPoly)
[&hUniversePoly, poInvertMP](OGRPolygon *poPoly)
{
for (int i = poPoly->getNumInteriorRings() - 1; i >= 0; --i)
{
auto poNewPoly = new OGRPolygon();
poNewPoly->addRingDirectly(poPoly->stealInteriorRing(i));
poInvertMP->addGeometryDirectly(poNewPoly);
auto poNewPoly = std::make_unique<OGRPolygon>();
std::unique_ptr<OGRLinearRing> poRing(
poPoly->stealInteriorRing(i));
poNewPoly->addRing(std::move(poRing));
poInvertMP->addGeometry(std::move(poNewPoly));
}
poUniversePoly->addRingDirectly(poPoly->stealExteriorRing());
std::unique_ptr<OGRLinearRing> poShell(poPoly->stealExteriorRing());
hUniversePoly.addRing(std::move(poShell));
};

if (eGType == wkbPolygon)
Expand Down
2 changes: 1 addition & 1 deletion apps/gdaltindex_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ GDALDatasetH GDALTileIndex(const char *pszDest, int nSrcCount,
auto poRing = std::make_unique<OGRLinearRing>();
for (int k = 0; k < 5; k++)
poRing->addPoint(adfX[k], adfY[k]);
poPoly->addRingDirectly(poRing.release());
poPoly->addRing(std::move(poRing));
poFeature->SetGeometryDirectly(poPoly.release());

if (poLayer->CreateFeature(poFeature.get()) != OGRERR_NONE)
Expand Down
6 changes: 3 additions & 3 deletions apps/gdalwarp_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3427,7 +3427,7 @@ static CPLErr LoadCutline(const std::string &osCutlineDSNameOrWKT,
OGRwkbGeometryType eType = wkbFlatten(poGeom->getGeometryType());

if (eType == wkbPolygon)
poMultiPolygon->addGeometryDirectly(poGeom.release());
poMultiPolygon->addGeometry(std::move(poGeom));
else if (eType == wkbMultiPolygon)
{
for (const auto *poSubGeom : poGeom->toMultiPolygon())
Expand Down Expand Up @@ -5401,7 +5401,7 @@ static CPLErr TransformCutlineToSource(GDALDataset *poSrcDS,
{
const double dfCutlineBlendDist = CPLAtof(CSLFetchNameValueDef(
*ppapszWarpOptions, "CUTLINE_BLEND_DIST", "0"));
OGRLinearRing *poRing = new OGRLinearRing();
auto poRing = std::make_unique<OGRLinearRing>();
poRing->addPoint(-dfCutlineBlendDist, -dfCutlineBlendDist);
poRing->addPoint(-dfCutlineBlendDist,
dfCutlineBlendDist + poSrcDS->GetRasterYSize());
Expand All @@ -5411,7 +5411,7 @@ static CPLErr TransformCutlineToSource(GDALDataset *poSrcDS,
-dfCutlineBlendDist);
poRing->addPoint(-dfCutlineBlendDist, -dfCutlineBlendDist);
OGRPolygon oSrcDSFootprint;
oSrcDSFootprint.addRingDirectly(poRing);
oSrcDSFootprint.addRing(std::move(poRing));
OGREnvelope sSrcDSEnvelope;
oSrcDSFootprint.getEnvelope(&sSrcDSEnvelope);
OGREnvelope sCutlineEnvelope;
Expand Down
6 changes: 3 additions & 3 deletions apps/ogr2ogr_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,21 +688,21 @@ static std::unique_ptr<OGRGeometry> LoadGeometry(const std::string &osDS,
"should be manually inspected.",
poFeat->GetFID(), osDS.c_str());

oGC.addGeometryDirectly(poValid.release());
oGC.addGeometry(std::move(poValid));
}
else
{
CPLError(CE_Failure, CPLE_AppDefined,
"Geometry of feature " CPL_FRMT_GIB " of %s "
"is invalid, and could not been made valid.",
"is invalid, and could not be made valid.",
poFeat->GetFID(), osDS.c_str());
oGC.empty();
break;
}
}
else
{
oGC.addGeometryDirectly(poSrcGeom.release());
oGC.addGeometry(std::move(poSrcGeom));
}
}
}
Expand Down

0 comments on commit fb36ab7

Please sign in to comment.