def test_multipoint_cx_selection(gp_point, rect): x0, y0, x1, y1 = rect for xslice in get_slices(x0, x1): for yslice in get_slices(y0, y1): expected = PointArray.from_geopandas(gp_point.cx[xslice, yslice]) result = PointArray.from_geopandas(gp_point).cx[xslice, yslice] assert all(expected == result)
def test_construct_pointarray_tuple(): src_array = np.array([[0, 1], [2, 3], [4, 5], [6, 7]], dtype=np.float32) points = PointArray((src_array[:, 0], src_array[:, 1])) np.testing.assert_array_equal(points.x, src_array[:, 0]) np.testing.assert_array_equal(points.y, src_array[:, 1]) np.testing.assert_array_equal(points.isna(), np.isnan(src_array[:, 0])) np.testing.assert_array_equal(points.flat_values, src_array.flatten())
def test_construct_pointarray_interleaved(): src_array = np.array([0, 1, 2, 3, 4, 5, 6, 7], dtype=np.float32) points = PointArray(src_array) np.testing.assert_array_equal(points.x, src_array[0::2]) np.testing.assert_array_equal(points.y, src_array[1::2]) np.testing.assert_array_equal(points.isna(), np.isnan(src_array[0::2])) np.testing.assert_array_equal(points.flat_values, src_array)
def test_construct_pointarray_2d_with_None(): src_array = np.array([None, [2, 3], [4, 5], None], dtype=object) expected_xs = np.array([np.nan, 2, 4, np.nan], dtype=np.float64) expected_ys = np.array([np.nan, 3, 5, np.nan], dtype=np.float64) points = PointArray(src_array) np.testing.assert_array_equal(points.x, expected_xs) np.testing.assert_array_equal(points.y, expected_ys) np.testing.assert_array_equal(points.isna(), np.isnan(expected_xs)) np.testing.assert_array_equal(points.flat_values[2:6], np.array([2, 3, 4, 5], dtype=np.int64))
def data(): """Length-100 array for this type. * data[0] and data[1] should both be non missing * data[0] and data[1] should not gbe equal """ return PointArray([[0, 1], [1, 2], [3, 4], None, [-1, -2]] * 20, dtype='float64')
def test_points_intersects_multipoint(gp_points, gp_multipoint): # Get scalar Point sg_multipoint = gp_multipoint[0] if len(gp_points) > 0: # Add gp_point to gp_multipoints so we know something will intersect gp_points = from_shapely(list(gp_points) + [gp_multipoint[0][-1]]) # Compute expected intersection expected = gp_points.intersects(sg_multipoint) # Create spatialpandas PointArray multipoint = MultiPoint.from_shapely(sg_multipoint) 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(multipoint) for point_el in points ]) np.testing.assert_equal(result, expected) # Test PointArray.intersect result = points.intersects(multipoint) np.testing.assert_equal(result, expected) # Test PointArray.intersects with inds inds = np.flipud(np.arange(0, len(points))) result = points.intersects(multipoint, inds) np.testing.assert_equal(result, np.flipud(expected)) # Test GeoSeries.intersects pd.testing.assert_series_equal( points_series.intersects(multipoint), pd.Series(expected, index=points_series.index) )
def test_points_intersects_line(gp_points, gp_line): # Get scalar Line sg_line = gp_line[0] # Compute expected intersection expected = gp_points.intersects(sg_line) # Create spatialpandas objects line = Line.from_shapely(sg_line) 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(line) for point_el in points ]) np.testing.assert_equal(result, expected) # Test PointArray.intersect result = points.intersects(line) np.testing.assert_equal(result, expected) # Test PointArray.intersects with inds inds = np.flipud(np.arange(0, len(points))) result = points.intersects(line, inds) np.testing.assert_equal(result, np.flipud(expected)) # Test GeoSeries.intersects pd.testing.assert_series_equal( points_series.intersects(line), pd.Series(expected, index=points_series.index) )
def test_points_intersects_multipolygon(gp_points, gp_multipolygon): # Get scalar MultiPolygon sg_multipolygon = gp_multipolygon[0] # Compute expected intersection expected = gp_points.intersects(sg_multipolygon) # Create spatialpandas objects multipolygon = MultiPolygon.from_shapely(sg_multipolygon) 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(multipolygon) for point_el in points ]) np.testing.assert_equal(result, expected) # Test PointArray.intersect result = points.intersects(multipolygon) np.testing.assert_equal(result, expected) # Test PointArray.intersects with inds inds = np.flipud(np.arange(0, len(points))) result = points.intersects(multipolygon, inds) np.testing.assert_equal(result, np.flipud(expected)) # Test GeoSeries.intersects pd.testing.assert_series_equal( points_series.intersects(multipolygon), 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 data_for_grouping(): """Data for factorization, grouping, and unique tests. Expected to be like [B, B, NA, NA, A, A, B, C] Where A < B < C and NA is missing """ return PointArray([[1, 0], [1, 0], None, None, [0, 0], [0, 0], [1, 0], [2, 0]])
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_point_intersects_rect(gp_point, rect): sg_rect = sg.box(*rect) expected = gp_point.intersects(sg_rect) points = PointArray.from_geopandas(gp_point) # Test MultiPointArray.intersects_rect result = points.intersects_bounds(rect) np.testing.assert_equal(result, expected) # Test MultiPointArray.intersects_rect with inds inds = np.flipud(np.arange(0, len(points))) result = points.intersects_bounds(rect, inds) np.testing.assert_equal(result, np.flipud(expected)) # Test MultiPoint.intersects_rect result = np.array([point.intersects_bounds(rect) for point in points]) np.testing.assert_equal(result, expected)
def test_points_intersects_point_offset(gp_points, offset, gp_point): # Get scalar Point sg_point = gp_point[0] if len(gp_points) > 0: # Add gp_point to gp_points so we know something will intersect gp_points = pd.concat([pd.Series(gp_points), pd.Series(gp_point)]).array # Compute expected intersection expected = gp_points.intersects(sg_point)[offset:] # Create spatialpandas PointArray point = Point.from_shapely(sg_point) points = PointArray.from_geopandas(gp_points)[offset:] # Test PointArray.intersect result = points.intersects(point) np.testing.assert_equal(result, expected) # Test PointArray.intersects with inds inds = np.flipud(np.arange(0, len(points))) result = points.intersects(point, inds) np.testing.assert_equal(result, np.flipud(expected))
def test_point_array(): points = PointArray(unit_square_cw) np.testing.assert_equal(points.length, [0.0, 0.0, 0.0, 0.0, 0.0]) np.testing.assert_equal(points.area, [0.0, 0.0, 0.0, 0.0, 0.0]) assert points.total_bounds == (1, 1, 2, 2)
def test_equality(): a = PointArray([[0, 1], [1, 2], None, [-1, -2], [7, 3]], dtype='float64') assert all(a == a) assert all(a[1:-1] == a[[1, 2, 3]]) assert not any(a[1:-1] == a[[2, 3, 1]])
def data_missing(): """Length-2 array with [NA, Valid]""" return PointArray([None, [-1, 0]], dtype='int64')
def geohashes_to_geoseries(s: pd.Series) -> GeoSeries: """Create spatialpandas GeoSeries from geohashes.""" s = pd.Series(s) return GeoSeries(PointArray(s.apply(geohash_decode_xy)), index=s.index)
def data_missing_for_sorting(): """Length-3 array with a known sort order. This should be three items [B, NA, A] with A < B and NA missing. """ return PointArray([[1, 0], None, [0, 0]])
def data_for_sorting(): """Length-3 array with a known sort order. This should be three items [B, C, A] with A < B < C """ return PointArray([[1, 0], [2, 0], [0, 0]])