point_in_polygon#

named_arrays.geometry.point_in_polygon(x, y, vertices_x, vertices_y, axis)#

Check if a given point is inside or on the boundary of a polygon.

This function is a wrapper around regridding.geometry.point_is_inside_polygon().

Parameters:
  • x (PointT) – The \(x\)-coordinates of the test points.

  • y (PointT) – The \(y\)-coordinates of the test points.

  • vertices_x (VertexT) – The \(x\)-coordinates of the polygon’s vertices.

  • vertices_y (VertexT) – The \(y\)-coordinates of the polygon’s vertices.

  • axis (str) – The logical axis representing the different vertices of the polygon.

Return type:

PointT | VertexT

Examples

Check if some random points are inside a randomly-generated polygon.

import numpy as np
import matplotlib.pyplot as plt
import named_arrays as na

# Define a random polygon
axis = "vertex"
num_vertices = 7
radius = na.random.uniform(5, 15, shape_random={axis: num_vertices})
angle = na.linspace(0, 2 * np.pi, axis=axis, num=num_vertices)
vertices_x = radius * np.cos(angle)
vertices_y = radius * np.sin(angle)

# Define some random points
x = na.random.uniform(-20, 20, shape_random=dict(r=1000))
y = na.random.uniform(-20, 20, shape_random=dict(r=1000))

# Select which points are inside the polygon
where = na.geometry.point_in_polygon(
    x=x,
    y=y,
    vertices_x=vertices_x,
    vertices_y=vertices_y,
    axis=axis,
)

# Plot the results as a scatter plot
fig, ax = plt.subplots()
na.plt.fill(
    vertices_x,
    vertices_y,
    ax=ax,
    facecolor="none",
    edgecolor="black",
)
na.plt.scatter(
    x,
    y,
    where=where,
    ax=ax,
);
../_images/named_arrays.geometry.point_in_polygon_0_0.png