1

I'm trying to extract a raster from another but the result must have only a range of colors. All other colors must be transparent.

Source raster:

enter image description here enter image description here

My goal is to create another TIF only with orange and yellow colors (and variations). All other colors must be transparent.

My first try was to use

gdaldem color-relief -exact_color_entry -alpha source.tif ramp.txt dest.tif

being ramp.txt:

14000   244 102 93 1
14333   243 116 92 1
14666   243 130 92 1
15000   243 145 92 1
15333   243 159 92 1
15666   242 174 92 1
16000   242 188 92 1
16333   242 203 92 1
16666   242 217 92 1
17000   242 232 92 1

and the raster info:

Size is 10964, 11008
Coordinate System is:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
Data axis to CRS axis mapping: 2,1
Origin = (-69.901787437934559,4.523924452406691)
Pixel Size = (0.000090275693031,-0.000090275693031)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  ( -69.9017874,   4.5239245) ( 69d54' 6.43"W,  4d31'26.13"N)
Lower Left  ( -69.9017874,   3.5301696) ( 69d54' 6.43"W,  3d31'48.61"N)
Upper Right ( -68.9120047,   4.5239245) ( 68d54'43.22"W,  4d31'26.13"N)
Lower Right ( -68.9120047,   3.5301696) ( 68d54'43.22"W,  3d31'48.61"N)
Center      ( -69.4068961,   4.0270470) ( 69d24'24.83"W,  4d 1'37.37"N)
Band 1 Block=10964x1 Type=UInt16, ColorInterp=Gray
    Computed Min/Max=0.000,16054.000
Band 2 Block=10964x1 Type=UInt16, ColorInterp=Undefined
    Computed Min/Max=0.000,15660.000
Band 3 Block=10964x1 Type=UInt16, ColorInterp=Undefined
    Computed Min/Max=0.000,7380.000

but all I've got was a big orange rectangle.

Or ...

If you have another way to find fire hot spots in an image using GDAL I'll accept too (only code, no QGIS).

3
  • I would consider using a supervised classification algorithm for this task. This way you will be able to incorporate all of the bands into the classification. Once you get a binary mask from the classification, use it to mask the original image.
    – Aaron
    Feb 12, 2022 at 4:25
  • Too broad. I understood nothink you said. I'm a programmer, not an analist. Can you show the code? At least the tools
    – Magno C
    Feb 12, 2022 at 18:33
  • Could you please describe both the images you posted? It looks like the source image is single band and the smaller, secondary image is multiband RGB. Please clarify as this will help determine the best approach.
    – Aaron
    Feb 13, 2022 at 2:13

1 Answer 1

1

I think I've found a solution with much less custom code.

First thing ( and harder ) was to discover "what is fire?" ...

The solution was to merge all bands and publish it in Geoserver, so I so i could query every pixel that represents what I need. I've found that "fire" is any pixel with value below -0.4.

Now I know what is fire I think I'll never need to do this steps again.

Go further...

Now I'll create another raster tif only with fire = 1 and any other information is zero.

gdal_calc.py --type="Float32" --co BIGTIFF=YES -A original.tif --calc="A <= -0.4" --outfile calculated.tif

Now I have a big black image with fire spots being white areas.

Lets extract fire areas...

gdaldem color-relief -alpha calculated.tif fire.txt final.tif

being fire.txt:

0       0 0 0 0
1       255 0 0 1

Done!

This is my final result ( please ignore the zoom level ) :

enter image description here

This is the original image ( zoomed in ):

enter image description here

As you can see I caught even the faintest areas of fire.

I even had the luxury of creating polygons...

gdal_polygonize.py -b 1 final.tif -f "GeoJSON" polygonized.geojson

Here is. You can see it here: https://geojson.io/#map=13/4.4290/-69.4964

{
"type": "FeatureCollection",
"name": "out",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.483088773658181, 4.475536680942238 ], [ -69.483088773658181, 4.475175578170115 ], [ -69.483269325044247, 4.475175578170115 ], [ -69.483269325044247, 4.474995026784053 ], [ -69.483449876430313, 4.474995026784053 ], [ -69.483449876430313, 4.474633924011931 ], [ -69.483088773658181, 4.474633924011931 ], [ -69.483088773658181, 4.474995026784053 ], [ -69.48290822227213, 4.474995026784053 ], [ -69.48290822227213, 4.475536680942238 ], [ -69.483088773658181, 4.475536680942238 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.870010393987755, 4.462175878373695 ], [ -69.870010393987755, 4.461995326987633 ], [ -69.869829842601689, 4.461995326987633 ], [ -69.869829842601689, 4.462175878373695 ], [ -69.870010393987755, 4.462175878373695 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.870010393987755, 4.46163422421551 ], [ -69.870010393987755, 4.461453672829449 ], [ -69.870190945373821, 4.461453672829449 ], [ -69.870190945373821, 4.461273121443387 ], [ -69.870010393987755, 4.461273121443387 ], [ -69.870010393987755, 4.461092570057327 ], [ -69.869829842601689, 4.461092570057327 ], [ -69.869829842601689, 4.461273121443387 ], [ -69.869649291215637, 4.461273121443387 ], [ -69.869649291215637, 4.461453672829449 ], [ -69.869829842601689, 4.461453672829449 ], [ -69.869829842601689, 4.46163422421551 ], [ -69.870010393987755, 4.46163422421551 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.534184815913562, 4.453509411842748 ], [ -69.534184815913562, 4.452967757684563 ], [ -69.534004264527496, 4.452967757684563 ], [ -69.534004264527496, 4.453148309070625 ], [ -69.533823713141445, 4.453148309070625 ], [ -69.533823713141445, 4.453509411842748 ], [ -69.534184815913562, 4.453509411842748 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.534907021457812, 4.452606654912441 ], [ -69.534907021457812, 4.452426103526379 ], [ -69.535087572843864, 4.452426103526379 ], [ -69.535087572843864, 4.452065000754256 ], [ -69.534726470071746, 4.452065000754256 ], [ -69.534726470071746, 4.452606654912441 ], [ -69.534907021457812, 4.452606654912441 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.435062104965851, 4.432384899673565 ], [ -69.435062104965851, 4.432204348287503 ], [ -69.435242656351917, 4.432204348287503 ], [ -69.435242656351917, 4.432023796901442 ], [ -69.435603759124035, 4.432023796901442 ], [ -69.435603759124035, 4.431482142743258 ], [ -69.435423207737983, 4.431482142743258 ], [ -69.435423207737983, 4.431301591357196 ], [ -69.435062104965851, 4.431301591357196 ], [ -69.435062104965851, 4.431482142743258 ], [ -69.434701002193734, 4.431482142743258 ], [ -69.434701002193734, 4.431662694129319 ], [ -69.434520450807668, 4.431662694129319 ], [ -69.434520450807668, 4.432384899673565 ], [ -69.435062104965851, 4.432384899673565 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.434701002193734, 4.431482142743258 ], [ -69.434701002193734, 4.431301591357196 ], [ -69.434520450807668, 4.431301591357196 ], [ -69.434520450807668, 4.431482142743258 ], [ -69.434701002193734, 4.431482142743258 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.435062104965851, 4.430940488585073 ], [ -69.435062104965851, 4.430759937199012 ], [ -69.4348815535798, 4.430759937199012 ], [ -69.4348815535798, 4.430940488585073 ], [ -69.435062104965851, 4.430940488585073 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.437048170212535, 4.428051666408091 ], [ -69.437048170212535, 4.42787111502203 ], [ -69.437228721598601, 4.42787111502203 ], [ -69.437228721598601, 4.427510012249907 ], [ -69.436867618826469, 4.427510012249907 ], [ -69.436867618826469, 4.428051666408091 ], [ -69.437048170212535, 4.428051666408091 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.686209082977257, 4.38255271712062 ], [ -69.686209082977257, 4.382191614348497 ], [ -69.686028531591191, 4.382191614348497 ], [ -69.686028531591191, 4.382372165734559 ], [ -69.685847980205139, 4.382372165734559 ], [ -69.685847980205139, 4.38255271712062 ], [ -69.686209082977257, 4.38255271712062 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.190234425466613, 4.378219483855147 ], [ -69.190234425466613, 4.377858381083024 ], [ -69.190053874080547, 4.377858381083024 ], [ -69.190053874080547, 4.378219483855147 ], [ -69.190234425466613, 4.378219483855147 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.192220490713282, 4.37731672692484 ], [ -69.192220490713282, 4.377136175538778 ], [ -69.192401042099348, 4.377136175538778 ], [ -69.192401042099348, 4.376775072766655 ], [ -69.192039939327216, 4.376775072766655 ], [ -69.192039939327216, 4.376955624152717 ], [ -69.191859387941165, 4.376955624152717 ], [ -69.191859387941165, 4.37731672692484 ], [ -69.192220490713282, 4.37731672692484 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.190053874080547, 4.377136175538778 ], [ -69.190053874080547, 4.376955624152717 ], [ -69.18969277130843, 4.376955624152717 ], [ -69.18969277130843, 4.377136175538778 ], [ -69.190053874080547, 4.377136175538778 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.188609462992062, 4.376775072766655 ], [ -69.188609462992062, 4.376413969994533 ], [ -69.188428911605996, 4.376413969994533 ], [ -69.188428911605996, 4.376775072766655 ], [ -69.188609462992062, 4.376775072766655 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.335939394018155, 4.371900185342998 ], [ -69.335939394018155, 4.371719633956936 ], [ -69.335758842632089, 4.371719633956936 ], [ -69.335758842632089, 4.371900185342998 ], [ -69.335939394018155, 4.371900185342998 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.33377277738542, 4.359983793862946 ], [ -69.33377277738542, 4.359803242476885 ], [ -69.333953328771472, 4.359803242476885 ], [ -69.333953328771472, 4.359622691090824 ], [ -69.333411674613288, 4.359622691090824 ], [ -69.333411674613288, 4.359983793862946 ], [ -69.33377277738542, 4.359983793862946 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.064390109381819, 4.356192214755657 ], [ -69.064390109381819, 4.356011663369595 ], [ -69.064029006609701, 4.356011663369595 ], [ -69.064029006609701, 4.356192214755657 ], [ -69.064390109381819, 4.356192214755657 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.06529286631212, 4.353664495350797 ], [ -69.06529286631212, 4.353483943964736 ], [ -69.065112314926068, 4.353483943964736 ], [ -69.065112314926068, 4.353664495350797 ], [ -69.06529286631212, 4.353664495350797 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 255 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -69.595391735788368, 3.661430481191417 ], [ -69.595391735788368, 3.660888827033233 ], [ -69.595211184402316, 3.660888827033233 ], [ -69.595211184402316, 3.660708275647171 ], [ -69.59503063301625, 3.660708275647171 ], [ -69.59503063301625, 3.660888827033233 ], [ -69.594850081630184, 3.660888827033233 ], [ -69.594850081630184, 3.661249929805356 ], [ -69.59503063301625, 3.661249929805356 ], [ -69.59503063301625, 3.661430481191417 ], [ -69.595391735788368, 3.661430481191417 ] ] ] } }
]
}
1
  • Be free to improve this method. I'm not a GDAL professional. All of this I've made copying codes from web by try and error.
    – Magno C
    Feb 13, 2022 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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