# How to find the nearest point on a grid?

**URL:** <https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 1, 2021, 7:46pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402 "2021-12-01T19:46:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 1, 2021, 7:46pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/1 "2021-12-01T19:46:20Z")

</div>

I have a grid defined as

```julia
Lon = [-180:0.25:180];
Lat = [-90:0.25:90];
X, Y = meshgrid(Lon, Lat);

```

I have many pairs of station data with corresponding longitude and latitude values and my goal is to find the nearest grid point for each station. For example, the results for a station with longitude of 101.02, and latitude of 24.97, would be [101, 25].

I did a google search and it seems that in Python, people can do this:

```julia
from scipy.interpolate import RegularGridInterpolator

itp = RegularGridInterpolator( (lat, lon), data, method='nearest') 
res = itp(some_new_point)

```

I wonder what would be the Julia solution? Many thanks!

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [December 1, 2021, 8:03pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/2 "2021-12-01T20:03:18Z")

</div>

I think the Julia equivalent to the Python code would use Interpolations.jl:

```julia
using Interpolations

itp = interpolate((lat, lon), data, Gridded(Constant()))
res = itp(some_new_point)

```

---

<div class="post-metadata">

**Author:** ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)\
**Post date:** [December 1, 2021, 8:09pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/3 "2021-12-01T20:09:14Z")

</div>

> [@leon](#):
>
> For example, the results for a station with longitude of 101.02, and latitude of 24.97, would be [101, 25].

Maybe this is a simple rounding problem?

- `round(x)`_“round `x` to the nearest integer”_

```julia
julia> Int8(round(101.02)) , Int8(round(24.97))
(101, 25)

```

EDIT:

- my proposal is not working for the [Antimeridian](https://en.wikipedia.org/wiki/180th_meridian) ; so need some post-checking …

![](https://global.discourse-cdn.com/julialang/original/3X/f/b/fb16e4a0ffe3ce4a4759282f2cb4f19a76215b55.jpeg)

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [December 1, 2021, 8:12pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/4 "2021-12-01T20:12:09Z")

</div>

In that example yes, but in the original post, the grid points vary by 0.25 degrees, not integers. Could be easy to solve if the mesh is regular, of course.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 1, 2021, 8:20pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/5 "2021-12-01T20:20:44Z")

</div>

Many thanks all for the replies!

Actually, my 0.25 example is a simplified one. I have grids like the below:

```julia
  -53.6250
  -53.3750
  -53.1250
  -52.8750
  -52.6250
  -52.3750
  -52.1250
  -51.8750
  -51.6250
  -51.3750

```

---

<div class="post-metadata">

**Author:** ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)\
**Post date:** [December 1, 2021, 8:33pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/6 "2021-12-01T20:33:00Z")

</div>

> [@leon](#):
>
> Actually, my 0.25 example is a simplified one. I have grids like the below:

so complex …

```julia
julia> (round( ( -52.1251 - 0.1250) *4*1000 ) / (4*1000) ) + 0.125
-52.125

julia> (round( ( -52.1249 - 0.1250) *4*1000 ) / (4*1000) ) + 0.125
-52.125

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 1, 2021, 8:34pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/7 "2021-12-01T20:34:49Z")

</div>

Thanks, Steven! Does this mean I would have to interpolate my lon lat pairs as if it is on a smooth line? In reality, my data points can be quite random and unrelated.

Is there a function that allows me to find the last value in [-180:0.25:180] that is smaller than my longitude? For example, if my longitude is 101.02, the value I’m looking for would be 101. Then I can find out whether my value is closer to 101 or closer to the next value 101.25, and decide which one to use.

Something like the below?

```julia
Ind = Lon .< 101.02;
Lon1 = Lon[Ind][end];
Lon2 = Lon1+0.25

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 1, 2021, 9:50pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/8 "2021-12-01T21:50:24Z")

</div>

If the spacing is regular, the mapping can always be expressed in integer multiples of the grid spacing, wouldn’t you agree?

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 1, 2021, 10:07pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/9 "2021-12-01T22:07:05Z")

</div>

Absolutely. However, in global models, it is common to use very complex grids to highlight regions with large changes. That said, I’m able to use the function` findfirst` for my needs.

Here is my code:

```julia
Ind0 = findfirst(Lon_grid .> Lon1[k]);
L2 = Lon_grid[Ind0]; 
L1 = Lon_grid[Ind0-1];
if abs(L2 - Lon1[k]) > abs(Lon1[k]-L1)
    Lon_id = Ind0-1;
else
    Lon_id = Ind0;
end

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 2, 2021, 1:46am UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/11 "2021-12-02T01:46:53Z")

</div>

Example

```julia
using GMT

# Have to add *z* too because this program expects *x y z*
blockmean([11.4 11.6 0], region=(10,15,10,15), spacing=1, center=true)

Vector{GMTdataset} with 1 segments
First segment DATA
Global BoundingBox: [11.0, 11.0, 12.0, 12.0, 0.0, 0.0]
First seg BoundingBox: [11.0, 11.0, 12.0, 12.0, 0.0, 0.0]
1×3 Matrix{Float64}:
 11.0 12.0 0.0

```

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [December 2, 2021, 2:07pm UTC](https://discourse.julialang.org/t/how-to-find-the-nearest-point-on-a-grid/72402/12 "2021-12-02T14:07:20Z")

</div>

I posted the Interpolations.jl code based on the Python code you originally posted, but upon further inspection it looks like you are not interpolating but discretizing. So what I posted seems irrelevant to what you’re looking for.
