Skip to content

Add an example for the usage of OpenStreetMap coastlines to the gallery/CookBook #3041

Closed

Description

@KristofKoch

I was asked by @leouieda to come up with an example on how to use OSM coastlines in GMT. Additionally this issue is supposed to serve as an example on how to contribute to the documentation for future reference. As I have never contributed to the documentation, following along this journey might be useful to other novice contributors.

Here is what I got so far. I tried to follow the style of the other gallery examples. Your input on how it can be improved is greatly appreciated. If deemed worthy to become part of the docs I'll have to figure out on how to actually add it to the repository in a second step.


(XX) OpenStreetMap coastlines in GMT

Note: This example makes use of data provided by third parties outside the GMT universe. As the internet is constantly changing, the source of this data might move or vanish altogether.

This example shows how to use coastlines derived from the OpenStreetMap (OSM) project in GMT. When working with the high resolution SRTM datasets provided by GMT, the GSHHG coastlines might not be precise enough on small scales. Here comes the OSM project to the rescue as it provides high resolution coastlines in the WGS84 datum. Not as convenient and fast as GSHHG but easy enough to give it a try – as long as you have GDAL installed, some space on your hard drive and some processor cycles to spare.

First we need to download the appropriate shape file. The OSM Wiki has some sources, https://osmdata.openstreetmap.de/data/land-polygons.html is very useful. The nice people of the German FOSSGIS society already did some of the heavylifting and converted the OSM coastlines to a shape file for us. Make sure to get the Land Polygon in the Large polygons not split version with WGS84 datum. It’s a ~630 MB download.

If your GMT installation comes with GDAL support then you are able to make use of this file right away but GMT converts it under the hood to something more useful on the fly. Neither very fast nor very efficient if you want to use the file multiple times and have to deal with large file sizes as we do here. So we use GDALs ogr2ogr and convert the shape file to a native GMT format.

As the resulting raw ASCII file is very large (>1 GB), GMTs convert is used to reduce the file size to a third by converting it to a single-precision binary file. The resulting file is smaller than the initial download while still maintaining precision in the range of centimeters.

To speed up the plotting process even more, we extract the region of interest with select. Finally we are all set to use the coastlines with plot to get nice, precise coastlines in our illustrations as presented in figure XX.

#!/usr/bin/env bash
# GMT EXAMPLE XX
#
# Purpose:	Illustrate usage of OpenStreetMap coastlines in GMT
# GMT modules:	convert, select, coast, plot
# GDAL progs:	ogr2ogr
#
# First we convert the shapefile to something more GMT friendly. Here we make
# into a large ASCII file. Human-readable but not very efficient. GDALs ogr2ogr
# is used with the "-F GMT" option indicating that the output should have the
# native GMT format. Input is the downloaded land_polygons.shp, and the output
# is named land_polygons_osm_planet.gmt. The resulting file is very large 
# (>1 GB) but already usable. But wait, there is more ...

ogr2ogr -F GMT land_polygons_osm_planet.gmt land_polygons.shp

# Resorting to GMTs convert program we take our very large ASCII file and
# reduce it to about a third of its original size by converting it to a binary
# file.
# Lets have a closer look at -bo2f: 
# -bo selects native binary output
# 2 makes it two data columns
# f indicates 4-byte single-precision for the data

gmt convert land_polygons_osm_planet.gmt -bo2f > land_polygons_osm_planet.bf2

# Again we could use the file as is but there are more performance gains to
# collect. We extract just the area we need for our plot. Here the island of
# Sao Vicente, Cape Verde. select to the rescue! -bo2f is nothing new and -bi2f
# looks very similar: the -bi is indicating native binary input. -R...r gives
# the area which we are interested in.

gmt select land_polygons_osm_planet.bf2 \
 -bi2f -bo2f \
 -R-25.14/16.75/-24.8/16.95r \
 > land_polygons_osm_caboverde.bf2

# Finally it is time to do the thing we are here for: Plot some OSM coastlines
# and compare them to the GSHHG coastlines.

gmt begin caboverde
  
  # To make the lines a bit nicer we set the line endcaps and the way lines are 
  # joined to round
  
  gmt set PS_LINE_CAP round
  gmt set PS_LINE_JOIN round
  
  # To get an idea where the GSHHG coastlines are we lay them down first with a
  # thin red pen. -JL defines a Lambert conic conformal projection and -R...r
  # is the area of the plot. -W defines the pen and -B the style of the plot
  # borders, gridlines and annotations. 
  
  gmt coast -JL-24.9/16.55/16.3/16.7/20c -R-25.14/16.75/-24.8/16.95r \
    -Wred -Ba10mg10m
  
  # Time to use the OSM coastlines we prepared earlier. Straightforward we
  # supply the extracted area, tell plot about the binary format (-bi), define
  # pen (-W) and fill (-G)
  
  gmt plot land_polygons_osm_caboverde.bf2 -bi2f -Wthinnest,black -Ggrey
  
  # Here we plot the GSHHG coastlines a second time but now with a dashed pen
  # to highlight the areas where they would otherwise be hidden behind the grey
  # OSM landmass. 
  
  gmt coast -Wred,dashed
  
gmt end show

caboverde

Activity

  1. leouieda commented on Apr 9, 2020

    @leouieda
    Member

    @KristofKoch this is a really nice example! It would be great to have the shapefile as a remote dataset so that users can actually run this. Alternatively, if that shapefile already exists on a public server, we can use gmt which to download it automatically.

  2. KristofKoch commented on Apr 9, 2020

    @KristofKoch
    ContributorAuthor

    @leouieda glad you like it.

    I thought about the data source as well. Unfortunately there is no hard link to the latest version of the shapefile. When looking at the source page the desired file is hiding behind the first green "Download" button. It currently links to

    https://osmdata.openstreetmap.de/download/land-polygons-complete-4326.zip

    where the four digits in the file name can change. So I don't see a way to use gmt which on it reliably.

    As GMT provides SRTM data already (a superb service!) it came to my mind that maybe it is justifiable to provide a OSM coastline derivative as well until GSHHG3 is available. This would mean providing a ~430 MB file for download as it would be smart to distribute the file in binary form. The data is provided under the ODbL 1.0 licence which should allow for this.

    Your thoughts on this matter?

  3. leouieda commented on Apr 10, 2020

    @leouieda
    Member

    Right, this would be a good candidate for an official GMT remote file. @osm_coastlines maybe? We could store it on the GMT server, probably gzip or xz compressed and unpack it after download (as is done for the SRTM tiles with JPEG2000 compression). That would eliminate the need for the ogr2org and gmt convert calls. You could do gmt select @osm_coastlines directly.

    @PaulWessel what do you think?

  4. KristofKoch commented on Apr 11, 2020

    @KristofKoch
    ContributorAuthor

    @leouieda I think @osm_coastlines is way to easy to understand. I opt for @ghpclfoig as acronym for "global high precision coast lines from OSM in GMT". Just kidding ;-)

  5. KristofKoch commented on May 8, 2020

    @KristofKoch
    ContributorAuthor

    To pick this up again I think it would be a good idea to have the example as is. It might serve as an example on how to use shape files in GMT even when the OSM data is readily available as a GMT dataset one day. What do you think @PaulWessel and @leouieda ?

  6. KristofKoch commented on May 9, 2020

    @KristofKoch
    ContributorAuthor

    I installed Sphinx on my machine but boy it is a steep learning curve. Do you guys have a good primer somewhere on how to expand the GMT documentation?

    I'm trying to make a new example … I figured so far I must change doc/rst/source/gallery.rst add a new example file doc/rst/source/gallery/ex51.rst. I didn't find where to put the example script? Is it doc/examples/ex51/? Ist there a way to generate the Windows .bat file? Only unixoid systems available here.

    I don't feel very confident that I'll do everything right here. Your help and guidance is really appreciated.

  7. seisman commented on May 9, 2020

    @seisman
    Member

    @KristofKoch Thank you for trying to contribute an example to GMT!

    To add a new example, you need to:

    1. Create a directory doc/examples/ex51 and put your scripts in. A .bat is not required
    2. Create the example documentation file doc/rst/source/gallery/ex51.rst
    3. At line 20 of file doc/rst/source/gallery.rst, change 51 to 52.

    You can add the scripts first and open a PR, then we can help you improve it.

  8. KristofKoch commented on May 9, 2020

    @KristofKoch
    ContributorAuthor

    Hi @seisman – thank you for your help. I'm still wondering how to include the sample script into ex51.rst. When looking at ex50.rst there is the following block:

    .. literalinclude:: /_verbatim/ex50.txt
       :language: bash
    

    which I presume includes the actual example script. I didn't find a ex50.txt in my installation so I'm not sure how to properly include it.

  9. seisman commented on May 9, 2020

    @seisman
    Member

    When building GMT, the bash script ex51.sh will be converted to plaintext file ex51.txt and saved in the build/doc/rst/source/_verbatim/ directory, then you can use .. literalinclude:: /_verbatim/ex51.txt to include the script.

  10. KristofKoch commented on May 9, 2020

    @KristofKoch
    ContributorAuthor

    Let's see what you think about my first attempt. Your comments for improvement are very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions