# 2.2 Distance-Based Weights

Distance-based weights are constructed using distance measures between all pairs of observations. The neighbor relation can be determined by a given ***distance band*****,** so every other observation that falls within the distance band is considered as neighbors of one observation. Another way to determine neighbor relation is to find ***K nearest neighbors*** (***KNN***) for each observation.&#x20;

For more information, please read: <https://geodacenter.github.io/workbook/4b_dist_weights/lab4b.html>

**CONTENTS**

1. min\_distthreshold()
2. distance\_weights()
3. knn\_weights()

### 1. min\_distthreshold()

In order to avoid isolates (islands) that would result from too stringent a critical distance, the distance must be chosen such that each location has at least one neighbor. Such a distance conforms to a *max-min* criterion, i.e., it is the largest of the nearest neighbor distances.

**Synopsis**

Short version

```sql
float min_distthreshold(integer gid, geometry the_geom)
```

Full version

```sql
float min_distthreshold(integer gid, geometry the_geom,
    boolean is_arc, boolean is_mile)
```

**Arguments**

| Input Arguments | Type     | Description                                                                    |
| --------------- | -------- | ------------------------------------------------------------------------------ |
| gid             | integer  | the feature id of geometry: e.g. gid, fid, ogc\_*fid,* cartod&#x62;*\_*&#x69;d |
| the\_geom       | geometry | the geometry (only points and polygons are supported)                          |
| is\_arc         | boolean  | if compute arc distance between two observations. Default: FALSE.              |
| is\_mile        | boolean  | if convert distance unit from mile to kilometer(KM). Default: TRUE.            |

**Return**

| **Value** | Description                                                                     |
| --------- | ------------------------------------------------------------------------------- |
| float     | the minimum distance that makes sure each observation has at least one neighbor |

**Examples**

```sql
SELECT min_distthreshold(gid, the_geom) FROM natregimes;

 min_distthreshold 
-------------------
           1.46578
(1 row)
```

### 2. distance\_weights()

With the distance band, one can create a distance-based spatial weights using WINDOW function distance\_weights()

**Synopsis**

Short version:

```sql
float distance_weights(integer gid, geometry the_geom, float dist_band)
```

Full Version:

```sql
float distance_weights(integer gid, geometry the_geom, float dist_band,
    float power, 
    boolean is_inverse, 
    boolean is_arc,
    boolean is_mile)
```

**Arguments:**

| Input Arguments | Type     | Description                                                                                                  |
| --------------- | -------- | ------------------------------------------------------------------------------------------------------------ |
| gid             | integer  | the feature id of geometry: e.g. gid, fid, ogc*fid,* cartod&#x62;*\_*&#x69;d                                 |
| the\_geom       | geometry | the geometry (only points and polygons are supported)                                                        |
| dist\_band      | float    | the distance band/threshold that makes sure each observation has at least one neighbor                       |
| power           | float    | the power/exponent corresponds to the number of times the base (dist\_band) is used as a factor. Default: 1. |
| is\_inverse     | boolean  | if apply inverse on distance value. Default: False.                                                          |
| is\_arc         | boolean  | if compute arc distance between two observations. Default: FALSE.                                            |
| is\_mile        | boolean  | if convert distance unit from mile to kilometer(KM). Default: TRUE.                                          |

**Return**

| **Value** | Description                                                                                 |
| --------- | ------------------------------------------------------------------------------------------- |
| bytea     | the weights structure for each observation in binary format, which is defined in table 2.1. |

**Examples**

```sql
SELECT distance_weights(gid, the_geom, 1.46578) OVER() FROM natregimes;

                         
                  distance_weights                                                                                                                                                                              
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 \x770700001400600800001608000014080000130800005f08000017080000c3070000c5070000c70700007107000074070000750700001e0700002a0700002107000020070000b9060000be060000c407000062060000e8e6a83f95d1b13f7049833f76be5f3f622eae3f10b88f3ff95bdf3e1218953f2716533fb6f0893fa2cc093ff686333f3787dd3e9c8b943f74c
8303f7074533f3d608c3f9fe47c3fec4c313faf3bad3f
 \x7a0000000e007b0000006d000000cc000000b300000098000000970000008d000000b20000005c0000004e00000025000000670000006800000039000000b9d4af3f5f5dad3f0ba3aa3f80a43a3ffeb2d93eb76d613fa2ed8d3fa3828e3fdba3753f36f80c3f4354b63f5839993fe7b6403fbeb4983f
 \xeb0000001200260100003f01000066010000ce000000490100002501000024010000010100000401000002010000ea000000c9000000cb00000096000000b0000000af0000009500000094000000678c663fa55ab03ff45eac3f5639613fe17ea53f227f6a3f6d34943f8fcf523fa3487e3f49c6b73eeb61513f3336683ff2cdb03ece2b9e3fb1c47c3f3a57393f771
6ab3f99bf883f
...
```

### 3. knn\_weights()

**Synopsis**

Short version:

```sql
float knn_weights(integer gid, geometry the_geom, integer k)
```

Full Version:

```sql
float knn_weights(integer gid, geometry the_geom, integer k,
    float power, 
    boolean is_inverse, 
    boolean is_arc,
    boolean is_mile)
```

**Arguments:**

| Input Arguments | Type     | Description                                                                                                  |
| --------------- | -------- | ------------------------------------------------------------------------------------------------------------ |
| gid             | integer  | the feature id of geometry: e.g. gid, fid, ogc*fid,* cartod&#x62;*\_*&#x69;d                                 |
| geom            | geometry | the geometry (only points and polygons are supported)                                                        |
| k               | integer  | the k nearest neighbors                                                                                      |
| power           | float    | the power/exponent corresponds to the number of times the base (dist\_band) is used as a factor. Default: 1. |
| is\_inverse     | boolean  | if apply inverse on distance value. Default: False.                                                          |
| is\_arc         | boolean  | if compute arc distance between two observations. Default: FALSE.                                            |
| is\_mile        | boolean  | if convert distance unit from mile to kilometer(KM). Default: TRUE.                                          |

**Return**

| **Value** | Description                                                                                 |
| --------- | ------------------------------------------------------------------------------------------- |
| bytea     | the weights structure for each observation in binary format, which is defined in table 2.1. |

**Examples**

```sql
SELECT knn_weights(gid, the_geom, 5) OVER() FROM natregimes;

                         
                                          knn_weights                                           
------------------------------------------------------------------------------------------------
 \x770700000500c40700007407000021070000c30700001e070000ec4c313fa2cc093f74c8303ff95bdf3e3787dd3e
 \x7a000000050097000000b3000000680000004e00000098000000b76d613f80a43a3fe7b6403f36f80c3ffeb2d93e
 \xeb000000050001010000ea000000af000000cb000000020100008fcf523feb61513f3a57393ff2cdb03e49c6b73e
...
```


---

# 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://xunli.gitbook.io/postgeoda/2.-spatial-weights/2.2-distance-based-weights.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.
