Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OGRGeometryFactory::forceTo(): fix assertion with empty geometry and target type = unknown #9181

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions autotest/cpp/test_ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ TEST_F(test_ogr, geometry_get_point)
}
}

TEST_F(test_ogr, OGR_G_CreateGeometry_unknown)
{
EXPECT_EQ(OGR_G_CreateGeometry(wkbUnknown), nullptr);
}

TEST_F(test_ogr, style_manager)
{
OGRStyleMgrH hSM = OGR_SM_Create(nullptr);
Expand Down
25 changes: 25 additions & 0 deletions autotest/ogr/ogr_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,31 @@ def test_ogr_factory_8():
ogrtest.check_feature_geometry(dst_geom, exp_wkt)


###############################################################################
# Test forceTo() to wkbUnknown


@pytest.mark.parametrize(
"src_wkt,target_type,exp_wkt",
[
(
"POINT (1 2)",
ogr.wkbUnknown,
"POINT (1 2)",
),
(
"POINT EMPTY",
ogr.wkbUnknown,
"POINT EMPTY",
),
],
)
def test_ogr_factory_forceTo_unknown(src_wkt, target_type, exp_wkt):
src_geom = ogr.CreateGeometryFromWkt(src_wkt)
dst_geom = ogr.ForceTo(src_geom, target_type)
ogrtest.check_feature_geometry(dst_geom, exp_wkt)


###############################################################################
# Test forceTo()

Expand Down
11 changes: 7 additions & 4 deletions ogr/ogrgeometryfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,9 @@ OGRGeometryFactory::createGeometry(OGRwkbGeometryType eGeometryType)
poGeom = new (std::nothrow) OGRTriangulatedSurface();
break;

case wkbUnknown:
break;

default:
CPLAssert(false);
break;
Expand Down Expand Up @@ -4551,6 +4554,10 @@ OGRGeometry *OGRGeometryFactory::forceTo(OGRGeometry *poGeom,
if (poGeom == nullptr)
return poGeom;

const OGRwkbGeometryType eTargetTypeFlat = wkbFlatten(eTargetType);
if (eTargetTypeFlat == wkbUnknown)
return poGeom;

if (poGeom->IsEmpty())
{
OGRGeometry *poRet = createGeometry(eTargetType);
Expand All @@ -4564,10 +4571,6 @@ OGRGeometry *OGRGeometryFactory::forceTo(OGRGeometry *poGeom,
return poRet;
}

const OGRwkbGeometryType eTargetTypeFlat = wkbFlatten(eTargetType);
if (eTargetTypeFlat == wkbUnknown)
return poGeom;

OGRwkbGeometryType eType = poGeom->getGeometryType();
OGRwkbGeometryType eTypeFlat = wkbFlatten(eType);

Expand Down
Loading