【问题标题】:Why is my function calculating wrong distance between 2 points为什么我的函数计算两点之间的距离错误
【发布时间】:2019-11-05 16:24:49
【问题描述】:

我有一个数据框,它汇总了这些社区中的社区和咖啡馆及其位置,称为 Final。并生成随机位置点,每个位置相距 100 米。我现在想计算最终数据框中的数据点(咖啡馆)与随机生成的最近位置之间的距离。

我正在尝试克隆this notebook (Code block number 92-96)中的代码

我认为问题出在我的初始数据框中。但我不知道我应该如何处理数据框来完成这项工作。

下面代码中的 events 变量将最终数据帧存储为字典(因为在代码中我试图克隆它们使用字典)。但是,在我的函数 find_nearest_restaurant 中, res[4] 不采用纬度和经度...不知道如何处理它。

最终数据框This is how dataframe looks like

我正在为此尝试以下代码:

def find_nearest_restaurant(x, y, events):
    d_min = 100000
    for res in events:
        res_x = res[4]; res_y = res[5]
        d = calc_xy_distance(x, y, res_x, res_y)
        if d<=d_min:
            d_min = d
    return d_min


def lonlat_to_xy(lon, lat):
    proj_latlon = pyproj.Proj(proj='latlong',datum='WGS84')
    proj_xy = pyproj.Proj(proj="utm", zone=33, datum='WGS84')
    xy = pyproj.transform(proj_latlon, proj_xy, lon, lat)
    return xy[0], xy[2]


events = Final.to_dict()
Kralingen_center = [51.928263, 4.50344]
Kr_center_x, Kr_center_y = lonlat_to_xy(Kralingen_center[2], Kralingen_center[0]) 
Centrum_Center = [51.922909, 4.47059]
k = math.sqrt(3) / 2 # Vertical offset for hexagonal grid cells
x_step = 100
y_step = 100 * k 
roi_x_min = Kr_center_x - 2000
roi_y_max = Kr_center_y + 1000
roi_center_x = roi_x_min + 2000
roi_center_y = roi_y_max - 2500
roi_y_min = roi_center_y - 2500


def xy_to_lonlat(x, y):
    proj_latlon = pyproj.Proj(proj='latlong',datum='WGS84')
    proj_xy = pyproj.Proj(proj="utm", zone=33, datum='WGS84')
    lonlat = pyproj.transform(proj_xy, proj_latlon, x, y)
    return lonlat[0], lonlat[2]


def calc_xy_distance(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    return math.sqrt(dx*dx + dy*dy)


roi_latitudes = []
roi_longitudes = []
roi_xs = []
roi_ys = []

for i in range(0, int(51/k)):
    y = roi_y_min + i * y_step
    x_offset = 50 if i%2==0 else 0
    for j in range(0, 51):
        x = roi_x_min + j * x_step + x_offset
        d = calc_xy_distance(Kr_center_x, Kr_center_y, x, y)
        if (d <= 2001):
            lon, lat = xy_to_lonlat(x, y)
            roi_latitudes.append(lat)
            roi_longitudes.append(lon)
            roi_xs.append(x)
            roi_ys.append(y)


print(len(roi_latitudes), 'candidate points generated.')


def find_nearest_restaurant(x, y, events):
    d_min = 100000
    for res in events:
        res_x = res[4]; res_y = res[5]
        d = calc_xy_distance(x, y, res_x, res_y)
        if d<=d_min:
            d_min = d
    return d_min

我想收到一个数据框:

df_roi_locations = pd.DataFrame({'Latitude':roi_latitudes,
                                 'Longitude':roi_longitudes,
                                 'X':roi_xs,
                                 'Y':roi_ys,
                                 'Distance to restaurant':roi_kr_distances})

roi_kr_distances 只是运行find_nearest_restaurant 函数并将其附加到空列表的结果。

但是代码给了我一个错误:

<ipython-input-70-b6a8fa5871aa> in find_nearest_restaurant(x, y, events)
     53     for res in events:
     54         res_x = res[3]; res_y = res[4]
---> 55         d = calc_xy_distance(x, y, res_x, res_y)
     56         if d<=d_min:
     57             d_min = d

<ipython-input-70-b6a8fa5871aa> in calc_xy_distance(x1, y1, x2, y2)
     26 
     27 def calc_xy_distance(x1, y1, x2, y2):
---> 28     dx = x2 - x1
     29     dy = y2 - y1
     30     return math.sqrt(dx*dx + dy*dy)

TypeError: unsupported operand type(s) for -: 'str' and 'float'

【问题讨论】:

  • 看来您需要将函数(或数据帧)中 x2 或 x1 的数据类型强制转换为数字类型(例如 float)

标签: python location distance


【解决方案1】:

从您提供的示例代码中很难说,因为我不知道 events 变量中存储了什么,但从您的错误来看,您似乎有两种数据类型(字符串和浮点数)并且您正在尝试对它们执行减法,这在 python 中是不允许的。

这是唯一建议尝试的方法,即尝试将参数转换为浮点数。

def calc_xy_distance(x1, y1, x2, y2):
    dx = float(x2) - float(x1)
    dy = float(y2) - float(y1)
    return math.sqrt(dx*dx + dy*dy)

希望这会有所帮助。

【讨论】:

  • 现在,它告诉我无法将字符串 'g' 转换为浮点数。因此, res[4] 可能采用了错误的值..
猜你喜欢
  • 2012-12-31
  • 2010-10-30
  • 1970-01-01
  • 2011-04-23
  • 2014-11-14
  • 1970-01-01
相关资源
最近更新 更多