我有两个一维数组,想将它们组合成一个 Point GeoSeries,如下所示:
import numpy as np
from geopandas import GeoSeries
from shapely.geometry import Point
x = np.random.rand(int(1e6))
y = np.random.rand(int(1e6))
GeoSeries(map(Point, zip(x, y)))
在我的笔记本电脑上大约需要 5 秒。是否可以加速GeoSeries的生成?
为了加快此过程,您需要使用矢量化运算,而不是使用
map
。 GeoPandas 提供的points_from_xy
功能专门为此目的进行了优化。
这是在我的机器上运行的示例:
import numpy as np
from geopandas import GeoSeries
from shapely.geometry import Point
import geopandas as gpd
import time
x = np.random.rand(int(1e6))
y = np.random.rand(int(1e6))
s = time.time()
GeoSeries(map(Point, zip(x, y)))
f = time.time()
print("time elapsed with `map` : ", f - s)
geo_series = gpd.GeoSeries(gpd.points_from_xy(x, y))
print("time elapsed with `points_from_xy` : ", time.time() - f)
输出:
time elapsed with `map` : 9.318699359893799
time elapsed with `points_from_xy` : 0.654371976852417
看,
points_from_xy
几乎快了 10 倍,因为它使用了矢量化方法。
从此处查看
geopandas.points_from_xy
文档以了解更多信息:https://geopandas.org/en/stable/docs/reference/api/geopandas.points_from_xy.html