How to properly prepare a grid for draping over terrain?

I have a categorical land classes grid I’d like to color and then apply some hill shading based on @earth_relief_15s DEM. So far my experimentation always ended up distorted with the colored image offset to the hill shading.

Until I used grdimage -JQ. Now it seems to register properly. However this is also a projection and as far as I understand it this recalculates the grid and I therefore loose some resolution.

What would be the proper way to prepare a categorical grid for draping?

My current command:
gmt grdimage -JQ60c -R0/40/20/60+r de_discclass.grd -Cland_class_colors.cpt -nn+a -png de9

-JQ is just lon-lat and no projection actually takes place. All other projection requires resampling from the lon/lat equidistant grid onto the projected equidistant x-y grid.

Hi @pwessel, thank you for your reply.

So my -JQ would be the proper way to achieve this? Doesn’t -png bring in some scaling and therefore loss of detail together with -JQ60c?

I basically just want to replace one pixel’s grey value in the grid for a color value from a categorical CPT.

Yes, I was just thinking in terms of the data. Your nx by ny geo grid, if plotted with JQ is still nx by ny. The size is affected by the JQsize, but in a PDF this is just a zoom (in effect). But yes, once png and actual pixel resolutions enter then you get mx by my pixels and they may be larger or smaller than nx,ny, giving interpolation or averaging.

Ok so I can feed a PDF into grdview -G? I wasn’t aware of that. I’ll give it a try.

Currently my next step is

gmt grdview -JM60c -R0/40/20/60+r @earth_relief_03s -Qi300 -I+a-45+nt0.3+m0 -Gde9.png -png de_lit

where the plot width of -G and the output image is identical. So only minor problems there. But I plan on recycle my “colored grid” and that’s why I’m keen on keeping it full resolution.

In grdview, the resampling of the drape grid/image is automatically done, right? If yes, grdimage could do the same??

Just tested it – you can’t feed a PDF into grdview -G. I misinterpreted @pwessel’s answer there. My apologies.

ERROR 4: `hi11.pdf' not recognized as a supported file format.
grdview (gmtapi_import_grid): Not a supported grid format [hi11.pdf]

No, not at all. I mean GMT produces PS (or PDF) which are vector formats so no loss in -JQ.

Please bear with me @pwessel I have the feeling I don’t get it. You said there is no loss in -JQ. I agree as long as I don’t convert it into some bitmap image. But as I understand it I can only feed grids/images to grdview -G. So no vector formats and therefore loss in resolution if not perfectly matched.

There might be a chance we talk past each other so I try again:

I have a categorical GeoTIFF with a given size 20160 x 20160 px. I want to replace every pixels grey value with a color from a categorical CPT. No interpolation or similar. Basically make a colorized image from the grey-scale GeoTIFF.

As the resulting image appears somewhat flat I want to add some hill shading. Here I use the colorized image to drape it over some illuminated DEM.

My problem is the first step: How do I switch the pixels from their grey value to some kind of colored pixel without changing the resolution or interpolating the pixels when I need a grid/image for further processing?

Again, thank you for your patience with me @pwessel

Maybe it helps to see what I’m plotting (cropped and resized):

Perhaps grdimage -A is what you need: Read the geotiff as a grid and use the z-values with your CPT to create a color image you can write directly via -A. No plotting. You may be able to write it as a new geotiff if you specify the correct driver, I think (@Joaquim knows best) on that).

OK, to start with I don’t understand this point.What is changing the resolution? Selecting an image size is already setting a resolution.

Now, you have a Geotiff image with classes that you want to paint. Fine, that is known as an indexed image to which you attribute colors via a color table. Next, that image is a bit boring and you want to give it some salt. Also fine. Compute a shade with grdgradient of a DTM that you also seem to have and use it as argument of -I in grdimage (if they don’t have de same size, ask help to grdsample)

Hi @Joaquim, thank you for chiming in.

That’s exactly the step I want to avoid but I’m not sure how. I want to replace the grey values with color values without selecting an image size.

Then it’s like Paul said, create a geotiff with grdimage .... -A<img.tif>,where you can still use the retro-illumination trick I mentioned before.

But doesn’t grdimage require -J which selects an image size?

Maybe it does (I’m not sure, even though the docs say that) but I think it will be ignored because it’s the internal image array that will be saved as a geotiff and not a rasterization of a PS fig.

Yes, the -J is ignored.

Thank you @pwessel, thank you @Joaquim for your perseverance in explaining this to me. It works explained.

I feel the documentation is a bit ambiguous there. Do you think that warrants a bit more explanation in the man page or maybe even some expanded explanation in the cookbook?

Thank you again and all the best,

Sure, please propose what documentation would be helpful.