您好,登錄后才能下訂單哦!
不知道大家之前對(duì)類似Mysql空間如何搜GeoHash的文章有無(wú)了解,今天我在這里給大家再簡(jiǎn)單的講講。感興趣的話就一起來(lái)看看正文部分吧,相信看完Mysql空間如何搜GeoHash你一定會(huì)有所收獲的。
應(yīng)用場(chǎng)景介紹:
公司新業(yè)務(wù)的需求,希望能夠?qū)崿F(xiàn)當(dāng)前位置快速顯示共享信息數(shù)據(jù),針對(duì)5.7新特性的支持度調(diào)研,利用GeoHash封裝成內(nèi)置數(shù)據(jù)庫(kù)函數(shù)的簡(jiǎn)易方案,實(shí)現(xiàn)前期的初期業(yè)務(wù)。
實(shí)現(xiàn)過(guò)程:
1、構(gòu)建表
CREATE TABLE `tongzhou` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`cname` VARCHAR(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`oint` POINT NOT NULL,
PRIMARY KEY (`id`),
SPATIAL KEY `sp_index` (`oint`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
2、構(gòu)造簡(jiǎn)單數(shù)據(jù)
insert into `tongzhou` (`cname`, oint) values('景欣園',GEOMFROMTEXT('POINT(39.8885917679 116.6576038966)'));
insert into `tongzhou` (`cname`, oint) values('京通羅斯福廣場(chǎng)',GEOMFROMTEXT('POINT(39.8890214887 116.6473661241)'));
insert into `tongzhou` (`cname`, oint) values('中國(guó)北京市北京市通州區(qū)',GEOMFROMTEXT('POINT(39.8898670523 116.6566729546)'));
insert into `tongzhou` (`cname`, oint) values('中國(guó)北京市北京市通州區(qū)',GEOMFROMTEXT('POINT(39.8883852752 116.655728817)'));
insert into `tongzhou` (`cname`, oint) values('楊莊路22號(hào)院',GEOMFROMTEXT('POINT(39.8984936518 116.6339063644)'));
insert into `tongzhou` (`cname`, oint) values('中國(guó)北京市北京市朝陽(yáng)區(qū) ',GEOMFROMTEXT('POINT(39.8975388526 116.613779068)'));
insert into `tongzhou` (`cname`, oint) values('北京市朝陽(yáng)區(qū)第三醫(yī)院 約31米',GEOMFROMTEXT('POINT(39.8873809413 116.603307724)'));
insert into `tongzhou` (`cname`, oint) values('雙橋溫泉北里小區(qū)',GEOMFROMTEXT('POINT(39.8918427053 116.6076636314)'));
insert into `tongzhou` (`cname`, oint) values('京客隆配送中心',GEOMFROMTEXT('POINT(39.8916616061 116.5908622742)'));
insert into `tongzhou` (`cname`, oint) values('世紀(jì)賓館',GEOMFROMTEXT('POINT(39.8918591688 116.6024065018)'));
insert into `tongzhou` (`cname`, oint) values('雙橋六號(hào)井小區(qū)-北區(qū) 約48米',GEOMFROMTEXT('POINT(39.8899164443 116.6050457954)'));
insert into `tongzhou` (`cname`, oint) values('翠屏里小區(qū) 約190米',GEOMFROMTEXT('POINT(39.8883194176 116.6495060921)'));
insert into `tongzhou` (`cname`, oint) values('京通羅斯福廣場(chǎng)',GEOMFROMTEXT('POINT(39.8894719148 116.6584646702)'));
insert into `tongzhou` (`cname`, oint) values('life新生活廣場(chǎng) 約87米',GEOMFROMTEXT('POINT(39.8895542353 116.65579319)'));
insert into `tongzhou` (`cname`, oint) values('life新生活廣場(chǎng) 約74米',GEOMFROMTEXT('POINT(39.8896242077 116.6568982601)'));
insert into `tongzhou` (`cname`, oint) values('life新生活廣場(chǎng)',GEOMFROMTEXT('POINT(39.8901428239 116.6564154625)'));
insert into `tongzhou` (`cname`, oint) values('漫春園',GEOMFROMTEXT('POINT(39.8913364496 116.658115983)'));
insert into `tongzhou` (`cname`, oint) values('7天連鎖酒店(北京通州果園環(huán)島店) 約58米',GEOMFROMTEXT('POINT(39.8915998677 116.6560935974)'));
insert into `tongzhou` (`cname`, oint) values('北京金松賓館 約77米',GEOMFROMTEXT('POINT(39.9036172391 116.6577833891)'));
insert into `tongzhou` (`cname`, oint) values('新華南路小區(qū)-東區(qū) 約32米',GEOMFROMTEXT('POINT(39.9034444015 116.6588240862)'));
insert into `tongzhou` (`cname`, oint) values('紅旗小區(qū) 約61米',GEOMFROMTEXT('POINT(39.9046707164 116.6598540545)'));
insert into `tongzhou` (`cname`, oint) values('祥云天地家園 約62米',GEOMFROMTEXT('POINT(39.9067899674 116.659129858)'));
insert into `tongzhou` (`cname`, oint) values('梨園地鐵站 約40米',GEOMFROMTEXT('POINT(39.8832975966 116.6687965393)'));
3、搜索當(dāng)前1公里范圍內(nèi)的位置信息
SET @jl=1; //多少公里范圍內(nèi)的
SET @jd=118.1964111328;
SET @wd=39.5591182422; //當(dāng)前的位置信息
oint是實(shí)際已經(jīng)存在的經(jīng)緯度信息
SELECT *,ASTEXT(oint)FROM tongzhou WHERE MBRCONTAINS( LINESTRING(POINT( @wd + @jl / ( 111.12 / COS(RADIANS(@jd))),
@jd + @jl / 111.12 ),
POINT ( @wd - @jl / ( 111.12 / COS(RADIANS(@jd))),@jd - @jl / 111.12) ), oint)
###經(jīng)緯弧度(1° latitude = 111.12 kilometers)即 10/111.12,表示查找附近10公里。
4、測(cè)試
通過(guò)百度地圖獲取一些數(shù)據(jù),對(duì)
SET @jd=118.1964111328
SET @wd=39.5591182422
值的更新,分析獲取范圍內(nèi)的景點(diǎn)信息
5、問(wèn)題:
如果直接在WHERE采用一系列的表達(dá)式,這樣會(huì)導(dǎo)致無(wú)法使用空間索引,如圖所示:
解決辦法:
1、把WHERE后的表達(dá)式處理成一個(gè)變量進(jìn)行引用,然后進(jìn)行周邊搜
SET @aa=(SELECT LINESTRING(POINT( 39.5591182422 + 10/ ( 111.12 / COS(RADIANS(118.1964111328))),
118.1964111328 + 10 / 111.12 ),
POINT ( 39.5591182422 - 10 / ( 111.12 / COS(RADIANS(118.1964111328))),118.1964111328 -10 / 111.12)) AS heji)
2、執(zhí)行周邊搜
SELECT ASTEXT(oint)FROM tongzhou WHERE MBRWITHIN(@aa,oint)
順帶執(zhí)行計(jì)劃效果:
其他SQL寫法補(bǔ)充:
表示3公里范圍內(nèi)的點(diǎn):
DDL:
CREATE TABLE `shop_id` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lat` double DEFAULT NULL,
`lng` double DEFAULT NULL,
`oint` point NOT NULL,
PRIMARY KEY (`id`),
SPATIAL KEY `sp_index` (`oint`)
) ENGINE=InnoDB AUTO_INCREMENT=275915 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SELECT id, ST_Distance_Sphere(POINT(-73.951368, 40.716743), oint) AS dist, ST_AsText(oint) FROM shop_id
WHERE ST_Contains( ST_MakeEnvelope( POINT((-73.951368+(3/111)), (40.716743+(3/111))),
POINT((-73.951368-(3/111.12)), (40.716743-(3/111.12))) ), oint )
看完Mysql空間如何搜GeoHash這篇文章,大家覺(jué)得怎么樣?如果想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。