def spatial_poly_select(xvals, yvals, geometry): try: from shapely.geometry import Polygon boxes = (Polygon(np.column_stack([xs, ys])) for xs, ys in zip(xvals, yvals)) poly = Polygon(geometry) return np.array([poly.contains(p) for p in boxes]) except ImportError: raise ImportError("Lasso selection on geometry data requires " "shapely to be available.")
def spatial_select_columnar(xvals, yvals, geometry): try: from spatialpandas.geometry import Polygon, PointArray points = PointArray((xvals.astype('float'), yvals.astype('float'))) poly = Polygon([np.concatenate([geometry, geometry[:1]]).flatten()]) return points.intersects(poly) except Exception: pass try: from shapely.geometry import Point, Polygon points = (Point(x, y) for x, y in zip(xvals, yvals)) poly = Polygon(geometry) return np.array([poly.contains(p) for p in points]) except ImportError: raise ImportError("Lasso selection on tabular data requires " "either spatialpandas or shapely to be available.")
def spatial_geom_select(x0vals, y0vals, x1vals, y1vals, geometry): try: from shapely.geometry import box, Polygon boxes = (box(x0, y0, x1, y1) for x0, y0, x1, y1 in zip(x0vals, y0vals, x1vals, y1vals)) poly = Polygon(geometry) return np.array([poly.contains(p) for p in boxes]) except ImportError: raise ImportError("Lasso selection on geometry data requires " "shapely to be available.")
def spatial_select_columnar(xvals, yvals, geometry): if 'cudf' in sys.modules: import cudf if isinstance(xvals, cudf.Series): xvals = xvals.values.astype('float') yvals = yvals.values.astype('float') try: import cuspatial result = cuspatial.point_in_polygon( xvals, yvals, cudf.Series([0], index=["selection"]), [0], geometry[:, 0], geometry[:, 1], ) return result.values except Exception: xvals = np.asarray(xvals) yvals = np.asarray(yvals) x0, x1 = geometry[:, 0].min(), geometry[:, 0].max() y0, y1 = geometry[:, 1].min(), geometry[:, 1].max() mask = (xvals >= x0) & (xvals <= x1) & (yvals >= y0) & (yvals <= y1) masked_xvals = xvals[mask] masked_yvals = yvals[mask] try: from spatialpandas.geometry import Polygon, PointArray points = PointArray( (masked_xvals.astype('float'), masked_yvals.astype('float'))) poly = Polygon([np.concatenate([geometry, geometry[:1]]).flatten()]) geom_mask = points.intersects(poly) except Exception: pass try: from shapely.geometry import Point, Polygon points = (Point(x, y) for x, y in zip(masked_xvals, masked_yvals)) poly = Polygon(geometry) geom_mask = np.array([poly.contains(p) for p in points]) except ImportError: raise ImportError("Lasso selection on tabular data requires " "either spatialpandas or shapely to be available.") mask[np.where(mask)[0]] = geom_mask return mask
def test_polygon(): polygon = Polygon([large_square_ccw, unit_square_cw]) assert polygon.length == 16.0 assert polygon.area == 8.0