numpy meshgrid to Shapely polygons

前端 未结 1 1919
猫巷女王i
猫巷女王i 2021-01-03 12:28

I\'m trying to create a numpy meshgrid and convert it to Shapely polygons. I can likely solve this with a very brute force method but it feels like there has to be a good tr

相关标签:
1条回答
  • 2021-01-03 13:07

    You basically have to define every line before you construct the MultiLineString.

    import numpy as np
    from shapely.geometry import MultiLineString
    from shapely.ops import polygonize
    
    x = np.linspace(-5, -1, 8)
    y = np.linspace(1, 5, 8)
    
    hlines = [((x1, yi), (x2, yi)) for x1, x2 in zip(x[:-1], x[1:]) for yi in y]
    vlines = [((xi, y1), (xi, y2)) for y1, y2 in zip(y[:-1], y[1:]) for xi in x]
    
    grids = list(polygonize(MultiLineString(hlines + vlines)))
    
    0 讨论(0)
提交回复
热议问题