2

I want to rasterize a MultiPolygon Shapefile layer in Python and use values of one column to write to the raster. However, I cannot preclude that features of the Shapefile overlap. Is there a way to select the value from one of the features randomly if this should be the case? The code looks like this:

gdal.RasterizeLayer(mask, [1], layer, options = ["ATTRIBUTE=Species"])

where "Species" is the column containing the values to write to the raster. Those values describe classes and should be Integers. In case of overlapping features, it doesn't matter which one is written to the raster, as long as it is one of the feature values at that position (min, max, or random value; a mean would not be helpful).

I searched the documentation, but I found it not particularly helpful.

2
  • 1
    Have you tried what happens? I suppose that what you want is the default behavior and the value that remains is the one that comes from the last rendered feature. The only option is to use -add that would give the sum. The options are the same than in the command line utility gdal.org/programs/gdal_rasterize.html. I would also change the data type into byte from the default Float64 if you have just integers in the data.
    – user30184
    May 19, 2021 at 18:05
  • Thanks for your suggestion; I just tried it and it indeed happened to work as you suspected! May 19, 2021 at 18:54

1 Answer 1

1

After extensive testing, the problem is now solved (at least for my case, where a random value, but not a mean is desired): As user30184 supposed, the desired behavior is the default. It appears the value assigned to the raster pixel is that of the polygon feature which was created last. The function will probably iterate through the polygon features and overwrite previously set values.

1
  • 1
    You can perhaps control the value to some extent by sorting the source data with SQL statement.
    – user30184
    May 19, 2021 at 19:18

Your Answer

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

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