如何检查点在给定半径内? [英] How to check that a point is inside the given radius?

查看:105
本文介绍了如何检查点在给定半径内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码需要很长时间才能执行.熊猫DataFrames dfdf_plants非常小(小于1Mb).我想知道是否有任何方法可以优化此代码:

I have the following code that takes very long time to execute. The pandas DataFrames df and df_plants are very small (less than 1Mb). I wonder if there is any way to optimise this code:

import pandas as pd
import geopy.distance
import re

def is_inside_radius(latitude, longitude, df_plants, radius):
    if (latitude != None and longitude != None):
        lat = float(re.sub("[a-zA-Z]", "", str(latitude)))
        lon = float(re.sub("[a-zA-Z]", "", str(longitude)))
        for index, row in df_plants.iterrows():
            coords_1 = (lat, lon)
            coords_2 = (row["latitude"], row["longitude"])
            dist = geopy.distance.distance(coords_1, coords_2).km
            if dist <= radius:
                return 1
    return 0

df["inside"] = df.apply(lambda row: is_inside_radius(row["latitude"],row["longitude"],df_plants,10), axis=1)

我使用正则表达式处理df中的纬度和经度,因为这些值包含一些错误(字符),应将其删除.

I use regex to process latitude and longitude in df because the values contain some errors (characters) which should be deleted.

函数is_inside_radius验证row[latitude]row[longitude]是否在距df_plants中任意点10公里的半径之内.

The function is_inside_radius verifies if row[latitude] and row[longitude] are inside the radius of 10 km from any of the points in df_plants.

推荐答案

我以前遇到过这样的问题,并且我看到了一个简单的优化方法:尝试尽可能避免浮点计算,您可以这样做如下:
想象一下:
您有一个圆,由Mx和My(中心坐标)和R(半径)定义.
您有一个点,由X和Y坐标定义.

I've encountered such a problem before, and I see one simple optimisation: try to avoid the floating point calculation as much a possible, which you can do as follows:
Imagine:
You have a circle, defined by Mx and My (center coordinates) and R (radius).
You have a point, defined by is coordinates X and Y.

如果您的点(X,Y)不在由(Mx,My)和大小2 * R定义的正方形内,那么它也将不在由(Mx,My)和半径定义的圆内R.
用伪代码:

If your point (X,Y) is not even within the square, defined by (Mx, My) and size 2*R, then it will also not be within the circle, defined by (Mx, My) and radius R.
In pseudo-code:

function is_inside(X,Y,Mx,My,R):
  if (abs(Mx-X) >= R) OR (abs(My-Y) >= R)
  then return false
  else:
    // and only here you perform the floating point calculation

这篇关于如何检查点在给定半径内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆