[gdal-dev] Calling GDAL directly from within Python

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view

[gdal-dev] Calling GDAL directly from within Python

Dear list, 

Below you will find a question from my intern Bram. Unfortunately I could not help him out either since I only use the gdal executables.
We would highly appreciate your help calling GDAL functions from within Python.

Kind regards,
Siebe Bosch

Hi devs,

This is my first time using the Gdal library directly in Python using the Anaconda Spyder shell. I want to write a program that automatically imports GRIB files from a public ftp server and use GDAL to perform some actions. I installed GDAL with the PIP installer. Python version 2.7.13, Anaconda 32-bit, GDAL 3.0.8.
First off it worked well; GDAL opened the file and could read its information (as in raster size and coordinates (not taking the automatically importing from the server into account)). I did noticed the difference in commands (for example, gdal_Translate turns into gdal.Translate(src_ds, DestName, **kwargs)):

Import gdal
From osgeo import osr
src_ds = gdal.Open('C:\Users\wijna_000\H11_V72_NED_SINGLE_LEVEL_00_000_GB')

raster_wkt = src_ds.GetProjection()
spatial_ref = osr.SpatialReference()
print spatial_ref.ExportToPrettyWkt()

From here I want to transform the GRIB file into ,preferably ASCII files, but tried .tif first since it’s the default. But when I run the gdal.Translate command and do the coordinate check again it still returns as data imported from a grib-file:

tif_ds = gdal.Translate("C:\Users\wijna_000\output.tif", src_ds)

raster_wkt2 = tif_ds.GetProjection()
spatial_ref2 = osr.SpatialReference()
print spatial_ref2.ExportToPrettyWkt()

GEOGCS["Coordinate System imported from GRIB file", DATUM["unknown",SPHEROID["Sphere",6367470,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]

Help on internet is rare since its within the Python environment. Can anyone point me towards a better direction?

Regards, Bram


gdal-dev mailing list
[hidden email]
Reply | Threaded
Open this post in threaded view

Re: [gdal-dev] Calling GDAL directly from within Python


To start with a side note, your GDAL version of 3.0.8 doesn't make much sense. The utilities from the bindings are available since 2.1 if I'm correct, and the latest release is 2.2. Doesn't matter though.

The text about "GRIB" you are seeing is only the name of the coordinate system, 'gdal.Translate' simply copies this projection to the TIFF file. Its a bit confusing indeed. The name probably comes from GDAL's GRIB driver, or some library it uses.

Your call to gdal.Translate appears to work fine, although its always good to check if the projection, extent etc are correct. And preferably also find some documentation from the data provider to confirm.