Skip to content

Commit

Permalink
Python bindings: make MDArray.Write(array_of_strings) work with a 0-d…
Browse files Browse the repository at this point in the history
… string variable
  • Loading branch information
rouault committed Aug 12, 2024
1 parent c0c6ed4 commit af45c6e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
39 changes: 21 additions & 18 deletions swig/include/MultiDimensional.i
Original file line number Diff line number Diff line change
Expand Up @@ -691,8 +691,8 @@ public:
{

const int nExpectedDims = (int)GDALMDArrayGetDimensionCount(self);
std::vector<size_t> count_internal(nExpectedDims);
if( nExpectedDims != 1 )
std::vector<size_t> count_internal(nExpectedDims + 1);
if( nExpectedDims > 1 )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Unsupported number of dimensions");
Expand All @@ -707,23 +707,26 @@ public:
return CE_Failure;
}
}
if( nDims1 != 1 )
if( nExpectedDims == 1 )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Wrong number of values in array_start_idx");
return CE_Failure;
}
if( nDims2 != 1 )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Wrong number of values in count");
return CE_Failure;
}
if( nDims3 != 1 )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Wrong number of values in array_step");
return CE_Failure;
if( nDims1 != 1 )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Wrong number of values in array_start_idx");
return CE_Failure;
}
if( nDims2 != 1 )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Wrong number of values in count");
return CE_Failure;
}
if( nDims3 != 1 )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Wrong number of values in array_step");
return CE_Failure;
}
}

CPLErr eErr = GDALMDArrayWrite(self,
Expand Down
6 changes: 3 additions & 3 deletions swig/include/python/gdal_python.i
Original file line number Diff line number Diff line change
Expand Up @@ -1921,13 +1921,13 @@ def GetMDArrayNames(self, options = []) -> "list[str]":
if not buffer_datatype:
buffer_datatype = self.GetDataType()

is_1d_string = self.GetDataType().GetClass() == GEDTC_STRING and buffer_datatype.GetClass() == GEDTC_STRING and dimCount == 1
is_0d_or_1d_string = self.GetDataType().GetClass() == GEDTC_STRING and buffer_datatype.GetClass() == GEDTC_STRING and dimCount <= 1

if not array_start_idx:
array_start_idx = [0] * dimCount

if not count:
if is_1d_string:
if is_0d_or_1d_string:
assert type(buffer) == type([])
count = [ len(buffer) ]
else:
Expand All @@ -1946,7 +1946,7 @@ def GetMDArrayNames(self, options = []) -> "list[str]":
stride *= cnt
buffer_stride.reverse()

if is_1d_string:
if is_0d_or_1d_string:
return _gdal.MDArray_WriteStringArray(self, array_start_idx, count, array_step, buffer_datatype, buffer)

return _gdal.MDArray_Write(self, array_start_idx, count, array_step, buffer_stride, buffer_datatype, buffer)
Expand Down

0 comments on commit af45c6e

Please sign in to comment.