{"id":6460,"date":"2025-04-03T18:53:44","date_gmt":"2025-04-03T09:53:44","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=6460"},"modified":"2025-04-03T18:54:44","modified_gmt":"2025-04-03T09:54:44","slug":"address_to_distance","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/address_to_distance\/","title":{"rendered":"\u4f4f\u6240\u304b\u3089\u8ddd\u96e2\u3092\u7c21\u5358\u306b\u8a08\u7b97\uff01sklearn\u3068\u56fd\u571f\u5730\u7406\u9662API\u3092\u6d3b\u7528\u3057\u305f\u8ddd\u96e2\u7b97\u51fa\u6cd5"},"content":{"rendered":"\n<p>\u5730\u7406\u60c5\u5831\u3092\u6d3b\u7528\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3084\u30c7\u30fc\u30bf\u5206\u6790\u304c\u9032\u3080\u4e2d\u3001\u4f4f\u6240\u304b\u3089\u8ddd\u96e2\u3092\u8a08\u7b97\u3059\u308b\u9700\u8981\u306f\u5897\u52a0\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>\u5730\u7406\u60c5\u5831\u3092\u6d3b\u7528\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3084\u30c7\u30fc\u30bf\u5206\u6790\u304c\u9032\u3080\u4e2d\u3001\u4f4f\u6240\u304b\u3089\u8ddd\u96e2\u3092\u8a08\u7b97\u3059\u308b\u9700\u8981\u306f\u5897\u52a0\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u914d\u9001\u30eb\u30fc\u30c8\u306e\u6700\u9069\u5316\u3084\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u4f4d\u7f6e\u60c5\u5831\u3092\u57fa\u306b\u3057\u305f\u30b5\u30fc\u30d3\u30b9\u63d0\u4f9b\u306a\u3069\u3001\u69d8\u3005\u306a\u30b7\u30fc\u30f3\u3067\u6d3b\u7528\u53ef\u80fd\u3067\u3059\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001Python\u3092\u4f7f\u3063\u3066\u56fd\u571f\u5730\u7406\u9662API\u304b\u3089\u4f4f\u6240\u306b\u5bfe\u5fdc\u3059\u308b\u7def\u5ea6\u7d4c\u5ea6\u3092\u53d6\u5f97\u3057\u30012\u70b9\u9593\u306e\u8ddd\u96e2\u3092\u8a08\u7b97\u3059\u308b\u65b9\u6cd5\u3092\u30b9\u30c6\u30c3\u30d7\u30d0\u30a4\u30b9\u30c6\u30c3\u30d7\u3067\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n<h2>\u56fd\u571f\u5730\u7406\u9662API\u3068\u306f<\/h2>\n<p><strong>\u56fd\u571f\u5730\u7406\u9662API<\/strong>\u306f\u3001\u4f4f\u6240\u304b\u3089\u7def\u5ea6\u30fb\u7d4c\u5ea6\u3092\u53d6\u5f97\u3067\u304d\u308bAPI\u3067\u3059\u3002\u3053\u306eAPI\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u5730\u56f3\u4e0a\u306e\u7279\u5b9a\u306e\u4f4d\u7f6e\u3092\u52b9\u7387\u7684\u306b\u53d6\u5f97\u3067\u304d\u3001\u4f4d\u7f6e\u60c5\u5831\u3092\u57fa\u306b\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3084\u30b5\u30fc\u30d3\u30b9\u958b\u767a\u306b\u5927\u304d\u306a\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\r\n<p>\u5b9f\u969b\u306b\u3001\u5730\u7406\u7684\u306a\u60c5\u5831\u3092\u6271\u3046\u969b\u306b\u3001<strong>geopy<\/strong>\u30e9\u30a4\u30d6\u30e9\u30ea\u306a\u3069\u3082\u5e83\u304f\u4f7f\u308f\u308c\u3066\u3044\u307e\u3059\u304c\u3001geopy\u306f\u8a73\u7d30\u306a\u4f4f\u6240\u3092\u7def\u5ea6\u30fb\u7d4c\u5ea6\u306b\u5909\u63db\u3059\u308b\u7cbe\u5ea6\u304c\u9650\u5b9a\u7684\u3067\u3059\u3002\u7279\u306b\u3001\u65e5\u672c\u56fd\u5185\u306b\u304a\u3044\u3066\u306f\u3001\u7d30\u304b\u3044\u4f4f\u6240\u306e\u6271\u3044\u304c\u96e3\u3057\u304f\u3001\u610f\u56f3\u3057\u305f\u901a\u308a\u306e\u7cbe\u5ea6\u3067\u7def\u5ea6\u30fb\u7d4c\u5ea6\u3092\u53d6\u5f97\u3067\u304d\u306a\u3044\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002\u305d\u3053\u3067\u3001\u7cbe\u5ea6\u9ad8\u304f\u3001\u6b63\u78ba\u306a\u65e5\u672c\u56fd\u5185\u306e\u4f4f\u6240\u5909\u63db\u304c\u6c42\u3081\u3089\u308c\u308b\u5834\u5408\u3001<strong>\u56fd\u571f\u5730\u7406\u9662API<\/strong>\u304c\u975e\u5e38\u306b\u6709\u52b9\u3067\u3059\u3002<\/p>\r\n<p><!-- notionvc: d6a7e2a0-210c-4d7d-9cf0-421e2926c1e9 --><\/p>\n\n<h2>\u5b9f\u88c5\u624b\u9806<\/h2>\n<p>\u5b9f\u88c5\u306f\u4ee5\u4e0b\u306e\u624b\u9806\u306b\u884c\u3044\u307e\u3059\u3002<\/p>\r\n<p><strong>\u2460\u56fd\u571f\u5730\u7406\u9662API\u3092\u4f7f\u3063\u3066\u4f4f\u6240\u304b\u3089\u7def\u5ea6\u30fb\u7d4c\u5ea6\u3092\u53d6\u5f97<\/strong><\/p>\r\n<p>\u56fd\u571f\u5730\u7406\u9662API\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u7c21\u5358\u306b\u4f4f\u6240\u304b\u3089\u4f4d\u7f6e\u60c5\u5831\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><strong>\u2461sklearn\u306eBallTree\u3092\u4f7f\u3063\u3066\u3001\u6700\u3082\u8fd1\u3044\uff12\u70b9\u3092\u6c42\u3081\u308b\u3002<\/strong><\/p>\r\n<p>BallTree\u306f\u3001\u52b9\u7387\u7684\u306b\u7a7a\u9593\u691c\u7d22\u3092\u884c\u3046\u305f\u3081\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u3067\u3001\u8ddd\u96e2\u304c\u8fd1\u3044\u30c7\u30fc\u30bf\u70b9\u3092\u7d20\u65e9\u304f\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u8907\u6570\u306e\u5730\u70b9\uff08\u7def\u5ea6\u30fb\u7d4c\u5ea6\u306e\u30da\u30a2\uff09\u304b\u3089\u3001\u6700\u3082\u8fd1\u3044\u5730\u70b9\u3092\u7279\u5b9a\u3059\u308b\u305f\u3081\u306b\u3001BallTree\u3092\u4f7f\u3063\u3066\u691c\u7d22\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><strong>\u2462\u2461\u3067\u6c42\u3081\u305f\uff12\u70b9\u9593\u3092\u95a2\u6570\u306b\u5165\u308c\u3001\u8ddd\u96e2\u3092\u6c42\u3081\u308b\u3002<\/strong> \u3053\u3053\u3067\u306f\u3001<strong>haversine<\/strong>\u306a\u3069\u306e\u5730\u7406\u7684\u8ddd\u96e2\u8a08\u7b97\u3092\u884c\u3046\u305f\u3081\u306e\u95a2\u6570\u3092\u5229\u7528\u3057\u307e\u3059\u3002haversine\u516c\u5f0f\u306f\u3001\u7403\u9762\u4e0a\u3067\u306e2\u70b9\u9593\u306e\u5927\u5186\u8ddd\u96e2\u3092\u8a08\u7b97\u3059\u308b\u305f\u3081\u306e\u3082\u306e\u3067\u3059\u3002<\/p>\r\n<p><!-- notionvc: 21a1f001-8346-4b18-8ffe-71b41a2ec423 --><\/p>\n\n<h2>\u5b9f\u88c5<\/h2>\n<p>\u4ee5\u4e0b\u304c\u5b9f\u88c5\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\r\n<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u3001\u4f4f\u6240\u304b\u3089\u7def\u5ea6\u30fb\u7d4c\u5ea6\u3092\u6c42\u3081\u3001\u8fd1\u3044\uff12\u70b9\u9593\u306e\u8ddd\u96e2\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>\u56fd\u571f\u5730\u7406\u9662API\u3092\u4f7f\u7528\u3057\u3066\u3001\u4f4f\u6240\u304b\u3089\u7def\u5ea6\u30fb\u7d4c\u5ea6\u3092\u6c42\u3081\u308b\u30b3\u30fc\u30c9<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>#\u56fd\u571f\u5730\u7406\u9662API\r\nmakeUrl = \"https:\/\/msearch.gsi.go.jp\/address-search\/AddressSearch?q=\"\r\n\r\n#\u4f4f\u6240\u304b\u3089\u7def\u5ea6\u30fb\u7d4c\u5ea6\u3092\u53d6\u5f97\r\nlats = []\r\nlongs = []\r\nfor address in df[\"\u4f4f\u6240\"]:\r\ns_quote = urllib.parse.quote(address)\r\nresponse = requests.get(makeUrl + s_quote)\r\nif response:\r\nlats.append(response.json()[0][\"geometry\"][\"coordinates\"][0])\r\nlongs.append(response.json()[0][\"geometry\"][\"coordinates\"][1])\r\nelse:\r\nprint(\"\u4f4f\u6240\u304c\u3042\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002\")<\/code><\/pre>\r\n<\/div>\r\n<p>2\u3064\u306e\u7def\u5ea6\u30fb\u7d4c\u5ea6\u304b\u3089\u3001\u8ddd\u96e2\u3092\u6c42\u3081\u308b\u95a2\u6570<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def distance(dist):\r\nearth_radius_km = 6371\r\ndistance_km = dist * earth_radius_km\r\nreturn distance_km<\/code><\/pre>\r\n<\/div>\r\n<p>\u7def\u5ea6\u30fb\u7d4c\u5ea6\u304b\u3089\u6700\u3082\u8fd1\u30442\u70b9\u3092\u6c42\u3081\u3066\u3001\u305d\u306e\u8ddd\u96e2\u3092\u6c42\u3081\u308b\u30b3\u30fc\u30c9<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code><\/code>lists = [] coordinates = df[['\u7def\u5ea6', '\u7d4c\u5ea6']].values tree = BallTree(np.radians(coordinates), metric='haversine') for lat,long in zip(df[\"\u7def\u5ea6\"],df[\"\u7d4c\u5ea6\"]): store1_coords = np.radians([lat,long]) dist, ind = tree.query([store1_coords], k=2) dist = dist[0][1] closest_store = df.iloc[ind[0][1]] d = distance(dist) lists.append(d)<\/pre>\r\n<\/div>\r\n<p>\u5168\u4f53\u30b3\u30fc\u30c9<\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code><\/code>#\u4f7f\u7528\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea import pandas as pd import requests import urllib import numpy as np from sklearn.neighbors import BallTree #\u56fd\u571f\u5730\u7406\u9662API makeUrl = \"https:\/\/msearch.gsi.go.jp\/address-search\/AddressSearch?q=\" #\u4f4f\u6240\u304b\u3089\u7def\u5ea6\u30fb\u7d4c\u5ea6\u3092\u53d6\u5f97 lats = [] longs = [] for address in df[\"\u4f4f\u6240\"]: s_quote = urllib.parse.quote(address) response = requests.get(makeUrl + s_quote) if response: lats.append(response.json()[0][\"geometry\"][\"coordinates\"][0]) longs.append(response.json()[0][\"geometry\"][\"coordinates\"][1]) else: print(\"\u4f4f\u6240\u304c\u3042\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002\") #\u7def\u5ea6\u30fb\u7d4c\u5ea6\u304b\u3089\u8ddd\u96e2\u3092\u6c42\u3081\u308b\u95a2\u6570 def distance(dist): earth_radius_km = 6371 distance_km = dist * earth_radius_km return distance_km #\u7def\u5ea6\u30fb\u7d4c\u5ea6\u304b\u3089\u6700\u3082\u8fd1\u30442\u70b9\u3092\u53d6\u5f97 lists = [] coordinates = df[['\u7def\u5ea6', '\u7d4c\u5ea6']].values tree = BallTree(np.radians(coordinates), metric='haversine') for lat,long in zip(df[\"\u7def\u5ea6\"],df[\"\u7d4c\u5ea6\"]): store1_coords = np.radians([lat,long]) dist, ind = tree.query([store1_coords], k=2) dist = dist[0][1] closest_store = df.iloc[ind[0][1]] d = distance(dist) lists.append(d)<\/pre>\r\n<\/div>\n\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001Python\u3092\u4f7f\u3063\u3066\u56fd\u571f\u5730\u7406\u9662\u306eAPI\u3092\u6d3b\u7528\u3057\u3001\u4f4f\u6240\u304b\u3089\u7def\u5ea6\u7d4c\u5ea6\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u305d\u3057\u3066\u3001\u53d6\u5f97\u3057\u305f2\u3064\u306e\u7def\u5ea6\u30fb\u7d4c\u5ea6\u3068haversine\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3063\u30662\u70b9\u9593\u306e\u8ddd\u96e2\u3092\u8a08\u7b97\u3059\u308b\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3057\u305f\u3002\u3053\u306e\u6280\u8853\u3092\u6d3b\u7528\u3059\u308c\u3070\u3001\u4f4d\u7f6e\u60c5\u5831\u3092\u6271\u3063\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u958b\u767a\u3059\u308b\u969b\u306b\u5f79\u7acb\u3064\u77e5\u8b58\u304c\u5f97\u3089\u308c\u307e\u3059\u3002\u5730\u7406\u60c5\u5831\u3092\u52b9\u7387\u7684\u306b\u6d3b\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u3055\u3089\u306b\u9ad8\u5ea6\u306a\u30c7\u30fc\u30bf\u5206\u6790\u306b\u3082\u5bfe\u5fdc\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u5730\u7406\u60c5\u5831\u3092\u6d3b\u7528\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3084\u30c7\u30fc\u30bf\u5206\u6790\u304c\u9032\u3080\u4e2d\u3001\u4f4f\u6240\u304b\u3089\u8ddd\u96e2\u3092\u8a08\u7b97\u3059\u308b\u9700\u8981\u306f\u5897\u52a0\u3057\u3066\u3044\u307e\u3059\u3002 \u306f\u3058\u3081\u306b \u5730\u7406\u60c5\u5831\u3092\u6d3b\u7528\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3084\u30c7\u30fc\u30bf\u5206\u6790\u304c\u9032\u3080\u4e2d\u3001\u4f4f\u6240\u304b\u3089\u8ddd\u96e2\u3092\u8a08\u7b97\u3059\u308b\u9700\u8981\u306f\u5897\u52a0\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001 [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":4516,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1249],"tags":[145,899,331,679,898],"class_list":["post-6460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge","tag-api","tag-haversine","tag-python","tag-sklearn"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/6460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=6460"}],"version-history":[{"count":0,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/6460\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/4516"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=6460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=6460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=6460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}