我是Python新手,希望重新构建这个example。我有纽约市出租车接送的经纬度数据,但是,我需要将数据更改为webmercartor格式(在上面的例子中找不到)。 我发现了一个函数,它可以取一对经度和纬度值,并将其更改为Web-Mercartor格式,它来自here,如下所示:
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()
如何将这些数据应用于pandas数据帧中的每对long/lat坐标,并将输出保存在同一pandasDF中?在
^{pr2}$
对于这样大的数据集,最有帮助的是理解如何以
pandas
的方式来做事情。与内置的矢量化方法相比,遍历行将产生糟糕的性能。在这个数据集有11135470行,这不是“大数据”,但也不小。与其编写一个函数并将其应用于每一行,不如将函数的某些部分执行到各个列,从而获得更高的性能。我会把这个函数转过来:
^{pr2}$在这方面:
然后,}列包含计算值。在
pickup_easting
和{对于我的笔记本电脑,这需要:
所有1100万行。15分钟>秒。在
我取消了价值观的检查-你可以做些类似的事情:
这使用了布尔索引,这同样比循环快几个数量级。在
尝试:
如果要保留可读的数学函数,以及当前函数的简单转换,请使用^{} :
由于不能使用
if
语句,因此您必须在语法上做一些工作,但是可以在调用eval
之前轻松地过滤出边界外的数据。如果要直接分配新列,也可以使用inplace=True
。在如果你对保持数学语法不感兴趣,并且正在全速搜索,那么numpy答案的执行速度可能会更快。在
相关问题 更多 >
编程相关推荐