# Computing the spatial autocorrelation of POIs locations in Berlin

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

## Requirements

To run this example you'll need:

* An active CARTO organization
* The latest version of the [Analytics Toolbox Advanced](broken://pages/1eMONTIWiApJb33x3dGZ) installed in your Snowflake database
* [CARTO Academy - Data for tutorials and examples](https://app.snowflake.com/marketplace/listing/GZT0Z4CM1E9J2) from Snowflake's Marketplace. With this free listing, users can access a set of sample tables with geospatial data that can be used to test and evaluate the CARTO geospatial analytics platform running natively on Snowflake

## Guide

In this example we are going to analyze the spatial correlation of POIs locations in Berlin using OpenStreetMap data and the [Moran’s I ](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-snowflake/sql-reference/statistics#morans_i_h3)function available in the statistics module. POIs data can be found in the publicly available `CARTO_ACADEMY_DATA.CARTO.OSM_POIS_BERLIN` table.

First, we are going to visually analyze the distribution of the POIs in the Berlin area by plotting the aggregation of POIs in each H3 cell of resolution 9. This can be done simply by applying the [H3\_FROMGEOGPOINT](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-snowflake/sql-reference/h3#h3_fromgeogpoint) function to compute the H3 cell that each POI belongs to and then performing a group by to count the number of POIs inside each cell (*n\_pois*).

By looking at the resulting map below, it is clear that there is a level of spatial autocorrelation in the distribution of the POIs:

{% embed url="<https://gcp-us-east1.app.carto.com/map/a1bd26d7-0a30-4f3d-a2e5-8cbc06a04124>" %}

We can measure this spatial autocorrelation using the [MORANS\_I\_H3](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-snowflake/sql-reference/statistics#morans_i_h3) function, which yields a result of `0.673` by applying the query below:

```sql
-- Create table with POI counts by grid cell
CREATE TABLE MYDB.MYSCHEMA.BERLIN_POI_COUNT_GRID AS
SELECT
    H3, COUNT(*) AS N_POIS
FROM (
    SELECT CARTO.CARTO.H3_FROMGEOGPOINT(GEOM, 9) AS h3
    FROM CARTO_ACADEMY_DATA.CARTO.OSM_POIS_BERLIN )
GROUP BY H3;

-- Compute Moran's I
CALL CARTO.CARTO.MORANS_I_H3(
    'MYDB.MYSCHEMA.BERLIN_POI_COUNT_GRID', 
    'MYDB.MYSCHEMA.BERLIN_POI_COUNT_GRID_MI',
    'H3', 
    'N_POIS', 
    1, 
    'exponential');

-- Read computed value
SELECT * FROM MYDB.MYSCHEMA.BERLIN_POI_COUNT_GRID_MI;
```

<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-snowflake/step-by-step-tutorials/computing-the-spatial-autocorrelation-of-pois-locations-in-berlin.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.
