Skip to content

Commit

Permalink
netCDF: avoid warnings when opening in raster/vector mode a vector la…
Browse files Browse the repository at this point in the history
…yer that has max-width string field
  • Loading branch information
rouault committed Mar 1, 2020
1 parent 73f599f commit fda508b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
14 changes: 14 additions & 0 deletions autotest/gdrivers/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4917,6 +4917,20 @@ def test_netcdf_create():
gdal.Unlink('tmp/test_create.nc')


def test_netcdf_sg1_8_max_variable_with_max_width_string_field_no_warning():

gdal.VectorTranslate("tmp/poly.nc", "../ogr/data/poly.shp", format="netCDF")

gdal.ErrorReset()
# Check that opening in raster/vector mode doesn't emit warning
ds = gdal.OpenEx("tmp/poly.nc")
assert gdal.GetLastErrorType() == 0
assert ds
assert ds.GetLayerCount() == 1
ds = None
gdal.Unlink('tmp/poly.nc')


def test_clean_tmp():
# [KEEP THIS AS THE LAST TEST]
# i.e. please do not add any tests after this one. Put new ones above.
Expand Down
34 changes: 22 additions & 12 deletions gdal/frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11323,6 +11323,12 @@ CPLErr netCDFDataset::FilterVars( int nCdfId, bool bKeepRasters,
szTemp[0] = '\0';
NCDF_ERR_RET(nc_inq_varname(nCdfId, v, szTemp));

if( strstr(szTemp, "_node_coordinates") || strstr(szTemp, "_node_count") )
{
// Ignore CF-1.8 Simple Geometries helper variables
continue;
}

if( nVarDims == 1 && (NCDFIsVarLongitude(nCdfId, -1, szTemp) ||
NCDFIsVarProjectionX(nCdfId, -1, szTemp)) )
{
Expand Down Expand Up @@ -11357,7 +11363,7 @@ CPLErr netCDFDataset::FilterVars( int nCdfId, bool bKeepRasters,
// Only accept 2+D vars.
else if( nVarDims >= 2 )
{

bool bRasterCandidate = true;
// Identify variables that might be vector variables
if( nVarDims == 2 )
{
Expand All @@ -11367,22 +11373,26 @@ CPLErr netCDFDataset::FilterVars( int nCdfId, bool bKeepRasters,
nc_type vartype = NC_NAT;
nc_inq_vartype(nCdfId, v, &vartype);

char szDimNameX[NC_MAX_NAME + 1];
char szDimNameY[NC_MAX_NAME + 1];
szDimNameX[0] = '\0';
szDimNameY[0] = '\0';
char szDimNameFirst[NC_MAX_NAME + 1];
char szDimNameSecond[NC_MAX_NAME + 1];
szDimNameFirst[0] = '\0';
szDimNameSecond[0] = '\0';
if( vartype == NC_CHAR &&
nc_inq_dimname(nCdfId, anDimIds[0], szDimNameY) ==
nc_inq_dimname(nCdfId, anDimIds[0], szDimNameFirst) ==
NC_NOERR &&
nc_inq_dimname(nCdfId, anDimIds[1], szDimNameX) ==
nc_inq_dimname(nCdfId, anDimIds[1], szDimNameSecond) ==
NC_NOERR &&
!NCDFIsVarLongitude(nCdfId, -1, szDimNameX) &&
!NCDFIsVarProjectionX(nCdfId, -1, szDimNameX) &&
!NCDFIsVarLatitude(nCdfId, -1, szDimNameY) &&
!NCDFIsVarProjectionY(nCdfId, -1, szDimNameY) )
!NCDFIsVarLongitude(nCdfId, -1, szDimNameSecond) &&
!NCDFIsVarProjectionX(nCdfId, -1, szDimNameSecond) &&
!NCDFIsVarLatitude(nCdfId, -1, szDimNameFirst) &&
!NCDFIsVarProjectionY(nCdfId, -1, szDimNameFirst) )
{
anPotentialVectorVarID.push_back(v);
oMapDimIdToCount[anDimIds[0]]++;
if( strstr( szDimNameSecond, "_max_width") )
{
bRasterCandidate = false;
}
}
else
{
Expand All @@ -11393,7 +11403,7 @@ CPLErr netCDFDataset::FilterVars( int nCdfId, bool bKeepRasters,
{
bIsVectorOnly = false;
}
if( bKeepRasters )
if( bKeepRasters && bRasterCandidate )
{
*pnGroupId = nCdfId;
*pnVarId = v;
Expand Down

0 comments on commit fda508b

Please sign in to comment.