Open RWParsons opened 1 month ago
update: I was able to get the writeRaster
step to work by making tb
a maximum of 255 rows (only 255 possible values for the outcome variable in the spatRaster). This still gets messed up by the projection, though. Happy to push a fix if you'd like, @walkerke.
Yes a PR would be great!
In debugging this, I'm also noticing that terra::project()
resamples the raster, so we have to project before doing the data rescaling. I think I'm getting closer to an implementation that'll work here.
Right - I've been struggling to get it working to do the projection after and reliably add the colour tab back in. Not quite sure what the best approach will be to address this issue.
I modified the code to throw a warning for now that the existing color table won't be used. Because you no longer have a 1:1 correspondence between cells after projection, we'd need to establish the old color table, project, then associate the value in the new raster with the corresponding nearest value in the old color table. I haven't quite gotten this working yet.
There are a couple problem that I've identified in
add_image_source()
Here is an example using a sample of data that I'm working with:
This is just a sample of 50 rows from the input data. The full data is of an Australian state and looks fine when running
plot(test_raster)
:One of the issues is that this raster has colour layer that add_image_source() should accept. However, after it is projected to EPSG:4326 here: https://github.com/walkerke/mapgl/blob/7f3e2a5932dd466f7b5e4874127cbb40d99580d3/R/sources.R#L161
This causes it to lose the it's attribute and
terra::has.colors(data)
then returnsFALSE
. This could arguably being a feature request toterra
to but I think it could be solved if thecoltb
is created before the data are projected inadd_image_source()
.The second problem I found is that even if I skip the projection, with these data, I get an error when running: https://github.com/walkerke/mapgl/blob/7f3e2a5932dd466f7b5e4874127cbb40d99580d3/R/sources.R#L190
I don't think the rgb values are out of range but it fails to write the image and then nothing is shown on the map.
If you would like, I can make a PR to solve the first issue with the projection but I'm not sure what to do about the problem with
writeRaster
(and you're probably much more familiar with this).Cheers, Rex