我是Python新手,想重建此example。我有關於紐約市出租車出租車和出租車的經緯度數據,但是,我需要將數據更改爲Web Mercartor格式(這在以上示例中找不到)。 我發現這可能需要一對經度和緯度值並將其更改爲網絡Mercartor格式,這是從here拍攝功能,它看起來如下:將函數應用於Pandas Dataframe中的每一行
import math
def toWGS84(xLon, yLat):
# Check if coordinate out of range for Latitude/Longitude
if (abs(xLon) < 180) and (abs(yLat) > 90):
return
# Check if coordinate out of range for Web Mercator
# 20037508.3427892 is full extent of Web Mercator
if (abs(xLon) > 20037508.3427892) or (abs(yLat) > 20037508.3427892):
return
semimajorAxis = 6378137.0 # WGS84 spheriod semimajor axis
latitude = (1.5707963267948966 - (2.0 * math.atan(math.exp((-1.0 * yLat)/semimajorAxis)))) * (180/math.pi)
longitude = ((xLon/semimajorAxis) * 57.295779513082323) - ((math.floor((((xLon/semimajorAxis) * 57.295779513082323) + 180.0)/360.0)) * 360.0)
return [longitude, latitude]
def toWebMercator(xLon, yLat):
# Check if coordinate out of range for Latitude/Longitude
if (abs(xLon) > 180) and (abs(yLat) > 90):
return
semimajorAxis = 6378137.0 # WGS84 spheriod semimajor axis
east = xLon * 0.017453292519943295
north = yLat * 0.017453292519943295
northing = 3189068.5 * math.log((1.0 + math.sin(north))/(1.0 - math.sin(north)))
easting = semimajorAxis * east
return [easting, northing]
def main():
print(toWebMercator(-105.816001, 40.067633))
print(toWGS84(-11779383.349100526, 4875775.395628653))
if __name__ == '__main__':
main()
我怎麼這個數據應用到每對在我的熊貓數據框中的長/緯度座標,並保存在同一個pandasDF?
df.tail()
| longitude | latitude
____________|__________________|______________
11135465 | -73.986893 | 40.761093
1113546 | -73.979645 | 40.747814
11135467 | -74.001244 | 40.743172
11135468 | -73.997818 | 40.726055
...
非常感謝,這確實非常快,非常有幫助。 – CFM