Skip to content

Commit

Permalink
Merge pull request #10754 from rouault/OGRJSONFGGetOGRGeometryType
Browse files Browse the repository at this point in the history
JSONFG: avoid Polyhedron/Prism geometry instantiation during initial scan
  • Loading branch information
rouault committed Sep 18, 2024
2 parents 86cfe19 + b2e5b23 commit 5dfd063
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion autotest/ogr/ogr_jsonfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ def test_jsonfg_write_several_layers():
[["GEOMETRYCOLLECTION (POINT (1 2))"], ogr.wkbGeometryCollection],
[["GEOMETRYCOLLECTION Z (POINT Z (1 2 3))"], ogr.wkbGeometryCollection25D],
[["POINT (1 2)", "LINESTRING (1 2,3 4)"], ogr.wkbUnknown],
[["POLYHEDRALSURFACE EMPTY"], ogr.wkbPolyhedralSurface],
[["POLYHEDRALSURFACE Z EMPTY"], ogr.wkbPolyhedralSurfaceZ],
[
[
"POLYHEDRALSURFACE Z (((0 0 0,0 1 0,1 1 0,0 0 0)),((0 0 0,1 0 0,0 0 1,0 0 0)),((0 0 0,0 1 0,0 0 1,0 0 0)),((0 1 0,1 0 0,0 0 1,0 1 0)))"
Expand Down
64 changes: 48 additions & 16 deletions ogr/ogrsf_frmts/jsonfg/ogrjsonfgreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,50 @@ const char *OGRJSONFGReader::GetLayerNameForFeature(json_object *poObj) const
return pszName;
}

/************************************************************************/
/* OGRJSONFGGetOGRGeometryType() */
/************************************************************************/

static OGRwkbGeometryType OGRJSONFGGetOGRGeometryType(json_object *poObj)
{
const auto eType = OGRGeoJSONGetOGRGeometryType(poObj);
if (eType != wkbUnknown)
return eType;

json_object *poObjType = CPL_json_object_object_get(poObj, "type");
const char *pszType = json_object_get_string(poObjType);
if (!pszType)
return wkbNone;

if (strcmp(pszType, "Polyhedron") == 0)
{
return wkbPolyhedralSurfaceZ;
}
else if (strcmp(pszType, "Prism") == 0)
{
auto poBase = CPL_json_object_object_get(poObj, "base");
if (!poBase || json_object_get_type(poBase) != json_type_object)
{
return wkbNone;
}

const auto eBaseGeomType = OGRGeoJSONGetOGRGeometryType(poBase);
if (eBaseGeomType == wkbPoint)
{
return wkbLineString25D;
}
else if (eBaseGeomType == wkbLineString)
{
return wkbMultiPolygon25D;
}
else if (eBaseGeomType == wkbPolygon)
{
return wkbPolyhedralSurfaceZ;
}
}
return wkbNone;
}

/************************************************************************/
/* OGRJSONFGCreateNonGeoJSONGeometry() */
/************************************************************************/
Expand Down Expand Up @@ -708,6 +752,8 @@ OGRJSONFGCreateNonGeoJSONGeometry(json_object *poObj, bool bWarn)
if (poGeom->addGeometryDirectly(poPoly) != OGRERR_NONE)
return nullptr;
}
if (nPolys == 0)
poGeom->set3D(true);

return poGeom;
}
Expand Down Expand Up @@ -904,22 +950,8 @@ bool OGRJSONFGReader::GenerateLayerDefnFromFeature(json_object *poObj)
(eGeometryElement_ != GeometryElement::PLACE);
if (poPlace && json_object_get_type(poPlace) == json_type_object)
{
const auto eType = OGRGeoJSONGetOGRGeometryType(poPlace);
if (eType == wkbUnknown)
{
auto poGeom =
OGRJSONFGCreateNonGeoJSONGeometry(poPlace, /*bWarn=*/true);
if (poGeom)
{
bFallbackToGeometry = false;
poContext->bDetectLayerGeomType =
OGRGeoJSONUpdateLayerGeomType(
poContext->bFirstGeometry,
poGeom->getGeometryType(),
poContext->eLayerGeomType);
}
}
else
const auto eType = OGRJSONFGGetOGRGeometryType(poPlace);
if (eType != wkbNone)
{
bFallbackToGeometry = false;
poContext->bDetectLayerGeomType = OGRGeoJSONUpdateLayerGeomType(
Expand Down

0 comments on commit 5dfd063

Please sign in to comment.