# How to convert a Rasters.Raster{UInt8,2} to a Raster{Float32,2}?

**URL:** <https://discourse.julialang.org/t/how-to-convert-a-rasters-raster-uint8-2-to-a-raster-float32-2/103322>\
**Category:** Geo\
**Tags:** question, geo, rasters\
**Created:** [August 29, 2023, 9:25am UTC](https://discourse.julialang.org/t/how-to-convert-a-rasters-raster-uint8-2-to-a-raster-float32-2/103322 "2023-08-29T09:25:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [August 29, 2023, 9:25am UTC](https://discourse.julialang.org/t/how-to-convert-a-rasters-raster-uint8-2-to-a-raster-float32-2/103322/1 "2023-08-29T09:25:50Z")

</div>

Hello, as from the title, how can I convert a `Rasters.Raster{UInt8,2}` to a `Raster{Float32,2}` ?  
I have tried with `convert(Raster{Float64,2},myraster)` but this method is not defined…

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 29, 2023, 12:56pm UTC](https://discourse.julialang.org/t/how-to-convert-a-rasters-raster-uint8-2-to-a-raster-float32-2/103322/2 "2023-08-29T12:56:12Z")

</div>

Use broadcast: `Float32.(myraster)`

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [August 29, 2023, 1:58pm UTC](https://discourse.julialang.org/t/how-to-convert-a-rasters-raster-uint8-2-to-a-raster-float32-2/103322/3 "2023-08-29T13:58:35Z")

</div>

Thank you, it “almost” works, but I still have the problem to update also the `missingval` and if I try to do it directly (e.g. `myraster_converted_to_float32.missingval = 0.0f0`) I got the error that the `Raster` struct is not mutable…

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 29, 2023, 2:12pm UTC](https://discourse.julialang.org/t/how-to-convert-a-rasters-raster-uint8-2-to-a-raster-float32-2/103322/4 "2023-08-29T14:12:18Z")

</div>

`replace_missing` first may help. Or `rebuild(A; missingval=x)`

Maybe we could apply the broadcast to it automatically…

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 29, 2023, 6:27pm UTC](https://discourse.julialang.org/t/how-to-convert-a-rasters-raster-uint8-2-to-a-raster-float32-2/103322/5 "2023-08-29T18:27:05Z")

</div>

Just to follow up: `convert` _should_ work as it works on regular arrays. At the same time we can convert the `missingval`.

I’ll fix this in DimensionalData.jl and Rasters.jl.

You should also be able to do this with any other `AbstractDimArray` like a `YAXArray`
