I'm trying to realize an OBIA on a MSI. For this, I've generated shapefiles with the points and labels to link with the generated segments.

I'm using this code:

# modules import
import ogr
import gdal

# MSI read
tif_fn = './data/image/Landsat_ETM_2001-08-26_multispectral.tif'
driverTiff = gdal.GetDriverByName('GTiff')
tif_ds = gdal.Open(tif_fn)

# Shapefile read
train_fn = './data/train/train.shp'
train_ds = ogr.Open(train_fn)
lyr = train_ds.GetLayer()

# create a new raster layer in memory
driver = gdal.GetDriverByName('MEM')
target_ds = driver.Create('', tif_ds.RasterXSize, tif_ds.RasterYSize, 1, gdal.GDT_UInt16)

# rasterize the training points
options = ['ATTRIBUTE=id']
gdal.RasterizeLayer(target_ds, [1], lyr, options=options)

The problem is that when I generate an array with the values of the raster layer:

ground_truth = target_ds.GetRasterBand(1).ReadAsArray()

Its statistics (min, max, mean) show me that none of the points in the shapefile have been written (only 0 in the array).

I'm a Junior DS, so many of the tries to solve this problem have been based on copy other possible solutions from blogs like StackExchange, but none has worked. Solutions I've tried are:

  • Cleaning cache (deleting target_ds with flushCache())
  • Adding a burnout value to gdal.RasterizeLayer

This problem seems recurrent in other codes I've extracted from the web, so maybe it's related with my system spec. or something similar (because the shapefiles do indeed contain the points and the labels).

How can I do this?

New contributor
solysombra98 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.


Your Answer

solysombra98 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.