Each feature in my geojson file contains a property with hex color codes. Is it possible to convert these vector feature-collection to a geotiff using this color code on every feature ? I populate the properties myself, so I could add any property I want.
2 Answers 2
All that gdal_rasterize can do is documented in https://gdal.org/programs/gdal_rasterize.html. Without changing the code the best you can to is to burn a coded value for the colors with -a
"
-a <attribute_name> Identifies an attribute field on the features to be used for a burn-in value. The value will be burned into all output bands.
I suggest to create a single band 8-bit image with values 1,2,3,4.... Once you have the image it should be possible to color it by creating a vrt file and adding a color palette into it as in this question How to add a color table to a one band tiff using GDAL?. It did not work in the question for 64-bit data but with 8-bit data it should be OK.
Just tried @user30184 suggestion, still not sure if I'm doing this right.
So I have 2 Features in my geojson-file with the property "color_prop". One feature has value 1, the other value 2.
1: Creating the getiff from geojson, single band, Byte
gdal_rasterize -l TestLayer -a color_prop -tr 0.1 0.1 -a_nodata 0.0 -te 856360.2911711 6863101.66193259 856687.30377687 6863290.112259374 -ot Byte -of GTiff INPUT.geojson OUTPUT.tif
2: Create test VRT File
<ColorInterp>Palette</ColorInterp>
<ColorTable>
<Entry c1="255" c2="0" c3="0" c4="255"/>
<Entry c1="0" c2="255" c3="0" c4="255"/>
</ColorTable>
3: Translate to VRT Format
gdal_translate -of VRT OUTPUT.tiff test.vrt
4: Translate again:
gdal_translate test.vrt OUTPUT_2.tiff
So the feature with property value 1 should be red, the other should be green, right? I can see only one feature in QGIS, and it's black. What I'm doing wrong?
EDIT: Sorry for my stupidity, the other polygon was just white, and my background was also white... works now:
1: Convert geojson to geotif with gdal_rasterize
2: Create VRT from the generated tif
gdal_translate -f vrt OUTPUT.tif generated.vrt
3: Edit VRT, add ColorTable
4: Translate back to new tif
gdal_translate test.vrt OUTPUT_2.tiff