简体   繁体   English

将 numpy 数组转换为 Shapely Points 的最有效方法是什么?

[英]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.

相关问题 将带有numpy数组列表的字典转换为pandas数据帧的最有效方法? - Most efficient way to convert a dictionary with list of numpy arrays into pandas dataframe? 处理 NumPy 数组上的循环的最有效方法是什么? - What is the most efficient way to deal with a loop on NumPy arrays? 将 numpy 数组转换为字典的最有效方法 - Most efficient way to convert numpy array to dict 将numpy数组转换为字符串的最有效方法 - Most efficient way to convert numpy array to string 索引 Numpy 矩阵的最有效方法是什么? - What is the most efficient way of indexing Numpy matrices? 用Python检测numpy图片阵列中重复项的最有效方法是什么? - What's the most efficient way to detect duplicates in numpy arrays of images with Python? 用 numpy arrays 存储大型 Pandas 系列的最有效方法是什么? - What's the most efficient way to store large Pandas Series with numpy arrays? 将numpy数组列表转换为单个numpy数组的最直接方法是什么? - What is the most straightforward way to convert a list of numpy arrays into a single numpy array? 基于两个numpy数组获取排序索引的最有效方法 - Most efficient way to get sorted indices based on two numpy arrays 将 MySQL 结果集转换为 NumPy 数组的最有效方法是什么? - What's the most efficient way to convert a MySQL result set to a NumPy array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM