# How to trim a contour map?

**URL:** <https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477>\
**Category:** Q&A\
**Created:** [March 20, 2021, 10:36am UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477 "2021-03-20T10:36:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anuar](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@Anuar](https://forum.generic-mapping-tools.org/u/Anuar)\
**Post date:** [March 20, 2021, 10:36am UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/1 "2021-03-20T10:36:59Z")

</div>

Hello everyone!

Could you please give me a hint how I can crop my contour map that was created using surface command? I feel like I should use gridcut, however it seems I need t specify my region of interest by -R, whereas I would like to cut everything outside the red polygon. I have looked at “select” command but it seems it takes only points. Thank you in advance.

> gmt begin contour png  
> Rg=-R958800/959700/2117300/2118000  
> gmt surface vel.txt $Rg -JX20c -I25 -Gvelmap.nc  
> gmt grdview velmap.nc -Ccolors.cpt -Qs  
> gmt grdcontour velmap.nc -C0.5 -A0.5+f12p,Helvetica,black -Gd10c -S4   
> -Wa0.75p,black -Wc0.25p,black  
> gmt plot polygon.txt -W2p,red  
> gmt plot gps\_lamb.csv -i0,1 $Rg -JX20c -B -Sc0.5c -Gblack  
> gmt colorbar -DJBC -Bx1+l"Velocity (mm hr@±1@+)" -Ccolors.cpt  
> gmt end show

 ![contour](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/4/4818f030b93f44dc7f1b7a32e1e63cf705746319.png)

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [March 20, 2021, 1:41pm UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/2 "2021-03-20T13:41:14Z")

</div>

Maybe not the optimal way, but I think you could use `grd2xyz` to dump the grid, pipe it trough `gmtselect` (remove points outside the polygon) and then pipe that to `xyz2grd` to make a new grid.

```auto
grd2xyz velmap.nc | gmtselect -Fpolygon.txt | xyz2grd $(gmt info -I25 polygon.txt ) -I25 -Gvelmap_clip.nc

```

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [March 20, 2021, 3:50pm UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/3 "2021-03-20T15:50:32Z")

</div>

You can try “gmt clip”.

---

<div class="post-metadata">

**Author:** ![Anuar](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@Anuar](https://forum.generic-mapping-tools.org/u/Anuar)\
**Post date:** [March 20, 2021, 5:21pm UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/4 "2021-03-20T17:21:04Z")

</div>

Thank you, @Andreas and @seisman! GMT clip did the job. For those who have the same question, I have just added one line of code  
“gmt clip $Rg -JX20c polygon.txt” in the beginning.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [March 20, 2021, 6:03pm UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/5 "2021-03-20T18:03:43Z")

</div>

Remember to turn off clipping with another gmt clip -c after you are done. May not matter to this plot but will matter to other things you may do. Clipping is an ON then OFF paired operation.

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [March 20, 2021, 8:19pm UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/6 "2021-03-20T20:19:44Z")

</div>

The ability to cut a grid based on a polygon - would this be a nice addition to `grdcut`?

E.g.

```auto
gmt grdcut in.grd -Gout.nc -Fcut_polygon.gmt

```

What is the recommended way to cut a grid based on a polygon?

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [March 20, 2021, 9:23pm UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/7 "2021-03-20T21:23:47Z")

</div>

You can use grdmask. Create a grid with NaN outside and 1 inside the polygon and then multiplied both grids.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [March 20, 2021, 10:08pm UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/8 "2021-03-20T22:08:45Z")

</div>

Andreas has a point though. It seems like a good option to have for a module like grdcut. The grdmask alternative requires two steps and setting of some options. For the -Fpoly.txt option I think we simply want to retain all values inside the polygon and set the outside to NaN. So -F would make that much simpler.

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [March 21, 2021, 7:08am UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/9 "2021-03-21T07:08:55Z")

</div>

Although a trivial(?) task for a seasoned gmt user, I think this would be a very nice addition to `grdcut`. It is a fairly common operation. Saving time is always nice.

One thing though; retaining all values inside the polygon and set the outside to NaN is nice, but shouldnt there also be calculated a new `-R` based on the polygon? I can see many cases where using the original `-R` from the input grid, would cause a too big region to be used, containing only NaN’s.

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [March 21, 2021, 8:13am UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/10 "2021-03-21T08:13:49Z")

</div>

Yes of course, it is grdcut so it would cut the rectangular bounding box of the polygon, but unless your polygon is a rectangle there will be inside and outside parts.

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [March 21, 2021, 8:46am UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/11 "2021-03-21T08:46:44Z")

</div>

Sounds great! [padding - post must be \> 20 chars]

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [March 22, 2021, 10:25am UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/12 "2021-03-22T10:25:22Z")

</div>

I took the liberty to create a feature request; [https://github.com/GenericMappingTools/gmt/issues/5004](https://github.com/GenericMappingTools/gmt/issues/5004).

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [March 24, 2021, 8:46pm UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/13 "2021-03-24T20:46:21Z")

</div>

Implemented and merged into master: grdcut **-F** _polygon_[**+c**][**+i**].  
+c crops grid region to bounding box [Default uses inout grid region, possibly modified by -R]  
+i inverts the fill and sets all nodes inside the polygon to NaN [Default sets outside nodes to NaN]

> <https://github.com/GenericMappingTools/gmt/pull/5007>
>
> The output grid will reflect the bounding box of the polygon (clipped to the inp…ut grid bounds if the polygon exceeds them) and all nodes outside the polygon will be set to NaN. Closes #5004. Need @anbj to give it a spin. I tried
> 
> \`\`\`
> gmt coast -M -EES \> spain.txt
> gmt grdcut @earth\_relief\_10m -R-20/5/25/45 -Gt.grd
> gmt grdcut t.grd -Fspain.txt -V -Gnew.grd
> gmt grdimage new.grd -JM5i -Baf -pdf map
> 
> \`\`\`
> and lo and behold it worked on first test. But does not work directly with @earth\_relief\_10m yet, hence WIP for now.

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [March 25, 2021, 7:16am UTC](https://forum.generic-mapping-tools.org/t/how-to-trim-a-contour-map/1477/14 "2021-03-25T07:16:23Z")

</div>

A big thank you to Paul et al for implementing this!
