Creating a GeoTIFF using `gmt coast` fails for some regions

I’m creating maps using the gmt coast command and want them to export to GeoTIFF files for further processing in other tools like QGIS. Working on lots of country maps, I came across this “bad” region where the created GeoTIFF doesn’t correctly align with e.g. QGIS using the coastline data from Natural Earth. Here’s the code I’m using:

BAD_AREA=-5.5/41.9/9.0/51.8+r
gmt begin test_bad ps
gmt coast -R${BAD_AREA} -JM576p -S#a0d2dc
gmt coast -R${BAD_AREA} -JM576p -G#c0a0a0
gmt end
gmt psconvert test_bad.ps -Z -W+g -Tt

The strange thing is, using nearly the same region, the GeoTIFF I get fits perfectly:

GOOD_AREA=-5.5/41.9/9.0/51.9+r
gmt begin test_good ps
gmt coast -R${GOOD_AREA} -JM576p -S#a0d2dc
gmt coast -R${GOOD_AREA} -JM576p -G#c0a0a0
gmt end
gmt psconvert test_good.ps -Z -W+g -Tt

I’ve also tried to explicitly set page size and both possible orientations, but I keep getting these warnings for the “bad” region:

coast [WARNING]: Changing to PostScript landscape orientation based on your plot and paper dimensions, but we cannot be 100% sure.
coast [WARNING]: Use PS_MEDIA and/or PS_PAGE_ORIENTATION to specify correct paper dimensions and/or orientation if our guesses are inadequate.

My questions:

  1. Am I right this has something to do with the landscape orientation warning?
  2. How can I set PS_MEDIA and/or PS_PAGE_ORIENTATION in modern mode?
  3. Is there another way to create GeoTIFF files as a workaround? In my “real” application, I use a lot of grdimage, coast and plot commands.

I’m using gmt 6.5.0 on macOS.

Here are the result of the commands from above, added as QGIS layers and with the ne_10m_coastline as a red overlay.


The bad result:

The good result:

Indeed something fishy is going on in modern mode here.

The solution is to use eps

gmt coast -R-5.5/41.9/9.0/51.8+r -JM576p -G#c0a0a0 -eps lixo

or classic mode, but here the PS_MEDIA has to be increased. The messages about increasing PS_MEDIA in modern mode are also wrong as in that mode either PS_MEDIA or PS_PAGE_ORIENTATION are not available. (we print on 11 m square paper and crop the whites).

gmt pscoast -R-5.5/41.9/9.0/51.8+r -JM576p -G#c0a0a0 -P > lixo.ps
pscoast [WARNING]: Your plot (WxH = 20.32 x 20.31 cm) placed at (2.54, 2.54 cm) may exceed your PS_MEDIA (WxH = 20.99 x 29.70 cm)
1 Like

Why does the 0.1 difference in latitude trigger this?
And why does making an eps help?

I thought this may be a ‘spherical vs. ellipsoidal’ issue, but obvously not, if making an eps can fix it.

Thanks so far for trying to help me.

What does -eps do? I can’t find anything about it in the docs.

The solution is to use eps
gmt coast -R-5.5/41.9/9.0/51.8+r -JM576p -G#c0a0a0 -eps lixo

Using this option, I get this error:

gmt [ERROR]: Cannot run a one-liner modern command within an existing modern mode session

Obviously, I’m using it not correctly…


Why does the 0.1 difference in latitude trigger this?

It seems that little difference leads to completely different image dimensions. For the “bad” version, psconvert says:

Input file size is 2400, 2180

While on the “good” version, it says:

Input file size is 2401, 2426

Your good vs. bad area differs by 10 degrees in the latitude. I was very confused sometimes when mapproject would choose a ‘spherical solution’ since my region was ‘big’, and that was due to a region being larger than ‘10 degrees of central meridian’ when using the UTM projection.

See Select Ellipsoidal versus Spherical Solution for more.

Probably not related, but thought I’d mention it.

Ah, those are the debugging questions.

Unrelated to this problem. It means a previous modern mode command did not finish cleanly and left over stray files. Do a

gmt clear sessions

It creates the output in eps format. Those things are documented in the psconvert -T man page.

In the meantime, I made another observation: As a test, I created maps for all countries in South America and in Europe, using some custom bounding boxes from my database as region values. From the 58 maps, 54 maps were correct, but 4 maps were wrong: France, Croatia, Poland and Brazil. Here are the commands I used:

gmt coast -R-5.4833080335/41.9448976164/9.0102354535/51.4814569985+r -JM576p -G#c0a0a0
gmt coast -R13.0629568234/42.167624323/19.6006902118/46.7825392115+r -JM576p -G#c0a0a0
gmt coast -R13.1552333516/48.7018341646/24.5652703032/55.161944894+r -JM576p -G#c0a0a0
gmt coast -R-80.0/-35.0/-32.0/8.0+r -JL-56.0/-13.5/-13.5/-13.5/576p -G#c0a0a0

These four maps have one thing in common: All four have the same size of 2400x2180 pixels, while none of the other 54 maps has that size.
The nearest map has been for Belarus with 2400x2164 pixels. This map is correct, though.

The topic starter apparently adds -eps to the commands inside his modern mode session. This is not supposed to work. -eps is for one-liners like the one @Joaquim showed in his post.

instead you could try something like

BAD_AREA=-5.5/41.9/9.0/51.8+r
gmt begin test_bad eps
  gmt coast -R${BAD_AREA} -JM576p -S#a0d2dc -G#c0a0a0
gmt end
gmt psconvert test_bad.eps -Z -W+g -Tt

(or just a one-liner gmt coast -R${BAD_AREA} -JM576p -S#a0d2dc -G#c0a0a0 -eps test_bad)

BTW adding psconvert options to gmt begin, like gmt begin test_bad tiff Z,W+g,-Tt, does not add GIS header to the tiff, only combination above works gmt begin test_bad eps+gmt psconvert test_bad.eps -Z -W+g -Tt and it obviously calls gdal as the below is on the output, while gmt begin ... does not:

Input file size is 2401, 2399
0...10...20...30...40...50...60...70...80...90...100 - done.
1 Like

This is how Mercator projection works when defined with -JM... I believe: horizontal map size will be the same with -JM576p, while vertical size will not be the same, as the scale (dots per degree latitude) will be different, as defined by the projection.

Werent the vertical amount of pixels the same too?

Yes, that’s been my problem. I completely misunderstood what @Joaquim tried to tell me. Thank you both for your help! With your code, everything works as expected. :slight_smile:

So, when using an EPS file instead of a PS file, the conversion step to create a GeoTIFF works for all my maps. Great!

I’m not surprised that the width of all the maps are identical. I’ve been surprised by the fact that the incorrect maps had the exact same dimensions of 2400x2180 pixels. Especially when looking at the Brazil example where I didn’t even use Mercator, but the Lambert Conformal Conic projection.

I’ve compiled the sizes of the correct maps (that I get when I use EPS) and compared them to the dimensions of the incorrect maps:

country           size with PS           size with EPS
-------           ------------           -------------
France            2400x2180              2401x2305
Croatia           2400x2180              2401x2369
Poland            2400x2180              2401x2204
Brazil            2400x2180              2401x2245

Perhaps this may be helpful when debugging the underlying problem.

Oh, this does not look right.

Can you please summarize this into an issue? Otherwise risk is that this will be lost.

After all, I don’t think there a “failure for some regions”. The point is that the ps indeed requires that we set the paper size when the map overflows A4 in Portrait (which is the case in the failures). If we do

gmt coast -R-5.5/41.9/9.0/51.8+r -JM576p -G#c0a0a0 -ps lixo --PS_MEDIA=A3

there are no warnings and the generated GeoTIFF is correct. It’s the ps itself that seems to have wrong BoundingBox that clips it a bit. Oveall, using ps in modern mode doesn’t seem to be a very good idea.