Resizing a Grid after it has been created but not yet closed

After creating a grid with Grid.CreateNew, is there a way to change the NumberRows field in the header such that the file written has a different number of rows than when it was created?

The reason I would like to do this is because I am working on very large files and occasionally, I would like to stop the process at some point and pick it up again at a later time. If I only write part of the grid and then close the file, the Close statement seems to continue to write the rest of the grid even though the data has not been specified. It does not seem to matter if I change NumberRows or not, the full file is still written. Basically, it fills the remaining rows with NoDataValue. This can take a considerable amount of time and I would rather truncate the file and then expand it at a later point in time.

Is this possible with MapWinGis? (i.e. without creating a secondary file)

Hello @markinpt, and welcome.

I’m sorry there have been no replies as of yet. I know nothing about Grids, but I could try to have a look at the source code to see if I might find some clues. Please give me a day or so.


Thanks for your help.

Hello again.

Ok, from what it looks like in the code, the Grid attributes, including the width and height, are established up front based on the values specified in the Header, and are never referred to again. From this, the grid is set up, memory is allocated, etc. The output algorithm, likewise, iterates rows and columns based on the initial values.

So changing any values in the Header, after creation, will have no affect on the grid (or grid size) that has already been established. The only way to change this behavior would be to modify the OCX source code.

I’m sorry.

Thanks! That is kind of what I was expecting. If I am using GeoTIFF output raster (float32), which routine is actually used, tkGridRaster.cpp? That uses GDAL correct? And there is no way to rewrite the header without creating a new file?

Hello Mark.

Here’s an excerpt from the top-level CreateNew, which in turn calls the tkGridRaster.CreateNew. As you can see, it extracts the values from the Header prior to calling tkGridRaster.CreateNew. From that point on, the Header is not involved in the process.

		if (_trgrid->CanCreate(newFileType))
			double dx;
			double dy;
			VARIANT ndv;
			double dndv;
			CComBSTR projection;
			long ncols;
			long nrows;
			double xllcenter;
			double yllcenter;
			CComBSTR cTbl;

			bool result = _trgrid->CreateNew(W2A(Filename), newFileType, dx, dy, xllcenter, yllcenter, 
				dndv, OLE2A(projection), ncols, nrows, DataType, boolInRam, value, true);

			if (result)
				_filename = OLE2W(Filename);
				*retval = VARIANT_TRUE;
				delete _trgrid;
				_trgrid = NULL;
			CallbackHelper::ErrorMsg("Grid.CreateNew: writing to this format is not allowed.");
			return S_OK;

Inside the tkGridRaster class, it uses GDAL as follows, passing in the rows and columns, with no reference to the Header.

    poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
    . . .
    _rasterDataset = poDriver->Create( Utility::ConvertToUtf8(filename), ncols, nrows, 1, _genericType, papszOptions );

So when you ask about re-writing the Header, I’m not sure if the Header is even a GDAL construct, but instead a higher-level MapWinGIS construct. From what I can tell, if you modify the Header properties after creation of the Grid, it will have no impact because it is no longer referenced.

That’s at least what I know (think I know) at the moment.


I guess I didn’t directly answer your question. I think the answer is Yes, there is no way to rewrite the header as it is only used in creation of a new file.

It may be that your only hope, if it is important enough, is to come to grips with the GDAL code inside the tkGridRaster class, and determine if it can be modified to accomplish what you are hoping. That’s the beauty, if you will, of open-source software.

Kind regards,

Thanks again! That is the information I was looking for.