我有一个大的.csv数据集,其中包含10e7个点,坐标(纬度,经度)代表访问者的位置。我有另一个包含10e3点的数据集,坐标代表商店的位置。
我希望使用某种测地公式将最近的商店与每位访客相关联。
我想要一些真正快速有效的东西,我可以在python(例如pandas)或Google BigQuery上运行。
有人能给我一些线索吗?
答案 0 :(得分:4)
添加到Felipe的回答:
您可以使用SQL UDF与JS UDF
JS UDF有一些SQL UDF不
与Felipe的其余代码一起使用的等效SQL UDF是
CREATE TEMPORARY FUNCTION distance(lat1 FLOAT64, lon1 FLOAT64, lat2 FLOAT64, lon2 FLOAT64)
RETURNS FLOAT64 AS ((
WITH constants AS (
SELECT 0.017453292519943295 AS p
)
SELECT 12742 * ASIN(SQRT(
0.5 - COS((lat2 - lat1) * p)/2 +
COS(lat1 * p) * COS(lat2 * p) *
(1 - COS((lon2 - lon1) * p))/2))
FROM constants
));
我尽可能保留各个JS UDF的布局,以便您可以看到它是如何创建的
答案 1 :(得分:3)
这是一个快速解决方案,可以找到DBpedia(2014年)中21,221个城市最近的NOAA气象站。
#standardSQL
CREATE TEMPORARY FUNCTION distance(lat1 FLOAT64, lon1 FLOAT64, lat2 FLOAT64, lon2 FLOAT64)
RETURNS FLOAT64
LANGUAGE js AS """
var p = 0.017453292519943295; // Math.PI / 180
var c = Math.cos;
var a = 0.5 - c((lat2 - lat1) * p)/2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p))/2;
return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
""";
SELECT *
FROM (
SELECT city, country_label, distance, name weather_station, country,
RANK() OVER(PARTITION BY city ORDER BY distance DESC) rank
FROM (
SELECT city, a.country_label, distance(a.lat,a.lon,b.lat,b.lon) distance, b.name, b.country
FROM (
SELECT rdf_schema_label city, country_label, country,
CAST(REGEXP_EXTRACT(point, r'(-?\d*\.\d*)') as FLOAT64) lat,
CAST(REGEXP_EXTRACT(point, r' (-?\d*\.\d*)') as FLOAT64) lon
FROM `fh-bigquery.dbpedia2014temp.City`
WHERE point!='NULL'
) a
JOIN (
SELECT name, country, usaf, wban, lat, lon
FROM `bigquery-public-data.noaa_gsod.stations`
WHERE lat != 0.0 AND lon !=0.0
) b
ON CAST(a.lat as INT64)=CAST(b.lat as INT64)
AND CAST(a.lon as INT64)=CAST(b.lon as INT64)
)
)
WHERE rank=1
注意事项: