我正在处理纽约市出租车数据集。该数据集的列包括日期时间、接送纬度/经度、下车纬度/经度等。现在我想对纬度/经度进行反向地理编码以找到行政区/社区。
我有两个数据框。
1)第一个数据框包含我想要用最近的纽约社区名称进行分类的所有点。
2)第二个数据框包含邻域名称及其质心。
我给你看一个小例子。
df_points_to_classify: 单击此处下载原始 csv https://s3.amazonaws.com/nyc-tlc/trip+data/yellow_tripdata_2015-01.csv
longitude latitude
<dbl> <dbl>
1 -73.99037 40.73470
2 -73.98078 40.72991
3 -73.98455 40.67957
4 -73.99347 40.71899
df_neighborhood_names_and_their_centroids: 单击此处下载原始 csv https://data.cityofnewyork.us/City-Government/Neighborhood-Names-GIS/99bc-9p23
longitude latitude neighborhood
<dbl> <dbl> <chr>
1 -73.8472005205491 40.89470517661004 Wakefield
2 -73.82993910812405 40.87429419303015 Co-op City
3 -73.82780644716419 40.88755567735082 Eastchester
4 -73.90564259591689 40.895437426903875 Fieldston
要将单个点分配给邻域,我必须计算从该点到每个邻域质心的距离。显然,该点将属于距离最短的邻域。
预期输出包括向要分类的点的数据帧添加一列,其中包含每个点所属的邻域。
预期输出:
longitude latitude neighborhood
<dbl> <dbl> <chr>
1 -73.99037 40.73470 Fieldston
2 -73.98078 40.72991 Co-op City
3 -73.98455 40.67957 etc...
4 -73.99347 40.71899 etc...
我想使用一种计算效率高的方法,因为我要分类的点的数据库非常大(超过一千兆字节)。
因此,我在 R 上使用 Spark。文件已通过这种方式加载。
library(sparklyr)
sc <- spark_connect(master = "local")
df_points_to_classify <- spark_read_csv(sc, "D:\df_points_to_classify.csv")
是否可以使用 dplr 来解决这个问题?
编辑:
该解决方案在使用 Spark 时不适用,因为以下结果df_points_to_classify$any_variable
is NULL
library(spatialrisk)
ans <- purrr::map2_dfr(df_points_to_classify$longitude,
df_points_to_classify$latitude,
~spatialrisk::points_in_circle(df_neighborhood_names_and_their_centroids, .x, .y,
lon = longitude,
lat = latitude,
radius = 2000000)[1,])