﻿{"id":731,"date":"2012-02-07T12:16:24","date_gmt":"2012-02-07T04:16:24","guid":{"rendered":"http:\/\/nick.workao.org\/?p=731"},"modified":"2012-02-07T12:16:26","modified_gmt":"2012-02-07T04:16:26","slug":"%e8%bd%ac%e9%99%84%e8%bf%91%e5%9c%b0%e7%82%b9%e6%90%9c%e7%b4%a2%e5%88%9d%e6%8e%a2%ef%bc%8c%e9%9d%9egeohash","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/python\/731","title":{"rendered":"[\u8f6c]\u9644\u8fd1\u5730\u70b9\u641c\u7d22\u521d\u63a2\uff0c\u975egeohash"},"content":{"rendered":"<p>\u9644\u8fd1\u5730\u70b9\u641c\u7d22\uff0c\u987e\u540d\u601d\u4e49\uff0c\u5c31\u662f\u641c\u7d22\u7528\u6237\u9644\u8fd1\u6709\u54ea\u4e9b\u5730\u70b9\u3002\u968f\u7740GPS\u548c\u5e26\u6709GPS\u529f\u80fd\u7684\u79fb\u52a8\u8bbe\u5907\u7684\u666e\u53ca\uff0c \u9644\u8fd1\u5730\u70b9\u641c\u7d22\u4e5f\u53d8\u5f97\u7099\u624b\u53ef\u70ed\u3002\u4e0d\u8fc7\u5728\u7f51\u4e0a\u5374\u5f88\u5c11\u6709\u8fd9\u65b9\u9762\u7684\u8ba8\u8bba\u3002\u672c\u6587\u7684\u65b9\u6cd5\u5e76\u4e0d\u7b97\u6700\u597d\uff0c \u4f46\u8db3\u4ee5\u5e94\u4ed8\u4e00\u822c\u7684\u5e94\u7528\u4e86\u3002<\/p>\n<p>\u672c\u6587\u4e2d\uff0c\u6570\u636e\u5e93\u91c7\u7528MySQL\uff0c\u8bed\u8a00\u91c7\u7528python\u3002\u7406\u8bba\u4e0a\u522b\u7684\u6570\u636e\u5e93\u548c\u8bed\u8a00\u4e5f\u6ca1\u95ee\u9898\uff0c \u4f46\u6211\u4eec\u8981\u5728\u7ecf\u7eac\u5ea6\u4e0a\u8bbe\u7f6e\u4e24\u4e2a\u7d22\u5f15\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u7684\u6570\u636e\u5e93\u4e0d\u652f\u6301\u7d22\u5f15\uff0c\u6216\u8005\u4e0d\u652f\u6301\u5728\u4e00\u4e2a\u67e5\u8be2\u4e2d\u4f7f\u7528\u4e24\u4e2a\u7d22\u5f15\uff0c \u90a3\u5c31\u53ea\u80fd\u60f3\u522b\u7684\u529e\u6cd5\u4e86\u3002<\/p>\n<h2 id=\"content_2_0\">\u7403\u9762\u6700\u77ed\u8ddd\u79bb\u516c\u5f0f<\/h2>\n<p>\u7403\u9762\u4e0a\u4efb\u610f\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u8ba1\u7b97\u516c\u5f0f\u53ef\u4ee5\u53c2\u8003\u7ef4\u57fa\u767e\u79d1\u4e0a\u7684\u4e0b\u8ff0\u6587\u7ae0\uff0c\u8fd9\u91cc\u5c31\u4e0d\u518d\u8d58\u8ff0\u4e86\u3002<\/p>\n<ul>\n    <li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Great-circle_distance\">Great-circle distance<\/a><\/li>\n    <li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Haversine_formula\">Haversine formula<\/a><\/li>\n<\/ul>\n<p>\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u7ef4\u57fa\u767e\u79d1\u63a8\u8350\u4f7f\u7528Haversine\u516c\u5f0f\uff0c\u7406\u7531\u662fGreat-circle distance\u516c\u5f0f\u7528\u5230\u4e86\u5927\u91cf\u4f59\u5f26\u51fd\u6570\uff0c \u800c\u4e24\u70b9\u95f4\u8ddd\u79bb\u5f88\u77ed\u65f6\uff08\u6bd4\u5982\u5730\u7403\u8868\u9762\u4e0a\u76f8\u8ddd\u51e0\u767e\u7c73\u7684\u4e24\u70b9\uff09\uff0c\u4f59\u5f26\u51fd\u6570\u4f1a\u5f97\u51fa0.999&#8230;\u7684\u7ed3\u679c\uff0c \u4f1a\u5bfc\u81f4\u8f83\u5927\u7684\u820d\u5165\u8bef\u5dee\u3002\u800cHaversine\u516c\u5f0f\u91c7\u7528\u4e86\u6b63\u5f26\u51fd\u6570\uff0c\u5373\u4f7f\u8ddd\u79bb\u5f88\u5c0f\uff0c\u4e5f\u80fd\u4fdd\u6301\u8db3\u591f\u7684\u6709\u6548\u6570\u5b57\u3002 \u4ee5\u524d\u91c7\u7528\u4e09\u89d2\u51fd\u6570\u8868\u8ba1\u7b97\u65f6\u7684\u786e\u4f1a\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f46\u7ecf\u8fc7\u5b9e\u9645\u9a8c\u8bc1\uff0c\u91c7\u7528\u8ba1\u7b97\u673a\u6765\u8ba1\u7b97\u65f6\uff0c\u4e24\u4e2a\u516c\u5f0f\u7684\u533a\u522b\u4e0d\u5927\u3002 \u7a33\u59a5\u8d77\u89c1\uff0c\u8fd9\u91cc\u8fd8\u662f\u91c7\u7528Haversine\u516c\u5f0f\u3002<\/p>\n<div><img loading=\"lazy\" decoding=\"async\" title=\"distance-haversin-distance.png\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/02\/041624QrL.png\" alt=\"distance-haversin-distance.png\" width=\"441\" height=\"49\"><\/div>\n<p>\u5176\u4e2d<\/p>\n<div><img loading=\"lazy\" decoding=\"async\" title=\"distance-haversin.png\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/02\/041625ng2.png\" alt=\"distance-haversin.png\" width=\"279\" height=\"39\"><\/div>\n<ul>\n    <li>R\u4e3a\u5730\u7403\u534a\u5f84\uff0c\u53ef\u53d6\u5e73\u5747\u503c 6371km\uff1b<\/li>\n    <li>\u03c6<sub>1<\/sub>, \u03c6<sub>2<\/sub> \u8868\u793a\u4e24\u70b9\u7684\u7eac\u5ea6\uff1b<\/li>\n    <li>\u0394\u03bb \u8868\u793a\u4e24\u70b9\u7ecf\u5ea6\u7684\u5dee\u503c\u3002<\/li>\n<\/ul>\n<h2 id=\"content_2_1\">\u8ddd\u79bb\u8ba1\u7b97\u51fd\u6570<\/h2>\n<p>\u4e0b\u9762\u5c31\u662f\u8ba1\u7b97\u7403\u9762\u95f4\u4e24\u70b9(lat0, lng)-(lat1, lng1)\u4e4b\u95f4\u8ddd\u79bb\u7684\u51fd\u6570\u3002<\/p>\n<pre>from math import sin, asin, cos, radians, fabs, sqrt\n\nEARTH_RADIUS=6371           # \u5730\u7403\u5e73\u5747\u534a\u5f84\uff0c6371km\n\ndef hav(theta):\n    s = sin(theta \/ 2)\n    return s * s\n\ndef get_distance_hav(lat0, lng0, lat1, lng1):\n    \"\u7528haversine\u516c\u5f0f\u8ba1\u7b97\u7403\u9762\u4e24\u70b9\u95f4\u7684\u8ddd\u79bb\u3002\"\n    # \u7ecf\u7eac\u5ea6\u8f6c\u6362\u6210\u5f27\u5ea6\n    lat0 = radians(lat0)\n    lat1 = radians(lat1)\n    lng0 = radians(lng0)\n    lng1 = radians(lng1)\n\n    dlng = fabs(lng0 - lng1)\n    dlat = fabs(lat0 - lat1)\n    h = hav(dlat) + cos(lat0) * cos(lat1) * hav(dlng)\n    distance = 2 * EARTH_RADIUS * asin(sqrt(h))\n\n    return distance<\/pre>\n<h2 id=\"content_2_2\">\u8303\u56f4\u641c\u7d22\u7b97\u6cd5<\/h2>\n<p>\u5728\u5e9e\u5927\u7684\u5730\u7406\u6570\u636e\u5e93\u4e2d\u641c\u7d22\u5730\u70b9\uff0c\u7d22\u5f15\u662f\u5f88\u91cd\u8981\u7684\u3002\u4f46\u662f\uff0c\u6211\u4eec\u7684\u9700\u6c42\u662f\u641c\u7d22\u9644\u8fd1\u5730\u70b9\uff0c \u4f8b\u5982\uff0c\u5750\u6807(39.91, 116.37)\u9644\u8fd1500\u7c73\u5185\u6709\u4ec0\u4e48\u5730\u70b9\uff1f\u641c\u7d22\u6761\u4ef6\u662f\u5730\u70b9\u5750\u6807\u4e0e\u5f53\u524d\u5750\u6807\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c \u663e\u7136\u662f\u65e0\u6cd5\u5e94\u7528\u7d22\u5f15\u7684\u3002<\/p>\n<p>\u90a3\u4e48\u6362\u4e2a\u601d\u8def\uff1a\u9996\u5148\u7b97\u51fa\u201c\u7ed9\u5b9a\u5750\u6807\u9644\u8fd1500\u7c73\u201d\u8fd9\u4e2a\u8303\u56f4\u7684\u5750\u6807\u8303\u56f4\u3002 \u867d\u7136\u5b83\u662f\u4e2a\u5706\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5148\u6c42\u51fa\u8be5\u5706\u7684\u5916\u63a5\u6b63\u65b9\u5f62\uff0c\u7136\u540e\u62ff\u6b63\u65b9\u5f62\u7684\u7ecf\u7eac\u5ea6\u8303\u56f4\u53bb\u641c\u7d22\u6570\u636e\u5e93\u3002<\/p>\n<div><img loading=\"lazy\" decoding=\"async\" title=\"distance-map.png\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/02\/041625GOB.png\" alt=\"distance-map.png\" width=\"416\" height=\"330\"><\/div>\n<p>\u5982\u56fe\uff0c\u7ea2\u8272\u90e8\u5206\u4e3a\u8981\u6c42\u7684\u641c\u7d22\u8303\u56f4\uff0c\u7eff\u8272\u90e8\u5206\u4e3a\u5b9e\u9645\u641c\u7d22\u8303\u56f4\u3002<\/p>\n<p>\u5148\u6765\u6c42\u4e1c\u897f\u4e24\u4fa7\u7684\u7684\u8303\u56f4\u8fb9\u754c\u3002\u5728haversin\u516c\u5f0f\u4e2d\u4ee4\u03c6<sub>1<\/sub> = \u03c6<sub>2<\/sub>\uff0c\u53ef\u5f97<\/p>\n<div><img loading=\"lazy\" decoding=\"async\" title=\"distance-lng.png\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/02\/041626CxR.png\" alt=\"distance-lng.png\" width=\"194\" height=\"50\"><\/div>\n<p>\u5199\u6210python\u4ee3\u7801\u5c31\u662f<\/p>\n<pre>dlng = 2 * asin(sin(distance \/ (2 * EARTH_RADIUS)) \/ cos(lat))\ndlng = degrees(dlng)        # \u5f27\u5ea6\u8f6c\u6362\u6210\u89d2\u5ea6<\/pre>\n<p>\u7136\u540e\u6c42\u5357\u5317\u4e24\u4fa7\u7684\u8303\u56f4\u8fb9\u754c\uff0c\u5728haversin\u516c\u5f0f\u4e2d\u4ee4 \u0394\u03bb = 0\uff0c\u53ef\u5f97<\/p>\n<div><img loading=\"lazy\" decoding=\"async\" title=\"distance-lat.png\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2012\/02\/041626YhP.png\" alt=\"distance-lat.png\" width=\"78\" height=\"51\"><\/div>\n<p>\u5199\u6210python\u4ee3\u7801\u5c31\u662f<\/p>\n<pre>dlat = distance \/ EARTH_RADIUS\ndlng = degrees(dlat)     # \u5f27\u5ea6\u8f6c\u6362\u6210\u89d2\u5ea6<\/pre>\n<p>\u8fd9\u6837\uff0c\u6839\u636e\u5f53\u524d\u70b9\u5750\u6807\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u641c\u7d22\u8303\u56f4\u4e3a<\/p>\n<pre>left-top    : (lat + dlat, lng - dlng)\nright-top   : (lat + dlat, lng + dlng)\nleft-bottom : (lat - dlat, lng - dlng)\nright-bottom: (lat - dlat, lng + dlng)<\/pre>\n<p>\u7136\u540e\u5229\u7528\u8fd9\u4e2a\u8303\u56f4\u6784\u9020SQL\u8bed\u53e5\uff0c\u5373\u53ef\u5b9e\u73b0\u8303\u56f4\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT * FROM place WHERE lat > lat1 AND lat < lat2 AND lng > lng1 AND lng < lng2;<\/pre>\n<p>\u5728lat\u548clng\u5217\u4e0a\u5efa\u7acb\u7d22\u5f15\uff0c\u80fd\u4ece\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u9ad8\u8303\u56f4\u67e5\u8be2\u7684\u6548\u7387\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u8fd9\u6837\u67e5\u8be2\u5230\u7684\u5730\u70b9\u662f\u6b63\u65b9\u5f62\u8303\u56f4\u5185\u7684\u5730\u70b9\uff0c\u4e00\u4e9b\u7ed3\u679c\u4e0e\u5f53\u524d\u70b9\u7684\u8ddd\u79bb\u53ef\u80fd\u4f1a\u8d85\u51fa\u7ed9\u5b9a\u7684\u8ddd\u79bb\u3002 \u5982\u679c\u8981\u6c42\u4e25\u683c\uff0c\u53ef\u4ee5\u904d\u5386\u7ed3\u679c\u5e76\u8ba1\u7b97\u4e0e\u5f53\u524d\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u5e76\u8fc7\u6ee4\u6389\u4e0d\u7b26\u5408\u8981\u6c42\u7684\u7ed3\u679c\u3002<\/p>\n<h2 id=\"content_2_3\">\u603b\u7ed3<\/h2>\n<p>\u9644\u8fd1\u5730\u70b9\u641c\u7d22\u6761\u4ef6\u662f\u8ddd\u79bb\uff0c\u800c\u6570\u636e\u5e93\u4e2d\u4e00\u822c\u53ea\u4fdd\u5b58\u5730\u70b9\u7684\u7ecf\u7eac\u5ea6\uff0c\u56e0\u6b64\u65e0\u6cd5\u76f4\u63a5\u67e5\u8be2\u3002 \u672c\u6587\u5c06\u8ddd\u79bb\u8f6c\u5316\u6210\u7ecf\u7eac\u5ea6\u8303\u56f4\uff0c\u5229\u7528\u7ecf\u7eac\u5ea6\u4e0a\u7684\u7d22\u5f15\uff0c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u9644\u8fd1\u5730\u70b9\u641c\u7d22\uff0c\u987e\u540d\u601d\u4e49\uff0c\u5c31\u662f\u641c\u7d22\u7528\u6237\u9644\u8fd1\u6709\u54ea\u4e9b\u5730\u70b9\u3002\u968f\u7740GPS\u548c\u5e26\u6709GPS\u529f\u80fd\u7684\u79fb\u52a8\u8bbe\u5907\u7684\u666e\u53ca\uff0c \u9644\u8fd1\u5730\u70b9\u641c\u7d22\u4e5f\u53d8\u5f97\u7099\u624b\u53ef\u70ed\u3002\u4e0d\u8fc7\u5728\u7f51\u4e0a\u5374\u5f88\u5c11\u6709\u8fd9\u65b9\u9762\u7684\u8ba8\u8bba\u3002\u672c\u6587\u7684\u65b9\u6cd5\u5e76\u4e0d\u7b97\u6700\u597d\uff0c \u4f46\u8db3\u4ee5\u5e94\u4ed8\u4e00\u822c\u7684\u5e94&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[274],"tags":[],"class_list":["post-731","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=731"}],"version-history":[{"count":3,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/731\/revisions"}],"predecessor-version":[{"id":739,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/731\/revisions\/739"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=731"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}