/*根据半径(1000米)选择记录 */
SELECT * FROM pointlocation where earth_box(ll_to_earth(40.057031,116.307852),4000) @> ll_to_earth(pointlocation.lat, pointlocation.lon);
/*判两点间的距离*/
SELECT earth_distance(ll_to_earth(40.057031,116.307852), ll_to_earth(pointlocation.lat, pointlocation.lon)) FROM pointlocation where id=1112
/*矩形范围查找*/
SELECT *
from pointlocation
where
ST_Contains(ST_MakePolygon(ST_GeomFromText('LINESTRING ( 116.845397 40.008134 , 116.221114 40.008134 ,116.245397 40.108134 , 116.245397 41.108134 , 116.845397 40.008134) ')) ,st_point(pointlocation.lon,pointlocation.lat))='t'
- /*
- * postgreSQL之earthdistance学习笔记
- * author: wusuopubupt
- * date: 2013-03-31
- */
- /*创建表*/
- CREATE TABLE picture (
- id serial PRIMARY KEY ,
- p_uid char(12) NOT NULL,
- p_key char(23) NOT NULL,
- lat real not null,
- lng real NOT NULL,
- up int NOT NULL,
- down int NOT NULL,
- ip varchar(15) DEFAULT NULL,
- address varchar(256) DEFAULT NULL
- );
- /*插入记录*/
- INSERT INTO picture(p_uid, p_key, lat, lng, up, down, ip, address)
- VALUES('aaaabbbbcccc', '2014032008164023279.png', 40.043945, 116.413668, 0, 0, '', '');
- /*插入记录*/
- INSERT INTO picture(p_uid, p_key, lat, lng, up, down, ip, address)
- VALUES('xxxxccccmmmm', '2014032008164023111.png', 40.067183, 116.415230, 0, 0, '', '');
- /*选择记录*/
- SELECT * FROM picture;
- /*更新记录*/
- UPDATE picture SET address='LiShuiqiao' WHERE id=1;
- UPDATE picture SET address='TianTongyuan' WHERE id=2;
- /*对经纬度列创建索引*/
- CREATE INDEX ll_idx on picture USING gist(ll_to_earth(lat, lng));
- /*根据半径(1000米)选择记录*/
- SELECT * FROM picture where earth_box(ll_to_earth(40.059286,116.418773),1000) @> ll_to_earth(picture.lat, picture.lng);
- /*选择距离当前用户的距离*/
- SELECT picture.id, earth_distance(ll_to_earth(picture.lat, picture.lng), ll_to_earth(40.059286,116.418773))
- AS dis FROM picture
- ORDER BY dis ASC;
- /*
- * 以下内容是网上的一篇教程
- * 地址:http://www.cse.iitb.ac.in/dbms/Data/Courses/CS631/PostgreSQL-Resources/postgresql-9.2.4/contrib/earthdistance/expected/earthdistance.out
- */