LoginSignup
1
2

More than 3 years have passed since last update.

GIS×BigQuery 〜2地点間の距離を算出〜

Last updated at Posted at 2019-11-21

BigQueryである2地点間の距離を求める方法

①BigQueryの地理関数ST_GEOGPOINTST_DISTANCEで求める

※完全な球体として表面上の距離を計算している

②JavaScriptのユーザー定義関数(UDF)で求める

早速、任意の位置情報を2列用意して実践してみた

CREATE TEMP FUNCTION distance(lon1 FLOAT64, lat1 FLOAT64,  lon2 FLOAT64,  lat2 FLOAT64)
RETURNS FLOAT64
LANGUAGE js AS """
  lon1 *= Math.PI / 180;
  lat1 *= Math.PI / 180;
  lon2 *= Math.PI / 180;
  lat2 *= Math.PI / 180;
  return 6378.137 * Math.acos(Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1) + Math.sin(lat1) * Math.sin(lat2));
"""; 

SELECT
  name1
  ,lat1
  ,lon1
  ,name2
  ,lat2
  ,lon2
  ,(st_distance(st_geogpoint(lon1, lat1), st_geogpoint(lon2, lat2))) / 1000  as distanceA -- ①
  ,distance(lon1,lat1, lon2, lat2) as distanceB -- ②
FROM osako.test

2つの方法で求めた距離に多少の誤差はあるものの、計算自体に問題なさそう。
image.png

以上!!!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2