Geo-coordinate calculation

Source: Internet
Author: User

/* Select records by radius (1000 meters) */
SELECT * from Pointlocation where Earth_box (Ll_to_earth (40.057031,116.307852), 4000) @> Ll_to_earth ( Pointlocation.lat, Pointlocation.lon);

/* The distance between two points is awarded */
SELECT earth_distance (Ll_to_earth (40.057031,116.307852), Ll_to_earth (Pointlocation.lat, Pointlocation.lon)) from Pointlocation where id=1112

  1. /*
  2. * PostgreSQL's earthdistance study notes
  3. * Author:wusuopubupt
  4. * date:2013-03-31
  5. */
  6. /* CREATE TABLE */
  7. CREATE TABLE Picture (
  8. ID serial PRIMARY KEY,
  9. P_uid char () not NULL,
  10. P_key char (at) not NULL,
  11. Lat real not null,
  12. LNG real not NULL,
  13. the up int is not NULL,
  14. the down int is not NULL,
  15. IP varchar (+) DEFAULT NULL,
  16. Address varchar (DEFAULT) NULL
  17. );
  18. /* Insert Record */
  19. INSERT into Picture (P_uid, P_key, lat, LNG, up, down, IP, address)
  20. VALUES (' AAAABBBBCCCC ', ' 2014032008164023279.png ', 40.043945, 116.413668, 0, 0, ' , ');
  21. /* Insert Record */
  22. INSERT into Picture (P_uid, P_key, lat, LNG, up, down, IP, address)
  23. VALUES (' xxxxccccmmmm ', ' 2014032008164023111.png ', 40.067183, 116.415230, 0, 0, ' , ');
  24. /* Select Record */
  25. SELECT * from Picture ;
  26. /* Update record */
  27. UPDATE picture SET address=' Lishuiqiao ' WHERE id=1;
  28. UPDATE picture SET address=' Tiantongyuan ' WHERE id=2;
  29. /* Create an index on the latitude column */
  30. CREATE INDEX ll_idx on the picture USING gist (Ll_to_earth (lat, LNG));
  31. /* Select records by radius (1000 meters) */
  32. SELECT * from the picture where Earth_box (Ll_to_earth (40.059286,116.418773),,) @> Ll_to_earth (Picture.lat,   PICTURE.LNG);
  33. /* Select the distance from the current user */
  34. SELECT picture.id, Earth_distance (Ll_to_earth (Picture.lat, PICTURE.LNG), Ll_to_earth (40.059286,116.418773))
  35. As Dis from picture
  36. ORDER by dis ASC;
  37. /*
  38. * The following is a tutorial on the Web
  39. * Address: Http://www.cse.iitb.ac. in/dbms/data/courses/cs631/postgresql-resources/postgresql-9.2.4/contrib/earthdistance/expected/ Earthdistance. out
  40. */

Geo-coordinate calculation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.