{"id":4238,"date":"2018-12-26T22:56:49","date_gmt":"2018-12-26T14:56:49","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4238"},"modified":"2018-12-26T22:56:49","modified_gmt":"2018-12-26T14:56:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%972%e4%b8%aa%e5%9d%90%e6%a0%87%e4%b9%8b%e9%97%b4%e7%9a%84%e8%b7%9d%e7%a6%bb","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4238.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u8ba1\u7b972\u4e2a\u5750\u6807\u4e4b\u95f4\u7684\u8ddd\u79bb"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u6700\u8fd1\u5728\u5206\u6790\u4e00\u4e9b\u65e5\u5fd7\uff0c\u91cc\u9762\u6709\u6d89\u53ca\u5230\u7ecf\u7eac\u5ea6\u4ee5\u53ca\u76f8\u5e94\u7684\u8ddd\u79bb\u8ba1\u7b97\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u5b66\u4e60\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6><strong>1\u3001\u7ecf\u5ea6\u3001\u7eac\u5ea6\u3001\u7ecf\u7eac\u5ea6\u90fd\u5206\u522b\u662f\u4ec0\u4e48\uff1f<\/strong><\/h6>\n<p><strong><span style=\"color: #ff0000;\">\u7ecf\u7eac\u5ea6<\/span>\u662f\u7ecf\u5ea6\u4e0e\u7eac\u5ea6\u7684\u5408\u79f0\u7ec4\u6210<span style=\"color: #ff0000;\">\u4e00\u4e2a\u5750\u6807\u7cfb\u7edf<\/span>\u3002<\/strong>\u53c8\u79f0\u4e3a\u5730\u7406\u5750\u6807\u7cfb\u7edf\uff0c\u5b83\u662f\u4e00\u79cd\u5229\u7528\u4e09\u5ea6\u7a7a\u95f4\u7684\u7403\u9762\u6765\u5b9a\u4e49\u5730\u7403\u4e0a\u7684\u7a7a\u95f4\u7684\u7403\u9762\u5750\u6807\u7cfb\u7edf\uff0c\u80fd\u591f\u6807\u793a\u5730\u7403\u4e0a\u7684\u4efb\u4f55\u4e00\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u7ecf\u7eac\u5ea6 coordinates<\/strong><br \/>\n<strong>\u7ecf\u5ea6 longitude<\/strong> <span style=\"color: #ff0000;\">#\u4e3a\u65b9\u4fbf\u8bb0\u5fc6\uff0c\u53ef\u4ee5\u8ba4\u4e3along\u7684\u662f\u7ecf\u5ea6<\/span><br \/>\n<strong>\u7eac\u5ea6 latitude<\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">\u5730\u7403\u4e0a\u6240\u6709\u7684\u7ecf\u7ebf\u90fd\u4e00\u6837\u957f<\/span><\/strong>\uff0c\u5e76\u6c47\u96c6\u5230\u4e24\u6781\u3002\u901a\u8fc7\u82f1\u56fd\u683c\u6797\u5a01\u6cbb\u5929\u6587\u53f0\u7684\u7ecf\u7ebf\u4e3a\u96f6\u5ea6\uff0c\u79f0\u4f5c<strong>\u672c\u521d\u5b50\u5348\u7ebf<\/strong>\u3002\u7531\u6b64\u5411\u4e1c\u3001\u5411\u897f\u5404\u5206\u6210180\u00b0\uff0c\u5206\u522b\u79f0\u4e1c\u7ecf\u548c\u897f\u7ecf\u3002\u4e1c\u3001\u897f\u7ecf180\u00b0\u7ebf\u662f\u91cd\u5408\u5728\u4e00\u8d77\u7684\u3002<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u5728\u5730\u7403\u8868\u9762\u4e0e\u7ecf\u7ebf\u76f4\u4ea4\u7684\u7ebf\u53eb\u7eac\u7ebf\uff0c\u628a\u5730\u7403\u5206\u6210\u5357\u5317\u4e24\u534a<\/strong><\/span>\uff0c\u5230\u5357\u6781\u548c\u5317\u6781\u8ddd\u79bb\u76f8\u7b49\u7684\u7eac\u7ebf\u5c31\u662f<strong>\u8d64\u9053<\/strong>\u3002\u6240\u6709\u7684\u7eac\u7ebf\u90fd\u5448\u4e1c\u897f\u65b9\u5411\u3001\u4e0e\u8d64\u9053\u5e73\u884c\uff0c\u4f46\u957f\u77ed\u4e0d\u7b49\uff0c\u6108\u5f80\u6781\u5730\u6108\u77ed\u3002<\/p>\n<p><span style=\"color: #ff0000;\">\u8d64\u9053\u7684\u7eac\u5ea6\u662f0\u00b0\uff0c\u5357\u5317\u6781\u662f90\u00b0\uff0c\u8d64\u9053\u4ee5\u5317\u53eb\u5317\u7eac\uff0c\u8d64\u9053\u4ee5\u5357\u53eb\u5357\u7eac\u3002<\/span>\u4e60\u60ef\u4e0a\uff0c\u4eba\u4eec\u8fd8\u628a\u7eac\u5ea60\uff5e30\u00b0\u53eb\u4f4e\u7eac\uff0c30\uff5e60\u00b0\u53eb\u4e2d\u7eac\uff0c60\uff5e90\u00b0\u53eb\u9ad8\u7eac\u3002\u5357\u3001\u5317\u7eac23.5\u00b0\uff0c\u5206\u522b\u88ab\u79f0\u4e3a\u5357\u3001\u5317\u56de\u5f52\u7ebf\u3002<\/p>\n<p>\u7ecf\u5ea6\uff1a\u4e1c\u7ecf\u4e3a\u6b63\u6570\uff0c\u897f\u7ecf\u4e3a\u8d1f\u6570\u3002<br \/>\n\u7eac\u5ea6\uff1a\u5317\u7eac\u4e3a\u6b63\u6570\uff0c\u5357\u7eac\u4e3a\u8d1f\u6570\u3002<\/p>\n<h6><strong>2\u3001\u5982\u4f55\u53d6\u5730\u56fe\u4e0a\u67d0\u4e2a\u70b9\u7684\u5750\u6807\uff1f<\/strong><\/h6>\n<p><span style=\"color: #ff0000;\"><strong>\u7ecf\u7eac\u5ea6 -&gt; \u5730\u5740<\/strong><\/span><br \/>\n<a href=\"http:\/\/www.gpsspg.com\/maps.htm\">http:\/\/www.gpsspg.com\/maps.htm<\/a><\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u5730\u5740 -&gt; \u7ecf\u7eac\u5ea6<\/strong><\/span><br \/>\n#<a href=\"http:\/\/api.map.baidu.com\/lbsapi\/getpoint\/index.html\">\u767e\u5ea6\u62fe\u53d6\u5750\u6807\u7cfb\u7edf<\/a><br \/>\n#<a href=\"https:\/\/lbs.amap.com\/api\/javascript-api\/example\/map\/click-to-get-lnglat\/\">\u9ad8\u5fb7\u5730\u56fe\u83b7\u53d6\u9f20\u6807\u70b9\u51fb\u7ecf\u7eac\u5ea6<\/a><br \/>\n#<a href=\"https:\/\/lbs.qq.com\/tool\/getpoint\/\" target=\"_blank\" rel=\"noopener\">\u817e\u8baf\u5730\u56fe\u5750\u6807\u62fe\u53d6\u5668<\/a><\/p>\n<h6><strong>3\u3001\u5750\u6807\u4e4b\u95f4\u7684\u8ddd\u79bb\u8be5\u5982\u4f55\u8ba1\u7b97\uff1f<\/strong><\/h6>\n<p>\u5730\u7403\u4e0a\u7684\u4f4d\u7f6e\u73b0\u5728\u90fd\u7528\u7ecf\u7eac\u5ea6\u6765\u8fdb\u884c\u7cbe\u786e\u6807\u6ce8\uff0c\u53c8\u56e0\u4e3a\u5730\u7403\u662f\u4e00\u4e2a\u7403\u4f53\uff0c\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u4e0d\u80fd\u7b80\u5355\u7684\u7528\u5e73\u9762\u4e0a\u7684\u65b9\u6cd5\u6765\u7b97\uff0c\u8981\u8003\u8651\u7403\u9762\u7684\u56e0\u7d20\uff0c\u800c\u5e38\u7528\u7684\u662fHaversine\u516c\u5f0f\uff0c\u8fd9\u91cc\u516c\u5f0f\u7ec6\u8282\u4e0d\u7ec6\u8bf4\uff0c\u76f4\u63a5\u653e\u51fa\u7528Python\u8ba1\u7b97\u7684\u65b9\u6cd5\uff0c\u65b9\u4fbf\u53c2\u8003\u548c\u4f7f\u7528\u3002<\/p>\n<blockquote><p>\u6ce8\uff1a<strong><span style=\"color: #ff0000;\">\u867d\u7136\u53eb\u300c\u7ecf\u7eac\u5ea6\u300d\u4f46\u4e00\u822c\u5750\u6807\u91cc\u9762\u662f\u628a\u300c\u7eac\u5ea6\u653e\u5728\u524d\u9762\u300d\uff0c\u5373\uff1a\u300c\u5750\u6807 = \uff08\u7eac\u5ea6\uff0c\u7ecf\u5ea6\uff09\u300d\u3002<\/span><\/strong>\u4e14\u5bf9\u4e8e\u56fd\u5185\u7684\u5750\u6807\u60c5\u51b5\uff0c\u4e00\u822c\u662f\u7eac\u5ea6\u6570\u503c\u8981\u5c0f\u4e8e\u7ecf\u5ea6\u7684\u6570\u503c\u3002<\/p><\/blockquote>\n<pre class=\"lang:default decode:true \">import math\r\n\r\ndef distance(origin, destination):\r\n    lat1, lon1 = origin\r\n    lat2, lon2 = destination\r\n    radius = 6371 # km\r\n\r\n    dlat = math.radians(lat2-lat1)\r\n    dlon = math.radians(lon2-lon1)\r\n    a = math.sin(dlat\/2) * math.sin(dlat\/2) + math.cos(math.radians(lat1)) \\\r\n        * math.cos(math.radians(lat2)) * math.sin(dlon\/2) * math.sin(dlon\/2)\r\n    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))\r\n    d = radius * c\r\n\r\n    return d\r\n\r\n# 38.35252313201552 (km\u4e3a\u5355\u4f4d)\r\nprint distance((22.599578, 113.973129), (22.6986848, 114.3311032))<\/pre>\n<p>\u6216<\/p>\n<pre class=\"lang:default decode:true \">from math import sin, asin, cos, radians, fabs, sqrt\r\n\r\ndef haversine(lon1, lat1, lon2, lat2): # \u7ecf\u5ea61\uff0c\u7eac\u5ea61\uff0c\u7ecf\u5ea62\uff0c\u7eac\u5ea62 \uff08\u5341\u8fdb\u5236\u5ea6\u6570\uff09\r\n    \"\"\"\r\n    Calculate the great circle distance between two points \r\n    on the earth (specified in decimal degrees)\r\n    \"\"\"\r\n    # \u5c06\u5341\u8fdb\u5236\u5ea6\u6570\u8f6c\u5316\u4e3a\u5f27\u5ea6\r\n    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])\r\n \r\n    # haversine\u516c\u5f0f\r\n    dlon = lon2 - lon1 \r\n    dlat = lat2 - lat1 \r\n    a = sin(dlat\/2)**2 + cos(lat1) * cos(lat2) * sin(dlon\/2)**2\r\n    c = 2 * asin(sqrt(a)) \r\n    r = 6371 # \u5730\u7403\u5e73\u5747\u534a\u5f84\uff0c\u5355\u4f4d\u4e3a\u516c\u91cc\r\n    return c * r * 1000\r\n\r\n# 38352.523132 (m\u4e3a\u5355\u4f4d)\r\nprint haversine(113.973129, 22.599578, 114.3311032, 22.6986848)<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.dili520.com\/zhishizongjie\/zhongkaodili\/228.html\">\u4ec0\u4e48\u53eb\u5730\u7406\u7ecf\u5ea6\u7eac\u5ea6\uff1f\u600e\u4e48\u770b?\u5982\u4f55\u5212\u5206\u548c\u5b9a\u4f4d<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/u013401853\/article\/details\/73368850\">python\u5229\u7528\u5730\u56fe\u4e24\u4e2a\u70b9\u7684\u7ecf\u7eac\u5ea6\u8ba1\u7b97\u4e24\u70b9\u95f4\u8ddd\u79bb<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/vernice\/article\/details\/46581361\">Python\u8ba1\u7b97\u5730\u56fe\u4e0a\u4e24\u70b9\u7ecf\u7eac\u5ea6\u95f4\u7684\u8ddd\u79bb<\/a><\/li>\n<li><a href=\"https:\/\/blog.lovian.org\/python\/2018\/04\/16\/python-calculate-distance-in-lat-lon.html\">I. \u6839\u636e\u7ecf\u7eac\u5ea6\u5750\u6807\u8ba1\u7b97\u4e24\u70b9\u95f4\u8ddd\u79bb<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/19412462\/getting-distance-between-two-points-based-on-latitude-longitude\">Getting distance between two points based on latitude\/longitude<\/a><\/li>\n<li><a href=\"https:\/\/nathanrooy.github.io\/posts\/2016-09-07\/haversine-with-python\/\">Calculating the Distance Between Two GPS Coordinates with Python (Haversine Formula)<\/a><\/li>\n<li><a href=\"https:\/\/gist.github.com\/rochacbruno\/2883505\">Calculate distance between latitude longitude pairs with Python<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6700\u8fd1\u5728\u5206\u6790\u4e00\u4e9b\u65e5\u5fd7\uff0c\u91cc\u9762\u6709\u6d89\u53ca\u5230\u7ecf\u7eac\u5ea6\u4ee5\u53ca\u76f8\u5e94\u7684\u8ddd\u79bb\u8ba1\u7b97\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u5b66\u4e60\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[1274,8,1272,1271,1273],"class_list":["post-4238","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-coordinates","tag-python","tag-1272","tag-1271","tag-1273"],"views":18921,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4238"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4238\/revisions"}],"predecessor-version":[{"id":4239,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4238\/revisions\/4239"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}