Efficient way to create a list of Point. #2117
-
Please noteI want to create a list of p = [Point(x, y) for x, y in zip(xq, yq)]
df = pd.DataFrame({'x':xq, 'y':yq, 'coords': p}) df = pd.DataFrame({'x':xq, 'y':yq})
df['coords'] = list(zip(df['x'],df['y']))
df['coords'] = df['coords'].apply(Point) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Assuming you are using Shapely 2.x, you can use the import numpy as np
import shapely
rng = np.random.default_rng(123)
n = 10_000
xq = rng.random(n)
yq = rng.random(n)
pts = shapely.points(xq, yq) and when you get to do the point-in-polygon check, also use vectorized functions like poly = shapely.from_wkt("POLYGON((0 0, 0 0.1, 0.1 0.1, 0 0))")
pt_in_poly = shapely.contains(poly, pts)
# or poly.contains(pts)
pt_in_poly.sum() # 60 |
Beta Was this translation helpful? Give feedback.
-
I am trying to create a "equivalent" https://www.mathworks.com/help/matlab/ref/inpolygon.html def inpolygon(xq, yq, xv, yv):
polygon = Polygon(list(zip(xv, yv)))
if not polygon.is_valid:
polygon = polygon.buffer(0)
pts = shapely.points(xq, yq)
return polygon.touches(pts) | polygon.contains(pts) And this is the result with line_profile. Is there some way to improve it? Thank you so much!
|
Beta Was this translation helpful? Give feedback.
Assuming you are using Shapely 2.x, you can use the
points
function, which is vectorized to return numpy array types.and when you get to do the point-in-polygon check, also use vectorized functions like
contains
: