# 2D Interpolation on an irregular grid

**URL:** https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247
**Category:** Performance
**Tags:** interpolations, splines
**Created:** [October 20, 2023, 7:03pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247 "2023-10-20T19:03:34Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![k\_j](https://avatars.discourse-cdn.com/v4/letter/k/6f9a4e/32.png) [@k\_j](https://discourse.julialang.org/u/k_j)
#### Post date: [October 20, 2023, 7:03pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247/1 "2023-10-20T19:03:34Z")

</div>

I want to do 2D interpolation on an irregular grid.

In particular, I have a function f:\mathbb R^2 \rightarrow \mathbb R, (x, y) \mapsto z. I have an irregular grid D = (x\_i, y\_i)\_i as well as the corresponding values F = (f\_i)\_i. I would like to interpolate D, F to obtain f.

I have tried Dierckx, which according to the documentation supports 2D interpolation on irregular grids, but keeps giving me the following error:  
“No more knots can be added because the additional knot would  
(quasi) coincide with an old one: s too small or too large a weight to  
an inaccurate data point. The weighted least-squares spline  
corresponds to the current set of knots.”

I am currently using the “NaturalNeighbours” package in Julia to do the interpolation, and I have also used “Kriging”, but both are extremely slow.

I would love to hear any suggestions on how to speed up the performance of the interpolation, as well as if there are ways to fix the error I am getting with Dierckx.

Thanks!

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [October 20, 2023, 8:05pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247/2 "2023-10-20T20:05:22Z")

</div>

Have you tried Interpolations.jl.  
This is a link to the irregular grid interpolation feature:  
[https://juliamath.github.io/Interpolations.jl/latest/control/#Gridded-interpolation](https://juliamath.github.io/Interpolations.jl/latest/control/#Gridded-interpolation)

In any case, giving more info about the irregular grid (how big, dense, regular it is? How many dimensions?) would help.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [October 20, 2023, 8:13pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247/3 "2023-10-20T20:13:34Z")

</div>

It sounds like the OP needs scattered interpolation of some kind so perhaps ScatteredInterpolation.jl will work.

[https://eljungsk.github.io/ScatteredInterpolation.jl/dev/](https://eljungsk.github.io/ScatteredInterpolation.jl/dev/)

The idea is that you’re going to use some kind of radial basis function convolved with the points that you have sampled to intepolate over the remaining continuous space.

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [October 20, 2023, 8:37pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247/4 "2023-10-20T20:37:08Z")

</div>

You can also try [GitHub - gher-uliege/DIVAnd.jl: DIVAnd performs an n-dimensional variational analysis of arbitrarily located observations](https://github.com/gher-uliege/DIVAnd.jl), which allows smoother interpolation from scattered points, where multiple data at the same position can be dealt with.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 20, 2023, 8:38pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247/5 "2023-10-20T20:38:45Z")

</div>

One more suggestion: try `GMT.jl`’s splines in tension and see a [simple example here](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251/2).

---

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [October 20, 2023, 9:03pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247/6 "2023-10-20T21:03:14Z")

</div>

Another suggestion is the pure Julia version of the Clough-Tocher 2d interpolation that I ported from scipy:

> **[GitHub - fatteneder/CloughTocher2DInterpolation.jl: Shameless and direct...](https://github.com/fatteneder/CloughTocher2DInterpolation.jl)**
>
> Shameless and direct clone of scipy's CloughTocher2DInterpolator for Julia - GitHub - fatteneder/CloughTocher2DInterpolation.jl: Shameless and direct clone of scipy's CloughTocher2DInterpol...

---

<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: [October 20, 2023, 9:36pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247/7 "2023-10-20T21:36:31Z")

</div>

> [@rafael.guerra](#):
>
> One more suggestion: try `GMT.jl`’s splines in tension and see a [simple example here](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251/2).

And if one wants the choice of the full pack of GMT + GDAL interpolation methods see [gridit](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/utilities/gridit/index.html#gridit)

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [October 20, 2023, 9:41pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247/8 "2023-10-20T21:41:00Z")

</div>

Another native Julia option:

> **[GitHub - JuliaEarth/GeoStats.jl: An extensible framework for geospatial data...](https://github.com/JuliaEarth/GeoStats.jl)**
>
> An extensible framework for geospatial data science and geostatistical modeling fully written in Julia - GitHub - JuliaEarth/GeoStats.jl: An extensible framework for geospatial data science and geo...

Check this textbook, chapter 11:

[https://juliaearth.github.io/geospatial-data-science-with-julia/](https://juliaearth.github.io/geospatial-data-science-with-julia/)

---

<div class="post-metadata">

### Author: ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)
#### Post date: [October 20, 2023, 10:53pm UTC](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247/9 "2023-10-20T22:53:52Z")

</div>

> I am currently using the “NaturalNeighbours” package in Julia to do the interpolation, and I have also used “Kriging”, but both are extremely slow.

Just curious: What was slow for NaturalNeighbours.jl? Are you computing it in batches? What method are you using? It should be very performant, so I’m interested in improving it. If you give some example data and how you are calling it I can look into it also.
