我最近做了一个练习,尝试构建一个应用程序,同时假装自己是一个极简主义者。这要求我能够在不使用我久经考验的朋友 postgis 的情况下过滤 gps 点。
我快速浏览了三种最流行的空间数据索引方法:geohash、h3 和 s2。
所有这 3 个工具的工作原理都是将地球分割成形状,当您放大时,这些形状会细分为越来越小的单元。
geohash 使用矩形 h3 使用六边形 s2是狂野的,使用空间填充曲线探索这些类型的索引如何工作的一个好方法是https://geohash.softeng.co/.当您单击每个单元格时,它会放大并显示更深层次的内容。世界上我最喜欢的地方之一是 geohash ddk6p5
使用这些来“索引”gps 数据就像为您感兴趣的每个级别添加一个文本列一样简单。例如,我可以创建一个如下表:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
create table if not exists places (
id integer primary key autoincrement,
name text,
latitude real,
longitude real,
geohash_level_2 text,
geohash_level_4 text
);
insert into places (name,latitude,longitude,geohash_level_2,geohash_level_4)
values(mt shavano, 38.618840, -106.239364, 9w, 9wu7);
insert into places (name,latitude,longitude,geohash_level_2,geohash_level_4)
values(blanca peak, 37.578047, -105.485796, 9w, 9wsv);
insert into places (name,latitude,longitude,geohash_level_2,geohash_level_4)
values(mt princeton, 38.749148, -106.242578, 9w, 9wuk);
insert into places (name,latitude,longitude,geohash_level_2,geohash_level_4)
values(la soufriere, 13.336299, -61.177146, dd, ddk7);
sqlime 是尝试这些查询的好地方。
然后要查找科罗拉多州南部的地方,我会找到哪个哈希覆盖了地图上的“9w”部分:
1
select * from places where geohash_level_2 = 9w;
要查找加勒比海南部的地方,我可以使用:
1
select * from places where geohash_level_2 = dd;
这种方法确实需要预先规划您需要在数据库中保留哪些级别的索引。 否则,它非常适合需要快速查找特定区域或地图视口中找到的对象的简单应用程序。
拥有支持 like 查询的数据库可以帮助减少使用多列的情况。另请注意,h3 单元格编号并不完全重叠,因此“开头为”类型查询不适用于 h3。
最后,这些索引的一个巧妙功能是,单元格数量将成为很棒的实时事件主题。 因此,在上面的示例中,我可以为科罗拉多州南部创建一个名为“9w”的事件主题。然后,随着新地点的添加,我会在每个新地点的“9w”主题中发出一个事件。
以下是每种索引类型的一些示例 javascript (deno) 代码:
地理哈希
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { geocoordinatetogeohash } from npm:geocoordinate-to-geohash
import { geohashtopolygonfeature } from npm:geohash-to-geojson
import geohash from npm:ngeohash
const lat = 38.618840
const lng = -106.239364
const hash2 = geohash.encode(lat, lng, 2)
console.log(mt shavano hash at level 2, hash2)
const feat2 = geohashtopolygonfeature(hash2)
console.log(mt shavano hash at level 2 bounds, json.stringify(feat2))
// about a city block in size in co (size changes as you head towards poles)
const hash7 = geohash.encode(lat, lng, 7)
console.log(mt shavano hash at level 4, hash7)
const feat7 = geohashtopolygonfeature(hash7)
console.log(mt shavano hash at level 4 bounds, json.stringify(feat7))
h3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import h3 from npm:h3-js
import geojson2h3 from "npm:geojson2h3"
const lat = 38.618840
const lng = -106.239364
// level 2 (~1/3 colorado size)
const h3indexl2 = h3.latlngtocell(lat, lng, 2);
console.log(mt shavano cell at level 2, h3indexl2)
const featurel2 = geojson2h3.h3tofeature(h3indexl2)
console.log(mt shavano cell at level 2 bounds, json.stringify(featurel2))
// level 4 (~city of salida size)
const h3indexl4 = h3.latlngtocell(lat, lng, 4);
console.log(mt shavano cell at level 4, h3indexl4)
const featurel4 = geojson2h3.h3tofeature(h3indexl4)
console.log(mt shavano cell at level 4 bounds, json.stringify(featurel4))
s2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// This might be a better choice : https://www.npmjs.com/package/@radarlabs/s2
import s2 from npm:s2-geometry
const S2 = s2.S2
const lat = 38.618840
const lng = -106.239364
const key2 = S2.latLngToKey(lat, lng, 2)
const id2 = S2.keyToId(key2)
const feature2 = cellCornersToFeatureCollection(lat, lng, 2)
console.log(mt shavano key at level 2, key2)
console.log(mt shavano cell id at level 2, id2)
console.log(mt shavano cell at level 2 corners, JSON.stringify(feature2))
const key4 = S2.latLngToKey(lat, lng, 4)
const id4 = S2.keyToId(key4)
const feature4 = cellCornersToFeatureCollection(lat, lng, 4)
console.log(mt shavano key at level 4, key4)
console.log(mt shavano cell id at level 4, id4)
console.log(mt shavano cell at level 4 corners, JSON.stringify(feature4))
function cellCornersToFeatureCollection(lat, lng, level) {
const ll = S2.L.LatLng(lat, lng)
const cell = S2.S2Cell.FromLatLng(ll, level)
const corners = cell.getCornerLatLngs()
const coordinates = corners.map((pair) => {
return [pair.lng, pair.lat]
})
return {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [coordinates]
},
"properties": {}
}
]
}
}
以上就是地理空间索引采样器的详细内容,更多请关注php中文网其它相关文章!