# Interpolating elevation along a road using kriging

<div align="left"><figure><img src="/files/OG2RD7V5fNQpaw0lxn8Y" alt="Intermediate difficulty banner" width="175"><figcaption></figcaption></figure></div>

In this example, we will perform kriging interpolation of the elevation along the so-called roller coaster road on the island of Hokkaido, Japan, using as reference points a nearby elevation measurement.

## Reference elevation data <a href="#reference-elevation-data" id="reference-elevation-data"></a>

The elevation data that will be used as reference to perform the interpolation is composed of 50 elevation points within 500m of the route that were randomly sampled from [the NASADEM dataset](https://carto.com/spatial-data-catalog/browser/dataset/nasa_nasadem_e6e4b116/), available from [CARTO’s Data Observatory](https://docs.carto.com/data-and-analysis/data-observatory). This data can be accessed publicly from the table `cartobq.docs.nasadem_jp_extract` and is previewed in the map below:

{% embed url="<https://gcp-us-east1.app.carto.com/map/9f8801c6-6a51-447f-860f-a433af169df7>" %}

## Points to be interpolated <a href="#points-to-be-interpolated" id="points-to-be-interpolated"></a>

Since the route is straight, we can easily calculate evenly distributed points along the route and use them as the points for which to perform the interpolation. The result can be publicly accessed from `cartobq.docs.kriging_jp_interp_points`.

```sql
SELECT
  ST_GEOGPOINT(142.41*p+142.44*(1-p),43.496*p+43.53*(1-p)) point
FROM
  # generate 101 points evenly spaced along the route
  UNNEST(GENERATE_ARRAY(0,1,0.01)) p;
```

## Computing the interpolation <a href="#computing-the-interpolation" id="computing-the-interpolation"></a>

Now let’s use the kriging interpolation stored procedure, [ORDINARY\_KRIGING\_TABLE](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-bigquery/sql-reference/statistics#ordinary_kriging_table) to create a table with the result.

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.ORDINARY_KRIGING_TABLE(
         'cartobq.docs.nasadem_jp_extract',
         'cartobq.docs.kriging_jp_interp_points',
         'cartobq.docs.kriging_jp_result',
         50,
         1000,
         20,
         'exponential'
     );
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.ORDINARY_KRIGING_TABLE(
         'cartobq.docs.nasadem_jp_extract',
         'cartobq.docs.kriging_jp_interp_points',
         'cartobq.docs.kriging_jp_result',
         50,
         1000,
         20,
         'exponential'
     );
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.ORDINARY_KRIGING_TABLE(
         'cartobq.docs.nasadem_jp_extract',
         'cartobq.docs.kriging_jp_interp_points',
         'cartobq.docs.kriging_jp_result',
         50,
         1000,
         20,
         'exponential'
     );
```

{% endtab %}
{% endtabs %}

In this visualization we can see the computed elevation along the route accentuated by a factor of 10.

{% embed url="<https://gcp-us-east1.app.carto.com/map/9f285926-6e77-4e42-826d-f0e2683f9fae>" %}

<img src="/files/4m1BK9j4Wq34gat4HHd2" alt="EU flag" data-size="line"> This project has received funding from the [European Union’s Horizon 2020](https://ec.europa.eu/programmes/horizon2020/en) research and innovation programme under grant agreement No 960401.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://academy.carto.com/advanced-spatial-analytics/spatial-analytics-for-bigquery/step-by-step-tutorials/interpolating-elevation-along-a-road-using-kriging.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
