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_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 _shapely_to_coordinates(cls, shape): import shapely.geometry as sg if isinstance(shape, sg.MultiPolygon): multipolygon = [] for polygon in shape: polygon_coords = Polygon._shapely_to_coordinates(polygon) multipolygon.append(polygon_coords) return multipolygon elif isinstance(shape, sg.Polygon): return [Polygon._shapely_to_coordinates(shape)] else: raise ValueError(""" Received invalid value of type {typ}. Must be an instance of Polygon or MultiPolygon """.format(typ=type(shape).__name__))
def test_points_intersects_polygon(gp_points, gp_polygon): # Get scalar Polygon sg_polygon = gp_polygon[0] # Compute expected intersection expected = gp_points.intersects(sg_polygon) # Create spatialpandas objects polygon = Polygon.from_shapely(sg_polygon) points = PointArray.from_geopandas(gp_points) points_series = GeoSeries(points, index=np.arange(10, 10 + len(points))) # Test Point.intersects result = np.array([ point_el.intersects(polygon) for point_el in points ]) np.testing.assert_equal(result, expected) # Test PointArray.intersect result = points.intersects(polygon) np.testing.assert_equal(result, expected) # Test PointArray.intersects with inds inds = np.flipud(np.arange(0, len(points))) result = points.intersects(polygon, inds) np.testing.assert_equal(result, np.flipud(expected)) # Test GeoSeries.intersects pd.testing.assert_series_equal( points_series.intersects(polygon), pd.Series(expected, index=points_series.index) )
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 test_point_intersects_polygon(sg_polygon, points): polygon = Polygon.from_shapely(sg_polygon) for r in range(points.shape[0]): x, y = points[r, :] result = point_intersects_polygon(x, y, polygon.buffer_values, polygon.buffer_inner_offsets) expected = sg_polygon.intersects(sg.Point([x, y])) assert expected == result
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