Skip to content

Instantly share code, notes, and snippets.

@gadomski
Last active June 11, 2020 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gadomski/f90e464114e5bbfecdda9e6b262f5adf to your computer and use it in GitHub Desktop.
Save gadomski/f90e464114e5bbfecdda9e6b262f5adf to your computer and use it in GitHub Desktop.
Slope maps with GMT
#!/usr/bin/env sh
set -ex
mkdir dem
cd dem
wget https://prd-tnm.s3.amazonaws.com/StagedProducts/Elevation/13/GridFloat/USGS_NED_13_n40w106_GridFloat.zip
wget https://prd-tnm.s3.amazonaws.com/StagedProducts/Elevation/13/GridFloat/USGS_NED_13_n41w106_GridFloat.zip
unzip USGS_NED_13_n40w106_GridFloat.zip
unzip -n USGS_NED_13_n41w106_GridFloat.zip
cd ..
mkdir water
cd water
wget https://prd-tnm.s3.amazonaws.com/StagedProducts/Hydrography/NHD/HU8/HighResolution/Shape/NHD_H_10190005_Shape.zip
unzip NHD_H_10190005_Shape.zip
cd ..
mkdir roads
cd roads
wget https://prd-tnm.s3.amazonaws.com/StagedProducts/Tran/Shape/TRAN_8_Colorado_GU_STATEORTERRITORY.zip
unzip TRAN_8_Colorado_GU_STATEORTERRITORY.zip
cd ..
XMIN:=-105.70
XMAX:=-105.62
YMIN:=40.37
YMAX:=40.41
OGR2GMT:=ogr2ogr -f GMT -spat $(XMIN) $(YMIN) $(XMAX) $(YMAX) -clipsrc spat_extent
default: build/hidden-valley.png
.PHONY: default
clean:
rm -rf build
.PHONY: clean
build:
mkdir $@
build/dem.vrt: Makefile | build
gdalbuildvrt -te $(XMIN) $(YMIN) $(XMAX) $(YMAX) $@ $(wildcard dem/*.flt)
build/dem.tif: build/dem.vrt
gdal_translate $< $@
build/slope.nc: build/dem.tif
grdgradient -fg $< -S$@ -D
grdmath $@ ATAN PI DIV 180 MUL = $@
build/gradient.nc: build/dem.tif
grdgradient -fg $< -G$@ -A-45 -Nt0.6
build/flowline.gmt: water/Shape/NHDFlowline.shp
$(OGR2GMT) $@ $<
build/waterbody.gmt: water/Shape/NHDWaterbody.shp
$(OGR2GMT) $@ $<
build/roads.shp: $(wildcard roads/Shape/Trans_RoadSegment*.shp)
ogr2ogr -f 'ESRI Shapefile' -spat $(XMIN) $(YMIN) $(XMAX) $(YMAX) -clipsrc spat_extent $@ $(word 1,$^)
ogr2ogr -f 'ESRI Shapefile' -spat $(XMIN) $(YMIN) $(XMAX) $(YMAX) -clipsrc spat_extent -addfields $@ $(word 2,$^)
ogr2ogr -f 'ESRI Shapefile' -spat $(XMIN) $(YMIN) $(XMAX) $(YMAX) -clipsrc spat_extent -addfields $@ $(word 3,$^)
build/roads.gmt: build/roads.shp
ogr2ogr -f GMT $@ $<
build/slope.cpt: Makefile
makecpt -Cwhite,'#ffff33','#ff7f00','#e41a1c','#984ea3','#377eb8','#777777' -T0,25,30,35,40,45,50,90 -N > $@
build/hidden-valley.ps: build/dem.tif build/slope.nc build/gradient.nc build/flowline.gmt build/waterbody.gmt build/roads.gmt build/slope.cpt Makefile
grdimage build/slope.nc -Cbuild/slope.cpt -Ibuild/gradient.nc -Ba -B+t"Hidden Valley" -JM8i -Rbuild/slope.nc -K > $@
grdcontour build/dem.tif -C20 -A100 -J -R -K -O >> $@
psxy build/flowline.gmt -W0.6p,'#377eb8' -J -R -K -O >> $@
psxy build/waterbody.gmt -G'#377eb8' -J -R -K -O >> $@
psxy build/roads.gmt -W1p,'#f781bf' -J -R -K -O >> $@
psbasemap -Lx5.2i/-0.7i+c$(YMIN)+w1k -J -R -K -O >> $@
psscale -D0i/-0.7i+w3i/0.2i+h -Cbuild/slope.cpt -G0/60 -By+l"Slope angle" -I -O >> $@
%.png: %.ps
psconvert -TG -A -P $<
@noirchen
Copy link

Hi this is very interesting, but when I tried to reproduce the map, in build/dem.vrt, the gdalbuildvrt process completes without error but I got an empty dem.vrt. I wonder whether this is an issue related to different versions of gdal.

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