4

I have two arrays of different length. One contains shapely polygons, the other contains shapely points. I want to run the a_polygon.contains(a_point) shapely function for every possible combination of elements in both arrays.

I was looking at this post as building a two column matrix with all the possible combinations in the rows could be a desirable intermediate step. But the loop in the 'cartersian(arrays)' function might hinder performance when the input data is huge.

I tried to broadcast one of the arrays, and then applying the shapely function:

Polygons_array[:,newaxis].contains(Points_array)

but that, off course, does not work. I am aware of the recently released geopandas library, but it is not an option for my Canopy installation.

4

1 回答 1

5

以下代码显示了如何对包含在两个不同长度数组中的几何对象应用函数。这种方法避免使用循环。Pandas 的 apply 和 Numpy 的 .vectorize 和广播选项是必需的。

首先考虑做一些导入和以下两个数组:

import numpy as np
import pandas as pd
from shapely.geometry import Polygon, Point

polygons = [[(1,1),(4,3),(4,1),(1,1)],[(2,4),(2,6),(4,6),(4,4),(2,4)],[(8,1),(5,1),(5,4),(8,1)]]
points = [(3,5),(7,3),(7,6),(3,2)]

可以按照以下方式获得包含多边形和点的几何对象的数组:

geo_polygons = pd.DataFrame({'single_column':polygons}).single_column.apply(lambda x: Polygon(x)).values
geo_points = pd.DataFrame({'single_column':points}).single_column.apply(lambda x: Point(x[0], x[1])).values
# As you might noticed, the arrays have different length.

现在定义并向量化了要应用于两个数组的函数:

def contains(a_polygon, a_point):
    return a_polygon.contains(a_point)
contains_vectorized = np.vectorize(contains)

这样,该函数就可以应用于向量中的每个元素。广播点数组处理成对评估:

contains_vectorized(geo_polygons, geo_points[:,np.newaxis])

它返回以下数组:

array([[False,  True, False],
   [False, False, False],
   [False, False, False],
   [ True, False, False]], dtype=bool)

列对应于多边形,行对应于点。该数组中的布尔值显示,例如,第一个点在第二个多边形内。这没关系。映射多边形和点将证明是正确的:

from descartes import PolygonPatch
import matplotlib.pyplot as plt
fig = plt.figure(1, figsize = [10,10], dpi = 300)
ax = fig.add_subplot(111)
offset_x = lambda xy: (xy[0] + 0.1, xy[1])
offset_y = lambda xy: (xy[0], xy[1] - 0.5)
for i,j in enumerate(geo_polygons):
    ax.add_patch(PolygonPatch(j, alpha=0.5))
    plt.annotate('polygon {}'.format(i + 1), xy= offset_y(tuple(j.centroid.coords[0])))
for i,j in enumerate(geo_points):
    ax.add_patch(PolygonPatch(j.buffer(0.07),fc='orange',ec='black'))
    plt.annotate('point {}'.format(i + 1), xy= offset_x(tuple(j.coords[0])))
ax.set_xlim(0, 9)
ax.set_ylim(0, 7)
ax.set_aspect(1)
plt.show()

映射几何

于 2014-07-24T16:21:39.843 回答