|
| 1 | +How to use political borders from GADM |
| 2 | +--------------------------------------- |
| 3 | + |
| 4 | +GADM (https://gadm.org/) is a project that provides spatial data for all countries and their sub-divisions. |
| 5 | +They allow you to freely use their data (see `license <https://gadm.org/license.html>`_) to make your own maps. |
| 6 | +You can use this script to download the data from their server. |
| 7 | +You only need to select a data set (using a three-letter code) and a level. |
| 8 | +Not all levels are available. Check first in their website if the data set exists. |
| 9 | +Here is a table with the number of file available for each level. |
| 10 | + |
| 11 | + ======= ============ |
| 12 | + Level # of files |
| 13 | + ======= ============ |
| 14 | + 0 254 |
| 15 | + 1 230 |
| 16 | + 2 171 |
| 17 | + 3 69 |
| 18 | + 4 21 |
| 19 | + 5 2 |
| 20 | + ======= ============ |
| 21 | + |
| 22 | +The script will download the selected data and level in KMZ format. |
| 23 | +Then, it will unzip that file (to KML) and convert it to a GMT format. |
| 24 | +At the end, you will have a GMT file in the same directory. |
| 25 | +To demostrate, we will create a level 1 map of Mexico (code MEX). |
| 26 | + |
| 27 | +.. gmtplot:: |
| 28 | + |
| 29 | + # How to plot political borders from GADM data set |
| 30 | + # |
| 31 | + # GDAM data |
| 32 | + # ------------------------------------ |
| 33 | + # Set country with three-leter code. |
| 34 | + COUNTRY=MEX |
| 35 | + # Set level from 0 to 5. 0 = Country outline. |
| 36 | + LEVEL=1 |
| 37 | + # GADM data set version |
| 38 | + VERSION=4.1 |
| 39 | + # File formats (https://gadm.org/formats.html) |
| 40 | + FORMAT=kmz |
| 41 | + SITE="https://geodata.ucdavis.edu/gadm/" |
| 42 | + # Remove dot from version |
| 43 | + VERSION_WITHOUT_DOT=$(echo ${VERSION} | tr -d '.') |
| 44 | + # Set file name without extension |
| 45 | + NAME="gadm${VERSION_WITHOUT_DOT}_${COUNTRY}_${LEVEL}" |
| 46 | + # URL |
| 47 | + URL="${SITE}gadm${VERSION}/${FORMAT}/${NAME}.${FORMAT}" |
| 48 | + # ------------------------------------ |
| 49 | + # Download data from GADM |
| 50 | + echo "Downloading geographic data provided by the https://gadm.org project. It may take a while." |
| 51 | + gmt which $URL -Gc |
| 52 | + # Unzip KMZ to KML. Set to NOT (-n) overwrite existing file. |
| 53 | + unzip -n ${NAME}.${FORMAT} |
| 54 | + # Convert KML file to GMT format with OGR/GDAL |
| 55 | + ogr2ogr -f "GMT" ${NAME}.gmt ${NAME}.kml |
| 56 | + # Test Plot data |
| 57 | + gmt begin ${COUNTRY} png |
| 58 | + gmt plot ${NAME}.gmt -Wred -JM15c -Bf -Re |
| 59 | + gmt end |
| 60 | + # Delete KMZ and KML files |
| 61 | + rm ${NAME}.km? |
0 commit comments