[Python] What is the most efficient way to convert numpy arrays to Shapely Points?

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    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).

    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?

    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.

    Continue reading...

Compartilhe esta Página