# nearneighbor distance

**URL:** <https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563>\
**Category:** Q&A\
**Created:** [April 13, 2021, 4:47pm UTC](https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563 "2021-04-13T16:47:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![laure\_a](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@laure\_a](https://forum.generic-mapping-tools.org/u/laure_a)\
**Post date:** [April 13, 2021, 4:47pm UTC](https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563/1 "2021-04-13T16:47:38Z")

</div>

Hi,

Would there be a way to have the distance of the participating point(s) (at least the nearest) to the node of the cell in nearneighbor interpolation ?

Thanks for answer.  
Laure AVISSE  
SHOM  
French IHO

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 13, 2021, 8:49pm UTC](https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563/2 "2021-04-13T20:49:46Z")

</div>

Is it what you’re looking for ?  
[https://docs.generic-mapping-tools.org/6.1/nearneighbor.html](https://docs.generic-mapping-tools.org/6.1/nearneighbor.html) ?

---

<div class="post-metadata">

**Author:** ![laure\_a](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@laure\_a](https://forum.generic-mapping-tools.org/u/laure_a)\
**Post date:** [April 15, 2021, 7:28am UTC](https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563/3 "2021-04-15T07:28:51Z")

</div>

Is-it possible (what is the option) to have the distance to the node of the participating point(s) in an output file ?

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [April 15, 2021, 1:32pm UTC](https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563/4 "2021-04-15T13:32:36Z")

</div>

Not that I know of. The `gmtblock*` modules would be one place where that could be done.

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 15, 2021, 3:21pm UTC](https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563/5 "2021-04-15T15:21:55Z")

</div>

I guess you could realise it with two steps ?

1. Identify near neighbours with `nearneighbor` or `block*`
2. Compute the distance between points with `sphdistance`.

I’ve never used it so I’m not sure how it works, but here’s an example :

> **[(35) Spherical triangulation and distance calculations — GMT 6.1.1 documentation](https://docs.generic-mapping-tools.org/6.1/gallery/ex35.html#example-35)**

… Might be worth to explore.

---

<div class="post-metadata">

**Author:** ![laure\_a](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@laure\_a](https://forum.generic-mapping-tools.org/u/laure_a)\
**Post date:** [April 15, 2021, 3:28pm UTC](https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563/6 "2021-04-15T15:28:10Z")

</div>

I have some ideas of how to divert the gmtblock \* to do it but it seems complicated …

1. assign an identifier to each point
2. search (by dichotomy?) Until you have only one point in the search radius
3. get the identify of the point and then there coordinates and calculate the distance

Is there another easier way? (my data sets are large)

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [April 15, 2021, 3:42pm UTC](https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563/7 "2021-04-15T15:42:30Z")

</div>

> [@laure\_a](#):
>
> Is there another easier way? (my data sets are large)

Since you are SHOM I guess that means multi-beam bathymetry. I think the best solution would be to extend `blockmedian` to do it. You or any of colleagues with C knowledge would be willing to tackle that?

EDIT: or perhaps even simpler, add an option to `nearneighbor` to output a grid of those distances.

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 15, 2021, 4:01pm UTC](https://forum.generic-mapping-tools.org/t/nearneighbor-distance/1563/8 "2021-04-15T16:01:59Z")

</div>

Maybe you can adapt this code :

> [@How to calculate the shortest distance from one point to the coastline](https://flint.soest.hawaii.edu/t/how-to-calculate-the-shortest-distance-from-one-point-to-the-coastline/741/8):
>
> Very nice tips. I see that. It is good choice to use the -A to exclude the small islands for calculating distance to the mainland. I wrote a example to do this job. Hope it will help someone else who need this. #!/usr/bin/env bash # 2020-08-04 gmt gmtset FORMAT\_GEO\_MAP = ddd:mmF MAP\_FRAME\_WIDTH=2p gmt gmtset FONT\_ANNOT\_PRIMARY 7p,Helvetica,black FONT\_LABEL 7p,Helvetica,black ps=hainan.ps R=109:00/110/17:30/18:30 J=-Jm110/18/1:1000000 gmt grdmath -R$R -A10000/0/4 -Dh -I1m LDISTG = dist\_to\_…
