2014-10-31 93 views
0

是我知道這個問題有史以來問充足的時間,但似乎從2012年,這個問題/ ansewers的基礎, 我試圖執行經典搜索距離列點 ,但我有一些麻煩無法解決..Mysql POINT專欄爲距離搜索

是否正常我的POINT列看起來像這樣?

0x00000000010100000085B1852007052040C0B2D2A414684840 

這裏是我的腳步,我不能夠看到什麼錯, 我並基於從最後一個堆問題/解答。

我使用mariadb 10和Heideisql gui。

我有2個colums緯度和經度, 我創建了一個geopoints點一欄, 填入的GeoPoint這樣的:

UPDATE geoFRA 
SET  geopoints = GeomFromText(CONCAT('POINT (', lon, ' ', lat, ')')) 

之後,我geopoints列如下:

0x00000000010100000085B1852007052040C0B2D2A414684840 

然後我嘗試以2種方式執行查詢,首先嚐試:

[email protected] = 48.88; 
[email protected] = 2.34; 

SELECT * 
FROM geoFRA 
WHERE MBRContains(LineFromText(CONCAT(
     '(' 
     , @lon + 700/(111.1/cos(RADIANS(@lon))) 
     , ' ' 
     , @lat + 700/111.1 
     , ',' 
     , @lon - 700/(111.1/cos(RADIANS(@lat))) 
     , ' ' 
     , @lat - 700/111.1 
     , ')') 
     ,geopoints) 

和第二次嘗試:

[email protected] = 48.88; 
[email protected] = 2.34; 
SET @kmRange = 172; -- = 50 Miles 

SELECT *, (3956 * 2 * ASIN(SQRT(POWER(SIN((@lat - abs(`lat`)) * pi()/180/2),2) + COS(@lat * pi()/180) * COS(abs(`lat`) * pi()/180) * POWER(SIN((lon - `lon`) * pi()/180/2), 2)))) as distance 
FROM `geoFRA` 
WHERE MBRContains(LineString(Point(@lat + @kmRange/111.1, @lon + @kmRange/(111.1/COS(RADIANS(@lat)))), Point(@lat - @kmRange/111.1, @lon - @kmRange/(111.1/COS(RADIANS(@lat))))), `geopoints`) 
Order By distance 

我開始覺得有一些不兼容MariaDB的?!還是我錯過了什麼?

感謝您的幫助.., flau

回答

0
CREATE TABLE geoFRA (id int NOT NULL, geopoints point NOT NULL); 
INSERT INTO geoFRA (id, geopoints) VALUES 
(1, geomFromText('POINT(48 2)')), 
(2, geomFromText('POINT(48 3)')), 
(3, geomFromText('POINT(48.88 2.34)')), 
(4, geomFromText('POINT(49 2)')), 
(5, geomFromText('POINT(49 3)')); 

SET @p=geomFromText('POINT(48.88 2.34)'); 
SELECT X(geopoints), Y(geopoints), asText(geopoints), ST_Distance(geopoints, @p) as d 
FROM geoFRA 
ORDER BY d; 

這將返回按距離排序的geopoints。使用沒有X(),Y()和asText()的地址點以已知二進制(WKB)格式返回它們:http://dev.mysql.com/doc/refman/5.7/en/gis-data-formats.html#gis-wkb-format