> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/appendixes/contrib/earthdistance/earthdistance-cube-based.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# F.14.1. 以 Cube 為基礎的地球距離

## F.14.1. 以 Cube 為基礎的地球距離 [#](#earthdistance-cube-based) <a href="#earthdistance-cube-based" id="earthdistance-cube-based"></a>

資料會以 point cube（兩個角點相同）儲存，使用三個座標表示與地球中心的 x、y、z 距離。模組提供建置於 `cube` 型別上的 [*domain*](https://docs.postgresql.tw/19/appendixes/glossary.md#glossary-domain) `earth`，其中包含限制條件檢查，以確保值符合這些限制且合理接近地球的實際表面。

地球半徑由 `earth()` 函式取得，單位為公尺。但只要變更這一個函式，即可讓模組 使用其他單位，或使用你認為較適當的不同半徑值。

此套件也可用於天文資料庫。天文學家可能會想讓 `earth()` 傳回 `180/pi()` 的 半徑，使距離單位成為度。

提供的函式支援以緯度與經度（度）輸入、輸出緯度與經度、計算兩點之間的大圓距離， 以及方便地指定可用於索引搜尋的邊界方框。

提供的函式如[表 F.4](#earthdistance-cube-functions) 所示。

### 表 F.4. 以 Cube 為基礎的 Earthdistance 函式 [#](#earthdistance-cube-functions) <a href="#earthdistance-cube-functions" id="earthdistance-cube-functions"></a>

| <p>函式</p><p>說明</p>                                                                                                                                                                                                                                      |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>earth</code> () → <code>float8</code></p><p>傳回假定的地球半徑。</p>                                                                                                                                                                                     |
| <p><code>sec\_to\_gc</code> ( <code>float8</code> ) → <code>float8</code></p><p>將地球表面兩點間的一般直線（割線）距離轉換為兩點間的大圓距離。</p>                                                                                                                                     |
| <p><code>gc\_to\_sec</code> ( <code>float8</code> ) → <code>float8</code></p><p>將地球表面兩點間的大圓距離轉換為兩點間的一般直線（割線）距離。</p>                                                                                                                                     |
| <p><code>ll\_to\_earth</code> ( <code>float8</code>, <code>float8</code> ) → <code>earth</code></p><p>根據點的緯度（引數 1）及經度（引數 2）度數，傳回其在地球表面的位置。</p>                                                                                                          |
| <p><code>latitude</code> ( <code>earth</code> ) → <code>float8</code></p><p>傳回地球表面上一點的緯度（度）。</p>                                                                                                                                                        |
| <p><code>longitude</code> ( <code>earth</code> ) → <code>float8</code></p><p>傳回地球表面上一點的經度（度）。</p>                                                                                                                                                       |
| <p><code>earth\_distance</code> ( <code>earth</code>, <code>earth</code> ) → <code>float8</code></p><p>傳回地球表面兩點之間的大圓距離。</p>                                                                                                                             |
| <p><code>earth\_box</code> ( <code>earth</code>, <code>float8</code> ) → <code>cube</code></p><p>傳回適合索引搜尋的方框，可使用 <code>cube</code> <code>@></code> 運算子尋找與某位置相隔指定大圓距離內的點。此方框中的某些點與該位置 相隔的距離會超過指定大圓距離，因此查詢中應包含使用 <code>earth\_distance</code> 的第二次檢查。</p> |

<br>

[返回 F.14. earthdistance — 計算大圓距離](https://docs.postgresql.tw/19/appendixes/contrib/earthdistance.md#earthdistance)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/earthdistance.html)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/appendixes/contrib/earthdistance/earthdistance-cube-based.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
