本文介绍了MySQL - 在空间点附近选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的查询中选择了一个名为

 $ b添加空间键$ b $ query = sprintf(SELECT * FROM lastcrawl WHERE 
MBRContains(LineFromText(CONCAT(
'('
,%F + 0.0005 *(111.1 / cos(%F))
,'
,%F + 0.0005 / 111.1
','
,%F - 0.0005 /(111.1 / cos(%F))
'' '
,%F - 0.0005 / 111.1
,')')
,point),
$ lng,$ lng,$ lat,$ lng,$ lat,$ lat);
$ result = mysql_query($ query)或die(mysql_error());

我已经成功插入了几行:

$ query = sprintf(INSERT INTO lastcrawl(point)VALUES(GeomFromText('POINT(%F%F)')),$ lat,$ lng);

但是,我只是无法查询最近的点来获得非空结果集。你如何得到查询来选择离给定空间点最近的点?






插入 $ lat = 40,$ lng = -100 ,试图查询这个也不会返回任何东西(所以,甚至没有确切的坐标匹配):



SELECT * FROM lastcrawl WHERE MBRContains(LineFromText(CONCAT('(',-100.000000 + 0.0005 *(111.1 / cos(-100.000000)),'',40.000000 + 0.0005 / 111.1 ,',',-100.000000 - 0.0005 /(111.1 / cos(40.000000)),'',40.000000 - 0.0005 / 111.1,')')),point)

解决方案

我已经修正了一些公式。



p>

  CREATE TABLE lastcrawl(id INT NOT NULL PRIMARY KEY,pnt POINT NOT NULL)ENGINE = MyISAM; 

INSERT
INTO lastcrawl
VALUES(1,POINT(40,-100));

SET @lat = 40;
SET @lon = -100;

SELECT *
FROM lastcrawl
WHERE MBRContains

LineString

Point

@lat + 10 / 111.1,
@lon + 10 /(111.1 / COS(RADIANS(@lat)))
),
Point(
@lat - 10 / 111.1,
@lon - 10 /(111.1 / COS(RADIANS(@lat)))

),
pnt
);


I've based my query below to select points near a spatial point called point on the other SO solution, but I've not been able to get it to return any results, for the past few hours, and I'm a bit edgy now...

My table lastcrawl has a simple schema:

 id (primary int, autoinc)
 point (spatial POINT)

(Added the spatial key via ALTER TABLE lastcrawl ADD SPATIAL INDEX(point);)

$query = sprintf("SELECT * FROM lastcrawl WHERE
     MBRContains(LineFromText(CONCAT(
        '('
        , %F + 0.0005 * ( 111.1 / cos(%F))
        , ' '
        , %F + 0.0005 / 111.1
        , ','
        , %F - 0.0005 / ( 111.1 / cos(%F))
        , ' '
        , %F - 0.0005 / 111.1
        , ')' )
        ,point)",
        $lng,$lng,$lat,$lng,$lat,$lat);
$result = mysql_query($query) or die (mysql_error());

I've successfully inserted a few rows via:

$query = sprintf("INSERT INTO lastcrawl (point) VALUES( GeomFromText( 'POINT(%F %F)' ))",$lat,$lng);

But, I'm just not able to query the nearest points to get a non-null result set. How do you get the query to select the points nearest to a given spatial point?


After inserting $lat=40, $lng=-100, trying to query this returns nothing as well (so, not even exact coordinates match):

SELECT * FROM lastcrawl WHERE MBRContains(LineFromText(CONCAT( '(' , -100.000000 + 0.0005 * ( 111.1 / cos(-100.000000)) , ' ' , 40.000000 + 0.0005 / 111.1 , ',' , -100.000000 - 0.0005 / ( 111.1 / cos(40.000000)) , ' ' , 40.000000 - 0.0005 / 111.1 , ')' )) ,point)

解决方案

I've corrected the formula a little.

This one works for me:

CREATE TABLE lastcrawl (id INT NOT NULL PRIMARY KEY, pnt POINT NOT NULL) ENGINE=MyISAM;

INSERT
INTO    lastcrawl
VALUES  (1, POINT(40, -100));

SET @lat = 40;
SET @lon = -100;

SELECT  *
FROM    lastcrawl
WHERE   MBRContains
                (
                LineString
                        (
                        Point
                                 (
                                 @lat + 10 / 111.1,
                                 @lon + 10 / ( 111.1 / COS(RADIANS(@lat)))
                                 ),
                        Point    (
                                 @lat - 10 / 111.1,
                                 @lon - 10 / ( 111.1 / COS(RADIANS(@lat)))
                                 )
                        ),
                pnt
                );

这篇关于MySQL - 在空间点附近选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 02:25