Rya*_*yan 7 python numpy pandas shapely geopandas
我有一个函数可以将点网格输出为 x 和 y numpy 数组以进行插值,但是在进行插值之前,我想使用 Geopandas 与我的研究边界进行交集(否则我的一半插值点会落在海洋中)。
我正在生成这样的点:
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import Point
x = np.linspace(0,100,100)
y = np.linspace(0,100,100)
x, y = np.meshgrid(x, y)
x, y = x.flatten(), y.flatten()
f, ax = plt.subplots()
plt.scatter(x, y)
plt.axis('equal')
plt.show()
Run Code Online (Sandbox Code Playgroud)
有没有一种有效的方法可以将这些 numpy 数组转换为shapely.Point([x, y])
它们可以放置在 geopandas 地理数据框中?
这是我目前的方法:
interp_points = []
index = 0
y_list = yi.tolist()
for x in xi.tolist():
interp_points.append(Point(x,y_list[index]))
index += 1
Run Code Online (Sandbox Code Playgroud)
但似乎转换为列表然后迭代可能不是一个好的性能方法,我有大约 160,000 点。
没有内置方法可以使用 执行此操作shapely
,因此您需要自己迭代这些值。为此,这应该是一种相当有效的方法:
In [4]: from geopandas import GeoSeries\n\nIn [5]: s = GeoSeries(map(Point, zip(x, y)))\n\nIn [6]: s.head()\nOut[6]: \n0 POINT (0 0)\n1 POINT (1.01010101010101 0)\n2 POINT (2.02020202020202 0)\n3 POINT (3.03030303030303 0)\n4 POINT (4.040404040404041 0)\ndtype: object\n\nIn [6]: %timeit GeoSeries(map(Point, zip(x, y)))\n114 ms \xc2\xb1 8.45 ms per loop (mean \xc2\xb1 std. dev. of 7 runs, 10 loops each)\n
Run Code Online (Sandbox Code Playgroud)\n\n(或轻微的替代方案GeoSeries(list(zip(x, y))).map(Point)
)
请参阅此处的一些示例:http://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html
\n\n有一些(停滞的)工作可以将其直接包含在 geopandas 中: https: //github.com/geopandas/geopandas/pull/75
\n