簡體   English   中英

計算從特定經緯度到多個經緯度的距離問題

[英]Issue in calculating distance from particular lat long to multiple lat longs

import pandas as p
from cmath import sin, cos, atan, sqrt
import math
import numpy as np
import cmath


d = p.read_excel("codeTest.xlsx")

def distance():
    
    lat1 = d["Latitude"]
    lat1.astype("float64")
    lat1.dtype
    lon1 = d["Longitude"]
    lon1.astype("float64")
    lon1.dtype

    lat2, lon2 = 28.6225833 , 77.2127222
    radius = 6371 # km
    dlat = np.radians(lat2-lat1)
    dlon = math.radians(lon2-lon1)
    a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
        * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    e = radius * c
    print(e)
distance()

對於上面的代碼,我收到以下錯誤:

M/Desktop/Python/Python code/test1.py"
Traceback (most recent call last):
  File "c:\Users\Shaloo.M\Desktop\Python\Python code\test1.py", line 28, in <module>
    distance()
  File "c:\Users\Shaloo.M\Desktop\Python\Python code\test1.py", line 22, in distance
    dlon = math.radians(lon2-lon1)
  File "C:\Users\Shaloo.M\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\series.py", line 191, in wrapper
    raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class 'float'>
PS C:\Users\Shaloo.M\Desktop\Python\Python code>`

不確定是什么問題,但是......

我可以推薦geopandasshapely嗎?

import geopandas as gp
from shapely.geometry import Point

gdf = gp.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))

gdf = gdf.to_crs("EPSG:4326") # Specify that we're working with Lat/Long

gdf['distance'] = gdf.distance(Point(28.6225833, 77.2127222).buffer(6371))

地理熊貓

身材勻稱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM