I have been using gdal_grid to convert xyz files to a raster

I am wondering is there anyway to automatically detect the extents to use from the xyz file txe and tye using python so that I can use a more general code every time I grid?

This way I will be stating my input xyz file and interpolation method but the raster extents will be pre-calculated for best results using python??


gdal_grid  -a nearest:radius1=1.0:radius2=1.0:angle=0.0:min_points=0:nodata=0.0 -ot Float32 -of GTiff -txe [pre-calculated] -tye [pre-calculated] -tr 1 1 "input" output
  • Are you interested in the greatest extent of the raster file or a polygon that represents the non-NoData values in that raster?
    – GBG
    Commented Apr 23, 2022 at 5:14

1 Answer 1


You can use Rasterio to read the xyz file and the bounds method to get the bounding box of the dataset. For example (sample data):

import rasterio

xyz = '/temp/small.xyz'

dataset = rasterio.open(xyz)

Which would yield:

Out[1]: BoundingBox(left=172762.5, bottom=210787.5, right=172937.5, top=210637.5)

Then you can pull individual coords from the bounding box object:


Which would yield:

Out[1]: 210637.5
  • @fuzzy_raster9873 Yes, Rasterio can calculate cell size too: gis.stackexchange.com/a/243648/8104
    – Aaron
    Commented Apr 23, 2022 at 14:45
  • I've just noticed that some of my xyz datasets using the above code are getting the error rasterio.errors.RasterioIOError: Ungridded dataset: At line 48, change of Y direction. Does the dataset have to be gridded in order to use the above code? It was the extents I wanted for the gdal_grid command Commented Apr 23, 2022 at 18:33
  • I suspect that some of your xyz files do not follow the specification "no missing value is supported": gdal.org/drivers/raster/xyz.html. Here is a possible solution to fill in missing values: stackoverflow.com/a/44596664/1446289.
    – Aaron
    Commented Apr 24, 2022 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.