我在一条街道上有点,而我试图计算所有点与街道上第一个点之间的距离
这是我的科幻对象:
$b
我尝试了st_distance,但是距离不正确。 我要得到的是距街道起点至终点的每个点的距离。所以基本上它必须从0到5000m。
答案 0 :(得分:4)
如果将by_element
参数添加到st_distance()
,我想您会得到想要的结果。如果将by_element
保留为FALSE(默认设置),则会得到一个矩阵。
st_distance(x = df_sf,
y = df_sf[1,],
by_element = TRUE)
Units: [m]
[1] 0.000 1420.606 2841.141 4261.604
请注意,距离与其他答案b / c df_sf的投影略有不同。如果我们使用未投影的df
对象,则距离匹配:
st_distance(x = df,
y = df[1,],
by_element = T)
Units: [m]
[1] 0.000 1420.098 2840.125 4260.079
编辑以回复评论re:order
距离将按照数据框的顺序排列。在您的示例中,它们恰好按升序排列。
您可以将距离添加为新列,然后使用dplyr
在该列上进行排序。请注意,by_element
参数在这里是必需的,因为新列将不接受矩阵作为值。
library(dplyr)
df_sf %>%
mutate(dist_to_first = st_distance(x = df_sf,
y = df_sf[1,],
by_element = TRUE)) %>%
arrange(dist_to_first)
Simple feature collection with 4 features and 1 field
geometry type: POINT
dimension: XY
bbox: xmin: -175068.7 ymin: -72303.08 xmax: -172485.1 ymax: -68913.94
epsg (SRID): 3488
proj4string: +proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
dist_to_first geometry
1 0.000 [m] POINT (-172485.1 -72303.08)
2 1420.606 [m] POINT (-173346.5 -71173.46)
3 2841.141 [m] POINT (-174207.7 -70043.74)
4 4261.604 [m] POINT (-175068.7 -68913.94)
答案 1 :(得分:3)
对于这种类型的计算,geosphere软件包具有endlist = load_workbook(f"Endlist_{monat1}_2019.xlsx", data_only=True)
endlistws = endlist.active
#creating an empty dict, to store the dates and hours from endlist
endlist_hrs = {}
#creating a dict with dates as keys and empty lists as values
for cell in endlistws['A']:
if cell.value != None:
if weeknum(dateConverter(cell.value)) == kw_num:
if dateConverter(endlistws[f'A{cell.row}'].value) in endlist_hrs.keys(): #is the date already in the dict?
pass # it is, so pass
else:
endlist_hrs[dateConverter((endlistws[f'A{cell.row}'].value))] = [] #its not, so add it
else:
pass #does not match
# iterating over keys in the endlist_hrs dict, checking the dates in A column - not the best solution, iterating every time over whole A column - to be upgraded
for key in endlist_hrs.keys():
for cell in endlistws['A']:
if cell.value != None:
if dateConverter(cell.value) == key:
endlist_hrs[key].append(czasownik(endlistws[f'J{cell.row}'].value))
endlist.close() #closing the endlist workbook
#creating a dict with dates as keys and sum of hours as values - ready to be inserted into cells in the Check workbook
full_endlist_data = {k:sum(v) for (k,v) in endlist_hrs.items()}
#copying the dailycheck workbook and producing the final output
faylneym = f"DC{kw_num}.xlsx"
paf = os.path.join(values['Browse0'], faylneym)
shutil.copy2(values['Browse1'], paf)
dcwb = load_workbook(paf, write_only=True)
dcws = dcwb['KW_XX']
dcws.title = str(kw)
dcwb.save(paf)
dcwb = load_workbook(paf)
dcws = dcwb.active
for x,y in enumerate(strdate, start=2):
dcws[f'A{x}'].value = y
for x,y in enumerate(strdate, start=12):
dcws[f'A{x}'].value = y
for x,y in enumerate(hours_from_eos2, start=2):
dcws[f'E{x}'].value = y
for x,y in enumerate(full_endlist_data.values(), start=2):
dcws[f'D{x}'].value = y
函数:
distGeo
对于library(geosphere)
df <- data.frame(lon = c(-121.95, -121.96, -121.97, -121.98), lat = c(37.35,37.36,37.37,37.38))
#calculate dist from first row
# to all rows in df
distGeo(df[1,], df)
[1] 0.000 1420.098 2840.125 4260.079
,第一列为经度,第二列为纬度,输出单位为米。