1

I have a problem with exporting a GeoTIFF file by using python and gdal. What I want to do is to convert a NumPy array into a GeoTIFF file. There are reference GeoTIFF files, so I want to make sure that the produced GeoTIFF file has proper geometric coordinates.

The problem is that the tiff file seems to be produced, but the values it contains are not good. I tried to view the file by using QGIS, but the appearance of the image was completely black. In addition to the problem with the appearance of the image, the values are also changed from the original NumPy array. For example, the maximum value of the NumPy array is 149, but QGIS says that there is no such value in the file.

What is the cause of this problem, and how can I fix it?

The codes are here.

#Check the metadata of the reference file.
with rio.open('/content/drive/My Drive/Colab Notebooks/satellite_data/MCD12Q1/MCD12Q1.A2019001.IGBP.Buryat.geotiff.tif') as filename : filename.bounds
filename.meta["transform"]

#Output=> {'count': 1,'crs': CRS.from_epsg(4326),'driver': 'GTiff','dtype': 'uint8','height': 1920, 'nodata': None, 'transform': Affine(0.00416666, 0.0, 97.99955519999997, 0.0, -0.00416666, 58.0000512),
'width': 4800}

srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)

#Set some parameters
xsize=filename.meta["width"]
ysize=filename.meta["height"]
band=1
dtype = gdal.GDT_UInt16

output = gdal.GetDriverByName('GTiff').Create('/content/drive/My Drive/Colab Notebooks/outputs/output.tif', xsize, ysize, band, dtype)
output.SetGeoTransform((filename.meta["transform"][0],filename.meta["transform"][1],filename.meta["transform"][2],filename.meta["transform"][3],filename.meta["transform"][4],filename.meta["transform"][5]))
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
output.SetProjection(srs.ExportToWkt())
output.GetRasterBand(1).WriteArray(NumOfFiresExpanded["2019"]) #The "NumOfFireExpanded["2019"] is the target numpy array.
output.FlushCache()
output = None

  • is that how your code is actually indented? Also, why would you use `rasterio` _and_ `gdal`. You could easily stick with `rasterio` (or `gdal` for that matter) – Val Jan 28 '21 at 14:08
  • Thank you for your comment! I wanted to make a GeoTiff file with the same coordinates and projections as the reference image. However, parts of the codes were reproduced from other sources, so I haven't completely understood what the codes are doing... I'll use rasterio because it seems much easier to use for me. – Shoki Shimada Feb 01 '21 at 08:12
  • But to write the raster, you use `gdal`. You can use only `rasterio` to perform all operations you need – Val Feb 01 '21 at 08:16
  • Ok, thank you! Since I'm new to the rasterio package, I'll look for some useful materials for geospatial analysis with rasterio. – Shoki Shimada Feb 01 '21 at 08:21
  • Rasterio hast [very good documentation](https://rasterio.readthedocs.io/en/latest/quickstart.html#opening-a-dataset-in-writing-mode) on how to open rasters for reading and writing. – Val Feb 01 '21 at 08:24

1 Answers1

0

I have an example here which takes numpy arrays and builds a GeoTiff.

How do I write/create a GeoTIFF RGB image file in python?

Use gdalinfo -stats <geotiff> to view the GeoTiff and make sure the range is good. Viewing 16-bit imagery can be non-deterministic depending on the viewer. QGIS is a good test viewer.

msmith81886
  • 2,286
  • 2
  • 20
  • 27