码迷,mamicode.com
首页 > 数据库 > 详细

mysql空间数据相关操作

时间:2016-02-24 14:04:39      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:

建表语句:

CREATE TABLE ts.points
    (
    name        VARCHAR (20) NOT NULL,
    location    POINT NOT NULL,
    description VARCHAR (200)    
    );

添加记录如下:

INSERT INTO Points (name, location) VALUES ( p1 , GeomFromText(  POINT(10 20)  ) )
INSERT INTO Points (name, location) VALUES ( p2 , GeomFromText(  POINT(11 30)  ) )
INSERT INTO Points (name, location) VALUES ( p3 , GeomFromText(  POINT(10 21)  ) )

查询操作如下:

SELECT NAME, ASTEXT(LOCATION),X(LOCATION),Y(LOCATION) FROM POINTS

查询所有记录离指定点的距离

SELECT *,ROUND(6378.138*2*ASIN(SQRT(POW(SIN( (X(LOCATION)*PI()/180-10*PI()/180)/2),2)+COS(X(LOCATION)*PI()/180)*COS(10*PI()/180)* POW(SIN( (Y(LOCATION)*PI()/180-20*PI()/180)/2),2)))*1000) AS DISTANCE FROM POINTS ORDER BY DISTANCE ASC

注:指定点信息(10,20),ps:10为X(经度),20为Y(纬度)

查询附近100公里数据:

SELECT  name ,X(LOCATION),Y(LOCATION)
    FROM    points
    WHERE   MBRContains
                    (
                    LineString
                            (
                            Point
                                    (
                                    10 + 100 / ( 111.1 / COS(RADIANS(104.067221))),
                                    20 + 100 / 111.1
                                    ),
                            Point
                                    (
                                    10 - 100 / ( 111.1 / COS(RADIANS(104.067221))),
                                       20 - 100 / 111.1
                                    ) 
                            ),
                    location
                    )

目标点坐标为(10,20)

 

mysql空间数据相关操作

标签:

原文地址:http://www.cnblogs.com/wujf/p/5212521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!