Skip to content
Created: March 6, 2026

1. Why is spatial index needed? ​

Let's say the map has 1 million points.

If the user clicks on the map, they need to find:

text
离鼠标最近的点

The simplest way:

text
遍历所有点
O(n)

if:

text
n = 1000000

Iterate through each click:

text
1000000 次计算

The map will freeze.

So need:

text
空间索引(Spatial Index)

Goal:

text
O(n) → O(log n)

2. Core idea of spatial index ​

Ordinary database index:

text
B-Tree

is one-dimensional data.

But GIS data is:

text
二维 / 三维

For example:

text
(x, y)

So need:

text
多维索引

Common structures:

IndexPurpose
R-TreeGIS most common
QuadTreeMap tiles
KD-TreeNearest neighbor search

3. R-Tree (the most common in GIS) ​

R-Tree is rectangular tree 。

Core idea:

text
用矩形包裹对象

For example:

        Root
       /    \
    NodeA  NodeB

Each node is:

text
MBR
Minimum Bounding Rectangle

For example:

+--------+
| 点集合 |
+--------+

Insert process ​

Suppose you insert a point:

text
P(10,20)

Steps:

1 Find the most suitable node ​

Choose Minimum expansion area rectangle.

For example:

text
Area increase 最小

2 Insert leaf nodes ​

Leaf
 ├ Point1
 ├ Point2
 └ Point3

3 node overflow ​

if:

text
node > maxEntries

Just:

text
split

Split the node.

4. Query process (core) ​

For example query:

text
鼠标附近的点

Steps:

1 Start from root ​

Check:

text
query bbox

Whether it intersects with the node rectangle.

2 if they do not intersect ​

Directly:

text
skip

3 if intersected ​

Enter the child node.

4 arrive at leaf ​

Return data.

Query complexity:

text
O(log n)

instead of:

text
O(n)

5. QuadTree (commonly used for maps) ​

Map systems are common:

text
QuadTree

Principle:

text
空间递归四分

Indicate:

+--------+
|        |
|   +----+----+
|   |    |    |
|---+----+----|
|   |    |    |
|   +----+----+

Each node is divided into:

text
4 个象限

Advantages:

text
实现简单

Lots of maps:

text
tile

The essence is:

text
QuadTree

6. KD-Tree (nearest neighbor search) ​

If the question is:

text
找最近的点

Typically used:

text
KD-Tree

Principle:

text
交替按 x / y 分割

Example:

x < 50
      \
       y < 30

Suitable for:

text
Nearest Neighbor

7. How to implement spatial index on the front end ​

The front end generally does not write R-Tree by itself.

Common libraries:

rbush ​

Very popular.

javascript
import RBush from "rbush"

const tree = new RBush()

tree.insert({
  minX: 10,
  minY: 20,
  maxX: 10,
  maxY: 20,
  id: "point1"
})

Query:

javascript
tree.search({
  minX: 0,
  minY: 0,
  maxX: 50,
  maxY: 50
})

Complexity:

text
O(log n)

8. Real architecture of GIS system ​

Large map systems typically:

text
前端
 ↓
VectorTile
 ↓
TileServer
 ↓
PostGIS

Database layer:

text
GIST Index

For example:

sql
CREATE INDEX idx_geom
ON points
USING GIST(geom)

Query:

sql
ST_Intersects

9. Front-end real application scenarios ​

Common uses of spatial indexes:

1 Click on the map to select a point ​

text
click → nearest point

2 Frame selection query ​

text
bbox search

3 Aggregation algorithm ​

text
cluster

4 Collision detection ​

For example:

text
label collision

10. How to implement spatial index? ​

Spatial indexes are mainly used to optimize GIS data queries. Because spatial data is two-dimensional or three-dimensional, ordinary B-Tree can only process one-dimensional data, so GIS usually uses multi-dimensional index structures, such as R-Tree, QuadTree or KD-Tree.

Among them, R-Tree is the most common. It groups spatial objects through Minimum Bounding Rectangle to form a hierarchical tree structure.When querying, only the nodes that intersect the query range need to be traversed, thereby reducing the complexity from O(n) to O(log n).

In map systems, QuadTree is often used for tile map partitioning, while KD-Tree is more suitable for nearest neighbor search.

In front-end implementation, libraries such as rbush are generally used to build R-Tree indexes to quickly implement functions such as click point selection, frame selection query, or point aggregation.