[英]What is the most efficient way to convert numpy arrays to Shapely Points?
I have a function that outputs a grid of points as x and y numpy arrays for interpolation, but before I interpolate, I want to use Geopandas to perform an intersection with my research boundary (otherwise half of my interpolation points fall in the ocean).我有一个函数可以将点网格输出为 x 和 y numpy 数组以进行插值,但是在进行插值之前,我想使用 Geopandas 与我的研究边界进行交集(否则我的一半插值点会落在海洋中)。
I'm generating points like this:我正在生成这样的点:
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()
Is there an efficient way to convert these numpy arrays to shapely.Point([x, y])
so they can be placed in a geopandas geodataframe?有没有一种有效的方法可以将这些 numpy 数组转换为shapely.Point([x, y])
以便它们可以放置在 geopandas 地理数据框中?
This is my current approach:这是我目前的方法:
interp_points = []
index = 0
y_list = yi.tolist()
for x in xi.tolist():
interp_points.append(Point(x,y_list[index]))
index += 1
But it seems like converting to lists and then iterating is likely not a good approach for performance, and I have approximately 160,000 points.但似乎转换为列表然后迭代可能不是一个好的性能方法,我有大约 160,000 点。
There is no built-in way to do this with shapely
, so you need to iterate through the values yourself.没有内置的方法可以使用shapely
执行此操作,因此您需要自己迭代这些值。 For doing that, this should be a rather efficient way:为此,这应该是一种相当有效的方法:
In [4]: from geopandas import GeoSeries
In [5]: s = GeoSeries(map(Point, zip(x, y)))
In [6]: s.head()
Out[6]:
0 POINT (0 0)
1 POINT (1.01010101010101 0)
2 POINT (2.02020202020202 0)
3 POINT (3.03030303030303 0)
4 POINT (4.040404040404041 0)
dtype: object
In [6]: %timeit GeoSeries(map(Point, zip(x, y)))
114 ms ± 8.45 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
(or slight alternative GeoSeries(list(zip(x, y))).map(Point)
) (或轻微的替代GeoSeries(list(zip(x, y))).map(Point)
)
See here for some example doing this: http://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html有关执行此操作的示例,请参见此处: http : //geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html
There is some (stalled) work to include this in geopandas directly: https://github.com/geopandas/geopandas/pull/75有一些(停滞的)工作可以直接将其包含在 geopandas 中: https : //github.com/geopandas/geopandas/pull/75
I think this is a good way:我认为这是一个好方法:
import numpy as np
from shapely import geometry
points_np_array = np.random.rand(50,2)
polygon_1 = geometry.Polygon(np.squeeze(points_np_array))
Better use this list comprehention:更好地使用这个列表理解:
[tuple(x) for x in arr.tolist()] [元组(x) for x in arr.tolist()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.